2
votes

We are developing an application which is using Marklogic as the document store for entities. We are planning to establish relationship between entities using semantics.

For example: Company is an entity and "ABC Corporation" is an instance of Company entity. Likewise Truck is an entity and "Volvo 101" is an instance of Truck entity.

We intend to define these relations by creating the triples as below when user creates instance of business entities in UI.

However we are facing issues when we use GraphManager.merge() as this method is adding new triple to the graph every time when the subject, predicate and object are same . The existing triple is not being overwritten.

We also tried the write/writeAs method and still see the same behavior.

We also looked at sesame and jena api's provided by Marklogic but couldn't find good documentation. If we intend to do lot of semantic operations and build triples dynamically which is the better api to use for semantics ? Marklogic-java-api or sesame or jena?

Code snippet:

String subjectURI = "http://example.org/entityinstance/ABCCorporation";
String predicateURI = "http://example.org/relation/instanceof";
String objectURI = "http://example.org/entity/company";
String graphURI = "http://example.org/graph/Relation";

public void createTriple(String subjectURI, String predicateURI,
        String objectURI, String graphURI)

{
    DatabaseClient client = markLogicConnectionHelper.getMLConnection();

    String tripleStore = "<" + subjectURI + ">" + " " + " <" + predicateURI
            + ">" + " " + "<" + objectURI + ">" + ".";

    GraphManager graphManager = client.newGraphManager();
    graphManager.setDefaultMimetype(RDFMimeTypes.NTRIPLES);

    graphManager.merge(graphURI, new StringHandle(tripleStore));

}
1

1 Answers

3
votes

If you want to build triples, you will want to use an RDF API for Java. jena and sesame are two such APIs. sesame has morphed into being rdf4j.

Those APIs both provide nice ways to create and manipulate triples and graphs. If you use marklogic-jena, you can store these graphs in MarkLogic using Jena's persistence mechanisms.

DatabaseClient client = DatabaseClientFactory.newClient(...);
JenaDatabaseClient c = new JenaDatabaseClient(client);
MarkLogicDatasetGraph dsg = new MarkLogicDatasetGraph(c);

dsg.add(NodeFactory.createURI("http://..."),
    NodeFactory.createURI("http://example.org/..."),
    NodeFactory.createURI("http://..."),
    NodeFactory.createLiteral("10", XSDinteger));

If you want this method to be speedy, however, I recommend you use an upcoming release of marklogic-jena 1.0.2 or 3.0.2.

The mergeGraph method you mention does append triples. You can use replaceGraph to replace triples. What's important to note about a triple store is that it's like a key/value store for graphs. Use a graph name to determine what set of triples you want to update as a set. You'll see in jena JavaDocs that you can manipulate whole graphs at once:

dsg.addGraph(graphName, graph);