Search in sources :

Example 66 with Graph

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

the class TestClerezzaSpaces method testCreateSpace.

@Test
public void testCreateSpace() throws Exception {
    OntologySpace space = factory.createCustomOntologySpace(scopeId, dropSrc);
    OWLOntologyID logicalId = null;
    Object o = dropSrc.getRootOntology();
    if (o instanceof Graph)
        logicalId = OWLUtils.extractOntologyID((Graph) o);
    else if (o instanceof OWLOntology)
        logicalId = OWLUtils.extractOntologyID((OWLOntology) o);
    assertNotNull(logicalId);
    assertTrue(space.hasOntology(logicalId));
}
Also used : Graph(org.apache.clerezza.commons.rdf.Graph) OntologySpace(org.apache.stanbol.ontologymanager.servicesapi.scope.OntologySpace) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Test(org.junit.Test)

Example 67 with Graph

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

the class TestClerezzaSpaces method setup.

@BeforeClass
public static void setup() throws Exception {
    offline = new OfflineConfigurationImpl(new Hashtable<String, Object>());
    ScopeRegistry reg = new ScopeRegistryImpl();
    // This one is created from scratch
    Graph ont2 = ClerezzaOWLUtils.createOntology(baseIri2.toString());
    minorSrc = new GraphSource(ont2.getImmutableGraph());
    dropSrc = getLocalSource("/ontologies/droppedcharacters.owl");
    nonexSrc = getLocalSource("/ontologies/nonexistentcharacters.owl");
    inMemorySrc = new ParentPathInputSource(new File(TestClerezzaSpaces.class.getResource("/ontologies/maincharacters.owl").toURI()));
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    OWLClass cHuman = df.getOWLClass(IRI.create(baseIri + "/" + Constants.humanBeing));
    OWLIndividual iLinus = df.getOWLNamedIndividual(IRI.create(baseIri + "/" + Constants.linus));
    linusIsHuman = df.getOWLClassAssertionAxiom(cHuman, iLinus);
    factory = new ClerezzaCollectorFactory(new ClerezzaOntologyProvider(tcManager, offline, parser), new Hashtable<String, Object>());
    factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology/"));
}
Also used : Hashtable(java.util.Hashtable) OfflineConfigurationImpl(org.apache.stanbol.ontologymanager.core.OfflineConfigurationImpl) ClerezzaOntologyProvider(org.apache.stanbol.ontologymanager.multiplexer.clerezza.ontology.ClerezzaOntologyProvider) Graph(org.apache.clerezza.commons.rdf.Graph) ScopeRegistryImpl(org.apache.stanbol.ontologymanager.core.scope.ScopeRegistryImpl) GraphSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource) ParentPathInputSource(org.apache.stanbol.ontologymanager.sources.owlapi.ParentPathInputSource) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) File(java.io.File) ScopeRegistry(org.apache.stanbol.ontologymanager.servicesapi.scope.ScopeRegistry) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) ClerezzaCollectorFactory(org.apache.stanbol.ontologymanager.multiplexer.clerezza.collector.ClerezzaCollectorFactory) BeforeClass(org.junit.BeforeClass)

Example 68 with Graph

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

the class TestOntologyNetworkPersistence method updatesGraphOnSpaceModification.

@Test
public void updatesGraphOnSpaceModification() throws Exception {
    // Ensure the metadata graph is there.
    Graph meta = ontologyProvider.getMetaGraph(Graph.class);
    assertNotNull(meta);
    String scopeId = "updateTest";
    Scope scope = onm.createOntologyScope(scopeId, new GraphContentInputSource(getClass().getResourceAsStream("/ontologies/test1.owl")));
    IRI collector = new IRI(_NS_STANBOL_INTERNAL + OntologySpace.shortName + "/" + scope.getCoreSpace().getID());
    // Has no versionIRI
    IRI test1id = new IRI("http://stanbol.apache.org/ontologies/test1.owl");
    // Be strict: the whole property pair must be there.
    IRI predicate = MANAGES_URIREF;
    assertTrue(meta.contains(new TripleImpl(collector, predicate, test1id)));
    predicate = IS_MANAGED_BY_URIREF;
    assertTrue(meta.contains(new TripleImpl(test1id, predicate, collector)));
    // To modify the core space.
    scope.tearDown();
    scope.getCoreSpace().addOntology(new GraphContentInputSource(getClass().getResourceAsStream("/ontologies/minorcharacters.owl")));
    IRI minorId = new IRI("http://stanbol.apache.org/ontologies/pcomics/minorcharacters.owl");
    predicate = MANAGES_URIREF;
    assertTrue(meta.contains(new TripleImpl(collector, predicate, minorId)));
    predicate = IS_MANAGED_BY_URIREF;
    assertTrue(meta.contains(new TripleImpl(minorId, predicate, collector)));
    scope.getCustomSpace().addOntology(new GraphContentInputSource(getClass().getResourceAsStream("/ontologies/test1.owl")));
    scope.getCustomSpace().addOntology(new GraphContentInputSource(getClass().getResourceAsStream("/ontologies/minorcharacters.owl")));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Graph(org.apache.clerezza.commons.rdf.Graph) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) GraphContentInputSource(org.apache.stanbol.ontologymanager.sources.clerezza.GraphContentInputSource) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Test(org.junit.Test)

Example 69 with Graph

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

the class ResourceMapping method apply.

@Override
public boolean apply(Graph graph, BlankNodeOrIRI subject, Metadata metadata) {
    boolean added = false;
    BlankNodeOrIRI s = new BlankNode();
    mappingLogger.log(subject, ontProperty, null, s);
    if (!required.isEmpty()) {
        Graph g = new SimpleGraph();
        for (Mapping m : required) {
            if (!m.apply(g, s, metadata)) {
                return false;
            }
        }
        graph.addAll(g);
        added = true;
    }
    for (Mapping m : optional) {
        if (m.apply(graph, s, metadata)) {
            added = true;
        }
    }
    if (added) {
        for (Mapping m : additional) {
            m.apply(graph, s, metadata);
        }
        graph.add(new TripleImpl(subject, ontProperty, s));
    }
    return added;
}
Also used : SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) BlankNode(org.apache.clerezza.commons.rdf.BlankNode) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 70 with Graph

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

the class RestfulLangidentEngine method computeEnhancements.

/**
     * Compute enhancements for supplied ContentItem. The results of the process
     * are expected to be stored in the metadata of the content item.
     * <p/>
     * The client (usually an {@link org.apache.stanbol.enhancer.servicesapi.EnhancementJobManager}) should take care of
     * persistent storage of the enhanced {@link org.apache.stanbol.enhancer.servicesapi.ContentItem}.
     * <p/>
     * This method creates a new POSContentPart using {@link org.apache.stanbol.enhancer.engines.pos.api.POSTaggerHelper#createContentPart} from a text/plain part and
     * stores it as a new part in the content item. The metadata is not changed.
     *
     * @throws org.apache.stanbol.enhancer.servicesapi.EngineException
     *          if the underlying process failed to work as
     *          expected
     */
@Override
public void computeEnhancements(final ContentItem ci) throws EngineException {
    //get the plain text Blob
    Map.Entry<IRI, Blob> textBlob = getPlainText(this, ci, false);
    Blob blob = textBlob.getValue();
    //send the text to the server
    final HttpPost request = new HttpPost(serviceUrl);
    request.setEntity(new InputStreamEntity(blob.getStream(), blob.getContentLength(), ContentType.create(blob.getMimeType(), blob.getParameter().get("charset"))));
    //execute the request
    List<LangSuggestion> detected;
    try {
        detected = AccessController.doPrivileged(new PrivilegedExceptionAction<List<LangSuggestion>>() {

            public List<LangSuggestion> run() throws ClientProtocolException, IOException {
                return httpClient.execute(request, new LangIdentResponseHandler(ci, objectMapper));
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof ClientProtocolException) {
            throw new EngineException(this, ci, "Exception while executing Request " + "on RESTful Language Identification Service at " + serviceUrl, e);
        } else if (e instanceof IOException) {
            throw new EngineException(this, ci, "Exception while executing Request " + "on RESTful Language Identification Service at " + serviceUrl, e);
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
    Graph metadata = ci.getMetadata();
    log.debug("Detected Languages for ContentItem {} and Blob {}");
    ci.getLock().writeLock().lock();
    try {
        //write TextAnnotations for the detected languages
        for (LangSuggestion suggestion : detected) {
            // add a hypothesis
            log.debug(" > {}@{}", suggestion.getLanguage(), suggestion.hasProbability() ? suggestion.getProbability() : "-,--");
            IRI textEnhancement = EnhancementEngineHelper.createTextEnhancement(ci, this);
            metadata.add(new TripleImpl(textEnhancement, DC_LANGUAGE, new PlainLiteralImpl(suggestion.getLanguage())));
            metadata.add(new TripleImpl(textEnhancement, DC_TYPE, DCTERMS_LINGUISTIC_SYSTEM));
            if (suggestion.hasProbability()) {
                metadata.add(new TripleImpl(textEnhancement, ENHANCER_CONFIDENCE, literalFactory.createTypedLiteral(suggestion.getProbability())));
            }
        }
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) HttpPost(org.apache.http.client.methods.HttpPost) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) PrivilegedActionException(java.security.PrivilegedActionException) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) HttpException(org.apache.http.HttpException) ClientProtocolException(org.apache.http.client.ClientProtocolException) PrivilegedActionException(java.security.PrivilegedActionException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) ClientProtocolException(org.apache.http.client.ClientProtocolException) Graph(org.apache.clerezza.commons.rdf.Graph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Graph (org.apache.clerezza.commons.rdf.Graph)172 IRI (org.apache.clerezza.commons.rdf.IRI)110 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)66 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)57 Triple (org.apache.clerezza.commons.rdf.Triple)45 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)43 Test (org.junit.Test)38 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)36 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)34 IOException (java.io.IOException)27 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)26 HashSet (java.util.HashSet)24 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)24 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)24 InputStream (java.io.InputStream)21 HashMap (java.util.HashMap)20 Language (org.apache.clerezza.commons.rdf.Language)17 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)17 ArrayList (java.util.ArrayList)16 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)15