View Javadoc

1   package org.openprovenance.rdf;
2   
3   import java.util.Set;
4   import java.io.ByteArrayOutputStream;
5   import java.io.ByteArrayInputStream;
6   import java.io.IOException;
7   import java.io.File;
8   import java.io.FileOutputStream;
9   
10  import org.tupeloproject.provenance.ProvenanceAccount;
11  import org.tupeloproject.provenance.ProvenanceRole;
12  import org.tupeloproject.provenance.ProvenanceProcess;
13  import org.tupeloproject.provenance.ProvenanceArtifact;
14  import org.tupeloproject.provenance.impl.ProvenanceContextFacade;
15  import org.tupeloproject.rdf.Resource;
16  import org.tupeloproject.rdf.Triple;
17  import org.tupeloproject.kernel.Context;
18  import org.tupeloproject.kernel.UnionContext;
19  import org.tupeloproject.kernel.impl.ResourceContext;
20  import org.tupeloproject.kernel.impl.MemoryContext;
21  import org.tupeloproject.kernel.impl.FileContext;
22  import org.tupeloproject.kernel.impl.BasicLocalContext;
23  import org.tupeloproject.util.Xml;
24  import org.tupeloproject.rdf.xml.RdfXml;
25  import org.tupeloproject.kernel.OperatorException; 
26  
27  
28  public class Tutorial {
29      public void example (String [] args) throws OperatorException, IOException {
30        
31  
32          MemoryContext mc = new MemoryContext(); //
33          
34          ResourceContext rc = new ResourceContext("http://example.org/data/","/provenanceExample/");
35          Context context = new UnionContext();
36          context.addChild(mc);
37          context.addChild(rc);
38          ProvenanceContextFacade pcf = new ProvenanceContextFacade(mc);
39          ProvenanceAccount account = pcf.newAccount("example account");
40   
41          Resource sheet = Resource.uriRef("http://example.org/data/style.xsl");
42          Resource doc = Resource.uriRef("http://example.org/data/doc.xml");
43   
44          ProvenanceArtifact docArtifact = pcf.newArtifact("source doc", doc);
45          ProvenanceArtifact sheetArtifact = pcf.newArtifact("stylesheet", sheet);
46   
47          ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
48          //Xml.transform(context.read(doc), context.read(sheet), outBuffer);
49          ByteArrayInputStream inBuffer = new ByteArrayInputStream(outBuffer.toByteArray());
50  
51          Resource result = Resource.uriRef("http://example.org/data/result.xml");
52          context.write(result, inBuffer);
53   
54          // the process has completed, now let's record the provenance
55          ProvenanceArtifact resultArtifact = pcf.newArtifact("transform result");
56          ProvenanceProcess transformProcess = pcf.newProcess("xslt transform");
57   
58          pcf.assertAccount(account);
59          pcf.assertArtifact(sheetArtifact);
60          pcf.assertArtifact(docArtifact);
61          pcf.assertProcess(transformProcess);
62          pcf.assertArtifact(resultArtifact);
63   
64          // the input document and stylesheet are two different kinds of inputs for the transform
65          // process, so each has its own role
66          ProvenanceRole inputDocumentRole = pcf.newRole("input document");
67          ProvenanceRole stylesheetRole = pcf.newRole("stylesheet");
68          ProvenanceRole outputRole = pcf.newRole("output");
69   
70          pcf.assertUsed(transformProcess, docArtifact, inputDocumentRole, account);
71          pcf.assertUsed(transformProcess, sheetArtifact, stylesheetRole, account);
72          pcf.assertGeneratedBy(resultArtifact, transformProcess, outputRole, account);
73  
74  
75          Set<Triple> triplesToWrite = mc.getTriples();
76          RdfXml.write(triplesToWrite, new FileOutputStream(new File("target/foo.xml")));
77      }
78  }