Module: Nuri::Planner::Graph

Defined in:
lib/sfp/sfw2graph.rb

Constant Summary collapse

ActionColor =
'white'
ActionLabelWithParameters =
false

Class Method Summary collapse

Class Method Details

.clean(value) ⇒ Object



12
13
14
15
# File 'lib/sfp/sfw2graph.rb', line 12

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

.get_label(action, withparameters = true) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sfp/sfw2graph.rb', line 17

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



32
33
34
35
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
# File 'lib/sfp/sfw2graph.rb', line 32

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sfp/sfw2graph.rb', line 99

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



64
65
66
67
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
# File 'lib/sfp/sfw2graph.rb', line 64

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