@Override public void add(List<Statement> statements) { Model graph = null; GraphConnection graphConnection = null; try { graphConnection = openGraph(); graph = graphConnection.getGraph(); graph.enterCriticalSection(Lock.WRITE); for (Statement nuxStmt : statements) { com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStmt.getSubject()); com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStmt.getPredicate()); com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStmt.getObject()); Triple jenaTriple = Triple.create(subject, predicate, object); com.hp.hpl.jena.rdf.model.Statement jenaStmt = graph.asStatement(jenaTriple); // properties Map<Resource, Node[]> properties = nuxStmt.getProperties(); if (properties == null || properties.isEmpty()) { // no properties graph.add(jenaStmt); } else { List<com.hp.hpl.jena.rdf.model.Statement> stmts = new ArrayList<com.hp.hpl.jena.rdf.model.Statement>(); stmts.add(jenaStmt); // create reified statement if it does not exist com.hp.hpl.jena.graph.Node reifiedStmt = graph.getAnyReifiedStatement(jenaStmt).asNode(); for (Map.Entry<Resource, Node[]> property : properties.entrySet()) { com.hp.hpl.jena.graph.Node prop = getJenaNode(property.getKey()); for (Node node : property.getValue()) { com.hp.hpl.jena.graph.Node value = getJenaNode(node); Triple propTriple = Triple.create(reifiedStmt, prop, value); stmts.add(graph.asStatement(propTriple)); } } graph.add(stmts); } } } finally { if (graph != null) { graph.leaveCriticalSection(); } if (graphConnection != null) { graphConnection.close(); } } }
/** * Gets NXRelations statement corresponding to the Jena statement. * * <p>Reified statements may be retrieved from the Jena graph and set as properties on NXRelations * statements. * * @param graph the jena graph * @param jenaStatement jena statement * @return NXRelations statement */ private Statement getNXRelationsStatement( Model graph, com.hp.hpl.jena.rdf.model.Statement jenaStatement) { Node subject = getNXRelationsNode(jenaStatement.getSubject().asNode()); Node predicate = getNXRelationsNode(jenaStatement.getPredicate().asNode()); Node object = getNXRelationsNode(jenaStatement.getObject().asNode()); Statement statement = new StatementImpl(subject, predicate, object); // take care of properties if (graph.isReified(jenaStatement)) { com.hp.hpl.jena.rdf.model.Resource reifiedStmt = graph.getAnyReifiedStatement(jenaStatement); StmtIterator it = reifiedStmt.listProperties(); while (it.hasNext()) { com.hp.hpl.jena.rdf.model.Statement stmt = it.nextStatement(); Node nuxNode = getNXRelationsNode(stmt.getPredicate().asNode()); // ugly cast as a Resource Node value = getNXRelationsNode(stmt.getObject().asNode()); statement.addProperty((Resource) nuxNode, value); } } return statement; }
public void testGetAny() { Resource r = model.getAnyReifiedStatement(SPO); assertInstanceOf(ReifiedStatement.class, r); assertEquals("should get me the statement", SPO, ((ReifiedStatement) r).getStatement()); }