-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnoopy.rb
More file actions
209 lines (178 loc) · 5.79 KB
/
snoopy.rb
File metadata and controls
209 lines (178 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
63
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
98
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require 'rubygems'
require 'xmlsimple'
require 'mathn'
# an instance of this class represents a Petri Net
class Snoopy
# Nodes, Transitions, Edges, and their counts
attr_accessor :pValue
attr_accessor :filename, :strippedFile, :config
attr_accessor :petriNetClass
attr_accessor :nNodes, :nTransitions, :nEdges
attr_accessor :nodes, :edges, :transitions
attr_accessor :variables
attr_accessor :incidenceMatrix, :indicatorFunctions, :networks
def initialize( filename, pValue )
@pValue = nextPrime pValue
@filename = filename
parse()
# Currently, we solely work with standard petri nets
if (@petriNetClass != "Petri Net")
puts "Please use a Standard Petri Net. Other types of Petri Nets are not supported yet."
exit 1
end
puts "Here comes a #{pValue -1}-bounded Petri net converted to a system of polynomials over F_#{@pValue}."
# node id and variable index (from 1 to n)
@variables = {}
# for each transition, an indicator function
@indicatorFunctions = []
@networks = []
assignIndexToIDs()
#populateIncidenceMatrix()
#makeIndicatorFunctions()
#makeNetworks()
end
def printNames
names = []
@nodes.each_pair{ |id, v|
var = @variables[id.to_i]
name = v['attribute']['Name']['content'].strip
names.push "x#{var} = #{name}"
}
ret = ""
names.each { |n|
ret = ret + "#{n}\n"
}
ret
end
def simplifyFunction( input )
functionList = ""
input.each{ |function|
functionList = functionList + "\"" + function + "\","
}
functionList.chop!
ret = `cd lib/M2code; M2 indicatorFunc.m2 --stop --no-debug --silent -q -e 'simplify( {#{functionList}}, #{@pValue}, #{@nNodes}); exit 0'`
ret.split( /\n/)
end
def assignIndexToIDs
index = 1
@nodes.each_pair{ |id, v|
@variables[id.to_i] = index
index = index + 1
}
end
def makeRow(pairs, multiplier)
row = Array.new(@nNodes, 0)
pairs.each{ |pair|
nodeID = pair.first
edgeID = pair.last
multiplicity = @edges[edgeID]['attribute']['Multiplicity']['content'].strip.to_i
row[@variables[nodeID.to_i] - 1] = multiplier* multiplicity
}
row
end
def self.arrayAdd(a,b)
if (a.size != b.size)
puts "There is a problem adding arrays that do not have the same length. Exiting"
exit 1
end
a.each_with_index { |value, i|
a[i] = value + b[i]
}
a
end
# for each transition, make an array of functions
def makeNetworks
makeIndicatorFunctions()
populateIncidenceMatrix()
@transitions.keys.each_with_index { |id, index|
network = []
c = @indicatorFunctions[index]
at = @incidenceMatrix[index]
for i in 1..@nNodes
addOn = "(#{c}) * #{at[i-1]}"
f = "x#{i} + #{addOn}"
network.push f
end
@networks.push simplifyFunction network
}
end
# make the C(x) function
def makeIndicatorFunctions
@transitions.keys.each { |id|
# select all edges ending in this transition, then collect the IDs of the incoming nodes
# inputs: [IDs of nodes, ID of edge]
inputs = @edges.keys.select{ |k| @edges[k]['target'] == id }.collect{ |k|
[@edges[k]['source'], @edges[k]['attribute']['Multiplicity']['content'].strip.to_i ]
}
func = ""
inputs.each { |pair|
nodeID = pair.first
var = @variables[nodeID.to_i]
multiplicity = pair.last
ret = `cd lib/M2code; M2 indicatorFunc.m2 --stop --no-debug --silent -q -e 'indicatorF(#{multiplicity}, #{@pValue -1}, #{var}, #{@pValue}); exit 0'`
func = func + "(#{ret.chop})*"
}
func.chop!
@indicatorFunctions.push func
}
simplifyFunction @indicatorFunctions
end
def populateIncidenceMatrix
@incidenceMatrix=[]
@transitions.keys.each { |id|
# select all edges ending in this transition, then collect the IDs of the incoming nodes
# inputs: [IDs of nodes, ID of edge]
inputs = @edges.keys.select{ |k| @edges[k]['target'] == id }.collect{ |k| [@edges[k]['source'], k] }
outputs = @edges.keys.select{ |k| @edges[k]['source'] == id }.collect{|k| [@edges[k]['target'], k] }
row = Snoopy.arrayAdd( makeRow(inputs, -1), makeRow(outputs, 1))
@incidenceMatrix.push row
}
end
# return next largest prime number
def nextPrime p
a = Prime.new
a.each { |succ|
if succ.to_i >= p.to_i
break
end
}
a.instance_variable_get(:@primes).last
end
def parse
@strippedFile = stripGraphics
@config = XmlSimple.xml_in(@strippedFile, { 'KeyAttr' => ['name', 'node', 'id'] })
@petriNetClass = @config['netclass'].keys.to_s
@nNodes = @config['nodeclasses'].first['nodeclass']['Place']['count'].to_i
@nEdges = @config['edgeclasses'].first['edgeclass']['Edge']['count'].to_i
@nTransitions = @config['nodeclasses'].first['nodeclass']['Transition']['count'].to_i
@nodes = @config['nodeclasses'].first['nodeclass']['Place']['node']
@edges = @config['edgeclasses'].first['edgeclass']['Edge']['edge']
@transitions = @config['nodeclasses'].first['nodeclass']['Transition']['node']
end
# remove all graphics tags, they mess up content somehow
def stripGraphics
line_arr = File.readlines(self.filename)
new_arr = []
inGraphicsFlag = false
line_arr.each do |line|
if ( line =~ /^\s*<graphics/ )
unless( line =~ /^\s*<graphics.*\/>\s*$/ )
inGraphicsFlag = true
end
next
end
if ( line =~ /\s*<\/graphics/ )
inGraphicsFlag = false
next
end
if inGraphicsFlag
next
end
new_arr.push line
end
File.open(self.filename+".new", "w") do |f|
new_arr.each{|line| f.puts(line)}
end
return self.filename+".new"
end
end