コード例 #1
0
ファイル: TextQueryPF.java プロジェクト: enderunal/jena
  private String extractArg(String prefix, List<Node> objArgs) {
    String value = null;
    int pos = 0;
    for (Node node : objArgs) {
      if (node.isLiteral()) {
        String arg = node.getLiteral().toString();
        if (arg.startsWith(prefix + ":")) {
          value = arg.split(":")[1];
          break;
        }
      }
      pos++;
    }
    if (value != null) objArgs.remove(pos);

    return value;
  }
コード例 #2
0
 private ResultSet ConvertResults(org.apache.jena.query.ResultSet results) {
   ResultSet rs = new ResultSet();
   while (results.hasNext()) {
     Binding b = results.nextBinding();
     Result result = new Result();
     Iterator<Var> v = b.vars();
     while (v.hasNext()) {
       Var currentV = v.next();
       Node val = b.get(currentV);
       if (currentV.toString().contains("_info_")) {
         String[] parts = val.getLiteral().getLexicalForm().toString().split("=");
         if (parts.length > 1) {
           for (int i = 0; i < parts.length; i++) {
             String[] subParts = parts[i].split("\\.");
             if (subParts.length > 1) {
               if (!Character.isDigit(subParts[1].charAt(0))) result.addTable(subParts[0]);
             }
           }
         }
         result.addWhere(val.getLiteral().getLexicalForm());
       } else {
         if (val.isLiteral()) {
           String value = val.getLiteral().getLexicalForm();
           String datatype = val.getLiteralDatatypeURI();
           if (datatype.equals(S2SML.LITERAL_MAP_IRI)) {
             String[] parts = value.split("\\.");
             if (parts.length > 1) {
               if (!Character.isDigit(parts[1].charAt(0))) result.addTable(parts[0]);
             }
           }
         }
         //					System.out.println(currentV.toString().replace("?", "") +" "+val);
         result.addVarMapping(
             currentV.toString().replace("?", ""), FormatUtil.processNode(val, dialect));
       }
     }
     rs.add(result);
   }
   return rs;
 }
コード例 #3
0
ファイル: TextQueryPF.java プロジェクト: enderunal/jena
  /**
   * Deconstruct the node or list object argument and make a StrMatch The 'executionTime' flag
   * indciates whether this is for a build time static check, or for runtime execution.
   */
  private StrMatch objectToStruct(PropFuncArg argObject, boolean executionTime) {
    EntityDefinition docDef = textIndex.getDocDef();
    if (argObject.isNode()) {
      Node o = argObject.getArg();
      if (!o.isLiteral()) {
        if (executionTime) log.warn("Object to text query is not a literal");
        return null;
      }

      RDFDatatype dt = o.getLiteralDatatype();
      if (dt != null && dt != XSDDatatype.XSDstring) {
        log.warn("Object to text query is not a string");
        return null;
      }

      String qs = o.getLiteralLexicalForm();
      return new StrMatch(null, qs, -1, 0);
    }

    List<Node> list = argObject.getArgList();
    if (list.size() == 0 || list.size() > 3)
      throw new TextIndexException("Change in object list size");

    Node predicate = null;
    String field = null; // Do not prepend the field name - rely on default field
    int idx = 0;
    Node x = list.get(0);
    // Property?
    if (x.isURI()) {
      predicate = x;
      idx++;
      if (idx >= list.size())
        throw new TextIndexException("Property specificed but no query string : " + list);
      x = list.get(idx);
      field = docDef.getField(predicate);
      if (field == null) {
        log.warn("Predicate not indexed: " + predicate);
        return null;
      }
    }

    // String!
    if (!x.isLiteral()) {
      if (executionTime) log.warn("Text query string is not a literal " + list);
      return null;
    }

    if (x.getLiteralDatatype() != null && !x.getLiteralDatatype().equals(XSDDatatype.XSDstring)) {
      log.warn("Text query is not a string " + list);
      return null;
    }
    String queryString = x.getLiteralLexicalForm();
    idx++;

    int limit = -1;
    float score = 0;

    if (idx < list.size()) {
      // Limit?
      x = list.get(idx);
      idx++;
      if (!x.isLiteral()) {
        if (executionTime) log.warn("Text query limit is not an integer " + x);
        return null;
      }

      int v = NodeFactoryExtra.nodeToInt(x);
      limit = (v < 0) ? -1 : v;
    }

    String qs = queryString;
    if (field != null) qs = field + ":" + qs;

    return new StrMatch(predicate, qs, limit, score);
  }
コード例 #4
0
ファイル: TextQueryPF.java プロジェクト: enderunal/jena
  @Override
  public QueryIterator exec(
      Binding binding,
      PropFuncArg argSubject,
      Node predicate,
      PropFuncArg argObject,
      ExecutionContext execCxt) {
    if (textIndex == null) {
      if (!warningIssued) {
        Log.warn(getClass(), "No text index - no text search performed");
        warningIssued = true;
      }
      // Not a text dataset - no-op
      return IterLib.result(binding, execCxt);
    }

    DatasetGraph dsg = execCxt.getDataset();

    argSubject = Substitute.substitute(argSubject, binding);
    argObject = Substitute.substitute(argObject, binding);

    Node s = null;
    Node score = null;
    Node literal = null;

    if (argSubject.isList()) {
      // Length checked in build()
      s = argSubject.getArg(0);
      score = argSubject.getArg(1);

      if (!score.isVariable())
        throw new QueryExecException("Hit score is not a variable: " + argSubject);

      if (argSubject.getArgListSize() > 2) {
        literal = argSubject.getArg(2);
        if (!literal.isVariable())
          throw new QueryExecException("Hit literal is not a variable: " + argSubject);
      }
    } else {
      s = argSubject.getArg();
    }

    if (s.isLiteral())
      // Does not match
      return IterLib.noResults(execCxt);

    StrMatch match = objectToStruct(argObject, true);
    if (match == null) {
      // can't match
      return IterLib.noResults(execCxt);
    }

    // ----

    QueryIterator qIter =
        (Var.isVar(s))
            ? variableSubject(binding, s, score, literal, match, execCxt)
            : concreteSubject(binding, s, score, literal, match, execCxt);
    if (match.getLimit() >= 0) qIter = new QueryIterSlice(qIter, 0, match.getLimit(), execCxt);
    return qIter;
  }