Search in sources :

Example 21 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class OWLAPIToClerezzaConverterTest method setupClass.

@BeforeClass
public static void setupClass() {
    /*
         * Set-up the OWL ontology for the test. Simply add the axioms: AndreaNuzzolese isA Person -> class
         * assertion axiom EnricoDaga isA Person -> class assertion axiom AndreaNuzzolese knows EnricoDaga ->
         * object property assertion axiom
         */
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = manager.getOWLDataFactory();
    try {
        ontology = manager.createOntology(org.semanticweb.owlapi.model.IRI.create(ns + "testOntology"));
    } catch (OWLOntologyCreationException e) {
        log.error(e.getMessage());
    }
    if (ontology != null) {
        OWLClass personClass = factory.getOWLClass(org.semanticweb.owlapi.model.IRI.create(foaf + "Person"));
        OWLNamedIndividual andreaNuzzoleseOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "AndreaNuzzolese"));
        OWLNamedIndividual enricoDagaOWL = factory.getOWLNamedIndividual(org.semanticweb.owlapi.model.IRI.create(ns + "EnricoDaga"));
        OWLObjectProperty knowsOWL = factory.getOWLObjectProperty(org.semanticweb.owlapi.model.IRI.create(foaf + "knows"));
        OWLAxiom axiom = factory.getOWLClassAssertionAxiom(personClass, andreaNuzzoleseOWL);
        manager.addAxiom(ontology, axiom);
        axiom = factory.getOWLClassAssertionAxiom(personClass, enricoDagaOWL);
        manager.addAxiom(ontology, axiom);
        axiom = factory.getOWLObjectPropertyAssertionAxiom(knowsOWL, andreaNuzzoleseOWL, enricoDagaOWL);
        manager.addAxiom(ontology, axiom);
    }
    /*
         * Set-up the Clerezza model for the test. As before simply add the triples: AndreaNuzzolese isA
         * Person EnricoDaga isA Person AndreaNuzzolese knows EnricoDaga
         */
    mGraph = new SimpleGraph();
    IRI knowsInClerezza = new IRI(ns + "knows");
    IRI rdfType = new IRI(RDF.getURI() + "type");
    IRI foafPersonInClerezza = new IRI(foaf + "Person");
    BlankNodeOrIRI andreaNuzzoleseInClerezza = new IRI(ns + "AndreaNuzzolese");
    BlankNodeOrIRI enricoDagaInClerezza = new IRI(ns + "EnricoDaga");
    Triple triple = new TripleImpl(andreaNuzzoleseInClerezza, rdfType, foafPersonInClerezza);
    mGraph.add(triple);
    triple = new TripleImpl(enricoDagaInClerezza, rdfType, foafPersonInClerezza);
    mGraph.add(triple);
    triple = new TripleImpl(andreaNuzzoleseInClerezza, knowsInClerezza, enricoDagaInClerezza);
    mGraph.add(triple);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) BeforeClass(org.junit.BeforeClass)

Example 22 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class UserResource method getUserRolesGraph.

/**
     * Provides a graph containing Role triples associated with a given user
     *
     * @param userName
     * @return roles graph
     */
private Graph getUserRolesGraph(String userName) {
    GraphNode userNode = getUser(userName);
    Iterator<RDFTerm> functionIterator = userNode.getObjects(SIOC.has_function);
    SimpleGraph rolesGraph = new SimpleGraph();
    while (functionIterator.hasNext()) {
        GraphNode functionNode = new GraphNode(functionIterator.next(), systemGraph);
        Iterator<Triple> roleIterator = systemGraph.filter((BlankNodeOrIRI) functionNode.getNode(), RDF.type, PERMISSION.Role);
        // needs lock?
        while (roleIterator.hasNext()) {
            Triple roleTriple = roleIterator.next();
            // rolesGraph.add(roleTriple);
            BlankNodeOrIRI roleNode = roleTriple.getSubject();
            SimpleGraph detailsGraph = new SimpleGraph(systemGraph.filter(roleNode, null, null));
            rolesGraph.addAll(detailsGraph);
        }
    }
    return rolesGraph;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 23 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class OpenCalaisEngine method readModel.

/**
     * Parses an InputStream of RDF data and produces an Graph from them
     *
     * @param in The InputStream of RDF data
     * @param format the format of the RDF data
     *
     * @return the resulting Graph or null if the RDF serialization format is not supported by the parser
     */
public Graph readModel(InputStream in, String format) {
    Parser parser = Parser.getInstance();
    if (parser.getSupportedFormats().contains(format)) {
        ImmutableGraph graph = parser.parse(in, format);
        Graph model = new SimpleGraph(graph);
        return model;
    } else {
        log.warn("Unsupported RDF format: {}\nSupported RDF formats: {}", format, parser.getSupportedFormats());
    }
    return null;
}
Also used : 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) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) QueryParser(org.apache.clerezza.rdf.core.sparql.QueryParser) Parser(org.apache.clerezza.rdf.core.serializedform.Parser)

Example 24 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class MetaxaEngine method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    // get model from the extraction
    URIImpl docId;
    Model m = null;
    ci.getLock().readLock().lock();
    try {
        docId = new URIImpl(ci.getUri().getUnicodeString());
        m = this.extractor.extract(ci.getStream(), docId, ci.getMimeType());
    } catch (ExtractorException e) {
        throw new EngineException("Error while processing ContentItem " + ci.getUri() + " with Metaxa", e);
    } catch (IOException e) {
        throw new EngineException("Error while processing ContentItem " + ci.getUri() + " with Metaxa", e);
    } finally {
        ci.getLock().readLock().unlock();
    }
    // the extracted plain text from the model
    if (null == m) {
        log.debug("Unable to preocess ContentItem {} (mime type {}) with Metaxa", ci.getUri(), ci.getMimeType());
        return;
    }
    ContentSink plainTextSink;
    try {
        plainTextSink = ciFactory.createContentSink("text/plain");
    } catch (IOException e) {
        m.close();
        throw new EngineException("Unable to initialise Blob for storing" + "the plain text content", e);
    }
    HashMap<BlankNode, BlankNode> blankNodeMap = new HashMap<BlankNode, BlankNode>();
    RDF2GoUtils.urifyBlankNodes(m);
    ClosableIterator<Statement> it = m.iterator();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(plainTextSink.getOutputStream(), UTF8));
    //used to detect if some text was extracted
    boolean textExtracted = false;
    try {
        //first add to a temporary graph
        Graph g = new SimpleGraph();
        while (it.hasNext()) {
            Statement oneStmt = it.next();
            //the plain text Blob!
            if (oneStmt.getSubject().equals(docId) && oneStmt.getPredicate().equals(NIE_PLAINTEXT_PROPERTY)) {
                String text = oneStmt.getObject().toString();
                if (text != null && !text.isEmpty()) {
                    try {
                        out.write(oneStmt.getObject().toString());
                    } catch (IOException e) {
                        throw new EngineException("Unable to write extracted" + "plain text to Blob (blob impl: " + plainTextSink.getBlob().getClass() + ")", e);
                    }
                    textExtracted = true;
                    if (includeText) {
                        BlankNodeOrIRI subject = (BlankNodeOrIRI) asClerezzaResource(oneStmt.getSubject(), blankNodeMap);
                        IRI predicate = (IRI) asClerezzaResource(oneStmt.getPredicate(), blankNodeMap);
                        RDFTerm object = asClerezzaResource(oneStmt.getObject(), blankNodeMap);
                        g.add(new TripleImpl(subject, predicate, object));
                    }
                }
            } else {
                //add metadata to the metadata of the contentItem
                BlankNodeOrIRI subject = (BlankNodeOrIRI) asClerezzaResource(oneStmt.getSubject(), blankNodeMap);
                IRI predicate = (IRI) asClerezzaResource(oneStmt.getPredicate(), blankNodeMap);
                RDFTerm object = asClerezzaResource(oneStmt.getObject(), blankNodeMap);
                if (null != subject && null != predicate && null != object) {
                    Triple t = new TripleImpl(subject, predicate, object);
                    g.add(t);
                    log.debug("added " + t.toString());
                }
            }
        }
        //add the extracted triples to the metadata of the ContentItem
        ci.getLock().writeLock().lock();
        try {
            ci.getMetadata().addAll(g);
            g = null;
        } finally {
            ci.getLock().writeLock().unlock();
        }
    } finally {
        it.close();
        m.close();
        IOUtils.closeQuietly(out);
    }
    if (textExtracted) {
        //add plain text to the content item
        IRI blobUri = new IRI("urn:metaxa:plain-text:" + randomUUID());
        ci.addPart(blobUri, plainTextSink.getBlob());
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) Statement(org.ontoware.rdf2go.model.Statement) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) BlankNode(org.ontoware.rdf2go.model.node.BlankNode) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) URIImpl(org.ontoware.rdf2go.model.node.impl.URIImpl) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Triple(org.apache.clerezza.commons.rdf.Triple) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Model(org.ontoware.rdf2go.model.Model) ExtractorException(org.semanticdesktop.aperture.extractor.ExtractorException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) OutputStreamWriter(java.io.OutputStreamWriter) ContentSink(org.apache.stanbol.enhancer.servicesapi.ContentSink) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 25 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class RootResource method getGraph.

private Graph getGraph(String ontologyId, boolean merged, URI requestUri) {
    long before = System.currentTimeMillis();
    OWLOntologyID key = OntologyUtils.decode(ontologyId);
    log.debug("Will try to retrieve ontology {} from provider.", key);
    /*
         * Export directly to Graph since the OWLOntologyWriter uses (de-)serializing converters for the
         * other formats.
         * 
         * Use oTemp for the "real" graph and o for the graph that will be exported. This is due to the fact
         * that in o we want to change import statements, but we do not want these changes to be stored
         * permanently.
         */
    Graph o = null, oTemp = null;
    try {
        oTemp = ontologyProvider.getStoredOntology(key, Graph.class, merged);
    } catch (Exception ex) {
        log.warn("Retrieval of ontology with ID " + key + " failed.", ex);
    }
    if (oTemp == null) {
        log.debug("Ontology {} missing from provider. Trying libraries...", key);
        // TODO remove once registry supports OWLOntologyID as public key.
        IRI iri = URIUtils.sanitize(IRI.create(ontologyId));
        // See if we can touch a library. TODO: replace with event model on the ontology provider.
        int minSize = -1;
        IRI smallest = null;
        for (Library lib : registryManager.getLibraries(iri)) {
            int size = lib.getChildren().length;
            if (minSize < 1 || size < minSize) {
                smallest = lib.getIRI();
                minSize = size;
            }
        }
        if (smallest != null) {
            log.debug("Selected library for ontology {} is {} .", iri, smallest);
            try {
                oTemp = registryManager.getLibrary(smallest).getOntology(iri, Graph.class);
            } catch (RegistryContentException e) {
                log.warn("The content of library " + smallest + " could not be accessed.", e);
            }
        }
    }
    // resource-intensive IndexedGraph, since both o and oTemp will be GC'ed after serialization.
    if (oTemp != null) {
        o = new SimpleGraph(oTemp);
    }
    if (o == null) {
        log.debug("Ontology {} not found in any ontology provider or library.", ontologyId);
        return null;
    }
    log.debug("Retrieved ontology {} .", ontologyId);
    // Rewrite imports
    String uri = uriInfo.getRequestUri().toString();
    URI base = URI.create(uri.substring(0, uri.lastIndexOf(ontologyId) - 1));
    // Rewrite import statements
    /*
         * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
         * going upOne() add "session" or "ontology" if needed).
         */
    Iterator<Triple> imports = o.filter(null, OWL.imports, null);
    Set<Triple> oldImports = new HashSet<Triple>();
    while (imports.hasNext()) {
        oldImports.add(imports.next());
    }
    for (Triple t : oldImports) {
        // construct new statement
        String s = ((org.apache.clerezza.commons.rdf.IRI) t.getObject()).getUnicodeString();
        if (s.contains("::")) {
            s = s.substring(s.indexOf("::") + 2, s.length());
        }
        org.apache.clerezza.commons.rdf.IRI target = new org.apache.clerezza.commons.rdf.IRI(base + "/" + s);
        o.add(new TripleImpl(t.getSubject(), OWL.imports, target));
        // remove old statement
        o.remove(t);
    }
    // Versioning.
    OWLOntologyID id = OWLUtils.extractOntologyID(o);
    if (id != null && !id.isAnonymous() && id.getVersionIRI() == null) {
        org.apache.clerezza.commons.rdf.IRI viri = new org.apache.clerezza.commons.rdf.IRI(requestUri.toString());
        log.debug("Setting version IRI for export : {}", viri);
        o.add(new TripleImpl(new org.apache.clerezza.commons.rdf.IRI(id.getOntologyIRI().toString()), new org.apache.clerezza.commons.rdf.IRI(OWL2Constants.OWL_VERSION_IRI), viri));
    }
    log.debug("Exported as Clerezza ImmutableGraph in {} ms. Handing over to writer.", System.currentTimeMillis() - before);
    return o;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) URI(java.net.URI) UnsupportedFormatException(org.apache.clerezza.rdf.core.serializedform.UnsupportedFormatException) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) OntologyLoadingException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyLoadingException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OntologyHandleException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OntologyHandleException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OrphanOntologyKeyException(org.apache.stanbol.ontologymanager.servicesapi.ontology.OrphanOntologyKeyException) Triple(org.apache.clerezza.commons.rdf.Triple) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) HashSet(java.util.HashSet)

Aggregations

SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)46 Graph (org.apache.clerezza.commons.rdf.Graph)34 IRI (org.apache.clerezza.commons.rdf.IRI)24 Test (org.junit.Test)17 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)15 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)12 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)11 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)10 Triple (org.apache.clerezza.commons.rdf.Triple)10 HashSet (java.util.HashSet)9 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStream (java.io.InputStream)5 HtmlExtractor (org.apache.stanbol.enhancer.engines.htmlextractor.impl.HtmlExtractor)5 RdfEntityFactory (org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory)5 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 JenaParserProvider (org.apache.clerezza.rdf.jena.parser.JenaParserProvider)3