Beispiel #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;
  }
Beispiel #2
0
  /**
   * Inserts a statement into the graph
   *
   * @param stmt Statement (RDFStatement triple)
   * @return true if and only if the query execution was successful
   */
  public boolean insertStatement(RDFStatement stmt) {
    if (stmt == null) {
      return false;
    }

    try {
      stmt.setObject(RDFUtils.escapeString(stmt.getObject()));
      String query = "INSERT INTO GRAPH <" + this.graphName + "> { " + stmt.toString() + "}";
      VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(query, this.graph);
      vur.exec();
      return true;
    } catch (Exception ex) {
      logger.error("Exception while inserting a statement", ex);
    }
    return false;
  }
Beispiel #3
0
 /**
  * Delete a statement from a graph
  *
  * @param stmt (RDFStatement triple)
  * @return true if and only if the query execution was successful
  */
 public boolean deleteStatement(RDFStatement stmt) {
   if (stmt == null) {
     return false;
   }
   try {
     stmt.setObject(RDFUtils.escapeString(stmt.getObject()));
     String query =
         String.format(
             "DELETE FROM GRAPH <%s> { %s }",
             this.graphName, RDFUtils.escapeString(stmt.toString()));
     VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(query, this.graph);
     vur.exec();
     return true;
   } catch (Exception ex) {
     logger.error("Exception while deleting a statement", ex);
   }
   return false;
 }