Package org.openrdf.query

Examples of org.openrdf.query.TupleQueryResult


    String query = "SELECT testName, inputURL, outputURL " + "FROM {} mf:name {testName}; "
        + "        mf:result {outputURL}; " + "        mf:action {} qt:data {inputURL} "
        + "USING NAMESPACE " + "  mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
        + "  qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";

    TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query).evaluate();

    // Add all positive parser tests to the test suite
    while (queryResult.hasNext()) {
      BindingSet bindingSet = queryResult.next();
      testName = ((Literal)bindingSet.getValue("testName")).getLabel();
      inputURL = ((URI)bindingSet.getValue("inputURL")).toString();
      outputURL = ((URI)bindingSet.getValue("outputURL")).toString();

      baseURL = BASE_URL + testName + ".ttl";

      suite.addTest(new PositiveParserTest(testName, inputURL, outputURL, baseURL));
    }

    queryResult.close();

    // Add the manifest for negative test cases to a repository and query it
    con.clear();
    url = TurtleParserTest.class.getResource(MANIFEST_BAD_URL);
    con.add(url, url.toExternalForm(), RDFFormat.TURTLE);

    query = "SELECT testName, inputURL " + "FROM {} mf:name {testName}; "
        + "        mf:action {} qt:data {inputURL} " + "USING NAMESPACE "
        + "  mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, "
        + "  qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>";
    queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query).evaluate();

    // Add all negative parser tests to the test suite
    while (queryResult.hasNext()) {
      BindingSet bindingSet = queryResult.next();
      testName = ((Literal)bindingSet.getValue("testName")).toString();
      inputURL = ((URI)bindingSet.getValue("inputURL")).toString();

      baseURL = BASE_URL + testName + ".ttl";

      suite.addTest(new NegativeParserTest(testName, inputURL, baseURL));
    }

    queryResult.close();
    con.close();
    repository.shutDown();

    return suite;
  }
View Full Code Here


  private void testQueryResultFormat(TupleQueryResultFormat format)
    throws IOException, QueryResultParseException, TupleQueryResultHandlerException,
    UnsupportedQueryResultFormatException, QueryEvaluationException
  {
    TupleQueryResult input = createQueryResult();
    TupleQueryResult expected = createQueryResult();

    ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
    QueryResultIO.write(input, format, out);
    input.close();

    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    TupleQueryResult output = QueryResultIO.parse(in, format);

    assertTrue(QueryResultUtil.equals(expected, output));
  }
View Full Code Here

  }

  public void testGetBindingNames()
    throws Exception
  {
    TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SERQL, multipleResultQuery).evaluate();
    try {
      List<String> headers = result.getBindingNames();

      if (!headers.get(0).equals("P")) {
        fail("first header element should be 'P' but is '" + headers.get(0) + "'");
      }
      if (!headers.get(1).equals("D")) {
        fail("second header element should be 'D' but is '" + headers.get(1) + "'");
      }
    }
    finally {
      result.close();
    }
  }
View Full Code Here

  */

  public void testIterator()
    throws Exception
  {
    TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SERQL, multipleResultQuery).evaluate();

    try {
      int count = 0;
      while(result.hasNext()) {
        result.next();
        count++;
      }

      if (count <= 1) {
        fail("query should have multiple results.");
      }
    }
    finally {
      result.close();
    }
  }
View Full Code Here

  }

  public void testIsEmpty()
    throws Exception
  {
    TupleQueryResult result = con.prepareTupleQuery(QueryLanguage.SERQL, emptyResultQuery).evaluate();

    try {
      if (result.hasNext()) {
        fail("Query result should be empty");
      }
    }
    finally {
      result.close();
    }
  }
View Full Code Here

    }
   
    void processOntology(RepositoryConnection c) throws RepositoryException, QueryEvaluationException {
  String queryString = "SELECT * FROM {" + X_var + "} <http://www.w3.org/2000/01/rdf-schema#subClassOf> {" + Y_var + "}";
  try {
      TupleQueryResult sss = c.prepareTupleQuery(QueryLanguage.SERQL, queryString).evaluate();
      processSubsumptionStatements(sss);
      sss.close();
      processHierarchy(classHierarchy);
  }
  catch (MalformedQueryException ex){
      System.err.println("Error: malformed SeRQL query \n" + queryString);
      if (DEBUG){ex.printStackTrace();}
  }
  queryString = "SELECT * FROM {" + X_var + "} <http://www.w3.org/2000/01/rdf-schema#subPropertyOf> {" + Y_var + "}";
  try {
      TupleQueryResult sss = c.prepareTupleQuery(QueryLanguage.SERQL, queryString).evaluate();
      processSubsumptionStatements(sss);
      sss.close();
      processHierarchy(propertyHierarchy);
  }
  catch (MalformedQueryException ex){
      System.err.println("Error: malformed SeRQL query \n" + queryString);
      if (DEBUG){ex.printStackTrace();}
View Full Code Here

      try {
        Resource context;
        TupleQuery query = con.prepareTupleQuery(QueryLanguage.SERQL, REPOSITORY_CONTEXT_QUERY);
        query.setBinding("ID", vf.createLiteral(id));
        TupleQueryResult queryResult = query.evaluate();

        try {
          if (!queryResult.hasNext()) {
            writeError("Unable to find context information for repository '" + id + "'");
            logger.warn("Multiple contexts found for repository '{}'", id);
            return;
          }

          BindingSet bindings = queryResult.next();
          context = (Resource)bindings.getValue("C");

          if (queryResult.hasNext()) {
            writeError("Multiple contexts found for repository '" + id + "'");
            logger.error("Multiple contexts found for repository '{}'", id);
            return;
          }
        }
        finally {
          queryResult.close();
        }

        boolean proceed = askProceed("WARNING: you are about to drop repository '" + id + "'.", true);
        if (proceed) {
          if (id.equals(repositoryID)) {
View Full Code Here

  private void showRepositories() {
    try {
      Repository systemRepo = manager.getSystemRepository();
      RepositoryConnection con = systemRepo.getConnection();
      try {
        TupleQueryResult queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, REPOSITORY_LIST_QUERY).evaluate();
        try {
          if (!queryResult.hasNext()) {
            writeln("--no repositories found--");
          }
          else {
            writeln("+----------");
            while (queryResult.hasNext()) {
              BindingSet bindings = queryResult.next();
              String id = bindings.getValue("ID").stringValue();
              String title = bindings.getValue("Title").stringValue();

              write("|" + id);
              if (title != null) {
                write(" (\"" + title + "\")");
              }
              writeln();
            }
            writeln("+----------");
          }
        }
        finally {
          queryResult.close();
        }
      }
      catch (MalformedQueryException e) {
        writeError("Internal error: malformed preconfigured query");
        logger.error("Failed to show repository", e);
View Full Code Here

      writeln("Evaluating query...");
      long startTime = System.currentTimeMillis();

      Collection<Namespace> namespaces = con.getNamespaces().addTo(new ArrayList<Namespace>());

      TupleQueryResult tupleQueryResult = con.prepareTupleQuery(ql, queryString).evaluate();

      try {
        int resultCount = 0;
        List<String> bindingNames = tupleQueryResult.getBindingNames();

        if (bindingNames.isEmpty()) {
          while (tupleQueryResult.hasNext()) {
            tupleQueryResult.next();
            resultCount++;
          }
        }
        else {
          int columnWidth = (consoleWidth - 1) / bindingNames.size() - 3;

          // Build table header
          StringBuilder sb = new StringBuilder(consoleWidth);
          for (String bindingName : bindingNames) {
            sb.append("| ").append(bindingName);
            StringUtil.appendN(' ', columnWidth - bindingName.length(), sb);
          }
          sb.append("|");
          String header = sb.toString();

          // Build separator line
          sb.setLength(0);
          for (int i = bindingNames.size(); i > 0; i--) {
            sb.append('+');
            StringUtil.appendN('-', columnWidth + 1, sb);
          }
          sb.append('+');
          String separatorLine = sb.toString();

          // Write table header
          writeln(separatorLine);
          writeln(header);
          writeln(separatorLine);

          // Write table rows

          while (tupleQueryResult.hasNext()) {
            BindingSet bindingSet = tupleQueryResult.next();
            resultCount++;

            sb.setLength(0);
            for (String bindingName : bindingNames) {
              Value value = bindingSet.getValue(bindingName);
              String valueStr = getStringRepForValue(value, namespaces);

              sb.append("| ").append(valueStr);
              StringUtil.appendN(' ', columnWidth - valueStr.length(), sb);
            }
            sb.append("|");
            writeln(sb.toString());
          }

          writeln(separatorLine);
        }

        long endTime = System.currentTimeMillis();
        writeln(resultCount + " result(s) (" + (endTime - startTime) + " ms)");
      }
      finally {
        tupleQueryResult.close();
      }
    }
    finally {
      con.close();
    }
View Full Code Here

    queryBuilder.append(" SELECT name, mbox");
    queryBuilder.append(" FROM {} foaf:name {name};");
    queryBuilder.append("         foaf:mbox {mbox}");
    queryBuilder.append(" USING NAMESPACE foaf = <" + FOAF_NS + ">");

    TupleQueryResult result = testCon.prepareTupleQuery(QueryLanguage.SERQL, queryBuilder.toString()).evaluate();

    try {
      assertTrue(result != null);
      assertTrue(result.hasNext());

      while (result.hasNext()) {
        BindingSet solution = result.next();
        assertTrue(solution.hasBinding("name"));
        assertTrue(solution.hasBinding("mbox"));

        Value nameResult = solution.getValue("name");
        Value mboxResult = solution.getValue("mbox");

        assertTrue((nameAlice.equals(nameResult) || nameBob.equals(nameResult)));
        assertTrue((mboxAlice.equals(mboxResult) || mboxBob.equals(mboxResult)));
      }
    }
    finally {
      result.close();
    }
  }
View Full Code Here

TOP

Related Classes of org.openrdf.query.TupleQueryResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.