Esempio n. 1
0
  /**
   * Selects a triple by providing an RDFStatement object collection
   *
   * @param stmts
   * @return Query result
   */
  public List<RDFStatement> selectTriples(Collection<? extends RDFStatement> stmts) {

    if (stmts == null || stmts.isEmpty()) {
      return null;
    }

    RDFStatement rdfs = stmts.stream().findFirst().get();
    String si = rdfs.getSubject(),
        pi = rdfs.getPredicate(),
        oi = rdfs.getObject(); // doesnt get value

    StringBuilder sb = new StringBuilder();
    stmts
        .stream()
        .forEach(
            s -> {
              s.setObject(RDFUtils.escapeString(s.getObject()));
              sb.append(s.getSubject())
                  .append(" ")
                  .append(s.getPredicate())
                  .append(" ")
                  .append(s.getObject())
                  .append(" .\n");
            });
    String query =
        String.format(
            this.defaultPrefices + "SELECT * FROM <%s> WHERE { %s }",
            this.graphName,
            sb.toString());
    Query sparqlQuery = QueryFactory.create(query);
    VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparqlQuery, this.graph);
    List<RDFStatement> stmtsList = new ArrayList<>();
    ResultSet rs = vqe.execSelect();
    while (rs.hasNext()) {
      QuerySolution qs = rs.nextSolution();
      RDFNode s = qs.get(si);
      RDFNode p = qs.get(pi);
      RDFNode o = qs.get(oi);
      RDFStatement stmt =
          new RDFStatement(
              s != null ? s.toString() : "null",
              p != null ? p.toString() : "null",
              o != null ? RDFUtils.escapeString(o.toString()) : "null");
      stmtsList.add(stmt);
      logger.info("fetched: {}", stmt.toString());
    }
    return stmtsList;
  }