예제 #1
0
 @Override
 public List<Node> getPredicates(Node subject, Node object) {
   Model graph = null;
   GraphConnection graphConnection = null;
   try {
     graphConnection = openGraph();
     graph = graphConnection.getGraph();
     graph.enterCriticalSection(Lock.READ);
     SimpleSelector selector = getJenaSelector(graph, new StatementImpl(subject, null, object));
     StmtIterator it = graph.listStatements(selector);
     List<Statement> statements = getNXRelationsStatements(graph, it.toList());
     List<Node> res = new ArrayList<Node>();
     for (Statement stmt : statements) {
       Node predicate = stmt.getPredicate();
       if (!res.contains(predicate)) {
         // remove duplicates
         res.add(predicate);
       }
     }
     return res;
   } finally {
     if (graph != null) {
       graph.leaveCriticalSection();
     }
     if (graphConnection != null) {
       graphConnection.close();
     }
   }
 }
예제 #2
0
 @Override
 public void remove(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);
       graph.remove(jenaStmt);
       // remove properties
       RSIterator it = graph.listReifiedStatements(jenaStmt);
       while (it.hasNext()) {
         ReifiedStatement rs = it.nextRS();
         rs.removeProperties();
       }
       // remove quadlets
       graph.removeAllReifications(jenaStmt);
       // graph.removeReification(reifiedStmt);
     }
   } finally {
     if (graph != null) {
       graph.leaveCriticalSection();
     }
     if (graphConnection != null) {
       graphConnection.close();
     }
   }
 }
예제 #3
0
  @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();
      }
    }
  }
예제 #4
0
 /**
  * Gets Jena statement selector corresponding to the NXRelations statement.
  *
  * @param graph the jena graph
  * @param nuxStatement NXRelations statement
  * @return jena statement selector
  */
 private static SimpleSelector getJenaSelector(Model graph, Statement nuxStatement) {
   com.hp.hpl.jena.rdf.model.Resource subjResource = null;
   com.hp.hpl.jena.graph.Node subject = getJenaNode(nuxStatement.getSubject());
   if (subject != null && subject.isURI()) {
     subjResource = graph.getResource(subject.getURI());
   }
   Property predProp = null;
   com.hp.hpl.jena.graph.Node predicate = getJenaNode(nuxStatement.getPredicate());
   if (predicate != null && predicate.isURI()) {
     predProp = graph.getProperty(predicate.getURI());
   }
   com.hp.hpl.jena.graph.Node object = getJenaNode(nuxStatement.getObject());
   RDFNode objRDF = null;
   if (object != null) {
     objRDF = graph.asRDFNode(object);
   }
   return new SimpleSelector(subjResource, predProp, objRDF);
 }
 protected List<StatementInfo> getStatementsInfo(List<Statement> statements)
     throws ClientException {
   if (statements == null) {
     return null;
   }
   List<StatementInfo> infoList = new ArrayList<StatementInfo>();
   for (Statement statement : statements) {
     Subject subject = statement.getSubject();
     // TODO: filter on doc visibility (?)
     NodeInfo subjectInfo = new NodeInfoImpl(subject, getDocumentModel(subject), true);
     Resource predicate = statement.getPredicate();
     Node object = statement.getObject();
     NodeInfo objectInfo = new NodeInfoImpl(object, getDocumentModel(object), true);
     StatementInfo info =
         new StatementInfoImpl(statement, subjectInfo, new NodeInfoImpl(predicate), objectInfo);
     infoList.add(info);
   }
   return infoList;
 }