1 package org.openprovenance.rdf;
2
3 import javax.xml.bind.JAXBException;
4 import javax.xml.bind.JAXBElement;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.ByteArrayInputStream;
8 import java.io.IOException;
9 import java.io.File;
10 import java.io.FileOutputStream;
11 import java.io.OutputStream;
12
13 import java.util.List;
14 import java.util.LinkedList;
15 import java.util.Set;
16 import java.util.HashMap;
17 import java.util.Collection;
18
19
20 import org.openprovenance.model.OPMGraph;
21 import org.openprovenance.model.Edge;
22 import org.openprovenance.model.Annotation;
23 import org.openprovenance.model.Account;
24 import org.openprovenance.model.AccountRef;
25 import org.openprovenance.model.Processes;
26 import org.openprovenance.model.Node;
27 import org.openprovenance.model.Agent;
28 import org.openprovenance.model.Ref;
29 import org.openprovenance.model.Identifiable;
30 import org.openprovenance.model.Process;
31 import org.openprovenance.model.Artifact;
32 import org.openprovenance.model.Used;
33 import org.openprovenance.model.Role;
34 import org.openprovenance.model.Property;
35 import org.openprovenance.model.EmbeddedAnnotation;
36 import org.openprovenance.model.WasGeneratedBy;
37 import org.openprovenance.model.WasTriggeredBy;
38 import org.openprovenance.model.WasDerivedFrom;
39 import org.openprovenance.model.WasControlledBy;
40 import org.openprovenance.model.OPMUtilities;
41 import org.openprovenance.model.OPMFactory;
42 import org.openprovenance.model.OPMDeserialiser;
43
44
45
46 import org.tupeloproject.provenance.ProvenanceAccount;
47 import org.tupeloproject.provenance.ProvenanceRole;
48 import org.tupeloproject.provenance.ProvenanceAgent;
49 import org.tupeloproject.provenance.ProvenanceProcess;
50 import org.tupeloproject.provenance.ProvenanceArtifact;
51 import org.tupeloproject.provenance.ProvenanceDerivedArc;
52 import org.tupeloproject.provenance.ProvenanceUsedArc;
53 import org.tupeloproject.provenance.ProvenanceGeneratedArc;
54 import org.tupeloproject.provenance.ProvenanceControlledArc;
55 import org.tupeloproject.provenance.ProvenanceTriggeredArc;
56 import org.tupeloproject.provenance.impl.ProvenanceContextFacade;
57 import org.tupeloproject.provenance.impl.RdfProvenanceArtifact;
58 import org.tupeloproject.provenance.impl.RdfProvenanceProcess;
59 import org.tupeloproject.provenance.impl.RdfProvenanceArc;
60 import org.tupeloproject.provenance.impl.RdfProvenanceRole;
61 import org.tupeloproject.provenance.impl.RdfProvenanceAgent;
62 import org.tupeloproject.provenance.impl.RdfProvenanceAccount;
63
64 import org.tupeloproject.rdf.Vocabulary.Rdf;
65
66 import org.tupeloproject.rdf.Resource;
67 import org.tupeloproject.rdf.Triple;
68 import org.tupeloproject.rdf.xml.RdfXml;
69 import org.tupeloproject.kernel.Context;
70 import org.tupeloproject.kernel.UnionContext;
71 import org.tupeloproject.kernel.impl.ResourceContext;
72 import org.tupeloproject.kernel.impl.MemoryContext;
73 import org.tupeloproject.kernel.impl.FileContext;
74 import org.tupeloproject.kernel.impl.BasicLocalContext;
75 import org.tupeloproject.util.Xml;
76 import org.tupeloproject.kernel.OperatorException;
77 import org.apache.log4j.Logger;
78
79
80 import org.tupeloproject.provenance.impl.RdfProvenanceArtifact;
81
82
83 public class OPMXml2Rdf {
84 static Logger logger = Logger.getLogger(OPMXml2Rdf.class);
85
86 static String NULL_ACCOUNT="_null";
87 static String UNKNOWN_ROLE="_unknown";
88
89 OPMUtilities u=new OPMUtilities();
90
91 static OPMFactory oFactory=new OPMFactory();
92
93 public void convert (OPMGraph graph, String filename) throws OperatorException, IOException {
94 convert(graph,new FileOutputStream(new File(filename)));
95 }
96
97 HashMap<String,ProvenanceAccount> accountTable=new HashMap();
98 HashMap<String,ProvenanceProcess> processTable=new HashMap();
99 HashMap<String,ProvenanceArtifact> artifactTable=new HashMap();
100 HashMap<String,ProvenanceAgent> agentTable=new HashMap();
101 HashMap<String,Resource> idTable=new HashMap();
102
103 static String OPM_ANNOTATION="http://www.ipaw.info/2007/opm#Annotation";
104 static String OPM_HAS_ANNOTATION="http://www.ipaw.info/2007/opm#hasAnnotation";
105 static String OPM_HAS_ACCOUNT="http://www.ipaw.info/2007/opm#hasAccount";
106
107 Resource annotationType=Resource.uriRef(OPM_ANNOTATION);
108 Resource hasAnnotation=Resource.uriRef(OPM_HAS_ANNOTATION);
109 Resource hasAccount=Resource.uriRef(OPM_HAS_ACCOUNT);
110
111
112
113 public void convert (OPMGraph graph, OutputStream out) throws OperatorException, IOException {
114
115
116
117
118
119 MemoryContext mc = new MemoryContext();
120
121 ResourceContext rc = new ResourceContext("http://example.org/data/","/provenanceExample/");
122 Context context = new UnionContext();
123 context.addChild(mc);
124 context.addChild(rc);
125
126
127
128 ProvenanceContextFacade pcf = new ProvenanceContextFacade(mc);
129
130
131
132 Resource nullRes=Resource.uriRef(urify(NULL_ACCOUNT));
133 ProvenanceAccount nullRdfAccount=pcf.newAccount(NULL_ACCOUNT,nullRes);
134 pcf.assertAccount(nullRdfAccount);
135 accountTable.put(NULL_ACCOUNT,nullRdfAccount);
136
137 if (graph.getAccounts()!=null) {
138 for (Account acc: graph.getAccounts().getAccount()) {
139 Resource res=Resource.resource(urify(acc.getId()));
140 ProvenanceAccount rdfAccount=pcf.newAccount(acc.getId(),res);
141 pcf.assertAccount(rdfAccount);
142 accountTable.put(acc.getId(),rdfAccount);
143
144 RdfProvenanceAccount a2=(RdfProvenanceAccount) rdfAccount;
145 Resource subject=a2.getSubject();
146 idTable.put(acc.getId(),subject);
147
148 if (!(acc.getAnnotation().isEmpty())) {
149 mc.addTriples(triplifyAnnotations(subject,acc.getAnnotation()));
150 }
151 }
152 }
153
154
155 if (graph.getProcesses()!=null) {
156 for (Process p: graph.getProcesses().getProcess()) {
157 Resource res=Resource.uriRef(urify(p.getId()));
158 ProvenanceProcess rdfProcess;
159 rdfProcess = pcf.newProcess((String)oFactory.getLabel(p),res);
160
161 pcf.assertProcess(rdfProcess);
162 processTable.put(p.getId(),rdfProcess);
163
164
165 RdfProvenanceProcess a2=(RdfProvenanceProcess) rdfProcess;
166 Resource subject=a2.getSubject();
167 idTable.put(p.getId(),subject);
168
169 if (!(p.getAnnotation().isEmpty())) {
170 mc.addTriples(triplifyAnnotations(subject,p.getAnnotation()));
171 }
172 }
173 }
174
175 if (graph.getArtifacts()!=null) {
176 for (Artifact a: graph.getArtifacts().getArtifact()) {
177 Resource res=Resource.uriRef(urify(a.getId()));
178 ProvenanceArtifact rdfArtifact;
179 rdfArtifact = pcf.newArtifact((String)oFactory.getLabel(a),res);
180
181 pcf.assertArtifact(rdfArtifact);
182 artifactTable.put(a.getId(),rdfArtifact);
183
184
185 RdfProvenanceArtifact a2=(RdfProvenanceArtifact) rdfArtifact;
186 Resource subject=a2.getSubject();
187 idTable.put(a.getId(),subject);
188
189 if (!(a.getAnnotation().isEmpty())) {
190 mc.addTriples(triplifyAnnotations(subject,a.getAnnotation()));
191 }
192 }
193 }
194
195
196 if (graph.getAgents()!=null) {
197 for (Agent a: graph.getAgents().getAgent()) {
198 Resource res=Resource.uriRef(urify(a.getId()));
199 ProvenanceAgent rdfAgent;
200 rdfAgent = pcf.newAgent((String)oFactory.getLabel(a),res);
201
202 pcf.assertAgent(rdfAgent);
203 agentTable.put(a.getId(),rdfAgent);
204
205
206 RdfProvenanceAgent a2=(RdfProvenanceAgent) rdfAgent;
207 Resource subject=a2.getSubject();
208 idTable.put(a.getId(),subject);
209
210 if (!(a.getAnnotation().isEmpty())) {
211 mc.addTriples(triplifyAnnotations(subject,a.getAnnotation()));
212 }
213 }
214 }
215
216
217
218 List<Edge> edges=u.getEdges(graph);
219 for (Edge e: edges) {
220 List<AccountRef> accounts= e.getAccount();
221 String causeId=((Node)e.getCause().getRef()).getId();
222 String effectId=((Node)e.getEffect().getRef()).getId();
223
224 if (e instanceof Used) {
225 ProvenanceArtifact cause=artifactTable.get(causeId);
226 ProvenanceProcess effect=processTable.get(effectId);
227 Role thisRole=((Used)e).getRole();
228 ProvenanceRole role;
229 if (thisRole!=null) {
230 role=pcf.newRole(thisRole.getValue());
231 } else {
232 role=pcf.newRole(UNKNOWN_ROLE);
233 }
234 if (accounts.isEmpty()) {
235 ProvenanceAccount account=accountTable.get(NULL_ACCOUNT);
236 pcf.assertUsed(effect, cause, role, account);
237 } else {
238 for (AccountRef aid: accounts) {
239 ProvenanceAccount account=accountTable.get(((Account)aid.getRef()).getId());
240 pcf.assertUsed(effect, cause, role, account);
241 }
242 }
243
244
245 if (!(e.getAnnotation().isEmpty())) {
246
247 Collection<ProvenanceUsedArc> used=pcf.getUsed(effect);
248 for (ProvenanceUsedArc a: used) {
249 if (a.getSink().equals(cause)
250 && a.getSource().equals(effect)) {
251
252
253
254 RdfProvenanceArc rpa=(RdfProvenanceArc) a;
255 Resource subject=rpa.getSubject();
256 mc.addTriples(triplifyAnnotations(subject,e.getAnnotation()));
257 break;
258 }
259 }
260 }
261
262 if (!(thisRole.getAnnotation().isEmpty())) {
263 RdfProvenanceRole rdfRole=(RdfProvenanceRole) role;
264 Resource roleSubject=rdfRole.getSubject();
265 mc.addTriples(triplifyAnnotations(roleSubject,thisRole.getAnnotation()));
266 }
267
268
269 }
270 else if (e instanceof WasGeneratedBy) {
271 ProvenanceProcess cause=processTable.get(causeId);
272 ProvenanceArtifact effect=artifactTable.get(effectId);
273 Role thisRole=((WasGeneratedBy)e).getRole();
274 ProvenanceRole role;
275 if (thisRole!=null) {
276 role=pcf.newRole(thisRole.getValue());
277 } else {
278 role=pcf.newRole(UNKNOWN_ROLE);
279 }
280 if (accounts.isEmpty()) {
281 ProvenanceAccount account=accountTable.get(NULL_ACCOUNT);
282 pcf.assertGeneratedBy(effect, cause, role, account);
283 } else {
284 for (AccountRef aid: accounts) {
285 ProvenanceAccount account=accountTable.get(((Account)aid.getRef()).getId());
286 pcf.assertGeneratedBy(effect, cause, role, account);
287 }
288 }
289
290
291 Collection<ProvenanceGeneratedArc> generatedBy=pcf.getGeneratedBy(effect);
292 for (ProvenanceGeneratedArc a: generatedBy) {
293 if (a.getSink().equals(cause)
294 && a.getSource().equals(effect)) {
295 RdfProvenanceArc rpa=(RdfProvenanceArc) a;
296 Resource subject=rpa.getSubject();
297 mc.addTriples(triplifyAnnotations(subject,e.getAnnotation()));
298 break;
299 }
300 }
301
302 if (!(thisRole.getAnnotation().isEmpty())) {
303 RdfProvenanceRole rdfRole=(RdfProvenanceRole) role;
304 Resource roleSubject=rdfRole.getSubject();
305 mc.addTriples(triplifyAnnotations(roleSubject,thisRole.getAnnotation()));
306 }
307
308
309 }
310 else if (e instanceof WasTriggeredBy) {
311 ProvenanceProcess cause=processTable.get(causeId);
312 ProvenanceProcess effect=processTable.get(effectId);
313 if (accounts.isEmpty()) {
314 ProvenanceAccount account=accountTable.get(NULL_ACCOUNT);
315 pcf.assertTriggeredBy(effect, cause, account);
316 } else {
317 for (AccountRef aid: accounts) {
318 ProvenanceAccount account=accountTable.get(((Account)aid.getRef()).getId());
319 pcf.assertTriggeredBy(effect, cause, account);
320 }
321 }
322
323
324 if (!(e.getAnnotation().isEmpty())) {
325 Collection<ProvenanceTriggeredArc> triggered=pcf.getTriggeredBy(effect);
326 for (ProvenanceTriggeredArc a: triggered) {
327 if (a.getSink().equals(cause)
328 && a.getSource().equals(effect)) {
329 RdfProvenanceArc rpa=(RdfProvenanceArc) a;
330 Resource subject=rpa.getSubject();
331 mc.addTriples(triplifyAnnotations(subject,e.getAnnotation()));
332 break;
333 }
334 }
335 }
336
337 }
338 else if (e instanceof WasDerivedFrom) {
339 ProvenanceArtifact cause=artifactTable.get(causeId);
340 ProvenanceArtifact effect=artifactTable.get(effectId);
341 RdfProvenanceArc arc;
342 if (accounts.isEmpty()) {
343 ProvenanceAccount account=accountTable.get(NULL_ACCOUNT);
344 pcf.assertDerivedFrom(effect, cause, account);
345 } else {
346 for (AccountRef aid: accounts) {
347 ProvenanceAccount account=accountTable.get(((Account)aid.getRef()).getId());
348 pcf.assertDerivedFrom(effect, cause, account);
349 }
350 }
351
352 if (!(e.getAnnotation().isEmpty())) {
353 Collection<ProvenanceDerivedArc> derived=pcf.getDerivedFrom(effect);
354 for (ProvenanceDerivedArc a: derived) {
355 if (a.getAntecedent().equals(cause)
356 && a.getConsequent().equals(effect)) {
357 RdfProvenanceArc rpa=(RdfProvenanceArc) a;
358 Resource subject=rpa.getSubject();
359 mc.addTriples(triplifyAnnotations(subject,e.getAnnotation()));
360 break;
361 }
362 }
363 }
364 }
365
366 else if (e instanceof WasControlledBy) {
367 ProvenanceAgent cause=agentTable.get(causeId);
368 ProvenanceProcess effect=processTable.get(effectId);
369 ProvenanceRole role = pcf.newRole(((WasControlledBy)e).getRole().getValue());
370 if (accounts.isEmpty()) {
371 ProvenanceAccount account=accountTable.get(NULL_ACCOUNT);
372 pcf.assertControlledBy(effect, cause, role, account);
373 } else {
374 for (AccountRef aid: accounts) {
375 ProvenanceAccount account=accountTable.get(((Account)aid.getRef()).getId());
376 pcf.assertControlledBy(effect, cause, role, account);
377 }
378 }
379
380 if (!(e.getAnnotation().isEmpty())) {
381 Collection<ProvenanceControlledArc> controlled=pcf.getControlledBy(effect);
382 for (ProvenanceControlledArc a: controlled) {
383 if (a.getSink().equals(cause)
384 && a.getSource().equals(effect)) {
385 RdfProvenanceArc rpa=(RdfProvenanceArc) a;
386 Resource subject=rpa.getSubject();
387 mc.addTriples(triplifyAnnotations(subject,e.getAnnotation()));
388 break;
389 }
390 }
391 }
392
393 Role thisRole=((WasControlledBy)e).getRole();
394 if (!(thisRole.getAnnotation().isEmpty())) {
395 RdfProvenanceRole rdfRole=(RdfProvenanceRole) role;
396 Resource roleSubject=rdfRole.getSubject();
397 mc.addTriples(triplifyAnnotations(roleSubject,thisRole.getAnnotation()));
398 }
399 }
400 }
401
402
403 if (graph.getAnnotations()!=null) {
404 List<Annotation> annotations=graph.getAnnotations().getAnnotation();
405 for (Annotation ann: annotations) {
406 mc.addTriples(triplifyAnnotation(ann));
407 }
408 }
409
410
411
412 Set<Triple> triplesToWrite = mc.getTriples();
413 RdfXml.write(triplesToWrite, out);
414
415 }
416
417 public List<Triple> triplifyAnnotation(Annotation annotation) {
418 String id=(((Identifiable)annotation.getLocalSubject()).getId());
419 Resource subject=idTable.get(id);
420 List<Triple> triples=new LinkedList();
421 if (subject!=null) {
422 addTriplesForAnnotation(subject,annotation,triples);
423 } else {
424 logger.warn("$$$$$$$$$$$$$$$ " + id + ": no rdf subject!");
425 }
426 return triples;
427 }
428
429 public List<Triple> triplifyAnnotations(Resource subject, List<JAXBElement<? extends EmbeddedAnnotation>> annotations) {
430 List<Triple> triples=new LinkedList();
431 for (JAXBElement<? extends EmbeddedAnnotation> jann: annotations) {
432 EmbeddedAnnotation ann=jann.getValue();
433 addTriplesForAnnotation(subject,ann,triples);
434 }
435 return triples;
436 }
437
438
439 public void addTriplesForAnnotation(Resource subject, EmbeddedAnnotation ann, List<Triple> triples) {
440
441 oFactory.expandAnnotation(ann);
442
443 Resource annotationInRdf;
444 if (ann.getId()!=null) {
445 annotationInRdf=Resource.uriRef(urify(ann.getId()));
446 } else {
447 annotationInRdf=Resource.uriRef();
448 }
449 Triple t1=Triple.create(subject,
450 hasAnnotation,
451 annotationInRdf);
452
453
454 triples.add(t1);
455
456 Triple t2=Triple.create(annotationInRdf,
457 Rdf.TYPE,
458 annotationType);
459
460 triples.add(t2);
461
462
463 for (Property prop: ann.getProperty()) {
464 Resource predicate=Resource.uriRef(prop.getUri());
465 Resource value=makeLiteral(prop.getValue());
466
467
468 Triple t=Triple.create(annotationInRdf,
469 predicate,
470 value);
471
472 for (AccountRef aid: ann.getAccount()) {
473 ProvenanceAccount account=accountTable.get(((Account)aid.getRef()).getId());
474 RdfProvenanceAccount a2=(RdfProvenanceAccount) account;
475 Resource accountSubject=a2.getSubject();
476
477 Triple t3=Triple.create(annotationInRdf,
478 hasAccount,
479 accountSubject);
480 triples.add(t3);
481
482 }
483
484 triples.add(t);
485 }
486
487 List<Triple> moreTriples=triplifyAnnotations(annotationInRdf,
488 ann.getAnnotation());
489 triples.addAll(moreTriples);
490
491 }
492
493
494 public Resource makeLiteral(Object value) {
495 if (value instanceof String) return Resource.literal((String)value);
496 if (value instanceof Double) return Resource.literal((Double)value);
497 if (value instanceof Float) return Resource.literal((Float)value);
498 if (value instanceof Integer) return Resource.literal((Integer)value);
499 System.out.println("Foudn " + value);
500 System.out.println("Foudn " + value.getClass());
501 return null;
502 }
503
504 public static String URI_PREFIX="id:";
505
506 public String urify(String id) {
507 return URI_PREFIX + id.replaceAll("#","_");
508 }
509
510
511 public void convert (String inFilename, String outFilename) throws OperatorException, IOException, JAXBException {
512 OPMDeserialiser deserial=OPMDeserialiser.getThreadOPMDeserialiser();
513 OPMGraph graph=deserial.deserialiseOPMGraph(new File(inFilename));
514 convert(graph,outFilename);
515 }
516
517
518 public static void main(String [] args) throws OperatorException, IOException, JAXBException {
519 if ((args==null) || (args.length!=2)) {
520 System.out.println("Usage: opmxml2rdf fileIn fileOut");
521 return;
522 }
523 OPMXml2Rdf converter=new OPMXml2Rdf();
524 converter.convert(args[0],args[1]);
525 }
526
527
528
529 }