Search in sources :

Example 6 with ImmutableGraph

use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.

the class ScopeImpl method exportToGraph.

/**
     * Get a Clerezza {@link Graph} representation of the scope.
     * 
     * @param merge
     *            if true the core and custom spaces will be recursively merged with the scope graph,
     *            otherwise owl:imports statements will be added.
     * @return the RDF representation of the scope as a modifiable graph.
     */
protected Graph exportToGraph(boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // No need to store, give it a name, or anything.
    Graph root = new SimpleGraph();
    IRI iri = new IRI(universalPrefix + getID());
    if (root != null) {
        // Set the ontology ID
        root.add(new TripleImpl(iri, RDF.type, OWL.Ontology));
        if (merge) {
            ImmutableGraph custom, core;
            // Get the subjects of "bad" triples (those with subjects of type owl:Ontology).
            Iterator<Triple> it;
            Set<BlankNodeOrIRI> ontologies = new HashSet<BlankNodeOrIRI>();
            Set<RDFTerm> importTargets = new HashSet<RDFTerm>();
            custom = this.getCustomSpace().export(ImmutableGraph.class, merge);
            // root.addAll(space);
            it = custom.filter(null, RDF.type, OWL.Ontology);
            while (it.hasNext()) ontologies.add(it.next().getSubject());
            it = custom.filter(null, OWL.imports, null);
            while (it.hasNext()) importTargets.add(it.next().getObject());
            core = this.getCoreSpace().export(ImmutableGraph.class, merge);
            // root.addAll(space);
            it = core.filter(null, RDF.type, OWL.Ontology);
            while (it.hasNext()) ontologies.add(it.next().getSubject());
            it = core.filter(null, OWL.imports, null);
            while (it.hasNext()) importTargets.add(it.next().getObject());
            // Make sure the scope itself is not in the "bad" subjects.
            ontologies.remove(iri);
            for (BlankNodeOrIRI nl : ontologies) log.debug("{} -related triples will not be added to {}", nl, iri);
            // Merge the two spaces, skipping the "bad" triples.
            log.debug("Merging custom space of {}.", getID());
            for (Triple t : custom) if (!ontologies.contains(t.getSubject()))
                root.add(t);
            log.debug("Merging core space of {}.", getID());
            for (Triple t : core) if (!ontologies.contains(t.getSubject()))
                root.add(t);
            /*
                 * Reinstate import statements, though. If imported ontologies were not merged earlier, we are
                 * not doing it now anyway.
                 */
            for (RDFTerm target : importTargets) root.add(new TripleImpl(iri, OWL.imports, target));
        } else {
            IRI physIRI = new IRI(universalPrefix.toString() + this.getID() + "/" + SpaceType.CUSTOM.getIRISuffix());
            root.add(new TripleImpl(iri, OWL.imports, physIRI));
            physIRI = new IRI(universalPrefix.toString() + this.getID() + "/" + SpaceType.CORE.getIRISuffix());
            root.add(new TripleImpl(iri, OWL.imports, physIRI));
        }
    }
    return root;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) HashSet(java.util.HashSet)

Example 7 with ImmutableGraph

use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.

the class ZemantaAPIWrapper method parseResponse.

private ImmutableGraph parseResponse(InputStream is) {
    JenaParserProvider jenaParserProvider = new JenaParserProvider();
    //NOTE(rw): the new third parameter is the base URI used to resolve relative paths
    Graph g = new SimpleGraph();
    jenaParserProvider.parse(g, is, SupportedFormat.RDF_XML, null);
    log.debug("graph: " + g.toString());
    return g.getImmutableGraph();
}
Also used : JenaParserProvider(org.apache.clerezza.rdf.jena.parser.JenaParserProvider) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)

Example 8 with ImmutableGraph

use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.

the class ZemantaAPIWrapper method enhance.

public ImmutableGraph enhance(String textToAnalyze) throws IOException {
    InputStream is = sendRequest(textToAnalyze);
    ImmutableGraph zemantaResponseGraph = parseResponse(is);
    return zemantaResponseGraph;
}
Also used : InputStream(java.io.InputStream) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 9 with ImmutableGraph

use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.

the class TopicEngineTest method testImportModelFromSKOS.

@Test
public void testImportModelFromSKOS() throws Exception {
    log.info(" --- testImportModelFromSKOS --- ");
    Parser parser = Parser.getInstance();
    parser.bindParsingProvider(new JenaParserProvider());
    ImmutableGraph graph = parser.parse(getClass().getResourceAsStream("/sample-scheme.skos.rdf.xml"), SupportedFormat.RDF_XML);
    int imported = classifier.importConceptsFromGraph(graph, OntologicalClasses.SKOS_CONCEPT, Properties.SKOS_BROADER);
    assertEquals(imported, 4);
    assertEquals(0, classifier.getBroaderConcepts("http://example.com/ns#someconceptscheme/100").size());
    assertEquals(0, classifier.getBroaderConcepts("http://example.com/ns#someconceptscheme/200").size());
    assertEquals(1, classifier.getBroaderConcepts("http://example.com/ns#someconceptscheme/010").size());
    assertEquals(1, classifier.getBroaderConcepts("http://example.com/ns#someconceptscheme/020").size());
    assertEquals(2, classifier.getRootConcepts().size());
}
Also used : JenaParserProvider(org.apache.clerezza.rdf.jena.parser.JenaParserProvider) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Parser(org.apache.clerezza.rdf.core.serializedform.Parser) Test(org.junit.Test)

Example 10 with ImmutableGraph

use of org.apache.clerezza.commons.rdf.ImmutableGraph in project stanbol by apache.

the class TestAxiomInterpretation method testCustomAboxCoreTbox.

@Test
public void testCustomAboxCoreTbox() throws Exception {
    String path = "/ontologies/imports-disconnected";
    InputStream content = getClass().getResourceAsStream(path + "/abox.owl");
    OntologyInputSource<?> coreSrc = new GraphContentInputSource(content, SupportedFormat.TURTLE);
    Scope scope = onManager.createOntologyScope("imports-disconnected", coreSrc);
    assertNotNull(scope);
    content = getClass().getResourceAsStream(path + "/tbox.owl");
    OntologyInputSource<?> custSrc = new GraphContentInputSource(content, SupportedFormat.TURTLE);
    scope.getCustomSpace().addOntology(custSrc);
    ImmutableGraph g = scope.export(ImmutableGraph.class, true);
// for (Triple t : g)
// System.out.println(t);
//
// OWLOntology o = scope.export(OWLOntology.class, true);
// for (OWLAxiom ax : o.getAxioms())
// System.out.println(ax);
}
Also used : Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) InputStream(java.io.InputStream) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Test(org.junit.Test)

Aggregations

ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)25 Graph (org.apache.clerezza.commons.rdf.Graph)9 IRI (org.semanticweb.owlapi.model.IRI)8 GET (javax.ws.rs.GET)7 Produces (javax.ws.rs.Produces)7 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)7 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)7 HashSet (java.util.HashSet)6 Path (javax.ws.rs.Path)5 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)5 Triple (org.apache.clerezza.commons.rdf.Triple)5 IRI (org.apache.clerezza.commons.rdf.IRI)4 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)4 Test (org.junit.Test)4 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)4 InputStream (java.io.InputStream)3 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)3 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)3 Parser (org.apache.clerezza.rdf.core.serializedform.Parser)3 OntologySpace (org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace)3