Module: Sfp::Graph

Defined in:
lib/sfplanner/graph.rb

Constant Summary collapse

ActionColor =
'white'
ActionLabelWithParameters =
false

Class Method Summary collapse

Class Method Details

.clean(value) ⇒ Object



16
17
18
19
# File 'lib/sfplanner/graph.rb', line 16

def self.clean(value)
	return value[2, value.length-2] if value[0,2] == '$.'
	return value
end

.dot2image(dot, image_file) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/sfplanner/graph.rb', line 5

def self.dot2image(dot, image_file)
	dot_file = "/tmp/#{Time.now.getutc.to_i}.dot"
	File.open(dot_file, 'w') { |f|
		f.write(dot)
		f.flush
	}
	!!system("dot -Tpng -o #{image_file} #{dot_file}")
ensure
	File.delete(dot_file) if File.exist?(dot_file)
end

.get_label(action, withparameters = true) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sfplanner/graph.rb', line 21

def self.get_label(action, withparameters=true)
	label = clean(action["name"])
	if withparameters and ActionLabelWithParameters
		label += "("
		if action["parameters"].length > 0
			action["parameters"].each { |key,value|
				label += "#{clean(key)}=#{clean(value.to_s)},"
			}
			label.chop!
		end
		label += ')'
	end
	return label
end

.partial2dot(json) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sfplanner/graph.rb', line 36

def self.partial2dot(json)
	dot = "digraph {\n"

	dot += "init_state [label=\"\", shape=doublecircle, fixedsize=true, width=0.35];\n"
	dot += "final_state [label=\"\", shape=doublecircle, style=filled, fillcolor=black, fixedsize=true, width=0.35];\n"
	last_actions = Hash.new
	json["workflow"].each { |action|
		dot += "#{action["id"]}[label=\"#{get_label(action)}\", shape=rect, style=filled, fillcolor=#{ActionColor}];\n"
		last_actions[action["id"].to_s] = action
	}

	json["workflow"].each { |action|
		has_predecessor = false
		action["predecessors"].each { |prevId|
			dot += "#{prevId} -> #{action["id"]};\n"
			has_predecessor = true
			last_actions.delete(prevId.to_s)
		}
		if not has_predecessor
			dot += "init_state -> #{action["id"]};\n"
		end
	}

	last_actions.each { |id,action|
		dot += "#{id} -> final_state;\n"
	}

	dot += "}"

	return dot
end

.sequential2dot(json) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/sfplanner/graph.rb', line 103

def self.sequential2dot(json)
	dot = "digraph {\n"

	dot += "init_state [label=\"\", shape=doublecircle, fixedsize=true, width=0.35];\n"
	dot += "final_state [label=\"\", shape=doublecircle, style=filled, fillcolor=black, fixedsize=true, width=0.35];\n"
	id = 0
	json["workflow"].each { |action|
		dot += id.to_s + '[label="' + get_label(action) + '", shape=rect]' + ";\n"
		id += 1
	}

	id = 0
	prevActionId = nil
	json["workflow"].each { |action|
		if id == 0
			dot += "init_state -> " + id.to_s + ";\n"
		elsif id == json["workflow"].length-1
			dot += id.to_s + " -> final_state;\n"
		end
		if prevActionId != nil
			dot += prevActionId.to_s + " -> " + id.to_s + ";\n"
		end
		prevActionId = id
		id += 1
	}
	dot += "}"
	return dot
end

.stage2dot(json) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sfplanner/graph.rb', line 68

def self.stage2dot(json)
	dot = "digraph {\n"

	dot += "init_state [label=\"\", shape=doublecircle, fixedsize=true, width=0.35];\n"
	dot += "final_state [label=\"\", shape=doublecircle, style=filled, fillcolor=black, fixedsize=true, width=0.35];\n"
	index = 0
	prevState = "init_state"
	json["workflow"].each { |stage|
		id = 0
		stage.each { |action|
			dot += "a" + index.to_s + "a" + id.to_s + '[label="' + get_label(action) + '", shape=rect]' + ";\n"
			dot += prevState + " -> a" + index.to_s + "a" + id.to_s + ";\n"
			id += 1
		}
		if index < json["workflow"].length-1
			dot += "state" + index.to_s + ' [label="", shape=circle, fixedsize=true, width=0.35]' + ";\n"
			prevState = "state" + index.to_s
			id = 0
			stage.each { |action|
				dot += "a" + index.to_s + "a" + id.to_s + " -> " + prevState + ";\n"
				id += 1
			}
		else
			id = 0
			stage.each { |action|
				dot += "a" + index.to_s + "a" + id.to_s + " -> final_state;\n"
				id += 1
			}
		end
		index += 1
	}
	dot += "}"
	return dot
end