コード例 #1
0
  /*
   * Serialize Graph to turtle format and deserialize.
   */
  @Test
  public void testTurtleSerializer() {
    initializeGraph();
    SerializingProvider provider = new JenaSerializerProvider();

    ByteArrayOutputStream serializedGraph = new ByteArrayOutputStream();
    provider.serialize(serializedGraph, mGraph.getGraph(), "text/turtle");
    InputStream in = new ByteArrayInputStream(serializedGraph.toByteArray());

    Graph deserializedGraph = parse(in, "TURTLE");
    // due to http://issues.trialox.org/jira/browse/RDF-6 we cannot just
    // check
    // that the two graphs are equals
    Assert.assertEquals(deserializedGraph.size(), mGraph.getGraph().size());
    Assert.assertEquals(deserializedGraph.hashCode(), mGraph.getGraph().hashCode());
    // isomorphism delegated to jena
    JenaGraph jenaGraphFromNTriples = new JenaGraph(deserializedGraph);
    JenaGraph jenaGraphFromTurtle = new JenaGraph(mGraph.getGraph());
    Assert.assertTrue(jenaGraphFromNTriples.isIsomorphicWith(jenaGraphFromTurtle));
  }
コード例 #2
0
  /**
   * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
   * afterwards.
   *
   * @param merge
   * @return
   */
  protected MGraph exportToMGraph(boolean merge, IRI prefix) {
    // if (merge) throw new UnsupportedOperationException(
    // "Merge not implemented yet for Clerezza triple collections.");

    long before = System.currentTimeMillis();

    // No need to store, give it a name, or anything.
    MGraph root = new SimpleMGraph();
    UriRef iri = new UriRef(prefix + _id);
    // Add the import declarations for directly managed ontologies.
    if (root != null) {
      // Set the ontology ID
      root.add(new TripleImpl(iri, RDF.type, OWL.Ontology));

      if (merge) {
        log.warn(
            "Merging of Clerezza triple collections is only implemented one level down. Import statements will be preserved for further levels.");
        Iterator<Triple> it;
        Set<Resource> importTargets = new HashSet<Resource>();
        for (OWLOntologyID ontologyId : managedOntologies) {
          Graph g = getOntology(ontologyId, Graph.class, false);
          root.addAll(g);

          it = g.filter(null, OWL.imports, null);
          while (it.hasNext()) {
            IRI tgt;
            Resource r = it.next().getObject();
            try {
              if (r instanceof UriRef) tgt = IRI.create(((UriRef) r).getUnicodeString());
              else if (r instanceof Literal) tgt = IRI.create(((Literal) r).getLexicalForm());
              else tgt = IRI.create(r.toString());
              tgt = URIUtils.sanitize(tgt);
              importTargets.add(new UriRef(tgt.toString()));
            } catch (Exception ex) {
              log.error("FAILED to obtain import target from resource {}", r);
              continue;
            }
          }

          it = g.filter(null, RDF.type, OWL.Ontology);
          while (it.hasNext()) {
            NonLiteral ontology = it.next().getSubject();
            log.debug("Removing all triples related to {} from {}", ontology, iri);
            Iterator<Triple> it2 = g.filter(ontology, null, null);
            while (it2.hasNext()) root.remove(it2.next());
          }

          /*
           * Reinstate import statements, though. If imported ontologies were not merged earlier, we
           * are not doing it now anyway.
           */
          for (Resource target : importTargets) root.add(new TripleImpl(iri, OWL.imports, target));
        }

      } else {

        String base = prefix + getID();
        for (int i = 0; i < backwardPathLength; i++)
          base = URIUtils.upOne(URI.create(base)).toString();
        base += "/";

        // The key set of managedOntologies contains the ontology IRIs, not their storage keys.
        for (OWLOntologyID ontologyId : managedOntologies) {
          IRI physIRI =
              // ontologyId.getVersionIRI() == null ? URIUtils.sanitize(IRI
              // .create(base + ontologyId.getOntologyIRI())) : URIUtils.sanitize(IRI
              // .create(base + ontologyId.getVersionIRI()));
              IRI.create(base + OntologyUtils.encode(ontologyId));
          root.add(new TripleImpl(iri, OWL.imports, new UriRef(physIRI.toString())));
        }
      }

      log.debug(
          "Clerezza export of {} completed in {} ms.",
          getID(),
          System.currentTimeMillis() - before);
    }

    return root;
  }