Exemplo n.º 1
0
 /**
  * Extended find interface used in situations where the implementator may or may not be able to
  * answer the complete query. It will attempt to answer the pattern but if its answers are not
  * known to be complete then it will also pass the request on to the nested Finder to append more
  * results.
  *
  * @param pattern a TriplePattern to be matched against the data
  * @param continuation either a Finder or a normal Graph which will be asked for additional match
  *     results if the implementor may not have completely satisfied the query.
  */
 @Override
 public ExtendedIterator<Triple> findWithContinuation(TriplePattern pattern, Finder continuation) {
   if (graph == null) return new NullIterator<Triple>();
   if (continuation == null) {
     return graph.find(pattern.asTripleMatch());
   } else {
     return graph.find(pattern.asTripleMatch()).andThen(continuation.find(pattern));
   }
 }
Exemplo n.º 2
0
  /**
   * Returns a collection of RDFStatements that match the described subject
   *
   * @param subject Subject
   * @return collection of RDFStatements
   */
  public Collection<RDFStatement> getStatements(String subject) {

    if (subject == null || subject.isEmpty()) {
      return null;
    }
    List<RDFStatement> statement = null;

    try {
      // define a describe query
      String query = String.format("DESCRIBE %s FROM < %s >", subject, this.graphName);

      logger.debug("Query: \n{}", query);
      Query sparqlQuery = QueryFactory.create(query);
      VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparqlQuery, this.graph);
      // execute the query and get the graph
      Model model = vqe.execDescribe();
      Graph queriedGraph = model.getGraph();
      // itreate over the retrieved triples, and place them inside a list
      ExtendedIterator<Triple> iter = queriedGraph.find(Node.ANY, Node.ANY, Node.ANY);
      statement = new ArrayList<>();
      while (iter.hasNext()) {
        Triple t = (Triple) iter.next();
        RDFStatement stmt =
            new RDFStatement(
                t.getSubject().toString(), t.getPredicate().toString(), t.getObject().toString());
        statement.add(stmt);
      }
    } catch (Exception ex) {
      logger.error("Exception occured while querying for statements", ex);
    }
    return statement;
  }
 @Override
 public void addGraph(Node gn, Graph g) {
   // Convert to quads.
   // super.addGraph(gn, g) ;
   ExtendedIterator<Triple> iter = g.find(Node.ANY, Node.ANY, Node.ANY);
   for (; iter.hasNext(); ) {
     Triple t = iter.next();
     add(gn, t.getSubject(), t.getPredicate(), t.getObject());
   }
 }
Exemplo n.º 4
0
 public static void removeAll(Graph g, Node s, Node p, Node o) {
   ExtendedIterator<Triple> it = g.find(s, p, o);
   try {
     while (it.hasNext()) {
       Triple t = it.next();
       g.delete(t);
       it.remove();
     }
   } finally {
     it.close();
   }
 }
  public static Collection<Node> containerMembers(Graph graph, Node container, Node containerType) {
    if (!isContainer(graph, container, containerType)) return null;

    ExtendedIterator<Triple> iter = graph.find(container, Node.ANY, Node.ANY);

    SortedMap<Integer, Node> triples = new TreeMap<Integer, Node>(order);
    try {
      for (; iter.hasNext(); ) {
        Triple t = iter.next();
        int index = getIndex(t);
        if (index == NOT_FOUND) continue;
        // Insert
        triples.put(new Integer(index), t.getObject());
      }
    } finally {
      iter.close();
    }
    return triples.values();
  }
Exemplo n.º 6
0
  @Override
  public void delete(Graph g) {
    Model[] model = separateStatementsWithBlankNodes(g);
    deleteModel(model[1] /*statements without blank nodes*/);
    // replace blank nodes in remaining statements with variables

    StringBuffer patternBuff = new StringBuffer();
    Iterator<Triple> tripIt = g.find(null, null, null);
    while (tripIt.hasNext()) {
      Triple t = tripIt.next();
      patternBuff.append(SparqlGraph.sparqlNodeDelete(t.getSubject(), null));
      patternBuff.append(" ");
      patternBuff.append(SparqlGraph.sparqlNodeDelete(t.getPredicate(), null));
      patternBuff.append(" ");
      patternBuff.append(SparqlGraph.sparqlNodeDelete(t.getObject(), null));
      patternBuff.append(" .\n");
    }

    StringBuffer queryBuff = new StringBuffer();
    String graphURI = graph.getGraphURI();
    queryBuff.append(
        "DELETE { " + ((graphURI != null) ? "GRAPH <" + graphURI + "> { " : "") + " \n");
    queryBuff.append(patternBuff);
    if (graphURI != null) {
      queryBuff.append("    } \n");
    }
    queryBuff.append("} WHERE { \n");
    if (graphURI != null) {
      queryBuff.append("    GRAPH <" + graphURI + "> { \n");
    }
    queryBuff.append(patternBuff);
    if (graphURI != null) {
      queryBuff.append("    } \n");
    }
    queryBuff.append("} \n");

    log.debug(queryBuff.toString());

    graph.executeUpdate(queryBuff.toString());
  }
  private static int countContainerMember(
      Graph graph, Node container, Node containerType, Node member, boolean stopEarly) {
    if (graph == null) {
      Log.warn(GraphContainerUtils.class, "containerMember called with null graph");
      return 0;
    }

    if (container == null) {
      Log.warn(GraphContainerUtils.class, "containerMember called with null list");
      return 0;
    }
    if (member == null) {
      Log.warn(GraphContainerUtils.class, "containerMember called with null member");
      return 0;
    }

    if (!isContainer(graph, container, containerType)) return 0;

    int count = 0;
    ExtendedIterator<Triple> iter = graph.find(container, Node.ANY, member);
    try {
      for (; iter.hasNext(); ) {
        Triple t = iter.next();
        Node p = t.getPredicate();
        String u = p.getURI();

        if (u.matches(membershipPattern)) {
          count++;
          if (stopEarly) return count;
        }
      }
    } finally {
      iter.close();
    }
    return count;
  }
Exemplo n.º 8
0
 /**
  * Basic pattern lookup interface.
  *
  * @param pattern a TriplePattern to be matched against the data
  * @return a ClosableIterator over all Triples in the data set that match the pattern
  */
 @Override
 public ExtendedIterator<Triple> find(TriplePattern pattern) {
   if (graph == null) return new NullIterator<Triple>();
   return graph.find(pattern.asTripleMatch());
 }
 public static ExtendedIterator predicatesFor(Graph g, Node s, Node o) {
   Set predicates = CollectionFactory.createHashedSet();
   ClosableIterator it = g.find(s, Node.ANY, o);
   while (it.hasNext()) predicates.add(((Triple) it.next()).getPredicate());
   return WrappedIterator.createNoRemove(predicates.iterator());
 }
Exemplo n.º 10
0
 public static ExtendedIterator subjectsFor(Graph g, Node p, Node o) {
   Set objects = CollectionFactory.createHashedSet();
   ClosableIterator it = g.find(Node.ANY, p, o);
   while (it.hasNext()) objects.add(((Triple) it.next()).getSubject());
   return WrappedIterator.createNoRemove(objects.iterator());
 }