Example #1
0
  /**
   * LDP-Style to serialize a resource.
   *
   * @param writer the writer to serialize to
   * @param subject the resource to serialize
   * @param iteration the Iteration containing the data
   * @throws RDFHandlerException
   * @throws RepositoryException
   */
  public static void exportIteration(
      RDFWriter writer, URI subject, CloseableIteration<Statement, RepositoryException> iteration)
      throws RDFHandlerException, RepositoryException {
    writer.startRDF();

    writer.handleNamespace(LDP.PREFIX, LDP.NAMESPACE);
    writer.handleNamespace(RDF.PREFIX, RDF.NAMESPACE);
    writer.handleNamespace(XSD.PREFIX, XSD.NAMESPACE);
    writer.handleNamespace(DCTERMS.PREFIX, DCTERMS.NAMESPACE);

    writer.handleNamespace("parent", subject.getNamespace());
    writer.handleNamespace("child", subject.stringValue().replaceFirst("/*$", "/"));
    writer.handleNamespace("this", subject.stringValue().replaceFirst("/*$", "#"));

    while (iteration.hasNext()) {
      writer.handleStatement(iteration.next());
    }

    writer.endRDF();
  }
 public static void writeNanopub(Nanopub nanopub, OutputStream out, RDFFormat format)
     throws RDFHandlerException {
   RDFWriter writer = Rio.createWriter(format, out);
   writer.startRDF();
   String s = nanopub.getUri().toString();
   writer.handleNamespace("this", s);
   writer.handleNamespace("sub", s + ".");
   writer.handleNamespace("blank", s + "..");
   writer.handleNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
   writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
   writer.handleNamespace("rdfg", "http://www.w3.org/2004/03/trix/rdfg-1/");
   writer.handleNamespace("xsd", "http://www.w3.org/2001/XMLSchema#");
   writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#");
   writer.handleNamespace("dc", "http://purl.org/dc/terms/");
   writer.handleNamespace("pav", "http://swan.mindinformatics.org/ontologies/1.2/pav/");
   writer.handleNamespace("np", "http://www.nanopub.org/nschema#");
   for (Statement st : NanopubUtils.getStatements(nanopub)) {
     writer.handleStatement(st);
   }
   writer.endRDF();
 }
Example #3
0
  public void test_query() throws Exception {

    final BigdataSail sail = getSail();

    try {

      sail.initialize();

      if (!((BigdataSail) sail).database.getStatementIdentifiers()) {

        log.warn("Statement identifiers are not enabled");

        return;
      }

      /*
       * Load data into the sail.
       */
      {
        final DataLoader dataLoader = sail.database.getDataLoader();

        dataLoader.loadData(
            "/com/bigdata/rdf/sail/provenance01.ttlx",
            "" /*baseURL*/,
            ServiceProviderHook.TURTLE_RDR);
      }

      /*
       * Serialize as RDF/XML.
       *
       * Note: This is just for debugging.
       */
      if (log.isInfoEnabled()) {

        final BigdataStatementIterator itr = sail.database.getStatements(null, null, null);
        final String rdfXml;
        try {

          final Writer w = new StringWriter();

          //                final RDFXMLWriter rdfWriter = new RDFXMLWriter(w);
          final RDFWriterFactory writerFactory =
              RDFWriterRegistry.getInstance().get(RDFFormat.RDFXML);

          assertNotNull(writerFactory);

          final RDFWriter rdfWriter = writerFactory.getWriter(w);

          rdfWriter.startRDF();

          while (itr.hasNext()) {

            final BigdataStatementImpl stmt = (BigdataStatementImpl) itr.next();

            // only write the explicit statements.
            if (!stmt.isExplicit()) continue;

            rdfWriter.handleStatement(stmt);
          }

          rdfWriter.endRDF();

          rdfXml = w.toString();

        } finally {

          itr.close();
        }

        // write the rdf/xml
        log.info(rdfXml);
      }

      final SailConnection conn = sail.getConnection();

      try {

        final URI y = new URIImpl("http://www.foo.org/y");

        final URI B = new URIImpl("http://www.foo.org/B");

        final URI dcCreator = new URIImpl("http://purl.org/dc/terms/creator");

        final Literal bryan = new LiteralImpl("bryan");

        final Literal mike = new LiteralImpl("mike");

        /*
         * This is a hand-coded query.
         *
         * Note: When statement identifiers are enabled, the only way to
         * bind the context position is to already have a statement on hand -
         * there is no index which can be used to look up a statement by its
         * context and the context is always a blank node.
         */

        //            final TupleExpr tupleExpr =
        //                new Projection(
        //                new Join(//
        //                    new StatementPattern(//
        //                            new Var("X", y),//
        //                            new Var("1", RDF.TYPE),//
        //                            new Var("2", B),//
        //                            new Var("SID")),// unbound.
        //                    new StatementPattern(//
        //                            new Var("SID"),//
        //                            new Var("3", dcCreator),//
        //                            new Var("Y"))),
        //                new ProjectionElemList(new ProjectionElem[] { new ProjectionElem( "Y"
        // )}));

        //            final String q = "select ?Y where { ?SID <"+dcCreator+"> ?Y . graph ?SID {
        // <"+y+"> <"+RDF.TYPE+"> <"+B+"> . } }";
        final String q =
            "select ?Y where { <<<"
                + y
                + "> <"
                + RDF.TYPE
                + "> <"
                + B
                + ">>> <"
                + dcCreator
                + "> ?Y . }";

        /*
         * Create a data set consisting of the contexts to be queried.
         *
         * Note: a [null] DataSet will cause context to be ignored when the
         * query is processed.
         */
        //            final DatasetImpl dataSet = null; //new DatasetImpl();
        //
        //            final BindingSet bindingSet = new QueryBindingSet();
        //
        //            final CloseableIteration<? extends BindingSet, QueryEvaluationException> itr =
        // conn
        //                    .evaluate(tupleExpr, dataSet, bindingSet, true/* includeInferred */);

        final TupleQuery tq =
            new BigdataSailRepository(sail)
                .getReadOnlyConnection()
                .prepareTupleQuery(QueryLanguage.SPARQL, q);

        final TupleQueryResult itr = tq.evaluate();

        if (log.isInfoEnabled()) log.info("Verifying query.");

        /*
         * These are the expected results for the query (the bindings for Y).
         */

        final Set<Value> expected = new HashSet<Value>();

        expected.add(bryan);

        expected.add(mike);

        /*
         * Verify that the query results is the correct solutions.
         */

        final int nresults = expected.size();

        try {

          int i = 0;

          while (itr.hasNext()) {

            final BindingSet solution = itr.next();

            if (log.isInfoEnabled()) log.info("solution[" + i + "] : " + solution);

            final Value actual = solution.getValue("Y");

            assertTrue("Not expecting Y=" + actual, expected.remove(actual));

            i++;
          }

          assertEquals("#results", nresults, i);

        } finally {

          itr.close();
        }

      } finally {

        conn.close();
      }

    } finally {

      sail.__tearDownUnitTest();
    }
  }