private Map<String, Integer> getStats(String sparqlQuery, QueryExecutionFactory qef) {
    Map<String, Integer> stats = new HashMap<>();

    QueryExecution qe = null;
    try {
      qe = qef.createQueryExecution(sparqlQuery);
      ResultSet results = qe.execSelect();

      while (results.hasNext()) {
        QuerySolution qs = results.next();

        String s = qs.get("stats").toString();
        int c = 0;
        if (qs.contains("count")) {
          c = qs.get("count").asLiteral().getInt();
        }
        stats.put(s, c);
      }
    } finally {
      if (qe != null) {
        qe.close();
      }
    }

    return stats;
  }
示例#2
0
  @Test
  public void testNextWithLiteral() throws Exception {
    when(mockValue.getString()).thenReturn("x");
    final QuerySolution solution = testObj.next();

    assertTrue(solution.contains("a"));
    assertEquals("x", solution.get("a").asLiteral().getLexicalForm());
    assertEquals(solution.get("a"), solution.getLiteral("a"));
  }
示例#3
0
  @Test
  public void testNextWithResource() throws Exception {
    when(mockValue.getType()).thenReturn(PropertyType.PATH);
    when(mockValue.getString()).thenReturn("/x");
    when(mockGraphSubjects.getSubject("/x")).thenReturn(ResourceFactory.createResource("info:x"));
    final QuerySolution solution = testObj.next();

    assertTrue(solution.contains("a"));
    assertEquals("info:x", solution.get("a").asResource().getURI());
    assertEquals(solution.get("a"), solution.getResource("a"));
  }
  private void _query() throws UnregisteredInstanceException {
    ResultSet rs;
    rs = _source.sparqlSelect(Queries.labelQuery(_uri, _source));
    if (rs.hasNext()) {
      QuerySolution qs = rs.next();
      if (qs.contains("o")) _label = Utils.parseLiteral(qs.get("o").toString());
    }

    rs = _source.sparqlSelect(Queries.commentQuery(_uri, _source));
    if (rs.hasNext()) {
      QuerySolution qs = rs.next();
      if (qs.contains("o")) _comment = Utils.parseLiteral(qs.get("o").toString());
    }

    // info query
    rs = _source.sparqlSelect(Queries.interfaceInfoQuery(_uri, _source));
    if (!rs.hasNext())
      throw new UnregisteredInstanceException(
          "QueryInterface (" + _uri + ") not found in source: " + _source.toString());
    else {
      QuerySolution qs = rs.next();
      if (qs.contains("output")) _output = qs.get("output").toString();
      if (qs.contains("input")) _input = qs.get("input").toString();
      if (qs.contains("limit"))
        _limit = Integer.parseInt(Utils.parseLiteral(qs.get("limit").toString()));
    }

    // types query
    rs = _source.sparqlSelect(Queries.typeQuery(_uri, _source));
    if (!rs.hasNext())
      throw new UnregisteredInstanceException(
          "QueryInterface (" + _uri + ") not found in source: " + _source.toString());
    while (rs.hasNext()) {
      QuerySolution qs = rs.next();
      if (qs.contains("o")) _types.add(qs.get("o").toString());
    }
  }