1 package org.openprovenance.model;
2 import java.util.List;
3 import java.util.LinkedList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import javax.xml.bind.JAXBElement;
7 import java.util.Comparator;
8
9 public class Normalise {
10 OPMFactory oFactory;
11
12 public Normalise(OPMFactory oFactory) {
13 this.oFactory=oFactory;
14 }
15
16 public void sortGraph(OPMGraph graph) {
17
18 graph.setAnnotations(oFactory.getObjectFactory().createAnnotations());
19
20 sortById((List)graph.getAccounts().getAccount());
21
22 if (graph.getProcesses()!=null && graph.getProcesses().getProcess()!=null) {
23 sortById((List)graph.getProcesses().getProcess());
24 for (Process p: graph.getProcesses().getProcess()) {
25 sortAnnotations(p.getAnnotation());
26 sortByRef(p.getAccount());
27 }
28 }
29
30 if (graph.getArtifacts()!=null && graph.getArtifacts().getArtifact()!=null) {
31 sortById((List)graph.getArtifacts().getArtifact());
32 for (Artifact a: graph.getArtifacts().getArtifact()) {
33 sortAnnotations(a.getAnnotation());
34 sortByRef(a.getAccount());
35 }
36 }
37
38 if (graph.getAgents()!=null && graph.getAgents().getAgent()!=null) {
39 sortById((List)graph.getAgents().getAgent());
40 for (Agent a: graph.getAgents().getAgent()) {
41 sortAnnotations(a.getAnnotation());
42 sortByRef(a.getAccount());
43 }
44 }
45
46 for (Object e: graph.getCausalDependencies().getUsedOrWasGeneratedByOrWasTriggeredBy()) {
47 sortAnnotations(((Edge)e).getAnnotation());
48 sortByRef(((Edge)e).getAccount());
49 }
50
51 sortEdges(graph.getCausalDependencies().getUsedOrWasGeneratedByOrWasTriggeredBy());
52 }
53
54
55 public void updateFromRdfGraph(OPMGraph graph) {
56
57 IndexedOPMGraph igraph=new IndexedOPMGraph(oFactory,graph);
58 List<Account> accs=graph.getAccounts().getAccount();
59 for (Account acc: accs) {
60 if (acc.getId().equals("_null")) {
61 accs.remove(acc);
62 break;
63 }
64 }
65
66
67
68 Collection<Account> green=Collections.singleton(igraph.getAccount("green"));
69 Collection<Account> orange=Collections.singleton(igraph.getAccount("orange"));
70
71 List<Account> green_orange=new LinkedList();
72 green_orange.addAll(green);
73 green_orange.addAll(orange);
74
75
76 Overlaps ov1=oFactory.newOverlaps(green_orange);
77 graph.getAccounts().getOverlaps().add(ov1);
78
79
80 }
81
82 public void noAnnotation(OPMGraph graph) {
83 graph.setAnnotations(null);
84
85 destroy(graph.getAnnotation());
86
87 if (graph.getProcesses()!=null && graph.getProcesses().getProcess()!=null) {
88 for (Process p: graph.getProcesses().getProcess()) {
89 destroy(p.getAnnotation());
90 }
91 }
92
93 if (graph.getArtifacts()!=null && graph.getArtifacts().getArtifact()!=null) {
94 for (Artifact a: graph.getArtifacts().getArtifact()) {
95 destroy(a.getAnnotation());
96 }
97 }
98
99 if (graph.getAgents()!=null && graph.getAgents().getAgent()!=null) {
100 for (Agent a: graph.getAgents().getAgent()) {
101 destroy(a.getAnnotation());
102 }
103 }
104
105 for (Object e: graph.getCausalDependencies().getUsedOrWasGeneratedByOrWasTriggeredBy()) {
106
107 destroy(((Edge)e).getAnnotation());
108 }
109
110 }
111
112
113
114 public void embedExternalAnnotations(OPMGraph graph) {
115
116 IndexedOPMGraph igraph=new IndexedOPMGraph(oFactory,graph);
117
118
119
120 if (graph.getAnnotations()!=null) {
121 List<Annotation> anns=graph.getAnnotations().getAnnotation();
122 for (Annotation ann: anns) {
123 String id=(((Identifiable)ann.getLocalSubject()).getId());
124 EmbeddedAnnotation embedded=oFactory.newEmbeddedAnnotation(ann.getId(),
125 ann.getProperty(),
126 ann.getAccount(),
127 null);
128 Process p=igraph.getProcess(id);
129 if (p!=null) {
130 p.getAnnotation().add(oFactory.compactAnnotation(embedded));
131 } else {
132 Artifact a=igraph.getArtifact(id);
133 if (a!=null) {
134 a.getAnnotation().add(oFactory.compactAnnotation(embedded));
135 } else {
136 Agent ag=igraph.getAgent(id);
137 if (ag!=null) {
138 ag.getAnnotation().add(oFactory.compactAnnotation(embedded));
139 }
140 }
141 }
142 }
143 graph.setAnnotations(null);
144 }
145 }
146
147 public void updateOriginalGraph(OPMGraph graph) {
148
149 embedExternalAnnotations(graph);
150
151 if (graph.getProcesses()!=null && graph.getProcesses().getProcess()!=null) {
152 for (Process p: graph.getProcesses().getProcess()) {
153 List<AccountRef> accRefs=p.getAccount();
154 destroy(accRefs);
155 }
156 }
157
158 if (graph.getArtifacts()!=null && graph.getArtifacts().getArtifact()!=null) {
159 for (Artifact a: graph.getArtifacts().getArtifact()) {
160 List<AccountRef> accRefs=a.getAccount();
161 destroy(accRefs);
162 }
163 }
164
165 if (graph.getAgents()!=null && graph.getAgents().getAgent()!=null) {
166 for (Agent a: graph.getAgents().getAgent()) {
167 List<AccountRef> accRefs=a.getAccount();
168 destroy(accRefs);
169 }
170 }
171
172 for (Object e: graph.getCausalDependencies().getUsedOrWasGeneratedByOrWasTriggeredBy()) {
173 ((Identifiable)e).setId(null);
174
175
176
177 if (e instanceof WasGeneratedBy) {
178 WasGeneratedBy wgb=(WasGeneratedBy) e;
179 wgb.getRole().setId(null);
180 }
181 if (e instanceof Used) {
182 Used u=(Used) e;
183 u.getRole().setId(null);
184 }
185 if (e instanceof WasControlledBy) {
186 WasControlledBy wcb=(WasControlledBy) e;
187 wcb.getRole().setId(null);
188 }
189 }
190 }
191
192 public void sortById(List ll) {
193 Collections.sort(ll,
194 new Comparator() {
195 public int compare(Object o1, Object o2) {
196 Identifiable i1=(Identifiable) o1;
197 Identifiable i2=(Identifiable) o2;
198 return i1.getId().compareTo(i2.getId());
199 }
200 public boolean equals_IGNORE(Object o1, Object o2) {
201 Identifiable i1=(Identifiable) o1;
202 Identifiable i2=(Identifiable) o2;
203 return (i1.getId().equals(i2.getId()));
204 }
205 });
206 }
207
208 public void sortByRef(List ll) {
209 Collections.sort(ll,
210 new Comparator() {
211 public int compare(Object o1, Object o2) {
212 Identifiable i1=(Identifiable) ((Ref) o1).getRef();
213 Identifiable i2=(Identifiable) ((Ref) o2).getRef();
214 return i1.getId().compareTo(i2.getId());
215 }
216 });
217 }
218
219 public void sortEdges(List ll) {
220 Collections.sort(ll,
221 new Comparator() {
222 public int compare(Object o1, Object o2) {
223 Edge e1=(Edge) o1;
224 Edge e2=(Edge) o2;
225 String id1_1=((Identifiable)(e1.getCause().getRef())).getId();
226 String id1_2=((Identifiable)(e1.getEffect().getRef())).getId();
227 String s1=id1_1+id1_2;
228 String id2_1=((Identifiable)(e2.getCause().getRef())).getId();
229 String id2_2=((Identifiable)(e2.getEffect().getRef())).getId();
230 String s2=id2_1+id2_2;
231
232 return s1.compareTo(s2);
233 }
234
235 });
236 }
237
238 public void sortAnnotations(List<JAXBElement<? extends EmbeddedAnnotation>> ll) {
239 Collections.sort(ll,
240 new Comparator() {
241 public int compare(Object o1, Object o2) {
242 JAXBElement j1=(JAXBElement) o1;
243 JAXBElement j2=(JAXBElement) o2;
244 EmbeddedAnnotation a1=(EmbeddedAnnotation) j1.getValue();
245 EmbeddedAnnotation a2=(EmbeddedAnnotation) j2.getValue();
246
247 if (a1.getId()==null) {
248 if (a2.getId()==null) {
249 return a1.getClass().getName().compareTo(a2.getClass().getName());
250 } else {
251 return -1;
252 }
253 } else {
254 if (a2.getId()==null) {
255 return +1;
256 } else {
257 return a1.getId().compareTo(a2.getId());
258 }
259 }
260
261
262 }});
263 for (JAXBElement je: ll) {
264 EmbeddedAnnotation a=(EmbeddedAnnotation) je.getValue();
265 sortProperties(a.getProperty());
266 sortByRef(a.getAccount());
267 }
268 }
269
270
271 public void sortProperties(List<Property> ll) {
272
273 Collections.sort(ll,
274 new Comparator() {
275 public int compare(Object o1, Object o2) {
276 Property p1=(Property) o1;
277 Property p2=(Property) o2;
278
279 return p1.getUri().compareTo(p2.getUri());
280 }});
281 }
282
283 public void sortAccounts(List<Account> ll) {
284
285 Collections.sort(ll,
286 new Comparator() {
287 public int compare(Object o1, Object o2) {
288 Account acc1=(Account) o1;
289 Account acc2=(Account) o2;
290
291 return acc1.getId().compareTo(acc2.getId());
292 }});
293 }
294
295
296
297 public void destroy(List ll) {
298 for (int i=ll.size()-1; i>=0; i--) {
299 ll.remove(ll.get(i));
300 }
301 }
302
303 public void sortAnnotationsIGNORE(List<EmbeddedAnnotation> ll) {
304 Collections.sort(ll,
305 new Comparator() {
306 public int compare(Object o1, Object o2) {
307 EmbeddedAnnotation a1=(EmbeddedAnnotation) o1;
308 EmbeddedAnnotation a2=(EmbeddedAnnotation) o2;
309
310 if (a1.getId()==null) {
311 if (a2.getId()==null) {
312 return a1.getClass().getName().compareTo(a2.getClass().getName());
313 } else {
314 return -1;
315 }
316 } else {
317 if (a2.getId()==null) {
318 return +1;
319 } else {
320 return a1.getId().compareTo(a2.getId());
321 }
322 }
323
324
325 }});
326 }
327
328
329 }