Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Comparing stories with the use of Petri Nets
Loıs Mooiman10318364
Bachelor thesisCredits: 18 EC
Bachelor Opleiding Kunstmatige Intelligentie
University of AmsterdamFaculty of ScienceScience Park 904
1098 XH Amsterdam
SupervisorsG. Sileno & A. Boer
Leibniz Center for LawFaculty of Law
University of AmsterdamVendelstraat 8
1012 XX Amsterdam
June 26th, 2015
Abstract
In this thesis twenty basic legal stories are modelled into Petri nets to compare them in terms of differentsimilarities. Petri nets are state-transition systems which are used to represent the behaviour of systemswith the use of transitions, places, arcs and tokens. They can depict the storyline well and causationis based on the proceedings of the story. Therefore Petri nets are useful for representing legal storieshowever there is not yet a comparison measure to compare these stories. A consistent model is createdin this paper to represent the stories in Petri nets. After which three similarity measures are consid-ered: label, structural and execution similarity. Label similarity compares the labels of the nodes andthus compares the Petri nets on their subject and events. Structural similarity computes the differencebetween two stories by inserting and deleting nodes and compares them therefore in complexity. The dif-ference in execution uses the flow of the tokens to measure them and this is partically useful to representthe storyline. Label and structural similarity are implemented and there results are ranked in matrices.Beforehand the stories are ranked by a person and the results of the computational measures and arecorrelated with the use of Spearman’s rank correlation algorithm. Label similarity has the highest corre-lation because subject and events are important for human comparison instead of the complexity of thestory. However label comparison is still a mechanical way to compare stories and the execution compar-ison measure would probably be higher correlated because of a better representation of the story flow.This thesis concludes that label similarity is a better way to compare stories then structural similarity.
1
Contents
1 Introduction 3
2 Literature Review 3
3 Approach 43.1 Stories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43.2 Petri nets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2.1 Modelling the stories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53.2.2 Labeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63.2.3 Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Comparison measure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3.1 Label similarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3.2 Structure similarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3.3 Difference in execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Implementation 84.1 Label comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84.2 Structural comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5 Evaluation 95.1 Human comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
5.1.1 Subject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105.1.2 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105.1.3 Intent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5.2 Computational comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105.3 Correlation matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6 Conclusion & Discussion 15
7 Future work 15
8 Bibliography 16
A Short stories 17
B Groovy code 19B.1 comparison.groovy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19B.2 connection.groovy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2
1 Introduction
For the past few years programs and algorithms have been build and taught of to compare stories suchas movieplots, literature and business cases, because categorization of those stories would be easier ifa program could categorize them based on different similarities. However each different kind of storyhas different important aspects. For example, the main characters and events are important in literature,whereas the execution of steps and subject of the events are important in business cases. In the legaldomain discourse and causation are important for categorising legal cases (Lehmann et al., 2005). Thusdiscourse and causation need to be represented well to compare legal cases. Judges could use a compar-ison measure to find similar cases and their verdict to base the new verdict on the old one.
Petri nets are a way to represent the flow of a system, which is important to represent the storylineand thus causation (Lehmann et al., 2005). That is why Petri nets could be used to represent legal cases(Sileno and Boer, 2014). However, comparing stories with a program is mechanical and thus the flowof these stories cannot be compared in a humanlike way (Fisseni and Lowe, 2012). An appropriatecomparison measure is needed to compare stories without loss of the storyline which could be used forcomparing legal cases.
In this thesis a comparison measure is introduced to find similarities between legal stories repre-sented in Petri nets. The data set consists of simple legal cases, the stories, which need to be representedin Petri nets and the basics for each representation needs to be universal. An similarity matrix is madebeforehand to compare the legal stories and three comparison measures, label similarity, structural sim-ilarity and behavioural similarity (Dijkman et al., 2011), are researched to apply to this problem. Basedon these types of similarity and the assumption that the story flow needs to be represented a comparisonmeasure is created to achieve appropriate results.
This thesis focusses on finding one comparison measure that will satisfy the specified evaluation.The Petri nets will be evaluated on multiple similarities beforehand to ensure a comparison measurewith appropriate results. First they are ranked to find out which elements are important for human com-parison and secondly the correlation matrix is computed. This evaluation is used to create a comparisonmeasure which will compare the stories the same.
The remainder of this thesis is structured as follows. First, relevant literature is discussed and linkedto this thesis in section 2. Secondly, the procedure of creating the story base is explained, the comparisonmeasures and Petri nets are explained in section 3. After which the implementation of the program isexplained in section 4 and the results and evalution is discussed in the next section. The section 6discusses the whole thesis and presents conclusions and the last section states future research to bedone.
2 Literature Review
In literature multiple models are proposed to represent stories. The studies focussing on this topic areconstructing a structural model to represent the narrative in stories. Schank and Abelson (1979) is anexample of such a study. Schank and Abelson (1979) introduce scripts as a structural model. Scripts arelinear models and are about creating models which represent multiple events. However Sileno and Boer(2014) discusses a systematic representational model. Sileno and Boer (2014) want to describe stories inthe legal domain accurately and therefore it needs to represent discourse and causation. Lehmann et al.(2005) explains why causation is essential to attribute legal responsibility. Thus causation needs to berepresented to assign value to representations of legal cases.
3
Sileno and Boer (2014) propose a new method to represent these aspects. They use Petri nets torepresent the story flow of stories from the legal domain. A Petri net is a state-transition system whichuses places, transitions and tokens. Murata (1989) writes about them: ”Petri nets are a promising toolfor describing and studying information processing systems that are characterized as being concurrent,asynchronous, distributed, parallel, nondeterministic and/or stochastic.” Behrens and Dix (2007) elobo-rate on Petri nets and propose Simple Logic Petri nets which are Petri nets based on logical expressionsand an extension to the original ones. Petri nets have not been used before in the legal domain to repre-sent stories. Sileno and Boer (2014) show the newest en first research on this subject.
Sileno and Boer (2014) discuss the representation of legal stories and they mention the topic ofcomparing stories but do not eloborate on this subject. Psychological studies find that people comparestories in several different ways. Fisseni and Lowe (2012) suggest that human judgement for comparingobjects or events is not based on structural dimensions but on event structure and causal links. Thereforestructural comparison can be used to compare stories. However to depict the story flow for humans eventstructure and causal links need to be considered as well.
A structural comparison model used in computation is structural similarity. Structural similarity isdetermined on the number of steps used to get from one graph to another. Lowe (2011) discusses thiskind of similarity and states the advantages and disadvantages. High precision and accuracy is an advan-tage but relations or subjects of nodes are discarded. Dijkman et al. (2011) wants to find a comparisonmethod which can compare business models displayed in graphs. One of these methods is structuralsimilarity. Dijkman et al. (2011) compare the results of structural similarity to two other methods: nodeand behavioural similarity. Node similarity calculates the word similarity between the labels of thenodes by matching words or short sentences. Behavioural similarity calculates the similarity of relationsbetween events which could simulate an human-like comparison of stories if the theories of Fisseni andLowe (2012) about human judgement are correct. The results of Dijkman et al. (2011) conclude thatstructural similarity has the average highest precision over the trials. However behaviour similarity isbetter to represent the similarity between relations and sequences of events.
Behavioural similarity, introduced by Dijkman et al. (2011), could be used to compare stories fromthe legal domain represented in Petri nets, like Sileno and Boer (2014) state, because it stresses thesequences and relations of events and therefore contributes to represent discourse and causation. Thissubject will be eloborated upon in the upcoming paragraphs.
3 Approach
First a database of Petri nets based on legal stories is created to use as a data set for the comparisonmeasure and second different options are explored to find an appropriate comparison measure. Thisprocess is described in the following paragraphs.
3.1 Stories
The twenty stories included in A are all legal cases and consist of short sentences which display theoccuring events. The subjects of the stories are sale, robbery, theft, etc and can be malfunctioning ofsocial instution. For example the Failed Sale 2 story listed below displays a sale where the good doesnot arrive and thus one of the agents, in this case Y, is not pleased and this is for him considered as afailure.
F a i l e d S a l e 2 S t o r y :
4
X makes an o f f e r .The o f f e r c o n c e r n s a good exchanged f o r a c e r t a i n amount o f money .Y a c c e p t s t h e o f f e r .Y pays t h e money .X does n o t d e l i v e r .
Similar events over multiple stories are phrased the same and if needed the intent is described aswell, for instance in the Theft False story it says explicitly that the agent did steal for an emergencywhich is deemed as a good intent. The stories do not state what the exact ending is for the agents. Thisis induced from them when they need to be modelled into Petri nets.
3.2 Petri nets
Petri nets are state-transition systems which are used to represent the behaviour of systems with theuse of transitions, places, arcs and tokens. Transitions can only be connected to places and vice versabecause the transitions fire the tokens from one place to the other along the lines of the arcs. An emitterstarts firing the tokens to the first places and each connected place gets a token and once fired it cannotbe fired again. The places hold the tokens until the connected transition starts firing and the transitioncan only start firing when each of the input places has a token. Therefore it needs to wait when not allhis inputs has token until other transitions are executed. Places can hold multiple tokens, however itwill only pass one for each time the transition is fired. When the tokens activate the collector and thecollector collects the tokens when executed the Petri net is finished. Arcs regulate this whole flow of thetokens and point from place to a transition and vice versa. They can also be a biflow which means thatthe tokens go both ways therefore when the transition is fired a token will flow back into the place wherethe token also came from. (Murata, 1989)The software used for this thesis to model and create the Petri nets is Yasper (ASPT, 2005) and thesetransitions are displayed as green blocks, the places as yellow dots and the arcs as arrows. The emittorand collector are yellow blocks with respectively the letter E or C. The flow of the tokens is the same asdescribed above.
3.2.1 Modelling the stories
Each Petri net has one emitter and one collector. Sileno and Boer (2014) describe the different layers ofa story which need to be represented in Petri nets: the message layer and the agent layers. The messagelayer represents the basic story, for example: X makes offer is an event and is represented as the transi-tion made offer in the message layer. However X is involved in this event thus in the agent layer of Xtransitions and places are added to create the event of making the offer as seen in figure 1. The messagelayer is always in the middle line of the Petri net and is flanked by the agent layers.In the Failed Sale 2 story there is a failure for one of the agents because the package does not arrive.This is displayed in figure 1 as a transition labelled with Failure. Negation is represented in labeling asa no and does not have a representation in places or transitions. Thus negation is not considered whenmodelling the stories and is not handled.Biflow arcs are used to represent an ongoing relation between the agent layer and the message layer. Aloop is used to keep the tokens in place as seen in figure 1 when the agent is making acceptance whilein the message layer acceptance made is active.
5
Figure 1: FailedSale2Story - Petri net representation
In some of the stories an extra layer is necessary to display the intent of the agents, for instancean intent to steal a car or use someone elses car for an emergency. Stealing a car is considered a crimewhereas stealing a car for an emergency is not, therefore an intentional layer incoorperated into the agentlayer displays the difference between the stories and Petri nets without explicitly modelling what is donewith the car. In figure 2 one agent wants to own the good which is expressed by !own good and the !characterises the intent in all the representations.
Figure 2: TheftStory - Petri net representation
3.2.2 Labeling
The following points are uphold for labeling the Petri nets: transitions are labelled as offer made orgood sold, thus, each verb is after the noun and it consists of two or three words. Places are labelled
6
as making offer or waiting for goods. Each verb is a gerund and the length of the phrase is the same aswith transitions. Furthermore the grammar is as small as possible and no synonyms are used to avoiddifferences while the same events are modelled.Not each transition or place is labelled in the whole Petri net and the arcs are not labelled. The transitionsof the message layer are labelled but the places are not labelled, however in the agent layers the placesare labelled and the transitions are only labelled when a failure occurs as seen in figure 1. The loopswhich are used to display interaction between the layers without losing the flow of the tokens are onlylabelled in one place.
3.2.3 Execution
The program Yasper (ASPT, 2005) provides a system which executes the Petri net and displays the flowof the tokens in the net itself. In this case only one token is present in one place at the time when this isnot the case then the Petri net is not modelled accurately and needs to be redefined. Another point is thateach transition and place needs to be visited otherwise not the whole story is executed and there mostnot be a token left in the net when the collector is already activated. When the Petri net executes welland is modelled as described above then it can be used for the next phase.
3.3 Comparison measure
The short stories represented in Petri nets are different from eachother and the resemblance and differ-ences of the stories need to be analyzed correctly. After examining the Petri nets three methods couldbe used to compare the stories and compare them in different ways to find other results. Each one of themethods is examined on its advantages and usefulness.
3.3.1 Label similarity
Transitions and places are labeled in the Petri nets and the same wording or phrases are used to indicatesimilar events. For example in the Sale story and the Failed Sale 2 story an offer is made in the beginningby an agent and this place is labeled as making offer (figure 1). It is in both stories the same event andtherefore has exactly the same label. The structure of the wording in all the labels is consistent as far aspossible and for places this means that the verb is before the noun and for transitions it is vice versa.Thus the labels are generally alike eachother. However the placing of these labels differentiate for eachPetri net. The event buying occurs earlier in agent layer in the Complicated sale story then the Sale story.Also, not all labels occur in each Petri net. Both techniques combined can compare Petri nets on theirlabels. Dijkman et al. (2011) called this technique on business models node similarity and uses cosinesimilarity to calculate the difference between the two labels. Cosine similarity computes the similaritybetween two vectors in a space. It maps the labels in such a space and then calculates the difference ofthe angle with the use of the cosine.Label similarity does not take into account the flow of the tokens and therefore not the execution of thestory which is important in the legal domain to show why a person did the action.In Petri nets label similarity could be done on each independent layer to find out if the story is the samebut the agents act differently.
3.3.2 Structure similarity
Each Petri net has a message layer and agent layers and it has an emitter and a collector to fire and tocatch the tokens as well. Between these two nodes the structure depends on the story represented.Calculating the count of steps it takes to transform one Petri net into another is a measure of structuralcomparison. Dijkman et al. (2011) and Lowe (2011) prove that structural comparison are efficient ways
7
of comparing two models. Structure similarity accounts for the complexity of a story however it doesnot account for the subject of the story which label similarity does account for, however it can also notaccount for the flow of the tokens.
3.3.3 Difference in execution
Execution similarity is based on the flow of the tokens and it changes for each Petri net. Therefore eachexecution is unique to the story and could potentially be a similarity measure that does not look at themechanical way of the Petri nets but at the flow and behaviour. However it depends on the structure ofthe Petri net and thus the complexity of a story needs to be taken into account as well when comparingboth Petri nets on execution.Dijkman et al. (2011) suggests something similar with his behaviour similarity but he uses businessmodels which are different in respect of Petri nets because of the tokens.
4 Implementation
The program is written in Groovy and the program IntelliJ IDEA (Jetbrains, 2015) is used to execute it.The Petri nets are read into the program and broken up into a transition list, places list and an arc list andthis part of the program was already provided for by Sileno and Boer (2014). All the lists are not sortedand only convey the charactistics of that node, which include the label, the given id and other propertiesdepending on the node.Label similarity and structure similarity are implemented in the file comparison.groovy and executedover all the stories to find the similarities between them. The file connection.groovy consists of functionsused in the other file.
4.1 Label comparison
Label comparison takes both the transitions and the places list to find all the nodes with labels and itcompares each label of two stories to eachother. After which the labels it calculates the distance fromthe emitter to the current place or transition by adding the number of steps taken and uses the differencebetween the distance to compute the similarity with the other label. The calculation made is:
2∗Di f f erenceLengtho f labellednodes
Length of labelled nodes consists of the total number of node from both Petri nets which are labelled.The results should be a number between 0 and 1 with 1 as the highest similarity. However when tested ontwo exactly the same stories it returned 0 instead of 1. Therefore another implementation was necessaryto get accurate results.The second implementation also compares the labels of both stories but calculates the difference betweenthem with cosinus similarity, which is a build-in function of Groovy. It uses the same calculation as theother implementation to compute the similarity between the two stories. The results are also from 0 to 1with 1 meaning that the labelling is exactly similar.However there is no preprocessing done on the wording of phrases and a misspelling or ambiguity couldlead to a difference in similarity while it should be counted as a similar label but this is avoided bylabelling the stories the same.
8
4.2 Structural comparison
The arc list is used for the structural comparison to create a connection which counts the number ofincoming and outgoing arcs for each place or transition. It starts with the emitter and finds the nextnodes by looking into the connection list and the function does this each time for both stories.It checks the two nodes on outgoing arcs and looks for the next connection. Each time a node is visitedit transfers the node from the connection list to a visited list thus it does not repeat the same node. Nodeswith multiple outgoing arcs have two connection options and when one is visited the program needs togo back to do the other option as well. The second connection is therefore placed in a remember listwhich executed when there is no next connection.The following pseudocode is the loop which is executed to determine if an connection needs to be in-serted or deleted.
w h i l e c o n n e c t i o n l i s t i s n o t empty do :i f o u t g o i n g a r c s a r e n o t z e r o :
i f o u t g o i n g a r c s a r e e q u a l :F ind n e x t c o n n e c t i o n i n l i s t
i f o u t g o i n g a r c s f o r f i r s t s t o r y i s g r e a t e r :Add a c o n n e c t i o nFind n e x t c o n n e c t i o n f o r f i r s t s t o r yand keep new c o n n e c t i o n
i f o u t g o i n g a r c s f o r f i r s t s t o r y i s lower :D e l e t e c o n n e c t i o nFind n e x t c o n n e c t i o n
e l s e r e a c h e d t h e end :i f s t a r t n o d e i s l e f t :
Begin wi th s t a r t n o d ee l s e :
F ind n e x t c o n n e c t i o n i n remember l i s t
Finding the next connection is based on the connection list and if there is no other connections thenthere are two other options. The first option is to find another start connection which has not beenexplored before and the second option is the remember list where other connections are stored. It alwaysfinds a next connection depending on these three options.During the while loop the number of insertions and deletions are counted and at the end they are addedup to eachother. This is the measure used to compare structural similarity.
5 Evaluation
The story are compared to eachother on three different fronts. First the stories are ranked by humancomparison after which the program computes the label similarities and structural similarities. Theseresults are ranked as well and are correlated with the human comparison by using Spearman’s rankcorrelation matrix algorithm.
5.1 Human comparison
The stories are evaluated beforehand on their similarities by a human. The twenty stories are rankedin table 1 from 1 till 20. There are not compared to itself thus the x in the table and each story has adifferent ranking therefore the highest rank for one story could be equivalently lower for the comparedstory as seen with most of the stories.When evaluating the stories three aspects arose: subject, events and intent and the stories are rankedhigher when they are positively combined.
9
5.1.1 Subject
There are five big subjects among the stories: sale, theft, burglary, robbery and good-exchange whichare stories that do not mention sale and the swap or gift story belong in this category.Subject is an important aspect which means that it ranks higher than the other aspects as the table 1shows when looking at the subject sale. These stories are in eachother top ten.
5.1.2 Events
Same events rank higher as well, like the event buying or making an offer and multiple similar eventsin an story can lead to higher similarity. Events are different from the subject because the subject is anoverall look. Whereas another subject could have the same events. Swap and Sale will rank high becausethey have similar events even though their subject is not the same.
5.1.3 Intent
In legal stories the intent of the agents is important. Stories with bad intent will be associated witheachother and therefore intent is also important when ranking stories. The stories Burglary False andTheft False are high ranked to eachother because they share the same intent while the subject and eventsdifferentiate.However intent is less important than subject. Sale stories will be paired together even though one mightbe malfunctioning of social institution like the Failed Sale 2. Higher offer is a story which does not havea specific intent and is therefore categorized in the middle rank of all the stories.
5.2 Computational comparison
Table 2 displays the result of the label comparison measure and from this table there can be concludedthat label similarity depends heavily on the aspects subject and events, because they have more exactlabels. However it does not take intent into account and therefore it only groups by events.When short stories are compared with larger stories the results are low even though the subject is thesame. Failed Sale 1 is a short story and only returns low results.Table 3 displays the ranking of the results of the label comparison measure. Failed Sale 2 receives ahigh rank in six other stories and these stories all involve sale. However this subject grouping does notapply to the Gift story where two theft stories are ranked as second but the Gift story has a lot of eventsin common even though human comparison will not place them together as one subject.
Table 4 shows the results of the structural comparison and in this table the number of total insertionsand deletions are displayed. However comparing two exact same stories to eachother should result ina 0 and some stories do not have a zero. This could be because of the multiple ways to get from theemitter to the collector but it does not account for uneven numbers and the program does an insertion ordeletion too many which it should not do.The rankings are shown in table ?? and the rankings are scattered over multiple subjects and events.This is because structural comparison only looks ate the number of arcs, places and transitions and itdoes not include label matching, therefore it computes the difference in complexity of the stories andshort stories have a higher ranking to eachother.
10
Tabl
e1:
Man
ual-
Ran
king
ofth
est
orie
s
Bur
glar
yFa
lse
Bur
glar
yC
ompl
icat
edSa
le1
Com
plic
ated
Sale
2Fa
iled
Sale
1Fa
iled
Sale
2G
ift
Hig
her
Off
erL
oan
Off
erE
xtor
tion
Off
erIn
vita
tion
Rob
bery
succ
esR
obbe
ryU
nsuc
ces
Sale
Swap
Eva
sion
Swap
The
ftfa
lse
The
ftjo
yT
heft
rath
erT
heft
Bur
glar
yFal
sex
28
912
135
610
419
718
1714
61
315
16B
urgl
ary
6x
1514
179
1911
138
183
416
1012
75
12
Com
plic
ated
Sale
114
19x
15
311
710
64
1817
29
812
1315
16C
ompl
icat
edSa
le2
1419
1x
63
117
105
418
172
89
1213
1516
Faile
dSal
e114
173
4x
19
58
117
1918
210
612
1316
15Fa
iledS
ale2
1914
35
2x
104
96
815
161
1211
1817
137
Gif
t11
177
86
9x
131
183
1916
52
410
1214
15H
ighe
rOff
er16
159
104
216
x11
51
1718
312
614
813
7L
oan
1219
78
610
113
x9
218
175
43
1114
1615
Off
erE
xtor
tion
143
1110
137
198
17x
181
212
916
155
64
Off
erIn
vita
tion
717
910
313
12
419
x16
158
125
611
1418
Rob
bery
Succ
es15
211
912
619
1017
818
x1
137
1614
54
3R
obbe
ryU
nsuc
ces
142
119
137
1910
175
181
x12
816
156
43
Sale
1317
45
31
108
119
719
18x
62
1214
1615
Swap
Eva
sion
1318
56
89
711
310
419
162
x1
1214
1517
Swap
1319
45
68
107
911
218
173
1x
1214
1516
The
ftFa
lse
51
1516
1214
811
919
107
613
1817
x2
43
The
ftJo
y4
715
1417
1019
1112
918
56
168
131
x2
3T
heft
Rat
her
111
1413
156
1910
165
184
812
717
93
x2
The
ft5
1114
1316
719
1018
417
68
159
123
12
x
11
Tabl
e2:
Lab
elsi
mila
rity
Bur
glar
yFa
lse
Bur
glar
yC
ompl
icat
edSa
le1
Com
plic
ated
Sale
2Fa
iled
Sale
1Fa
iled
Sale
2G
ift
Hig
her
Off
erL
oan
Off
erE
xtor
tion
Off
erIn
vita
tion
Rob
bery
succ
esR
obbe
ryU
nsuc
ces
Sale
Swap
Eva
sion
Swap
The
ftfa
lse
The
ftjo
yT
heft
rath
erT
heft
Bur
glar
yFal
se1
0.53
80.
0758
0.07
800.
111
0.05
10.
191
0.06
00.
196
0.17
90.
126
0.15
50.
146
0.05
50.
111
0.04
90.
178
0.15
90.
325
0.11
1B
urgl
ary
0.53
81
00.
019
00.
065
0.09
90
0.04
20.
056
0.02
90.
121
0.16
70
0.04
80.
056
0.04
80.
130
0.60
30.
149
Com
plic
ated
Sale
10.
076
01
0.51
30.
159
0.32
30.
254
0.19
40.
253
0.19
00.
272
0.10
40.
099
0.28
00.
151
0.18
40.
230
0.21
20
0.23
042
Com
plic
ated
Sale
20.
078
0.01
90.
513
10.
092
0.22
00.
189
0.15
40.
173
0.16
20.
218
0.10
00.
097
0.15
00.
143
0.13
60.
121
0.11
30
0.12
1Fa
iledS
ale1
0.11
10
0.15
90.
092
10.
370
0.02
90.
276
0.07
40.
095
0.16
70.
078
0.07
40.
286
00.
290
00
00
Faile
dSal
e20.
051
0.06
50.
323
0.22
00.
370
10.
017
0.53
30.
046
0.05
30.
149
0.08
90.
114
0.73
90
0.38
10
00.
037
0G
ift
0.19
10.
099
0.25
40.
189
0.02
90.
017
10.
105
0.53
30.
379
0.38
60.
126
0.06
30.
034
0.26
20.
099
0.39
00.
347
00.
390
Hig
herO
ffer
0.05
90
0.19
40.
154
0.27
60.
533
0.10
51
0.12
60.
219
0.28
20.
083
0.08
00.
618
0.12
30.
345
0.08
90.
083
00.
089
Loa
n0.
196
0.04
20.
253
0.17
30.
074
0.04
60.
533
0.12
61
0.37
50.
355
0.10
50.
100
0.06
70.
259
0.13
20.
455
0.43
00
0.39
9O
ffer
Ext
ortio
n0.
179
0.05
60.
190
0.16
20.
095
0.05
30.
379
0.21
90.
375
10.
506
0.26
80.
250
0.07
70.
310
0.09
70.
249
0.21
80
0.24
9O
ffer
Invi
tatio
n0.
126
0.02
90.
272
0.21
80.
167
0.14
90.
386
0.28
20.
355
0.50
61
0.16
60.
158
0.16
70.
264
0.22
70.
264
0.23
80
0.26
4R
obbe
rySu
cces
0.15
50.
121
0.10
40.
100
0.07
80.
089
0.12
60.
083
0.10
50.
267
0.16
61
0.82
50.
086
00.
078
00
0.13
30
Rob
bery
Uns
ucce
s0.
146
0.16
70.
099
0.09
70.
074
0.11
40.
063
0.08
00.
100
0.25
00.
158
0.82
51
0.08
30
0.07
50
00.
125
0Sa
le0.
055
00.
280
0.14
90.
286
0.73
90.
034
0.61
80.
067
0.07
70.
167
0.08
60.
083
10.
029
0.45
70.
029
0.02
70
0.02
9Sw
apE
vasi
on0.
111
0.04
80.
151
0.14
30
00.
262
0.12
30.
259
0.31
00.
264
00
0.02
91
0.05
30.
292
0.25
90
0.29
1666
7Sw
ap0.
049
0.05
60.
184
0.13
60.
290
0.38
10.
099
0.34
50.
132
0.09
70.
227
0.07
80.
0754
0.45
70.
053
10.
074
0.06
90.
028
0.07
4T
heft
Fals
e0.
178
0.04
80.
230
0.12
10
00.
391
0.08
90.
455
0.24
90.
264
00
0.02
90.
292
0.07
41
0.83
30
0.77
1T
heft
Joy
0.15
70.
130
0.21
20.
113
00
0.34
70.
083
0.43
00.
218
0.23
80
00.
027
0.25
90.
069
0.83
31
0.08
80.
741
The
ftR
athe
r0.
325
0.60
30
00
0.03
30
00
00
0.13
30.
125
00
0.02
80
0.08
81
0.10
1T
heft
0.11
10.
149
0.23
00.
121
00
0.39
00.
089
0.39
90.
249
0.26
40
00.
029
0.29
20.
074
0.77
10.
741
0.10
11
Tabl
e3:
Ran
king
ofla
bels
imila
rity
Bur
glar
yFa
lse
Bur
glar
yC
ompl
icat
edSa
le1
Com
plic
ated
Sale
2Fa
iled
Sale
1Fa
iled
Sale
2G
ift
Hig
her
Off
erL
oan
Off
erE
xtor
tion
Off
erIn
vita
tion
Rob
bery
succ
esR
obbe
ryU
nsuc
ces
Sale
Swap
Eva
sion
Swap
The
ftfa
lse
The
ftjo
yT
heft
rath
erT
heft
Bur
glar
yFal
sex
115
1411
184
163
510
89
1711
196
72
11B
urgl
ary
2x
1615
168
716
1310
146
316
119
115
14
Com
plic
ated
Sale
117
18x
113
25
106
114
1516
314
127
918
7C
ompl
icat
edSa
le2
1718
1x
162
47
56
314
158
910
1113
1911
Faile
dSal
e17
146
9x
113
411
85
1011
314
214
1414
14Fa
iledS
ale2
1210
56
4x
152
1311
79
81
163
1616
1416
Gif
t9
148
1017
18x
121
54
1115
167
132
619
2H
ighe
rOff
er17
187
85
211
x9
64
1516
110
312
1418
12L
oan
918
810
1517
112
x5
613
1416
711
23
194
Off
erE
xtor
tion
1217
1113
1518
29
3x
15
616
414
710
197
Off
erIn
vita
tion
1718
511
1216
24
31
x14
1512
610
69
196
Rob
bery
Succ
es4
79
1014
116
138
23
x1
1216
1516
165
16R
obbe
ryU
nsuc
ces
53
910
147
1512
82
41
x11
1613
1616
616
Sale
1218
57
41
132
1110
68
9x
143
1417
1814
Swap
Eva
sion
1113
89
1515
510
61
415
1514
x12
26
152
Swap
1816
67
42
93
810
511
121
17x
1315
1913
The
ftFa
lse
913
810
1515
411
37
615
1514
512
x1
152
The
ftJo
y9
108
1116
164
133
76
1616
155
141
x12
2T
heft
Rat
her
21
99
97
99
99
93
49
98
96
x5
The
ft11
98
1016
164
133
76
1616
155
141
212
x
12
Tabl
e4:
Stru
ctur
alsi
mila
rity
-num
bero
fins
ertio
nsan
dde
letio
ns
Bur
glar
yFa
lse
Bur
glar
yC
ompl
icat
edSa
le1
Com
plic
ated
Sale
2Fa
iled
Sale
1Fa
iled
Sale
2G
ift
Hig
her
Off
erL
oan
Off
erE
xtor
tion
Off
erIn
vita
tion
Rob
bery
succ
esR
obbe
ryU
nsuc
ces
Sale
Swap
Eva
sion
Swap
The
ftfa
lse
The
ftjo
yT
heft
rath
erT
heft
Bur
glar
yFal
se0
1257
6910
5537
8037
2632
3633
6236
5130
3935
30B
urgl
ary
121
5464
756
4476
3424
3432
3264
3146
2938
2929
Com
plic
ated
Sale
157
5411
5828
6641
8046
5740
3434
7046
6024
3333
24C
ompl
icat
edSa
le2
6964
588
4090
7288
5963
7355
4691
5771
6670
5866
Faile
dSal
e110
728
400
5545
7337
2225
3232
6334
4329
3829
29Fa
iledS
ale2
5556
6690
551
5562
5357
2441
4111
5137
3847
3138
Gif
t37
4441
7245
550
8247
2742
3638
7641
5457
4521
57H
ighe
rOff
er80
7680
8873
6282
4042
3933
3437
6049
4042
5135
42L
oan
3734
4659
3753
4742
535
2733
3281
4860
2736
4227
Off
erE
xtor
tion
2624
5763
2257
2739
358
3040
4168
3446
3443
2934
Off
erIn
vita
tion
3234
4073
2524
4233
2730
530
3552
4139
2938
4429
Rob
bery
Succ
es36
3234
5532
4136
3433
4030
013
7047
5926
3644
26R
obbe
ryU
nsuc
ces
3332
3446
3241
3837
3241
3513
069
4766
2837
4728
Sale
6264
7091
6311
7660
8168
5270
691
5243
3746
3137
Swap
Eva
sion
3631
4657
3451
4149
4834
4147
4752
051
3645
3136
Swap
5146
6071
4337
5440
6046
3959
6643
510
3847
2638
The
ftFa
lse
3029
2466
2938
5742
2734
2926
2837
3638
010
270
The
ftJo
y39
3833
7038
4745
5136
4338
3637
4645
4710
540
10T
heft
Rat
her
3529
3358
2931
2135
4229
4444
4731
3126
2740
128
The
ft30
2924
6629
3857
4227
3429
2628
3736
380
1028
0
Tabl
e5:
Ran
king
ofst
ruct
ural
sim
ilari
ty
Bur
glar
yFa
lse
Bur
glar
yC
ompl
icat
edSa
le1
Com
plic
ated
Sale
2Fa
iled
Sale
1Fa
iled
Sale
2G
ift
Hig
her
Off
erL
oan
Off
erE
xtor
tion
Off
erIn
vita
tion
Rob
bery
succ
esR
obbe
ryU
nsuc
ces
Sale
Swap
Eva
sion
Swap
The
ftfa
lse
The
ftjo
yT
heft
rath
erT
heft
Bur
glar
yFal
sex
216
181
1511
1911
36
97
179
144
138
4B
urgl
ary
2x
1517
116
1319
103
108
817
714
412
44
Com
plic
ated
Sale
113
12x
153
179
1910
138
66
1810
161
44
1C
ompl
icat
edSa
le2
129
5x
118
1517
78
163
219
414
1013
510
Faile
dSal
e12
15
14x
1716
1912
34
99
1811
156
136
6Fa
iledS
ale2
1215
1819
12x
1217
1116
27
71
104
59
35
Gif
t4
96
1710
14x
1912
28
35
186
1315
101
15H
ighe
rOff
er15
1617
1913
1419
x8
51
24
1311
68
123
8L
oan
96
1317
916
1411
x7
15
419
1518
18
111
Off
erE
xtor
tion
32
1618
116
411
10x
612
1319
715
714
57
Off
erIn
vita
tion
810
1419
21
169
36
x6
1118
1513
412
174
Rob
bery
Succ
es10
58
175
1410
87
134
x1
1916
182
1015
2R
obbe
ryU
nsuc
ces
74
815
413
1210
413
91
x19
1618
210
162
Sale
1012
1519
111
179
1813
715
14x
75
36
23
Swap
Eva
sion
51
1119
316
815
143
812
1218
x16
510
15
Swap
129
1619
72
146
169
515
187
12x
311
13
The
ftFa
lse
118
319
815
1817
512
84
714
1315
x2
51
The
ftJo
y10
73
197
1613
184
127
46
1513
161
x11
1T
heft
Rat
her
125
1119
58
112
155
1616
188
82
314
x4
The
ft11
83
198
1518
175
128
46
1413
151
26
x
13
Structure LabelBurglaryFalse 0,036 0,311
Burglary 0,414 0,750ComplicatedSale1 -0,521 0,707ComplicatedSale2 -0,360 0,753
FailedSale1 -0.278 0,683FailedSale2 -0,332 0,601
Gift -0,341 0,162HigherOffer 0,049 0,824
Loan -0,546 0,353OfferExtortion -0,164 -0,271OfferInvitation -0,059 0,336RobberySucces 0,223 -0,05
RobberyUnsucces 0,109 0,253Sale -0,346 0,026
SwapEvasion 0,082 0,026Swap -0,021 0,588
TheftFalse 0,43 0,046TheftJoy 0,382 0,112
TheftRather -0,281 0,518Theft 0,303 0,001
Table 6: Correlation matrix of manual against structure and label
5.3 Correlation matrix
Spearman’s rank correlation matrix algorithm is used to compare the ranking of each story from eachmeasure to the manual ranking. However Spearman’s ranking cannot compare ranks with the mulitplesimilar rankings, therefore an expansion is used, called PvRank, to computed the correlation matrix.PvRank (Amerise et al., 2015) is an package for R (for statistical Computing, 2014) with different corre-lation matrices and the algorithm rankor is used to create the ρ which stands the correlation between thetwo rankings. It ranges from -1 till 1 where 1 means positively correlated and -1 negatively correlated.The usage of the function rankor is:
rankor(x, y, index = ”spearman”, approx = ”exact”, tiex = ”woodbury”, CC = FALSE, type =”two-sided”, print = TRUE, sizer = 1000000, repl = 1000).
Where x an y are the ranked stories which need to be compared and the index says which correlationalgorithm to use. The variables sizer and repl are used for the same ranking problem. They are thenumber of random steps it will do to create a new ranking when it encounters a similar ranking.
The results are displayed in table 6. The ranking of the label comparison is mostly positively cor-related and OfferExtortion and RobberySucces stories are low negativily correlated. The ranking ofstructural comparison, however, is mostly negatively correlated because human comparison is not basedon the length or complexity of the story.In both cases the Burglary story has a high correlation because it is a medium-sized story and has thetypical labels for the subjects theft and burglary. The Loan story has the lowest correlation but only withstructural comparison because it is a small story. However it has a high frequency of labels which canbe compared well with the label comparison
14
6 Conclusion & Discussion
In conclusion, the label comparison measure is better than the structural comparison to compare storieson subject and events which two aspects of human comparison. However it does not account for intentor the storyline and it is a mechanical way of comparing two stories which was not the comparisonmeasure this thesis wanted to find.The results of the structural comparison measure are skewed, because two similar stories do not resultin 0 insertions or deletions and the multiple way to go from the emitter to the collector do not accountfor fawlty measures. When inserting and deleting nodes the program probably sets too many nodes inthe remember list, which it does not visit again therefore it forgets these nodes while it deletes or insertsanother one.The weakness of the label comparison measure is that it does not do label matching and therefore itonly matches on exact labels. For example waiting for goods is not considered the same as waiting forgood and buying is not the same as buys. Whereas this could be an extension to get a higher correlation.However comparing exact labels has an advantage as well. All of the stories overlap because they arefrom the same domain and exact label matching provides strong difference in the results. While partiallabel matching can flatten the results by matching keywords which are used in every legal story.This thesis shows us that implementing one of these measures is not accurate enough for comparingstories in a humanlike manner. A combination of label and structural comparison could be a solutionto achieve a higher correlation between the rankings, because it should insert and delete transitions andplaces based on their labels instead of just inserting and deleting when necessary. The labels are alsocompared with the cosine similarity and therefore it creates a measure which looks at the complexity ofthe story and the subject.The difference in execution should be a measure to compare stories based on intent or the storyline,because it follows the firing of the tokens instead predefined labels or structure. This could receive apositive correlation with the manual ranking because it is less mechanical as label comparison or struc-tural and it is explored in this thesis but not implemented.
7 Future work
There is not yet a conventional way to model and represent stories in Petri nets. Future research couldbe done to develop guidelines for this, thus the Petri nets can have similar representations for negationor failure.Next research could be implementing the difference in execution measure. This could be very promising,because of its potential to represent the storyline and the flow of the tokens. The execution of the Petrinet can also be added to the structural comparison to create a structural comparison where the tokens aretaking into consideration.Other future work is to correct the structural comparison measure and to receive better results whichcould be used to create a new correlation matrix.As mentioned in section 6 a combination of label comparison and structural comparison could lead to anhigher correlation as well. Combining this with the execution plan could maybe result in an improvedcorrelation and therefore the comparison measure could imitate human comparison better.In the end there is no complete comparison measure to simulate humanlike comparison yet.
15
8 Bibliography
Amerise, I., Marozzi, M., and Tarsitano, A. (2015). pvrank: Rank correlations. www.cran.r-project.org/web/packages/pvrank/index.html.
ASPT, S. (2005). Yasper. https://www.yasper.org.
Behrens, T. and Dix, J. (2007). Model checking multi-agent system with logic based petri nets. Annalsof Mathematics and AI, 51:81–121.
Dijkman, R., Dumas, M., van Dongen, B., Kaarik, R., and Mendling, J. (2011). Similarity of businessprocess models: Metrics and evaluation. Information Systems, 36:498–516.
Fisseni, B. and Lowe, B. (2012). Which dimensions of narrative are relevant for human judgements ofstory equivalence? Workshop on Computational Models of Narrative, 3:114–118.
for statistical Computing, T. R. F. (2014). R. https://www.r-project.org.
Jetbrains (2015). Intellij idea. https://www.jetbrains.com/idea.
Lehmann, J., Breuker, J., and Brouwer, B. (2005). Modeling causation in ai & law. Law and the semanticweb, pages 77–96.
Lowe, B. (2011). Methodological remarks about comparing formal frameworks for narratives. Proceed-ings of the 3rd Workshop in the Philosophy of Information, pages 45–63.
Murata, T. (1989). Petri nets: properties, analysis and applications. Proceedings of the IEEE, 77(4):541–580.
Schank, R. and Abelson, R. (1979). Scripts, plans, goals and understanding, an inquiry into humanknowledge structures. Journal of Pragmatics, 3(2):211–217.
Sileno, G. and Boer, A. (2014). Legal knowledge conveyed by narratives: Towards a representationalmodel. Proceedings of the Workshop on Computational Models of Narrative (CMN 2014), pages182–191.
16
A Short stories
SaleStoryX makes an offer. The offer concerns a good exchanged for a certain amount of money. Y accepts theoffer. Y pays the money. X delivers the good.
FailedSale1StoryX makes an offer. The offer concerns a good exchanged for a certain amount of money. Nobody acceptsthe offer.
FailedSale2StoryX makes an offer. The offer concerns a good exchanged for a certain amount of money. Y accepts theoffer. Y pays the money. X does not deliver.
ComplicatedSale1StoryX buys a good from Y. Y sends the good to X. The good does not arrive. X complains to Y. The goodarrives in the meanwhile.
ComplicatedSale2StoryX buys a good from Y. Y sends the good to X. The good does not arrive. X complains to Y. Y asks towait some says more. The good does not arrive. X complains to Y, again. Y sends another good to X.
GiftStoryX wishes a certain good. Y gives that good to him.
SwapStoryX makes an offer. The offer concerns a good exchanged for another good. Y accepts the offer. X deliversthe good. Y delivers the other good.
SwapEvasionStoryGoods of type A and B usually cost a certain price. When you sell a good of type A or B you have topay taxes on it. X sells a good A to Y for half of the normal price. Y sells a good B to X for half of thenormal price.
LoanStoryX needs a certain good. Y lends that good to him. X gives the good back to X.
TheftStoryX owns a certain good. Y takes it with him
TheftFalseStoryX owns a car. Y takes it to bring someone to the urgencies. [theft is characterized by the intent ofpermanently deprive the owner of it]
TheftJoyStoryX owns a beautiful car. Y takes the car without consent, drives it for a spin and returns it where it was.
BurglaryStoryX enters in the house of Y, in order to steal something. [burglary is characterized by entering without
17
consent, with criminal intents]
TheftRatherStoryY invites X to his house. X enters in the house of Y, in order to steal something
BurglaryFalseStoryX enters in the house of Y, as the building was in flame and was trying an escape.
RobberySuccesStoryY is cashier at the supermarket. X threatens to shoot Y unless he hands over the loot. Y hands over theloot.
RobberyUnsuccesStoryY is cashier at the supermarket. X threatens to shoot Y unless he hands over the loot. Y says there is nomoney, it is too early. X sees that there is no money, and runs.
OfferInvitationStoryX offers Y to sell a good for a certain amount of money. Y accepts and sells the good to X.
OfferExtortionStoryX threats Y to sell a good for a certain amount of money. Y sell the good to X.
HigherOfferStoryX offers to sell good to Y. Y accepts the offer. Z offers to buy good at higher price. X accepts Z’s offer.X sells good to Z.
18
B Groovy code
B.1 comparison.groovy
package org . l e i b n i z c e n t e r . pneu . compar i son
i m p o r t groovy . u t i l . l o g g i n g . Log4ji m p o r t o rg . l e i b n i z c e n t e r . pneu . components . p e t r i n e t . Neti m p o r t o rg . l e i b n i z c e n t e r . pneu . components . p e t r i n e t . P l a c ei m p o r t o rg . l e i b n i z c e n t e r . pneu . components . p e t r i n e t . Nodei m p o r t o rg . l e i b n i z c e n t e r . pneu . components . p e t r i n e t . T r a n s i t i o n
i m p o r t o rg . s i m m e t r i c s . S t r i n g M e t r i c ;i m p o r t o rg . s i m m e t r i c s . S t r i n g M e t r i c s ;
@Log4jc l a s s Comparison {
/ / / / / / / / / / / / / / / / / LABEL COMPARISON
s t a t i c F l o a t l a b e l C o m p a r i s o n ( Net sou rceNe t , Net t a r g e t N e t ) {L i s t<Place> s o u r c e P l a c e s = s o u r c e N e t . p l a c e L i s tL i s t<Place> t a r g e t P l a c e s = t a r g e t N e t . p l a c e L i s t
L i s t<T r a n s i t i o n> s o u r c e T r a n s i t i o n s = s o u r c e N e t . t r a n s i t i o n L i s tL i s t<T r a n s i t i o n> t a r g e t T r a n s i t i o n s = t a r g e t N e t . t r a n s i t i o n L i s t
/ / With empty nodes :/ / I n t e g e r l e n g t h P l a c e = s o u r c e P l a c e s . s i z e ( ) + t a r g e t P l a c e s . s i z e ( )
/ / D e f in e l e n g t h w i t h o u t empty nodesI n t e g e r l e n g t h S o u r c e P l a c e s = s o u r c e P l a c e s . f i n d A l l ( ) { i t . name != ”” } . s i z e ( )I n t e g e r l e n g t h T a r g e t P l a c e s = t a r g e t P l a c e s . f i n d A l l ( ) { i t . name != ”” } . s i z e ( )
I n t e g e r l e n g t h P l a c e s = l e n g t h S o u r c e P l a c e s + l e n g t h T a r g e t P l a c e s
I n t e g e r d i f f 1 = 0F l o a t s i m P l a c e = 0F l o a t s imTrans = 0
f o r ( i i n s o u r c e P l a c e s ) {F l o a t sim = 0I n t e g e r d i f f 2 = 0i f ( i . name != ” ” ) {
f o r ( j i n t a r g e t P l a c e s ) {I n t e g e r d i f f = Math . abs ( d i f f 2 − d i f f 1 )l o g . t r a c e ( ” compar ing ”+ i . name + ”=?=” + j . name )i f ( i . name == j . name ) {
sim = (2 ∗ d i f f ) / l e n g t h P l a c e sl o g . t r a c e ( ” found e q u a l : $sim $ d i f f $ l e n g t h P l a c e s ” )s i m P l a c e = s i m P l a c e + sim
}d i f f 2 ++
}}d i f f 1 ++
}
I n t e g e r l e n g t h S o u r c e T r a n s i t i o n s = s o u r c e T r a n s i t i o n s . f i n d A l l ( ) { i t . name != ”” } . s i z e ( )I n t e g e r l e n g t h T a r g e t T r a n s i t i o n s = t a r g e t T r a n s i t i o n s . f i n d A l l ( ) { i t . name != ”” } . s i z e ( )
I n t e g e r l e n g t h T r a n s i t i o n s = l e n g t h S o u r c e T r a n s i t i o n s + l e n g t h T a r g e t T r a n s i t i o n s
d i f f 1 = 0
f o r ( i i n s o u r c e T r a n s i t i o n s ) {F l o a t sim = 0I n t e g e r d i f f 2 = 0i f ( i . name != ” ” ) {
f o r ( j i n t a r g e t T r a n s i t i o n s ) {I n t e g e r d i f f = Math . abs ( d i f f 2 − d i f f 1 )i f ( i . name == j . name ) {
sim = (2 ∗ d i f f ) / l e n g t h T r a n s i t i o n ss imTrans = s imTrans + sim
}d i f f 2 ++
}}d i f f 1 ++
}
/ / C a l c u l a t e l a b e l s i m i l a r i t yr e t u r n (2 ∗ ( s i m P l a c e + s imTrans ) ) / ( l e n g t h P l a c e s + l e n g t h T r a n s i t i o n s )
}
s t a t i c c l a s s NodeComparisonValue {F l o a t v a l u eBoolean a c t i v e
S t r i n g t o S t r i n g ( ) { r e t u r n ”{ $va lue , $ a c t i v e }” }}
19
s t a t i c c l a s s NodeComparisonKey {Node s o u r c eNode t a r g e t
S t r i n g t o S t r i n g ( ) { r e t u r n ”{${ s o u r c e . i d } , ${ t a r g e t . i d}}” }}
s t a t i c F l o a t l a b e l C o m p a r i s o n 2 ( Net sou rceNe t , Net t a r g e t N e t ) {L i s t<Place> s o u r c e P l a c e s = s o u r c e N e t . p l a c e L i s tL i s t<Place> t a r g e t P l a c e s = t a r g e t N e t . p l a c e L i s t
L i s t<T r a n s i t i o n> s o u r c e T r a n s i t i o n s = s o u r c e N e t . t r a n s i t i o n L i s tL i s t<T r a n s i t i o n> t a r g e t T r a n s i t i o n s = t a r g e t N e t . t r a n s i t i o n L i s t
/ / s o u r c e node , t a r g e t node , s i m i l a r i t y va lue , a c t i v eMap<NodeComparisonKey , NodeComparisonValue> l a b e l S i m i l a r i t y = [ : ]
S t r i n g M e t r i c m e t r i c = S t r i n g M e t r i c s . c o s i n e S i m i l a r i t y ( )
/ / compute t h e s i m i l a r i t y ove r a l l t h e nodes ( no d i s t i n c t i o n between p l a c e s and t r a n s i t i o n s )L i s t<Node> sourceNodes = s o u r c e P l a c e s + s o u r c e T r a n s i t i o n sL i s t<Node> t a r g e t N o d e s = t a r g e t P l a c e s + t a r g e t T r a n s i t i o n s
/ / f i n d how many nodes a r e l a b e l e dI n t e g e r nLabeledNodes = sourceNodes . f i n d A l l ( ) { i t . name != ”” } . s i z e ( ) +
t a r g e t N o d e s . f i n d A l l ( ) { i t . name != ”” } . s i z e ( )
f o r ( Node i i n sourceNodes ) {f o r ( Node j i n t a r g e t N o d e s ) {
i f ( i . name && j . name )/ / l a b e l S i m i l a r i t y [ i , j ] = m e t r i c . compare ( i . name , j . name )
l a b e l S i m i l a r i t y [ new NodeComparisonKey ( s o u r c e : i , t a r g e t : j ) ] = new NodeComparisonValue (v a l u e : m e t r i c . compare ( i . name , j . name ) ,a c t i v e : t r u e
)}
}
/ / g r e ed y a l g o r i t h mF l o a t max = 1F l o a t s i m i l a r i t y = 0
w h i l e ( max != 0) {F l o a t newMax = 0
/ / f i n d maxf o r ( NodeComparisonKey key i n l a b e l S i m i l a r i t y . k ey Se t ( ) ) {
NodeComparisonValue nodeCompar isonValue = l a b e l S i m i l a r i t y [ key ]i f ( nodeCompar isonValue . a c t i v e ) {
F l o a t v a l u e = nodeCompar isonValue . v a l u ei f ( v a l u e == max ) {
l o g . t r a c e ( key . t o S t r i n g ( ) + ” i s a max ( $max ) ” )s i m i l a r i t y += v a l u e
/ / pu rgef o r ( NodeComparisonKey inne rKey i n l a b e l S i m i l a r i t y . k ey Se t ( ) ) {
NodeComparisonValue nodeCompar i son Inne rVa lue = l a b e l S i m i l a r i t y [ inne rKey ]i f ( nodeCompar i son Inne rVa lue . a c t i v e ) {
i f ( key . s o u r c e == inne rKey . s o u r c e | | key . t a r g e t == inne rKey . t a r g e t ) {nodeCompar i son Inne rVa lue . a c t i v e = f a l s el o g . t r a c e ( ” remove ” + key . t o S t r i n g ( ) )
}}
}} e l s e {
i f ( newMax < v a l u e ) {l o g . t r a c e ( ” new p o t e n t i a l max ( $ v a l u e ) ” )newMax = v a l u e
}}
}}
max = newMax
}
/ / w e i g h t e d by t h e number o f l a b e l e d nodesr e t u r n 2 ∗ s i m i l a r i t y / nLabeledNodes
}
/ / / / / / / / / / / / / / / / / STRUCTURAL COMPARISON
/∗∗∗∗∗∗∗∗∗∗S t r u c t u r a l
− Use a r c s t o f i n d t a r g e t and s o u r c e− Does n o t r e f e r t o l a b e l l i n g∗∗∗∗∗∗∗∗∗∗/
d e f s t a t i c s t r u c t u r a l C o m p a r i s o n ( Net sou rceNe t , Net t a r g e t N e t ) {
20
L i s t<Connec t ion> s o u r c e C o n n e c t i o n sL i s t<Connec t ion> t a r g e t C o n n e c t i o n s
/ / c o u n t t h e number o f incoming and o u t g o i n g a r c s f o r s o u r c e n e ts o u r c e C o n n e c t i o n s = C o n n e c t i o n . r e a d C o n n e c t i o n s ( s o u r c e N e t )/ / c o u n t t h e number o f incoming and o u t g o i n g a r c s f o r t a r g e t n e tt a r g e t C o n n e c t i o n s = C o n n e c t i o n . r e a d C o n n e c t i o n s ( t a r g e t N e t )
/ / f i n d b e g i n p o i n tC o n n e c t i o n s o u r c e B e g i n P o i n t = C o n n e c t i o n . s e a r c h C o l l e c t o r ( s o u r c e C o n n e c t i o n s )C o n n e c t i o n t a r g e t B e g i n P o i n t = C o n n e c t i o n . s e a r c h C o l l e c t o r ( t a r g e t C o n n e c t i o n s )
/ / i n i t i a l v a l u e sL i s t<Connec t ion> v i s i t e d S o u r c e C o n n e c t i o n s = [ ]L i s t<Connec t ion> v i s i t e d T a r g e t C o n n e c t i o n s = [ ]C o n n e c t i o n sourceNode = s o u r c e B e g i n P o i n tC o n n e c t i o n t a r g e t N o d e = t a r g e t B e g i n P o i n tL i s t<Connec t ion> newConnec t ions = [ ]L i s t<Connec t ion> rememberSourceNodes = [ ]L i s t<Connec t ion> rememberTarge tNodes = [ ]I n t e g e r a d d D i f f = 0I n t e g e r d e l D i f f = 0
/∗I n s e r t i n g and d e l e t i n g nodes i f n e c e s s a r y ,remember nodes i f t h e y have m u l t i p l e a r c s
− R e p l a c e s nodes when v i s i t e d i n t o v i s i t e d C o n n e c t i o n− f i n d s n e x t nodes depend ing on c u r r e n t node ( end , b e g i n n i n g , remembered )∗ /
w h i l e ( s o u r c e C o n n e c t i o n s . s i z e ( ) > 0 && sourceNode && t a r g e t N o d e ) {
C o n n e c t i o n n e x t S o u r c e C o n n e c t i o n , n e x t T a r g e t C o n n e c t i o nBoolean s o u r c e B a c k t r a c k , t a r g e t B a c k t r a c k
l o g . t r a c e (”######################## New c y c l e : sourceNode $sourceNode , t a r g e t N o d e $ t a r g e t N o d e . ” )
i f ( sourceNode . n T a r g e t O u t A r c s != 0 && t a r g e t N o d e . n T a r g e t O u t A r c s != 0 && t a r g e t N o d e . n T a r g e t O u t A r c s != −1) {l o g . t r a c e ( ” t h e s o u r c e s o f t h e a r c s have bo th a number o f o u t p u t > 0 . . ” )
i f ( sourceNode . nSourceOutArcs == t a r g e t N o d e . nSourceOutArcs ) {l o g . t r a c e ( ” t h e y have t h e same number o f o u t p u t a r c s a t t h e s o u r c e ” )
( n e x t S o u r c e C o n n e c t i o n , s o u r c e B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d S o u r c e C o n n e c t i o n s , s o u r c e C o n n e c t i o n s , sourceNode , rememberSourceNodes )( n e x t T a r g e t C o n n e c t i o n , t a r g e t B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d T a r g e t C o n n e c t i o n s , t a r g e t C o n n e c t i o n s , t a r g e t N o d e , rememberTarge tNodes )
/ / p r i n t l n ” Next ” + n e x t C o n n e c t i o n + n e x t T a r g e t C o n n e c t i o ni f ( s o u r c e B a c k t r a c k ) {
l o g . t r a c e ( ” b a c k t r a c k ! ” )n e x t T a r g e t C o n n e c t i o n = rememberTarge tNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
} e l s e i f ( t a r g e t B a c k t r a c k ){l o g . t r a c e ( ” b a c k t r a c k ! ” )n e x t S o u r c e C o n n e c t i o n = rememberSourceNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
}
L i s t<Connec t ion> rSourceNodes = C o n n e c t i o n . remember ( s o u r c e C o n n e c t i o n s , sourceNode )L i s t<Connec t ion> r T a r g e t N o d e s = C o n n e c t i o n . remember ( t a r g e t C o n n e c t i o n s , t a r g e t N o d e )i f ( rSourceNodes . s i z e ( ) > 0) {
f o r ( rSourceNode i n rSourceNodes ) {f o r ( rTa rge tNode i n r T a r g e t N o d e s ) {
rememberSourceNodes << rSourceNoderememberTarge tNodes << rTa rge tNodes o u r c e C o n n e c t i o n s . remove ( rSourceNode )t a r g e t C o n n e c t i o n s . remove ( rTa rge tNode )
}}
}l o g . t r a c e ( ” Next $ n e x t S o u r c e C o n n e c t i o n $ n e x t T a r g e t C o n n e c t i o n ” )
} e l s e i f ( sourceNode . nSourceOutArcs > t a r g e t N o d e . nSourceOutArcs ) { / / Addl o g . t r a c e ( ” t h e f i r s t has more o u t p u t t h a n t h e second on t h e sou rce , I add one t h e second ” )
I n t e g e r d i f f = sourceNode . nSourceOutArcs − t a r g e t N o d e . nSourceOutArcsC o n n e c t i o n . sea rchBySource IdAdd ( t a r g e t C o n n e c t i o n s , t a r g e t N o d e . s o u r c e I d , d i f f )
f o r ( i n t i = 0 ; i < d i f f ; i ++) {C o n n e c t i o n newNode = new C o n n e c t i o n (
s o u r c e I d : t a r g e t N o d e . s o u r c e I d ,t a r g e t I d : ’n ’ + addDi f f ,n S o u r c e I n A r c s : t a r g e t N o d e . nSource InArcs ,nSourceOutArcs : t a r g e t N o d e . nSourceOutArcs −1,n T a r g e t O u t A r c s : −1
)newConnec t ions . add ( 0 , newNode )a d d D i f f ++
}
( n e x t S o u r c e C o n n e c t i o n , s o u r c e B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d S o u r c e C o n n e c t i o n s , s o u r c e C o n n e c t i o n s , sourceNode , rememberSourceNodes )( n e x t T a r g e t C o n n e c t i o n , t a r g e t B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d T a r g e t C o n n e c t i o n s , t a r g e t C o n n e c t i o n s , t a r g e t N o d e , rememberTarge tNodes )
i f ( s o u r c e B a c k t r a c k ) {
21
l o g . t r a c e ( ” b a c k t r a c k ! ” )n e x t T a r g e t C o n n e c t i o n = rememberTarge tNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
}
L i s t<Connec t ion> rSourceNodes = C o n n e c t i o n . remember ( s o u r c e C o n n e c t i o n s , sourceNode )L i s t<Connec t ion> r T a r g e t N o d e s = C o n n e c t i o n . remember ( t a r g e t C o n n e c t i o n s , t a r g e t N o d e )i f ( rSourceNodes . s i z e ( ) > 0) {
rememberSourceNodes << rSourceNodes [ 0 ]rememberTarge tNodes << newConnec t ions [ 0 ]
i f ( t a r g e t N o d e . nSourceOutArcs >= 2) {rememberSourceNodes << rSourceNodes [ 1 ]rememberTarge tNodes << r T a r g e t N o d e s [ 0 ]s o u r c e C o n n e c t i o n s . remove ( rSourceNodes [ 1 ] )t a r g e t C o n n e c t i o n s . remove ( r T a r g e t N o d e s [ 0 ] )
}s o u r c e C o n n e c t i o n s . remove ( rSourceNodes [ 0 ] )
}
p r i n t l n ”Add ” + n e x t S o u r c e C o n n e c t i o n + n e x t T a r g e t C o n n e c t i o n
} e l s e i f ( sourceNode . nSourceOutArcs < t a r g e t N o d e . nSourceOutArcs ) { / / d e ll o g . t r a c e ( ” t h e f i r s t has l e s s o u t p u t s t h a n t h e second on t h e sou rce , I remove from t h e second ” )
I n t e g e r d i f f = t a r g e t N o d e . nSourceOutArcs − sourceNode . nSourceOutArcsL i s t<Connec t ion> c o n n e c t i o n L i s t = C o n n e c t i o n . sea rchBySource IdAdd ( t a r g e t C o n n e c t i o n s , t a r g e t N o d e . s o u r c e I d , −d i f f )
f o r ( i n t i = 0 ; i < d i f f ; i ++) {c o n n e c t i o n L i s t . remove ( t a r g e t N o d e )i f ( c o n n e c t i o n L i s t [ 0 ] ) / / TOCHECK: t h e r e i s some NULL around !
v i s i t e d T a r g e t C o n n e c t i o n s << c o n n e c t i o n L i s t [ 0 ]t a r g e t C o n n e c t i o n s . remove ( c o n n e c t i o n L i s t [ 0 ] )c o n n e c t i o n L i s t . remove ( c o n n e c t i o n L i s t [ 0 ] )d e l D i f f ++
}
( n e x t S o u r c e C o n n e c t i o n , s o u r c e B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d S o u r c e C o n n e c t i o n s , s o u r c e C o n n e c t i o n s , sourceNode , rememberSourceNodes )( n e x t T a r g e t C o n n e c t i o n , t a r g e t B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d T a r g e t C o n n e c t i o n s , t a r g e t C o n n e c t i o n s , t a r g e t N o d e , rememberTarge tNodes )
i f ( s o u r c e B a c k t r a c k ) {n e x t T a r g e t C o n n e c t i o n = rememberTarge tNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
}L i s t<Connec t ion> rSourceNodes = C o n n e c t i o n . remember ( s o u r c e C o n n e c t i o n s , sourceNode )L i s t<Connec t ion> r T a r g e t N o d e s = C o n n e c t i o n . remember ( t a r g e t C o n n e c t i o n s , t a r g e t N o d e )
i f ( rSourceNodes . s i z e ( ) > 0) {f o r ( rSourceNode i n rSourceNodes ){
f o r ( rTa rge tNode i n r T a r g e t N o d e s ){rememberSourceNodes << rSourceNoderememberTarge tNodes << rTa rge tNodes o u r c e C o n n e c t i o n s . remove ( rSourceNode )t a r g e t C o n n e c t i o n s . remove ( rTa rge tNode )
}}
}
p r i n t l n ” Del ” + sourceNode + t a r g e t N o d e}
/ / I f one i s an end node and t h e o t h e r one i s n o t} e l s e i f ( sourceNode . n T a r g e t O u t A r c s > 0 && t a r g e t N o d e . n T a r g e t O u t A r c s == 0) {
l o g . t r a c e ( ” t h e f i r s t i s n o t an end node , t h e second yes ” )
C o n n e c t i o n newNode = new C o n n e c t i o n (s o u r c e I d : ” n”+ addDi f f ,t a r g e t I d : t a r g e t N o d e . t a r g e t I d ,n S o u r c e I n A r c s : t a r g e t N o d e . nSource InArcs ,nSourceOutArcs : 1
)newConnec t ions << newNodea d d D i f f ++
( n e x t S o u r c e C o n n e c t i o n , s o u r c e B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d S o u r c e C o n n e c t i o n s , s o u r c e C o n n e c t i o n s , sourceNode , rememberSourceNodes )
i f ( s o u r c e B a c k t r a c k ) {n e x t T a r g e t C o n n e c t i o n = rememberTarge tNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
} e l s e {n e x t T a r g e t C o n n e c t i o n = newNode
}
L i s t<Connec t ion> rSourceNodes = C o n n e c t i o n . remember ( s o u r c e C o n n e c t i o n s , sourceNode )i f ( rSourceNodes . s i z e ( ) > 0) {
f o r ( rSourceNode i n rSourceNodes ) {rememberSourceNodes << rSourceNoderememberTarge tNodes << t a r g e t N o d e
}}
} e l s e i f ( sourceNode . n T a r g e t O u t A r c s == 0 && t a r g e t N o d e . n T a r g e t O u t A r c s > 0){l o g . t r a c e ( ” t h e f i r s t i s an end node , t h e second no ” )
22
/ / d e l e t e noded e l D i f f ++
n e x t S o u r c e C o n n e c t i o n = sourceNode( n e x t T a r g e t C o n n e c t i o n , t a r g e t B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d T a r g e t C o n n e c t i o n s , t a r g e t C o n n e c t i o n s , t a r g e t N o d e , rememberTarge tNodes )
/ / p r i n t l n ”NODE ” + t a r g e t N o d e + n e x t T a r g e t C o n n e c t i o n/ / DELETE ALL INSTANCES TILL 0 FROM t a r g e t C o n n e c t i o n s
i f ( t a r g e t B a c k t r a c k ) {l o g . t r a c e ( ” b a c k t r a c k ! ” )n e x t S o u r c e C o n n e c t i o n = rememberSourceNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
} e l s e {n e x t S o u r c e C o n n e c t i o n = sourceNode
}
L i s t<Connec t ion> r T a r g e t N o d e s = C o n n e c t i o n . remember ( t a r g e t C o n n e c t i o n s , t a r g e t N o d e )i f ( r T a r g e t N o d e s . s i z e ( ) > 0) {
f o r ( rSourceNode i n r T a r g e t N o d e s ) {rememberTarge tNodes << rSourceNoderememberSourceNodes << sourceNode
}}
/ / i f bo th a r e end nodes , f i n d t h e n e x t remembered node or s t a r t from b e g i n n i n g} e l s e i f ( sourceNode . n T a r g e t O u t A r c s == 0 && t a r g e t N o d e . n T a r g e t O u t A r c s <= 0) {
l o g . t r a c e ( ” t h e y a r e bo th end nodes ” )
( n e x t S o u r c e C o n n e c t i o n , s o u r c e B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d S o u r c e C o n n e c t i o n s , s o u r c e C o n n e c t i o n s , sourceNode , rememberSourceNodes )( n e x t T a r g e t C o n n e c t i o n , t a r g e t B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d T a r g e t C o n n e c t i o n s , t a r g e t C o n n e c t i o n s , t a r g e t N o d e , rememberTarge tNodes )
/ / p r i n t l n ” Next l i n e ” + n e x t C o n n e c t i o n + n e x t T a r g e t C o n n e c t i o ni f ( n e x t S o u r c e C o n n e c t i o n == n u l l && ! rememberSourceNodes . i sEmpty ( ) ) {
n e x t S o u r c e C o n n e c t i o n = rememberSourceNodes [ 0 ]n e x t T a r g e t C o n n e c t i o n = rememberTarge tNodes [ 0 ]
}
} e l s e i f ( t a r g e t N o d e . n T a r g e t O u t A r c s == −1) {l o g . t r a c e ( ” The new node needs more a d d i t i o n s ” )C o n n e c t i o n newNode = new C o n n e c t i o n (
s o u r c e I d : sourceNode . s o u r c e I d ,t a r g e t I d : ” n”+ addDi f f ,n S o u r c e I n A r c s : 1 ,nSourceOutArcs : 1 ,n T a r g e t O u t A r c s : −1
)
newConnec t ions . add ( 0 , newNode )n e x t T a r g e t C o n n e c t i o n = newConnec t ions [ 0 ]a d d D i f f ++( n e x t S o u r c e C o n n e c t i o n , s o u r c e B a c k t r a c k ) = C o n n e c t i o n . f i n d N e x t ( v i s i t e d S o u r c e C o n n e c t i o n s , s o u r c e C o n n e c t i o n s , sourceNode , rememberSourceNodes )
i f ( s o u r c e B a c k t r a c k ) {l o g . t r a c e ( ” b a c k t r a c k ! ” )n e x t T a r g e t C o n n e c t i o n = rememberTarge tNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
} e l s e i f ( t a r g e t B a c k t r a c k ){l o g . t r a c e ( ” b a c k t r a c k ! ” )n e x t S o u r c e C o n n e c t i o n = rememberSourceNodes [ 0 ]rememberSourceNodes . remove ( n e x t S o u r c e C o n n e c t i o n )rememberTarge tNodes . remove ( n e x t T a r g e t C o n n e c t i o n )
}L i s t<Connec t ion> rSourceNodes = C o n n e c t i o n . remember ( s o u r c e C o n n e c t i o n s , sourceNode )
i f ( rSourceNodes . s i z e ( ) > 0) {f o r ( rSourceNode i n rSourceNodes ){
rememberSourceNodes << rSourceNoderememberTarge tNodes << newNodes o u r c e C o n n e c t i o n s . remove ( rSourceNode )
}}
}
s o u r c e C o n n e c t i o n s . remove ( sourceNode )t a r g e t C o n n e c t i o n s . remove ( t a r g e t N o d e )
I n t e g e r removeIndex = rememberSourceNodes . indexOf ( sourceNode )rememberSourceNodes . remove ( sourceNode )i f ( removeIndex >= 0) {
rememberTarge tNodes . remove ( removeIndex )}sourceNode = n e x t S o u r c e C o n n e c t i o nt a r g e t N o d e = n e x t T a r g e t C o n n e c t i o n
/ / p r i n t l n ”Rem ” + rememberNode + rememberNode1l o g . t r a c e ” s o u r c e C o n n e c t i o n s : ” + s o u r c e C o n n e c t i o n sl o g . t r a c e ” t a r g e t C o n n e c t i o n s : ” + t a r g e t C o n n e c t i o n sl o g . t r a c e ” v i s i t e d S o u r c e C o n n e c t i o n s : ” + v i s i t e d S o u r c e C o n n e c t i o n sl o g . t r a c e ” v i s i t e d T a r g e t C o n n e c t i o n s : ” + v i s i t e d T a r g e t C o n n e c t i o n sl o g . t r a c e ” rememberSourceNodes : ” + rememberSourceNodes
23
l o g . t r a c e ” rememberTarge tNodes : ” + rememberTarge tNodesl o g . t r a c e ” d e l D i f f : ” + d e l D i f fl o g . t r a c e ” a d d D i f f : ” + a d d D i f f
}
I n t e g e r c o n S i z e = t a r g e t C o n n e c t i o n s . s i z e ( )/ / p r i n t l n c o n S i z ed e l D i f f = d e l D i f f + c o n S i z e/ / p r i n t l n ” End T o t a l d e l ” + d e l D i f f
r e t u r n [ addDi f f , d e l D i f f ]}
}
B.2 connection.groovy
package org . l e i b n i z c e n t e r . pneu . compar i son
i m p o r t groovy . u t i l . l o g g i n g . Log4ji m p o r t o rg . l e i b n i z c e n t e r . pneu . components . p e t r i n e t . Arci m p o r t o rg . l e i b n i z c e n t e r . pneu . components . p e t r i n e t . Net
@Log4jc l a s s C o n n e c t i o n {
S t r i n g s o u r c e I dS t r i n g t a r g e t I dI n t e g e r n S o u r c e I n A r c s = 0I n t e g e r nSourceOutArcs = 0I n t e g e r n T a r g e t I n A r c s = 0I n t e g e r n T a r g e t O u t A r c s = 0
s t a t i c L i s t<Connec t ion> r e a d C o n n e c t i o n s ( Net n e t ) {L i s t<Arc> a r c s = n e t . a r c L i s tL i s t<Connec t ion> c o n n e c t i o n s = [ ]f o r ( a r c i n a r c s ) {
/ / a l i s t w i th a l l c o n n e c t i o n s b u t n o t i n e x e c u t i o n o r d e r .c o n n e c t i o n s << new C o n n e c t i o n ( s o u r c e I d : a r c . s o u r c e . id , t a r g e t I d : a r c . t a r g e t . i d )
}
f o r ( i i n c o n n e c t i o n s ) {f o r ( j i n c o n n e c t i o n s ) {
/ / p r i n t l n i . t o S t r i n g ( ) + ” wi th ” + j . t o S t r i n g ( )
i f ( i . s o u r c e I d == j . t a r g e t I d ) {i . n S o u r c e I n A r c s ++
}i f ( i . s o u r c e I d == j . s o u r c e I d ) {
i . nSourceOutArcs ++}i f ( i . t a r g e t I d == j . s o u r c e I d ) {
i . n T a r g e t O u t A r c s ++}i f ( i . t a r g e t I d == j . t a r g e t I d ) {
i . n T a r g e t I n A r c s ++}
}}c o n n e c t i o n s
}
s t a t i c L i s t<Connec t ion> s e a r c h B y S o u r c e I d ( L i s t<Connec t ion> c o n n e c t i o n s , S t r i n g i d ) {l o g . t r a c e ( ” s e a r c h B y S o u r c e I d ### c o n n e c t i o n s : $ c o n n e c t i o n s ### i d $ i d ” )L i s t<Connec t ion> r e s u l t = [ ]f o r ( c o n n e c t i o n i n c o n n e c t i o n s ) {
i f ( c o n n e c t i o n != n u l l ) {i f ( c o n n e c t i o n . s o u r c e I d == i d ) {
r e s u l t << c o n n e c t i o n}
}}r e t u r n r e s u l t
}
/ / s e a r c h f o r nodes and d e l e t e o r add an o u t p u t a r c on t h e s o u r c e/ / u s e f u l f o r v i s i t i n g nodes t w i c es t a t i c L i s t<Connec t ion> sea rchBySource IdAdd ( L i s t<Connec t ion> c o n n e c t i o n s , S t r i n g id , I n t e g e r a dd in g ) {
L i s t<Connec t ion> r e s u l t = [ ]f o r ( c o n n e c t i o n i n c o n n e c t i o n s ) {
i f ( c o n n e c t i o n . s o u r c e I d == i d ) {c o n n e c t i o n . nSourceOutArcs = c o n n e c t i o n . nSourceOutArcs + a dd ingr e s u l t << c o n n e c t i o n
}}r e t u r n r e s u l t
}
s t a t i c L i s t<Connec t ion> s e a r c h B y T a r g e t I d ( L i s t<Connec t ion> c o n n e c t i o n s , S t r i n g i d ) {L i s t<Connec t ion> r e s u l t = [ ]f o r ( c o n n e c t i o n i n c o n n e c t i o n s ) {
i f ( c o n n e c t i o n . t a r g e t I d == i d ) {r e s u l t << c o n n e c t i o n
24
}}r e t u r n r e s u l t
}
/ / TOCHECK: i s t h i s c o r r e c t ? t h e r e may be p l a c e s wi th no i n p u t s / o u t p u t ss t a t i c C o n n e c t i o n s e a r c h C o l l e c t o r ( L i s t<Connec t ion> c o n n e c t i o n s ) {
f o r ( c o n n e c t i o n i n c o n n e c t i o n s ) {i f ( c o n n e c t i o n . n S o u r c e I n A r c s == 0) {
r e t u r n c o n n e c t i o n}
}}
/ / remember ing a n o t h e r p a t hs t a t i c L i s t<Connec t ion> remember ( c o n n e c t i o n s , node ) {
i f ( node . nSourceOutArcs > 1 && node . n S o u r c e I n A r c s != 0 ) {L i s t<Connec t ion> rememberSourceNodes = sea rchBySource IdAdd ( c o n n e c t i o n s , node . s o u r c e I d , −1)rememberSourceNodes . remove ( node )r e t u r n rememberSourceNodes
} e l s e {r e t u r n [ ]
}}
/ / f i n d i n g n e x t c o n n e c t i o ns t a t i c d e f f i n d N e x t ( L i s t<Connec t ion> v i s i t e d C o n n e c t i o n s , L i s t<Connec t ion> c o n n e c t i o n s , C o n n e c t i o n node , L i s t<Connec t ion> remember ) {
l o g . t r a c e ( ” f i n d N e x t . v i s i t e d : $ v i s i t e d C o n n e c t i o n s , c o n n e c t i o n s : $ c o n n e c t i o n s , node : $node , remember : $remember ” )
C o n n e c t i o n n e x tl o g . t r a c e ( ” Next : $ n e x t ” )Boolean b a c k t r a c k = f a l s e
f o r ( c o n n e c t i o n i n c o n n e c t i o n s ) {i f ( v i s i t e d C o n n e c t i o n s . c o n t a i n s ( node ) && c o n n e c t i o n . n S o u r c e I n A r c s == 0) {
n e x t = c o n n e c t i o n} e l s e {
i f ( c o n n e c t i o n . s o u r c e I d == node . t a r g e t I d && c o n n e c t i o n . t a r g e t I d != node . s o u r c e I d && node . n T a r g e t O u t A r c s != 0) {n e x t = c o n n e c t i o n
} e l s e i f ( node . n T a r g e t O u t A r c s == 0 && c o n n e c t i o n . n S o u r c e I n A r c s == 0) {n e x t = c o n n e c t i o n
}}
}
i f ( ! n e x t ) {
/ / i f no new c o n n e c t i o n found , l ook i n v i s i t e d C o n n e c t i o n/ / i f remembered node i s used , o t h e r node needs t o be remembered as w e l lL i s t<Connec t ion> nextN = s e a r c h B y S o u r c e I d ( v i s i t e d C o n n e c t i o n s , node . t a r g e t I d )
i f ( nextN . s i z e ( ) > 0) {i f ( ! remember . i sEmpty ( ) ) {
n e x t = remember [ 0 ]b a c k t r a c k = t r u el o g . t r a c e ( ” HI ” )
} e l s e {f o r ( c o n n e c t i o n i n c o n n e c t i o n s ) {
i f ( c o n n e c t i o n . n S o u r c e I n A r c s == 0) {n e x t = c o n n e c t i o nb a c k t r a c k = t r u e
}}
}}
}
i f ( node )v i s i t e d C o n n e c t i o n s << node
r e t u r n [ nex t , b a c k t r a c k ]}
S t r i n g t o S t r i n g ( ) {r e t u r n s o u r c e I d + ”{ $nSource InArcs , $nSourceOutArcs}−>” + t a r g e t I d + ”{ $ n T a r g e t I n A r c s , $nTa r ge tOu tAr cs}”
}}
25