Search in sources :

Example 26 with OWLAxiom

use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.

the class ByteArrayInputProvider method getInput.

@Override
public <T> Iterator<T> getInput(Class<T> type) throws IOException {
    if (type.isAssignableFrom(OWLAxiom.class)) {
        // We add additional axioms
        OWLOntology fromUrl;
        try {
            fromUrl = createOWLOntologyManager().loadOntologyFromOntologyDocument(new ByteArrayInputStream(bytes));
        } catch (OWLOntologyCreationException e) {
            throw new IOException(e);
        }
        Set<OWLOntology> all = fromUrl.getImportsClosure();
        List<OWLAxiom> axiomList = new ArrayList<OWLAxiom>();
        for (OWLOntology o : all) {
            axiomList.addAll(o.getAxioms());
        }
        final Iterator<OWLAxiom> iterator = axiomList.iterator();
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) iterator.next();
            }

            @Override
            public void remove() {
                // This iterator is read-only
                throw new UnsupportedOperationException("Cannot remove statements from the iterator");
            }
        };
    } else if (type.isAssignableFrom(Statement.class)) {
        final OntModel input = ModelFactory.createOntologyModel();
        synchronized (bytes) {
            // XXX 
            // Not sure this would always work. What if we have an RDF/XML relying on an implicit base?
            input.read(new ByteArrayInputStream(bytes), "");
        }
        final StmtIterator iterator = input.listStatements();
        return new Iterator<T>() {

            @Override
            public boolean hasNext() {
                return iterator.hasNext();
            }

            @SuppressWarnings("unchecked")
            @Override
            public T next() {
                return (T) iterator.next();
            }

            @Override
            public void remove() {
                // This iterator is read-only
                throw new UnsupportedOperationException("Cannot remove statements from the iterator");
            }
        };
    } else {
        throw new UnsupportedOperationException("This provider does not adapt to the given type");
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) ArrayList(java.util.ArrayList) IOException(java.io.IOException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ByteArrayInputStream(java.io.ByteArrayInputStream) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) Iterator(java.util.Iterator) StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) OntModel(com.hp.hpl.jena.ontology.OntModel) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom)

Example 27 with OWLAxiom

use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.

the class AbstractOWLApiReasoningService method run.

/**
     * Merges the SWRL rules in the input ontology, then calls run(OWLOntology,List<InferredAxiomGenerator<?
     * extends OWLAxiom>>)
     * 
     * @param ontology
     * @param rules
     * @param generators
     * @return
     */
@Override
public Set<OWLAxiom> run(OWLOntology ontology, List<SWRLRule> rules, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
    log.debug("Called method run(OWLOntology,List<SWRLRule>,List)");
    OWLOntologyManager manager = ontology.getOWLOntologyManager();
    log.debug("Adding SWRL rules to the input ontology.");
    Set<SWRLRule> ruleSet = new HashSet<SWRLRule>();
    ruleSet.addAll(rules);
    manager.addAxioms(ontology, ruleSet);
    if (log.isDebugEnabled())
        for (OWLAxiom a : ontology.getAxioms()) {
            log.debug("Axiom {}", a);
        }
    log.debug("Calling the run method.");
    return run(ontology, generators);
}
Also used : SWRLRule(org.semanticweb.owlapi.model.SWRLRule) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) HashSet(java.util.HashSet)

Example 28 with OWLAxiom

use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.

the class AbstractOWLApiReasoningService method run.

/**
     * Generic method for running the reasoner
     * 
     * @param input
     * @param generators
     * @return
     */
@Override
public Set<OWLAxiom> run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators) throws ReasoningServiceException, InconsistentInputException {
    log.debug("run(OWLOntology input, List<InferredAxiomGenerator<? extends OWLAxiom>> generators)");
    try {
        // Get the manager
        OWLOntologyManager manager = createOWLOntologyManager();
        // Get the reasoner
        OWLReasoner reasoner = getReasoner(input);
        log.info("Running {} reasoner on {} ", reasoner.getClass(), input.getOntologyID());
        // To generate inferred axioms
        InferredOntologyGenerator inferred = new InferredOntologyGenerator(reasoner, generators);
        // We fill an anonymous ontology with the result, the return the
        // axiom set
        Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
        try {
            OWLOntology output = manager.createOntology();
            log.debug("Created output ontology: {}", output);
            try {
                inferred.fillOntology(manager, output);
            } catch (InconsistentOntologyException i) {
                throw i;
            } catch (Throwable t) {
                log.error("Some problem occurred:\n {}", t.getStackTrace());
                throw new ReasoningServiceException();
            }
            log.debug("Filled ontology: {}", output);
            log.debug("Temporary ID is {}", output.getOntologyID());
            axioms = manager.getOntology(output.getOntologyID()).getAxioms();
            // IMPORTANT We remove the ontology from the manager
            manager.removeOntology(output);
        } catch (OWLOntologyCreationException e) {
            log.error("An exception have been thrown when instantiating the ontology");
            throw new ReasoningServiceException();
        }
        return axioms;
    } catch (InconsistentOntologyException inconsistent) {
        /**
             * TODO Add report. Why it is inconsistent?
             */
        throw new InconsistentInputException();
    } catch (Exception exception) {
        log.error("An exception have been thrown while executing method run()", exception);
        throw new ReasoningServiceException();
    }
}
Also used : OWLReasoner(org.semanticweb.owlapi.reasoner.OWLReasoner) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) InconsistentInputException(org.apache.stanbol.reasoners.servicesapi.InconsistentInputException) InconsistentOntologyException(org.semanticweb.owlapi.reasoner.InconsistentOntologyException) UnsupportedTaskException(org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException) ReasoningServiceException(org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) InconsistentOntologyException(org.semanticweb.owlapi.reasoner.InconsistentOntologyException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) InferredOntologyGenerator(org.semanticweb.owlapi.util.InferredOntologyGenerator) HashSet(java.util.HashSet)

Example 29 with OWLAxiom

use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.

the class ScopeSetRenderer method getScopes.

public static OWLOntology getScopes(Set<Scope> scopes) {
    OWLOntologyManager mgr = OWLManager.createOWLOntologyManager();
    OWLOntology ont = null;
    try {
        ont = mgr.createOntology();
    } catch (OWLOntologyCreationException e) {
        LoggerFactory.getLogger(ScopeSetRenderer.class).error("KReS :: could not create empty ontology for rendering scopes.", e);
        return null;
    }
    List<OWLOntologyChange> additions = new LinkedList<OWLOntologyChange>();
    // The ODP metadata vocabulary is always imported.
    // TODO : also import the ONM meta when it goes online.
    additions.add(new AddImport(ont, __factory.getOWLImportsDeclaration(IRI.create("http://www.ontologydesignpatterns.org/schemas/meta.owl"))));
    for (Scope scope : scopes) {
        OWLNamedIndividual iScope = __factory.getOWLNamedIndividual(IRI.create(scope.getDefaultNamespace() + scope.getID()));
        OWLAxiom ax = __factory.getOWLClassAssertionAxiom(cScope, iScope);
        additions.add(new AddAxiom(ont, ax));
    }
    mgr.applyChanges(additions);
    return ont;
}
Also used : AddAxiom(org.semanticweb.owlapi.model.AddAxiom) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) Scope(org.apache.stanbol.ontologymanager.servicesapi.scope.Scope) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) AddImport(org.semanticweb.owlapi.model.AddImport) LinkedList(java.util.LinkedList)

Example 30 with OWLAxiom

use of org.semanticweb.owlapi.model.OWLAxiom in project stanbol by apache.

the class RuleListWriter method writeTo.

@Override
public void writeTo(RuleList ruleList, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType mediaType, MultivaluedMap<String, Object> arg5, OutputStream out) throws IOException, WebApplicationException {
    Logger log = LoggerFactory.getLogger(getClass());
    log.debug("Rendering the list of recipes.");
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLDataFactory factory = OWLManager.getOWLDataFactory();
    OWLOntology ontology;
    try {
        ontology = manager.createOntology();
        String recipeClassURI = Symbols.Recipe.toString().replace("<", "").replace(">", "");
        IRI recipeClassIRI = IRI.create(recipeClassURI);
        OWLClass owlRecipeClass = factory.getOWLClass(recipeClassIRI);
        String ruleClassURI = Symbols.Rule.toString().replace("<", "").replace(">", "");
        IRI ruleClassIRI = IRI.create(ruleClassURI);
        OWLClass owlRuleClass = factory.getOWLClass(ruleClassIRI);
        String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
        IRI descriptionIRI = IRI.create(descriptionURI);
        OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
        String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
        IRI hasRuleIRI = IRI.create(hasRuleURI);
        OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
        String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
        IRI ruleBodyIRI = IRI.create(ruleBodyURI);
        OWLDataProperty ruleBody = factory.getOWLDataProperty(ruleBodyIRI);
        String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
        IRI ruleHeadIRI = IRI.create(ruleHeadURI);
        OWLDataProperty ruleHead = factory.getOWLDataProperty(ruleHeadIRI);
        if (ruleList != null) {
            for (Rule rule : ruleList) {
                String recipeId = rule.getRecipe().getRecipeID().toString().replace("<", "").replace(">", "");
                IRI reicpeIRI = IRI.create(recipeId);
                OWLIndividual owlRecipe = factory.getOWLNamedIndividual(reicpeIRI);
                String ruleId = rule.getRuleID().toString().replace("<", "").replace(">", "");
                IRI ruleIRI = IRI.create(ruleId);
                OWLIndividual owlRule = factory.getOWLNamedIndividual(ruleIRI);
                OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, owlRecipe);
                manager.addAxiom(ontology, axiom);
                axiom = factory.getOWLClassAssertionAxiom(owlRuleClass, owlRule);
                manager.addAxiom(ontology, axiom);
                axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, owlRecipe, owlRule);
                manager.addAxiom(ontology, axiom);
                String recipeDescription = rule.getRecipe().getRecipeDescription();
                String ruleDescription = rule.getDescription();
                if (recipeDescription != null) {
                    axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRecipe, recipeDescription);
                    manager.addAxiom(ontology, axiom);
                }
                if (ruleDescription != null) {
                    axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, owlRule, ruleDescription);
                    manager.addAxiom(ontology, axiom);
                }
                String ruleContent = rule.toString();
                String[] parts = ruleContent.split("\\->");
                axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBody, owlRule, parts[0]);
                manager.addAxiom(ontology, axiom);
                axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHead, owlRule, parts[1]);
                manager.addAxiom(ontology, axiom);
            }
        }
        if (mediaType.toString().equals(KRFormat.RDF_XML)) {
            try {
                manager.saveOntology(ontology, new RDFXMLOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.OWL_XML)) {
            try {
                manager.saveOntology(ontology, new OWLXMLOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.MANCHESTER_OWL)) {
            try {
                manager.saveOntology(ontology, new ManchesterOWLSyntaxOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.FUNCTIONAL_OWL)) {
            try {
                manager.saveOntology(ontology, new OWLFunctionalSyntaxOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.TURTLE)) {
            try {
                manager.saveOntology(ontology, new TurtleOntologyFormat(), out);
            } catch (OWLOntologyStorageException e) {
                log.error("Failed to store ontology for rendering.", e);
            }
        } else if (mediaType.toString().equals(KRFormat.RDF_JSON)) {
            Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
            RdfJsonSerializingProvider provider = new RdfJsonSerializingProvider();
            provider.serialize(out, mGraph, SupportedFormat.RDF_JSON);
        }
    } catch (OWLOntologyCreationException e1) {
        log.error("An error occurred.", e1);
    }
    out.flush();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) TurtleOntologyFormat(org.coode.owlapi.turtle.TurtleOntologyFormat) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) Logger(org.slf4j.Logger) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) OWLDataProperty(org.semanticweb.owlapi.model.OWLDataProperty) Graph(org.apache.clerezza.commons.rdf.Graph) RdfJsonSerializingProvider(org.apache.clerezza.rdf.rdfjson.serializer.RdfJsonSerializingProvider) OWLXMLOntologyFormat(org.semanticweb.owlapi.io.OWLXMLOntologyFormat) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) Rule(org.apache.stanbol.rules.base.api.Rule) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)30 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)24 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)20 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)17 HashSet (java.util.HashSet)9 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)9 ArrayList (java.util.ArrayList)8 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)8 AddImport (org.semanticweb.owlapi.model.AddImport)7 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)7 UnsupportedTaskException (org.apache.stanbol.reasoners.servicesapi.UnsupportedTaskException)6 Statement (com.hp.hpl.jena.rdf.model.Statement)5 OWLClassAssertionAxiom (org.semanticweb.owlapi.model.OWLClassAssertionAxiom)5 OWLImportsDeclaration (org.semanticweb.owlapi.model.OWLImportsDeclaration)5 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)5 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)5 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)5 IRI (org.semanticweb.owlapi.model.IRI)4 OWLClass (org.semanticweb.owlapi.model.OWLClass)4 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)4