Skip to topic | Skip to bottom

Open Provenance Model

OPM
OPM.OpmDBTestSample

Start of topic | Skip to actions

Create OPM Graph: Java example

In this section, we provide some examples on how to use the API for buiding, storing, retrieving, updating, and deleting OPM objects. The Victoria Sponge Cake Provenance is used as a scenario to illustrate the different functionalities of the Plier API (OPMSample.java).
In order to facilitate the persistence of OPM objects, we used the java class PersistenceManager.java, which hide the details of managing the sessions and transactions.

/*
 *
 * @author Souley Madougou, Ammar Benabdelkader, Victor Guevara
 * @version 1.2
 *
 */
package nl.biggrid.plier.tools;

import java.util.*;
import nl.biggrid.plier.opm.*;

public class OPMSample {

    public static OPMGraph createOPM() {
        Accounts accountList = new Accounts();
        Agents agentList = new Agents();
        Artifacts artifactList = new Artifacts();
        Processes processList = new Processes();
        Overlaps overlapsList = new Overlaps();
        CausalDependencies cdList = new CausalDependencies();
        Annotations annotationList = new Annotations();

        // Create one account
        Account account1 = new Account();
        account1.setId("account 1");
        accountList.getAccount().add(account1);

        // create two agents
        Agent agent1 = new Agent();
        agent1.setId("John Doe");
        agent1.getAccount().add(account1);
        agentList.getAgent().add(agent1);

        // create one process
        nl.biggrid.plier.opm.Process process = new nl.biggrid.plier.opm.Process();
        process.setId("baking cake with ingredients");
        Date d = new Date(Long.parseLong("1301045589371"));
        process.setStartTime(d);
        d = new Date(Long.parseLong("1301046300776"));
        process.setTime(new Date());
        process.getAccount().add(account1);
        processList.getProcess().add(process);

        // create 5 artifacts
        Artifact a1 = new Artifact();
        a1.setId("butter"); a1.setValue("100 g butter"); a1.getAccount().add(account1);
        Artifact a2 = new Artifact();
        a2.setId("eggs"); a2.setValue("two eggs"); a2.getAccount().add(account1);
        Artifact a3 = new Artifact();
        a3.setId("flour"); a3.setValue("100 g flour"); a3.getAccount().add(account1);
        Artifact a4 = new Artifact();
        a4.setId("sugar"); a4.setValue("100 g sugar"); a4.getAccount().add(account1);
        Artifact a5 = new Artifact();
        a5.setId("cake"); a5.setValue("cake"); a5.getAccount().add(account1);

        artifactList.getArtifact().add(a1); artifactList.getArtifact().add(a2);
        artifactList.getArtifact().add(a3); artifactList.getArtifact().add(a4);
        artifactList.getArtifact().add(a5);

        // create 5 causal dependencies Used
        Used u1 = new Used();
        u1.setId("used1"); u1.setCause(a1); u1.setEffect(process); u1.setRole("butter");
        Used u2 = new Used();
        u2.setId("used2"); u2.setCause(a2); u2.setEffect(process); u2.setRole("egg");
        Used u3 = new Used();
        u3.setId("used3"); u3.setCause(a3); u3.setEffect(process); u3.setRole("flour");
        Used u4 = new Used();
        u4.setId("used4"); u4.setCause(a4); u4.setEffect(process); u4.setRole("sugar");
        cdList.getDependency().add(u1); cdList.getDependency().add(u2);
        cdList.getDependency().add(u3); cdList.getDependency().add(u4);
        
        // create 1 causal dependencies WasGeneratedBy
        WasGeneratedBy wgb1 = new WasGeneratedBy();
        wgb1.setId("wgb1"); wgb1.setCause(process); wgb1.setEffect(a5); wgb1.setRole("(cake)");
        cdList.getDependency().add(wgb1);

        // create 2 causal dependencies WasControlledBy
        WasControlledBy wcb1 = new WasControlledBy();
        wcb1.setId("wcb1"); wcb1.setCause(agent1); wcb1.setEffect(process); wcb1.setRole("baker");
        cdList.getDependency().add(wcb1);

        // create two events Start and End as properties
        Property p1 = new Property();
        p1.setUri("STARTED"); p1.setValue("1301045589371");
        Property p2 = new Property();
        p2.setUri("COMPLETED"); p2.setValue("1301046589371");

        // create process STATUS as annotation
        Annotation ann = new Annotation();
        ann.setLocalSubject("STATUS"); ann.setId(process.getId());
        ann.getPropertyList().add(p1); ann.getPropertyList().add(p2);
        annotationList.getAnnotation().add(ann);
        process.getAnnotation().add(ann);

        OPMGraph graph = new OPMGraph();
            graph.setId("Victoria Sponge Cake Provenance");
            graph.setAccounts(accountList);
            graph.setAgents(agentList);
            graph.setProcesses(processList);
            graph.setArtifacts(artifactList);
            graph.setCausalDependencies(cdList);
            graph.setAnnotations(annotationList);

        return graph;
    }
public static OPMGraph createOPM_usingFactory() {
        OPMFactory oFactory = new OPMFactory();
        Collection<Account> accountList = new ArrayList<Account>();
        Collection<Agent> agentList = new ArrayList<Agent>();
        Collection<Artifact> artifactList = new ArrayList<Artifact>();
        Collection<nl.biggrid.plier.opm.Process> processList = new ArrayList<nl.biggrid.plier.opm.Process>();
        Collection<CausalDependency> cdList = new ArrayList<CausalDependency>();
        Collection<Annotation> annotationList = new ArrayList<Annotation>();
        Collection<Overlaps> overlapsList = new ArrayList<Overlaps>();
        List<Property> propertyList = new ArrayList<Property>();

        Account mainAccount = oFactory.newAccount("main");
        accountList.add(mainAccount);

        Agent agent1 = oFactory.newAgent("John", accountList, "John Doe");
        Agent agent2 = oFactory.newAgent("bakery", accountList, "Fresh Bakery Amsterdam");
        agentList.add(agent1); agentList.add(agent2);


        nl.biggrid.plier.opm.Process process = oFactory.newProcess("baking cake with ingredients", accountList,"bake");
        processList.add(process);

        Artifact a1 = oFactory.newArtifact("butter", accountList, "100 g butter");
        a1.setValue("butter");
        Artifact a2 = oFactory.newArtifact("eggs", accountList, "two eggs");
        a2.setValue("eggs");
        Artifact a3 = oFactory.newArtifact("flour", accountList, "100 g flour");
        a3.setValue("flour");
        Artifact a4 = oFactory.newArtifact("sugar", accountList, "100 g sugar");
        a4.setValue("sugar");
        Artifact a5 = oFactory.newArtifact("cake", accountList, "cake");
        a5.setValue("cake");
        artifactList.add(a1); artifactList.add(a2); artifactList.add(a3);
        artifactList.add(a4); artifactList.add(a5);

        Used u1 = oFactory.newUsed("used1", process, "(butter)", a1, accountList);
        Used u2 = oFactory.newUsed("used2", process, "(egg)", a2, accountList);
        Used u3 = oFactory.newUsed("used3", process, "(flour)", a3, accountList);
        Used u4 = oFactory.newUsed("used4", process, "(sugar)", a4, accountList);
        cdList.add(u1); cdList.add(u2); cdList.add(u3); cdList.add(u4);

        WasGeneratedBy wgb1 = oFactory.newWasGeneratedBy("wgb1", a5, "cake", process, accountList);
        cdList.add(wgb1);

        WasControlledBy wcb1 = oFactory.newWasControlledBy("wcb1", process, "baker", agent1, accountList);
        WasControlledBy wcb2 = oFactory.newWasControlledBy("wcb2", process, "bakery", agent2, accountList);
        cdList.add(wcb1); cdList.add(wcb2);

        // create two events Start and End as properties
        Property p1 = oFactory.newProperty("STARTED", "1301045589371");
        Property p2 = oFactory.newProperty("COMPLETED", "1301046589371");
        propertyList.add(p1); propertyList.add(p2);

        // create process STATUS as annotation
        Annotation ann = oFactory.newAnnotation(process.getId(), "STATUS", propertyList, accountList);
        annotationList.add(ann);
        process.getAnnotation().add(ann);

        OPMGraph graph = oFactory.newOPMGraph("Victoria Sponge Cake Provenance",
                accountList,
                overlapsList,
                processList,
                artifactList,
                agentList,
                cdList,
                annotationList);

        return graph;
    }

public static void viewOPM(OPMGraph graph) {
    System.out.println("GRAPH: " + graph.getId());
    System.out.println("\t- ACCOUNTS: " + graph.getAccounts().getAccount().size());
    System.out.println("\t- OVERLAPS: " + graph.getAccounts().getOverlaps().size());
    System.out.println("\t- AGENTS: " + graph.getAgents().getAgent().size());
    System.out.println("\t- ARTIFACTS: " + graph.getArtifacts().getArtifact().size());
    System.out.println("\t- PROCESSES: " + graph.getProcesses().getProcess().size());
    System.out.println("\t- DEPENDENCIES: " + graph.getCausalDependencies().getDependency().size());
    System.out.println("\t- ANNOTATIONS: " + graph.getAnnotations().getAnnotation().size());
}
public static OPMGraph updateOPM(OPMGraph graph) {
    // create a new annotation with a new property (event) and attach it to the process
    Annotation ann = new Annotation();
    ann.setId(graph.getProcesses().getProcess().get(0).getId());
    ann.setLocalSubject("STATUS");
    Property p2 = new Property();
    p2.setUri("BURNED OUT");
    p2.setValue("1301047689371");
    ann.getPropertyList().add(p2);
    graph.getProcesses().getProcess().get(0).getAnnotation().add(ann);
    Date d = new Date(Long.parseLong("1301045589371"));
    graph.getProcesses().getProcess().get(0).setStartTime(d);
    d = new Date(Long.parseLong("1301046300776"));
    graph.getProcesses().getProcess().get(0).setTime(d);
    graph.getAnnotations().getAnnotation().add(ann);
    return graph;
}
public static void main(String args[]) {
        PersistenceManager persistenceManager = PersistenceManager.instance();
        persistenceManager.init("hibernate.cfg.xml");
        
        // Create the OPM graph using PLIER concepts
        persistenceManager.persist(createOPM());

        // read the OPM Graphs using getAll method;
        List<OPMGraph> graphs = (List<OPMGraph>) persistenceManager.getAll();
        Iterator it = graphs.iterator();
        while (it.hasNext()) {
            viewOPM((OPMGraph) it.next());
        }

        // read the OPM Graphs using executeQuery (Hibernate);
        // You can also read the OPM graphs using executeSQL method, (SQL compliant)
        List results = (List) persistenceManager.executeQuery("from OPMGraph where GraphId like '%Victoria Sponge Cake Provenance'");
        it = results.iterator();
        OPMGraph graph = null;
        while (it.hasNext()) {
            persistenceManager.delete((OPMGraph) it.next());
        }
        
        // Create the OPM graph using the Java OPMFactory
        persistenceManager.persist(createOPM_usingFactory());


        // You can use the GraphKey (database key) to retrive the Graph
        // graph = (OPMGraph) persistenceManager.get(OPMGraph.class, new Long(1));
        // viewOPM(graph);

        // to Delete a graph you need at first, to retrieve it using one of the methods described above
        // graph = (OPMGraph) persistenceManager.get(OPMGraph.class, new Long(1));
        // persistenceManager.delete(graph);

        // to upedate a graph, you need to retrive it, update it localy, and then persist the updates
        results = (List) persistenceManager.executeQuery("from OPMGraph where GraphId like '%Victoria Sponge Cake Provenance'");
        it = results.iterator();
        if (it.hasNext()) {
            graph = (OPMGraph) it.next();
        }
        persistenceManager.update(updateOPM(graph));
        viewOPM(graph);

    }
}


-- AmmarBenabdelkader - 11 Jan 2011


to top

I Attachment sort Action Size Date Who Comment
OPMSample.java manage 11.0 K 12 Jul 2011 - 13:00 AmmarBenabdelkader An example to read/write OPM objects using PLIER
PersistenceManager.java manage 27.4 K 12 Jul 2011 - 13:01 AmmarBenabdelkader Java class to facilitate the persitence of OPM objects using PLIER

You are here: OPM > CommunityContributions > Plier > OpmDBTest > OpmDBTestSample

to top

Copyright © 1999-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback