예제 #1
0
  private void upload(URI graphURI, Resource context) throws Exception {
    RepositoryConnection con = dataRep.getConnection();
    con.setAutoCommit(false);
    try {
      RDFFormat rdfFormat = Rio.getParserFormatForFileName(graphURI.toString(), RDFFormat.TURTLE);
      RDFParser rdfParser = Rio.createParser(rdfFormat, dataRep.getValueFactory());
      rdfParser.setVerifyData(false);
      rdfParser.setDatatypeHandling(DatatypeHandling.IGNORE);
      // rdfParser.setPreserveBNodeIDs(true);

      RDFInserter rdfInserter = new RDFInserter(con);
      rdfInserter.enforceContext(context);
      rdfParser.setRDFHandler(rdfInserter);

      URL graphURL = new URL(graphURI.toString());
      InputStream in = graphURL.openStream();
      try {
        rdfParser.parse(in, graphURI.toString());
      } finally {
        in.close();
      }

      con.setAutoCommit(true);
    } finally {
      con.close();
    }
  }
예제 #2
0
 private static String getSuffix(URI plainURI, URI baseURI) {
   if (baseURI == null) return null;
   String b = baseURI.toString();
   String p = plainURI.toString();
   if (p.equals(b)) {
     return null;
   } else if (p.startsWith(b)) {
     return p.substring(b.length());
   } else {
     return null;
   }
 }
예제 #3
0
  @Test
  public void testSize() throws Exception {
    assertEquals("Size of empty repository should be 0", 0, con.size());

    // Add some data to the repository
    con.begin();
    con.addStatement(painter, RDF.TYPE, RDFS.CLASS);
    con.addStatement(painting, RDF.TYPE, RDFS.CLASS);
    con.addStatement(picasso, RDF.TYPE, painter, context1);
    con.addStatement(guernica, RDF.TYPE, painting, context1);
    con.addStatement(picasso, paints, guernica, context1);
    con.commit();

    assertEquals("Size of repository should be 5", 5, con.size());
    assertEquals("Size of named context should be 3", 3, con.size(context1));

    URI unknownContext = new URIImpl(EXAMPLE_NS + "unknown");

    assertEquals("Size of unknown context should be 0", 0, con.size(unknownContext));

    URIImpl uriImplContext1 = new URIImpl(context1.toString());

    assertEquals(
        "Size of named context (defined as URIImpl) should be 3", 3, con.size(uriImplContext1));
  }
예제 #4
0
 private static String expandBaseURI(URI baseURI) {
   String s = baseURI.toString();
   if (s.matches(".*[A-Za-z0-9\\-_]")) {
     s += ".";
   }
   return s;
 }
예제 #5
0
 public static String normalize(URI uri, String hash) {
   String s = uri.toString();
   if (s.indexOf('\n') > -1 || s.indexOf('\t') > -1) {
     throw new RuntimeException("Newline or tab character in URI: " + s);
   }
   if (hash == null) return s;
   return s.replace(hash, " ");
 }
예제 #6
0
 // TODO: MimeType detector to null forces the execution of all extractors, but extraction
 //       tests should be based on mimetype detection.
 protected void extract(String resource) throws ExtractionException, IOException {
   SingleDocumentExtraction ex =
       new SingleDocumentExtraction(
           new HTMLFixture(copyResourceToTempFile(resource)).getOpener(baseURI.toString()),
           getExtractorFactory(),
           new RepositoryWriter(conn));
   ex.setMIMETypeDetector(null);
   report = ex.run();
 }
예제 #7
0
 public static URI getHashURI(
     Resource resource, URI baseURI, String hash, Map<String, Integer> blankNodeMap) {
   if (resource == null) {
     return null;
   } else if (resource instanceof URI) {
     URI plainURI = (URI) resource;
     if (plainURI.toString().matches(".*(\\n|\\t).*")) {
       throw new RuntimeException("Newline or tab character in URI: " + plainURI.toString());
     }
     String suffix = getSuffix(plainURI, baseURI);
     if (suffix == null && !plainURI.equals(baseURI)) {
       return plainURI;
     }
     return new URIImpl(getHashURIString(baseURI, hash, suffix));
   } else {
     BNode blankNode = (BNode) resource;
     int n = getBlankNodeNumber(blankNode, blankNodeMap);
     return new URIImpl(expandBaseURI(baseURI) + hash + ".." + n);
   }
 }
예제 #8
0
 @Override
 protected void writeURI(org.openrdf.model.URI uri) throws IOException {
   URI create = URI.create(uri.toString());
   URI rel = baseURI.relativize(create);
   if (!rel.isAbsolute()) {
     writer.write("<");
     writer.write(TurtleUtil.encodeURIString(rel.toString()));
     writer.write(">");
   } else {
     super.writeURI(uri);
   }
 }
예제 #9
0
 public String uriToNative(final URI value) {
   return GraphSail.URI_PREFIX + GraphSail.SEPARATOR + value.toString();
 }
예제 #10
0
  public static TestSuite suite(String manifestFileURL, Factory factory, boolean approvedOnly)
      throws Exception {
    logger.info("Building test suite for {}", manifestFileURL);

    TestSuite suite = new TestSuite(factory.getClass().getName());

    // Read manifest and create declared test cases
    Repository manifestRep = new SailRepository(new MemoryStore());
    manifestRep.initialize();
    RepositoryConnection con = manifestRep.getConnection();

    ManifestTest.addTurtle(con, new URL(manifestFileURL), manifestFileURL);

    suite.setName(getManifestName(manifestRep, con, manifestFileURL));

    // Extract test case information from the manifest file. Note that we
    // only
    // select those test cases that are mentioned in the list.
    StringBuilder query = new StringBuilder(512);
    query.append(
        " SELECT DISTINCT testURI, testName, resultFile, action, queryFile, defaultGraph, ordered ");
    query.append(" FROM {} rdf:first {testURI} ");
    if (approvedOnly) {
      query.append("                          dawgt:approval {dawgt:Approved}; ");
    }
    query.append("                             mf:name {testName}; ");
    query.append("                             mf:result {resultFile}; ");
    query.append("                             [ mf:checkOrder {ordered} ]; ");
    query.append("                             [ mf:requires {Requirement} ];");
    query.append("                             mf:action {action} qt:query {queryFile}; ");
    query.append("                                               [qt:data {defaultGraph}]; ");
    query.append("                                               [sd:entailmentRegime {Regime} ]");

    // skip tests involving CSV result files, these are not query tests
    query.append(" WHERE NOT resultFile LIKE \"*.csv\" ");
    // skip tests involving JSON, sesame currently does not have a
    // SPARQL/JSON
    // parser.
    query.append(" AND NOT resultFile LIKE \"*.srj\" ");
    // skip tests involving entailment regimes
    query.append(" AND NOT BOUND(Regime) ");
    // skip test involving basic federation, these are tested separately.
    query.append(" AND (NOT BOUND(Requirement) OR (Requirement != mf:BasicFederation)) ");
    query.append(" USING NAMESPACE ");
    query.append("  mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>, ");
    query.append("  dawgt = <http://www.w3.org/2001/sw/DataAccess/tests/test-dawg#>, ");
    query.append("  qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>, ");
    query.append("  sd = <http://www.w3.org/ns/sparql-service-description#>, ");
    query.append("  ent = <http://www.w3.org/ns/entailment/> ");
    TupleQuery testCaseQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString());

    query.setLength(0);
    query.append(" SELECT graph ");
    query.append(" FROM {action} qt:graphData {graph} ");
    query.append(" USING NAMESPACE ");
    query.append(" qt = <http://www.w3.org/2001/sw/DataAccess/tests/test-query#>");
    TupleQuery namedGraphsQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString());

    query.setLength(0);
    query.append("SELECT 1 ");
    query.append(" FROM {testURI} mf:resultCardinality {mf:LaxCardinality}");
    query.append(
        " USING NAMESPACE mf = <http://www.w3.org/2001/sw/DataAccess/tests/test-manifest#>");
    TupleQuery laxCardinalityQuery = con.prepareTupleQuery(QueryLanguage.SERQL, query.toString());

    logger.debug("evaluating query..");
    TupleQueryResult testCases = testCaseQuery.evaluate();
    while (testCases.hasNext()) {
      BindingSet bindingSet = testCases.next();

      URI testURI = (URI) bindingSet.getValue("testURI");
      String testName = bindingSet.getValue("testName").toString();
      String resultFile = bindingSet.getValue("resultFile").toString();
      String queryFile = bindingSet.getValue("queryFile").toString();
      URI defaultGraphURI = (URI) bindingSet.getValue("defaultGraph");
      Value action = bindingSet.getValue("action");
      Value ordered = bindingSet.getValue("ordered");

      logger.debug("found test case : {}", testName);

      // Query named graphs
      namedGraphsQuery.setBinding("action", action);
      TupleQueryResult namedGraphs = namedGraphsQuery.evaluate();

      DatasetImpl dataset = null;

      if (defaultGraphURI != null || namedGraphs.hasNext()) {
        dataset = new DatasetImpl();

        if (defaultGraphURI != null) {
          dataset.addDefaultGraph(defaultGraphURI);
        }

        while (namedGraphs.hasNext()) {
          BindingSet graphBindings = namedGraphs.next();
          URI namedGraphURI = (URI) graphBindings.getValue("graph");
          logger.debug(" adding named graph : {}", namedGraphURI);
          dataset.addNamedGraph(namedGraphURI);
        }
      }

      // Check for lax-cardinality conditions
      boolean laxCardinality = false;
      laxCardinalityQuery.setBinding("testURI", testURI);
      TupleQueryResult laxCardinalityResult = laxCardinalityQuery.evaluate();
      try {
        laxCardinality = laxCardinalityResult.hasNext();
      } finally {
        laxCardinalityResult.close();
      }

      // if this is enabled, Sesame passes all tests, showing that the
      // only
      // difference is the semantics of arbitrary-length
      // paths
      /*
       * if (!laxCardinality) { // property-path tests always with lax
       * cardinality because Sesame filters out duplicates by design if
       * (testURI.stringValue().contains("property-path")) {
       * laxCardinality = true; } }
       */

      // check if we should test for query result ordering
      boolean checkOrder = false;
      if (ordered != null) {
        checkOrder = Boolean.parseBoolean(ordered.stringValue());
      }

      SPARQLQueryTest test =
          factory.createSPARQLQueryTest(
              testURI.toString(),
              testName,
              queryFile,
              resultFile,
              dataset,
              laxCardinality,
              checkOrder);
      if (test != null) {
        suite.addTest(test);
      }
    }

    testCases.close();
    con.close();

    manifestRep.shutDown();
    logger.info("Created test suite with " + suite.countTestCases() + " test cases.");
    return suite;
  }