Example #1
0
 /** Get exactly one triple, or null for none or more than one. */
 public static Triple triple1(DatasetGraph dsg, Node s, Node p, Node o) {
   Iterator<Quad> iter = dsg.find(Node.ANY, s, p, o);
   if (!iter.hasNext()) return null;
   Quad q = iter.next();
   if (iter.hasNext()) return null;
   return q.asTriple();
 }
 @Override
 protected boolean accepts(Object key, QuadWritable tuple) {
   Quad q = tuple.get();
   if (!q.isConcrete()) return false;
   // Ground if all nodes are URI/Literal
   return (q.getGraph().isURI() || q.getGraph().isLiteral())
       && (q.getSubject().isURI() || q.getSubject().isLiteral())
       && (q.getPredicate().isURI() || q.getPredicate().isLiteral())
       && (q.getObject().isURI() || q.getObject().isLiteral());
 }
Example #3
0
 /**
  * Choose a graph from a DatasetGraph. If it's the union, provide a union graph (not always the
  * best way to deal with union).
  *
  * @param dataset
  * @param graphNode
  * @return Graph
  */
 protected static Graph chooseGraph(DatasetGraph dataset, Node graphNode) {
   if (graphNode == null) return dataset.getDefaultGraph();
   else if (Var.isVar(graphNode))
     throw new NotImplemented("Choosing a graph OpExecutorStage.executeBlockFilter[Variable]");
   else if (graphNode == Node.ANY)
     throw new NotImplemented("OpExecutorMain.executeBlockFilter[Node.ANY]");
   else if (Quad.isUnionGraph(graphNode)) {
     // TODO Check this!  Work needed here to consolidate union graph handling.
     List<Node> graphs = Iter.toList(dataset.listGraphNodes());
     return new GraphUnionRead(dataset, graphs);
   } else return dataset.getGraph(graphNode);
 }
Example #4
0
  private ListMultimap<String, TextHit> query(
      Node property, String queryString, int limit, ExecutionContext execCxt) {
    // use the graph information in the text index if possible
    if (textIndex.getDocDef().getGraphField() != null
        && execCxt.getActiveGraph() instanceof GraphView) {
      GraphView activeGraph = (GraphView) execCxt.getActiveGraph();
      if (!Quad.isUnionGraph(activeGraph.getGraphName())) {
        String uri =
            activeGraph.getGraphName() != null
                ? TextQueryFuncs.graphNodeToString(activeGraph.getGraphName())
                : Quad.defaultGraphNodeGenerated.getURI();
        String escaped = QueryParserBase.escape(uri);
        String qs2 = textIndex.getDocDef().getGraphField() + ":" + escaped;
        queryString = "(" + queryString + ") AND " + qs2;
      }
    }

    // for language-based search extension
    if (textIndex.getDocDef().getLangField() != null) {
      String field = textIndex.getDocDef().getLangField();
      if (langArg != null) {
        String qs2 = !"none".equals(langArg) ? field + ":" + langArg : "-" + field + ":*";
        queryString = "(" + queryString + ") AND " + qs2;
      }
    }

    Explain.explain(execCxt.getContext(), "Text query: " + queryString);
    if (log.isDebugEnabled()) log.debug("Text query: {} ({})", queryString, limit);

    String cacheKey = limit + " " + property + " " + queryString;
    Cache<String, ListMultimap<String, TextHit>> queryCache =
        (Cache<String, ListMultimap<String, TextHit>>) execCxt.getContext().get(cacheSymbol);
    if (queryCache == null) {
        /* doesn't yet exist, need to create it */
      queryCache = CacheFactory.createCache(CACHE_SIZE);
      execCxt.getContext().put(cacheSymbol, queryCache);
    }

    final String queryStr = queryString; // final needed for the lambda function
    ListMultimap<String, TextHit> results =
        queryCache.getOrFill(
            cacheKey,
            () -> {
              List<TextHit> resultList = textIndex.query(property, queryStr, limit);
              ListMultimap<String, TextHit> resultMultimap = LinkedListMultimap.create();
              for (TextHit result : resultList) {
                resultMultimap.put(result.getNode().getURI(), result);
              }
              return resultMultimap;
            });
    return results;
  }
Example #5
0
  @Override
  public QuadBlock rewrite(SDBRequest request, QuadBlock quadBlock) {
    // Does not consider if the property slot is a variable.

    if (!quadBlock.contains(null, null, rdfType, null)) return quadBlock;

    quadBlock = new QuadBlock(quadBlock);

    int i = 0;

    // Better/clearer : do as copy over from one block to another.
    while ((i = quadBlock.findFirst(i, null, null, rdfType, null)) != -1) {
      // { :s rdf:type :C } => { :s rdf:type ?V . ?V rdfs:subClassOf :C }
      Quad rdfTypeQuad = quadBlock.get(i);
      Var var = request.genVar();
      Quad q1 = new Quad(rdfTypeQuad.getGraph(), rdfTypeQuad.getSubject(), rdfType, var);
      Quad q2 =
          new Quad(rdfTypeQuad.getGraph(), var, RDFS.subClassOf.asNode(), rdfTypeQuad.getObject());
      quadBlock.set(i, q1); // replace rdf:type statement
      quadBlock.add(i + 1, q2); // add subClassOf statement
      i = i + 2; // Skip the two statements.
    }
    return quadBlock;
  }