Пример #1
0
  public static void main(String[] a) throws Exception {
    System.out.println("ARQ Example: " + Utils.classShortName(ExLucene3.class));
    System.out.println("ARQ: " + ARQ.VERSION);
    System.out.println();

    Model model = ModelFactory.createDefaultModel();
    IndexLARQ index = buildTitleIndex(model, "src/test/resources/LARQ/data-1.ttl");

    // Search for string
    String searchString = "+document";

    // This time, find documents with a matching DC title.
    String queryString =
        StrUtils.strjoin(
            "\n",
            "PREFIX pf:     <http://jena.hpl.hp.com/ARQ/property#>",
            "PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>",
            "PREFIX dc:     <http://purl.org/dc/elements/1.1/>",
            "PREFIX :       <http://example/>",
            "SELECT ?title {",
            "    ?title pf:textMatch '" + searchString + "'.",
            "}");

    // Two of three documents should match.
    ExLucene1.performQuery(model, index, queryString);
    index.close();
  }
Пример #2
0
  private void executeForm(HttpAction action) {
    String requestStr = action.request.getParameter(paramUpdate);
    if (requestStr == null) requestStr = action.request.getParameter(paramRequest);

    if (action.verbose) action.log.info(format("[%d] Form update = \n%s", action.id, requestStr));
    // A little ugly because we are taking a copy of the string, but hopefully shouldn't be too big
    // if we are in this code-path
    // If we didn't want this additional copy, we could make the parser take a Reader in addition to
    // an InputStream
    byte[] b = StrUtils.asUTF8bytes(requestStr);
    ByteArrayInputStream input = new ByteArrayInputStream(b);
    requestStr = null; // free it early at least
    execute(action, input);
    ServletOps.successPage(action, "Update succeeded");
  }
Пример #3
0
  public void print(PrintContext p) {

    Variable asVar = getAs();
    if (asVar != null) {
      p.print("(");
    }

    Resource aggType = getResource(RDF.type);
    String aggName = Aggregations.getName(aggType);
    p.printKeyword(aggName);
    p.print("(");

    if (isDistinct()) {
      p.print("DISTINCT ");
    }

    Statement exprS = getProperty(SP.expression);
    if (exprS != null && exprS.getObject().isResource()) {
      Resource r = exprS.getResource();
      RDFNode expr = SPINFactory.asExpression(r);
      if (expr instanceof Printable) {
        ((Printable) expr).print(p);
      } else {
        p.printURIResource(r);
      }
    } else {
      p.print("*");
    }
    String separator = getString(SP.separator);
    if (separator != null) {
      p.print("; ");
      p.printKeyword("SEPARATOR");
      p.print("='" + StrUtils.escapeString(separator) + "'");
    }
    if (asVar != null) {
      p.print(") ");
      p.printKeyword("AS");
      p.print(" ");
      p.print(asVar.toString());
    }
    p.print(")");
  }
  @Override
  public EntityDefinition open(Assembler a, Resource root, Mode mode) {
    String prologue = "PREFIX : <" + NS + ">   PREFIX list: <http://jena.apache.org/ARQ/list#> ";
    Model model = root.getModel();

    String qs1 =
        StrUtils.strjoinNL(
            prologue,
            "SELECT * {",
            "  ?eMap  :entityField  ?entityField ;",
            "         :map ?map ;",
            "         :defaultField ?dftField .",
            "  OPTIONAL {",
            "    ?eMap :graphField ?graphField",
            "  }",
            "  OPTIONAL {",
            "    ?eMap :langField ?langField",
            "  }",
            "  OPTIONAL {",
            "    ?eMap :uidField ?uidField",
            "  }",
            "}");
    ParameterizedSparqlString pss = new ParameterizedSparqlString(qs1);
    pss.setIri("eMap", root.getURI());

    Query query1 = QueryFactory.create(pss.toString());
    QueryExecution qexec1 = QueryExecutionFactory.create(query1, model);
    ResultSet rs1 = qexec1.execSelect();
    List<QuerySolution> results = ResultSetFormatter.toList(rs1);
    if (results.size() == 0) {
      Log.warn(this, "Failed to find a valid EntityMap for : " + root);
      throw new TextIndexException("Failed to find a valid EntityMap for : " + root);
    }

    if (results.size() != 1) {
      Log.warn(this, "Multiple matches for EntityMap for : " + root);
      throw new TextIndexException("Multiple matches for EntityMap for : " + root);
    }

    QuerySolution qsol1 = results.get(0);
    String entityField = qsol1.getLiteral("entityField").getLexicalForm();
    String graphField =
        qsol1.contains("graphField") ? qsol1.getLiteral("graphField").getLexicalForm() : null;
    String langField =
        qsol1.contains("langField") ? qsol1.getLiteral("langField").getLexicalForm() : null;
    String defaultField =
        qsol1.contains("dftField") ? qsol1.getLiteral("dftField").getLexicalForm() : null;
    String uniqueIdField =
        qsol1.contains("uidField") ? qsol1.getLiteral("uidField").getLexicalForm() : null;

    Multimap<String, Node> mapDefs = HashMultimap.create();
    Map<String, Analyzer> analyzerDefs = new HashMap<>();

    Statement listStmt = root.getProperty(TextVocab.pMap);
    while (listStmt != null) {
      RDFNode n = listStmt.getObject();
      if (!n.isResource()) {
        throw new TextIndexException("Text list node is not a resource : " + n);
      }
      Resource listResource = n.asResource();
      if (listResource.equals(RDF.nil)) {
        break; // end of the list
      }

      Statement listEntryStmt = listResource.getProperty(RDF.first);
      if (listEntryStmt == null) {
        throw new TextIndexException("Text map list is not well formed.  No rdf:first property");
      }
      n = listEntryStmt.getObject();
      if (!n.isResource()) {
        throw new TextIndexException("Text map list entry is not a resource : " + n);
      }
      Resource listEntry = n.asResource();

      Statement fieldStatement = listEntry.getProperty(TextVocab.pField);
      if (fieldStatement == null) {
        throw new TextIndexException("Text map entry has no field property");
      }
      n = fieldStatement.getObject();
      if (!n.isLiteral()) {
        throw new TextIndexException("Text map entry field property has no literal value : " + n);
      }
      String field = n.asLiteral().getLexicalForm();

      Statement predicateStatement = listEntry.getProperty(TextVocab.pPredicate);
      if (predicateStatement == null) {
        throw new TextIndexException("Text map entry has no predicate property");
      }
      n = predicateStatement.getObject();
      if (!n.isURIResource()) {
        throw new TextIndexException(
            "Text map entry predicate property has non resource value : " + n);
      }
      mapDefs.put(field, n.asNode());

      Statement analyzerStatement = listEntry.getProperty(TextVocab.pAnalyzer);
      if (analyzerStatement != null) {
        n = analyzerStatement.getObject();
        if (!n.isResource()) {
          throw new TextIndexException("Text map entry analyzer property is not a resource : " + n);
        }
        Resource analyzerResource = n.asResource();
        Analyzer analyzer = (Analyzer) a.open(analyzerResource);
        analyzerDefs.put(field, analyzer);
      }

      // move on to the next element in the list
      listStmt = listResource.getProperty(RDF.rest);
    }

    // Primary field/predicate
    if (defaultField != null) {
      Collection<Node> c = mapDefs.get(defaultField);
      if (c.isEmpty())
        throw new TextIndexException("No definition of primary field '" + defaultField + "'");
    }

    EntityDefinition docDef = new EntityDefinition(entityField, defaultField);
    docDef.setGraphField(graphField);
    docDef.setLangField(langField);
    docDef.setUidField(uniqueIdField);
    for (String f : mapDefs.keys()) {
      for (Node p : mapDefs.get(f)) docDef.set(f, p);
    }
    for (String f : analyzerDefs.keySet()) {
      docDef.setAnalyzer(f, analyzerDefs.get(f));
    }
    return docDef;
  }