示例#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;
  }
示例#2
0
  /**
   * Batch insert statements into the graph
   *
   * @param statements
   * @return true if and only if the query execution was successful
   */
  public boolean batchInsertStatements(Collection<? extends RDFStatement> statements) {
    if (statements == null || statements.isEmpty()) {
      return false;
    }

    try {
      StringBuilder sb = new StringBuilder();
      statements
          .stream()
          .forEach(
              s -> {
                s.setObject(RDFUtils.escapeString(s.getObject()));
                sb.append("INSERT INTO GRAPH <")
                    .append(this.graphName)
                    .append("> { ")
                    .append(s.toString())
                    .append(" };\n");
              });

      VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(sb.toString(), this.graph);
      vur.exec();
      return true;
    } catch (Exception ex) {
      logger.error("Exception while batch inserting statements", ex);
    }
    return false;
  }
示例#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;
 }
示例#4
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;
  }