コード例 #1
0
ファイル: QueryHandler.java プロジェクト: nvurgaft/Wiki2MARC
  /**
   * Returns a collection of RDFStatements that match the described subject
   *
   * @param subject Subject
   * @return collection of RDFStatements
   */
  public Collection<RDFStatement> getStatements(String subject) {

    if (subject == null || subject.isEmpty()) {
      return null;
    }
    List<RDFStatement> statement = null;

    try {
      // define a describe query
      String query = String.format("DESCRIBE %s FROM < %s >", subject, this.graphName);

      logger.debug("Query: \n{}", query);
      Query sparqlQuery = QueryFactory.create(query);
      VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(sparqlQuery, this.graph);
      // execute the query and get the graph
      Model model = vqe.execDescribe();
      Graph queriedGraph = model.getGraph();
      // itreate over the retrieved triples, and place them inside a list
      ExtendedIterator<Triple> iter = queriedGraph.find(Node.ANY, Node.ANY, Node.ANY);
      statement = new ArrayList<>();
      while (iter.hasNext()) {
        Triple t = (Triple) iter.next();
        RDFStatement stmt =
            new RDFStatement(
                t.getSubject().toString(), t.getPredicate().toString(), t.getObject().toString());
        statement.add(stmt);
      }
    } catch (Exception ex) {
      logger.error("Exception occured while querying for statements", ex);
    }
    return statement;
  }
コード例 #2
0
 public ResultSet executeQuery(String query, VirtDataset virtDataset) {
   VirtGraph vg =
       new VirtGraph(
           virtDataset.getGraphUrl(), virtDataset.getGraphUser(), virtDataset.getGraphPassword());
   VirtuosoQueryExecution vur = VirtuosoQueryExecutionFactory.create(query, vg);
   ResultSet rs = vur.execSelect();
   return rs;
 }
コード例 #3
0
ファイル: QueryHandler.java プロジェクト: nvurgaft/Wiki2MARC
  /**
   * 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;
  }
コード例 #4
0
 public QueryExecution sparqlService(String service, Query query, String defaultGraph) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.sparqlService(
       service, query, service);
 }
コード例 #5
0
 public QueryExecution sparqlService(String service, Query query) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.sparqlService(service, query);
 }
コード例 #6
0
 public QueryExecution create(String queryStr, Dataset dataset, QuerySolution initialBinding) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.create(
       queryStr, dataset, initialBinding);
 }
コード例 #7
0
 public QueryExecution create(Query query, QuerySolution initialBinding) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.create(query, initialBinding);
 }
コード例 #8
0
 public QueryExecution create(String queryStr, Model model) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.create(queryStr, model);
 }
コード例 #9
0
 public QueryExecution create(String queryStr, FileManager fm) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.create(queryStr, fm);
 }
コード例 #10
0
 public QueryExecution create(String queryStr, Dataset dataset) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.create(queryStr, dataset);
 }
コード例 #11
0
 public VirtuosoQueryExecution create(String query, VirtGraph graph) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.create(query, graph);
 }
コード例 #12
0
 public QueryExecution sparqlService(
     String service, Query query, List defaultGraphURIs, List namedGraphURIs) {
   return virtuoso.jena.driver.VirtuosoQueryExecutionFactory.sparqlService(
       service, query, defaultGraphURIs, namedGraphURIs);
 }