Search in sources :

Example 1 with OWLIndividual

use of org.semanticweb.owlapi.model.OWLIndividual in project goci by EBISPOT.

the class KBLoader method getTraitClass.

private Set<IRI> getTraitClass(OWLOntology ontology, OWLNamedIndividual individual) {
    OWLOntologyManager manager = ontology.getOWLOntologyManager();
    OWLDataFactory dataFactory = manager.getOWLDataFactory();
    Set<IRI> results = new HashSet<IRI>();
    // get all individuals related to this one by "is_about"
    OWLObjectProperty has_object = dataFactory.getOWLObjectProperty(IRI.create(OntologyConstants.HAS_OBJECT_IRI));
    Set<OWLIndividual> relatedInds = individual.getObjectPropertyValues(has_object, ontology);
    // for each related individual, get all types
    for (OWLIndividual related : relatedInds) {
        Set<OWLClassExpression> types = related.getTypes(ontology);
        for (OWLClassExpression type : types) {
            results.add(type.asOWLClass().getIRI());
        }
    }
    return results;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLObjectProperty(org.semanticweb.owlapi.model.OWLObjectProperty) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 2 with OWLIndividual

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

the class RegistryManagerImpl method createModel.

@Override
public Set<Registry> createModel(Set<OWLOntology> registryOntologies) {
    Set<Registry> results = new HashSet<Registry>();
    // Reset population
    population.clear();
    // Build the transitive imports closure of the union.
    Set<OWLOntology> closure = new HashSet<OWLOntology>();
    for (OWLOntology rego : registryOntologies) closure.addAll(rego.getOWLOntologyManager().getImportsClosure(rego));
    /*
         * For each value in this map, index 0 is the score of the library class, while 1 is the score of the
         * ontology class.
         */
    final Map<IRI, int[]> candidateTypes = new HashMap<IRI, int[]>();
    /*
         * Scans class assertions and object property values and tries to determine the type of each
         * individual it finds.
         */
    OWLAxiomVisitor scanner = new OWLAxiomVisitorAdapter() {

        /*
             * For a given identifier, returns the array of integers whose value determine the likelihood if
             * the corresponding entity being a library or an ontology. If no such array exists, it is
             * created.
             */
        private int[] checkScores(IRI key) {
            int[] scores;
            if (candidateTypes.containsKey(key))
                scores = candidateTypes.get(key);
            else {
                scores = new int[] { 0, 0 };
                candidateTypes.put(key, scores);
            }
            return scores;
        }

        @Override
        public void visit(OWLAnnotationAssertionAxiom axiom) {
            /*
                 * Works like object property assertions, in case hasOntology and isOntologyOf are not
                 * detected to be object properties (e.g. due to a failure to load codolight or the registry
                 * metamodel).
                 */
            OWLAnnotationProperty prop = axiom.getProperty();
            if (hasOntologyAnn.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its library score.
                OWLObject ind = axiom.getSubject();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[0]++;
                }
                // The axiom object gets a +1 in its ontology score.
                ind = axiom.getValue();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[1]++;
                }
            } else if (isOntologyOfAnn.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its ontology score.
                OWLObject ind = axiom.getSubject();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[1]++;
                }
                // The axiom object gets a +1 in its library score.
                ind = axiom.getValue();
                if (ind instanceof IRI) {
                    iri = (IRI) ind;
                    checkScores(iri)[0]++;
                }
            }
        }

        @Override
        public void visit(OWLClassAssertionAxiom axiom) {
            OWLIndividual ind = axiom.getIndividual();
            // Do not accept anonymous registry items.
            if (ind.isAnonymous())
                return;
            IRI iri = ind.asOWLNamedIndividual().getIRI();
            int[] scores = checkScores(iri);
            OWLClassExpression type = axiom.getClassExpression();
            // If the type is stated to be a library, increase its library score.
            if (cRegistryLibrary.equals(type)) {
                scores[0]++;
            } else // If the type is stated to be an ontology, increase its ontology score.
            if (cOntology.equals(type)) {
                scores[1]++;
            }
        }

        @Override
        public void visit(OWLObjectPropertyAssertionAxiom axiom) {
            OWLObjectPropertyExpression prop = axiom.getProperty();
            if (hasOntology.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its library score.
                OWLIndividual ind = axiom.getSubject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[0]++;
                }
                // The axiom object gets a +1 in its ontology score.
                ind = axiom.getObject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[1]++;
                }
            } else if (isOntologyOf.equals(prop)) {
                IRI iri;
                // The axiom subject gets a +1 in its ontology score.
                OWLIndividual ind = axiom.getSubject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[1]++;
                }
                // The axiom object gets a +1 in its library score.
                ind = axiom.getObject();
                if (!ind.isAnonymous()) {
                    iri = ind.asOWLNamedIndividual().getIRI();
                    checkScores(iri)[0]++;
                }
            }
        }
    };
    // First pass to determine the types.
    for (OWLOntology o : closure) for (OWLAxiom ax : o.getAxioms()) ax.accept(scanner);
    // Then populate on the registry
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (IRI iri : candidateTypes.keySet()) {
        int[] scores = candidateTypes.get(iri);
        if (scores != null && (scores[0] > 0 || scores[1] > 0)) {
            if (scores[0] > 0 && scores[1] == 0)
                population.put(iri, riFactory.createLibrary(df.getOWLNamedIndividual(iri)));
            else if (scores[0] == 0 && scores[1] > 0)
                population.put(iri, riFactory.createRegistryOntology(df.getOWLNamedIndividual(iri)));
        }
    // else log.warn("Unable to determine type for registry item {}", iri);
    }
    for (OWLOntology oReg : registryOntologies) {
        try {
            results.add(populateRegistry(oReg));
        } catch (RegistryContentException e) {
            log.error("An error occurred while populating an ontology registry.", e);
        }
    }
    return results;
}
Also used : OWLObject(org.semanticweb.owlapi.model.OWLObject) IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) OWLAxiomVisitor(org.semanticweb.owlapi.model.OWLAxiomVisitor) HashMap(java.util.HashMap) Registry(org.apache.stanbol.ontologymanager.registry.api.model.Registry) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLAxiomVisitorAdapter(org.semanticweb.owlapi.util.OWLAxiomVisitorAdapter) OWLAnnotationProperty(org.semanticweb.owlapi.model.OWLAnnotationProperty) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLObjectPropertyExpression(org.semanticweb.owlapi.model.OWLObjectPropertyExpression) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLObjectPropertyAssertionAxiom(org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLClassAssertionAxiom(org.semanticweb.owlapi.model.OWLClassAssertionAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 3 with OWLIndividual

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

the class RegistryManagerImpl method populateOntology.

protected RegistryOntology populateOntology(OWLNamedObject ind, Set<OWLOntology> registries) throws RegistryContentException {
    IRI ontId = ind.getIRI();
    RegistryItem ront = null;
    if (population.containsKey(ontId)) {
        // We are not allowing multityping either.
        ront = population.get(ontId);
        if (!(ront instanceof RegistryOntology))
            throw new RegistryContentException("Inconsistent multityping: for item " + ontId + " : {" + RegistryOntology.class + ", " + ront.getClass() + "}");
    } else {
        ront = riFactory.createRegistryOntology(ind);
        try {
            population.put(ront.getIRI(), ront);
        } catch (Exception e) {
            log.error("Invalid identifier for library item " + ront, e);
            return null;
        }
    }
    // EXIT nodes.
    Set<OWLNamedObject> libs = new HashSet<OWLNamedObject>();
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (OWLOntology o : registries) {
        if (ind instanceof OWLIndividual) {
            // Get usages of isOntologyOf as an object property
            for (OWLIndividual value : ((OWLIndividual) ind).getObjectPropertyValues(isOntologyOf, o)) if (value.isNamed())
                libs.add(value.asOWLNamedIndividual());
            // Get usages of isOntologyOf as an annotation property
            for (OWLAnnotationAssertionAxiom ann : o.getAnnotationAssertionAxioms(ind.getIRI())) if (isOntologyOfAnn.equals(ann.getProperty())) {
                OWLAnnotationValue value = ann.getValue();
                if (value instanceof OWLNamedObject)
                    libs.add((OWLNamedObject) value);
                else if (value instanceof IRI)
                    libs.add(df.getOWLNamedIndividual((IRI) value));
            }
        }
    }
    for (OWLNamedObject ilib : libs) {
        IRI parentId = ilib.getIRI();
        // If some populate*() method has created it, it will be there.
        RegistryItem rlib = population.get(parentId);
        // Otherwise populating it will also put it in population.
        if (rlib == null)
            rlib = populateLibrary(ilib, registries);
        ront.addParent(rlib);
        if (ontologyIndex.get(ontId) == null)
            ontologyIndex.put(ontId, new HashSet<IRI>());
        ontologyIndex.get(ontId).add(parentId);
    }
    return (RegistryOntology) ront;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) RegistryOntology(org.apache.stanbol.ontologymanager.registry.api.model.RegistryOntology) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLNamedObject(org.semanticweb.owlapi.model.OWLNamedObject) OWLAnnotationValue(org.semanticweb.owlapi.model.OWLAnnotationValue) RegistryContentException(org.apache.stanbol.ontologymanager.registry.api.RegistryContentException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) RegistryItem(org.apache.stanbol.ontologymanager.registry.api.model.RegistryItem) HashSet(java.util.HashSet) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

Example 4 with OWLIndividual

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

the class TestOWLAPIInputSources method testOfflineImport.

/**
 * Uses a {@link ParentPathInputSource} to load an ontology importing a modified FOAF, both located in the
 * same resource directory.
 *
 * @throws Exception
 */
@Test
public void testOfflineImport() throws Exception {
    URL url = getClass().getResource("/ontologies/maincharacters.owl");
    assertNotNull(url);
    File f = new File(url.toURI());
    assertNotNull(f);
    OntologyInputSource<OWLOntology> coreSource = new ParentPathInputSource(f);
    // // Check that all the imports closure is made of local files
    // Set<OWLOntology> closure = coreSource.getImports(true);
    // for (OWLOntology o : closure)
    // assertEquals("file", o.getOWLOntologyManager().getOntologyDocumentIRI(o).getScheme());
    assertEquals(coreSource.getRootOntology().getOntologyID().getOntologyIRI(), IRI.create(Constants.PEANUTS_MAIN_BASE));
    // Linus is stated to be a foaf:Person
    OWLNamedIndividual iLinus = df.getOWLNamedIndividual(IRI.create(Constants.PEANUTS_MAIN_BASE + "#Linus"));
    // Lucy is stated to be a foaf:Perzon
    OWLNamedIndividual iLucy = df.getOWLNamedIndividual(IRI.create(Constants.PEANUTS_MAIN_BASE + "#Lucy"));
    OWLClass cPerzon = df.getOWLClass(IRI.create("http://xmlns.com/foaf/0.1/Perzon"));
    Set<OWLIndividual> instances = cPerzon.getIndividuals(coreSource.getRootOntology());
    assertTrue(!instances.contains(iLinus) && instances.contains(iLucy));
}
Also used : OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLClass(org.semanticweb.owlapi.model.OWLClass) File(java.io.File) URL(java.net.URL) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) Test(org.junit.Test)

Example 5 with OWLIndividual

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

the class RecipeWriter method writeTo.

@Override
public void writeTo(Recipe recipe, 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.");
    if (mediaType.toString().equals(MediaType.TEXT_PLAIN)) {
        String recipeString = recipe.toString();
        out.write(recipeString.getBytes());
    } else if (mediaType.toString().equals(MediaType.TEXT_HTML)) {
        String recipeString = recipe.toString();
        out.write(recipeString.getBytes());
    } else {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLDataFactory factory = OWLManager.getOWLDataFactory();
        OWLOntology ontology;
        try {
            ontology = manager.createOntology();
            RuleList rules = recipe.getRuleList();
            IRI recipeID = recipe.getRecipeID();
            String recipeURI = recipeID.toString().replace("<", "").replace(">", "");
            org.semanticweb.owlapi.model.IRI recipeIRI = org.semanticweb.owlapi.model.IRI.create(recipeURI);
            OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
            String descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
            org.semanticweb.owlapi.model.IRI descriptionIRI = org.semanticweb.owlapi.model.IRI.create(descriptionURI);
            OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
            OWLAxiom axiom;
            String recipeDescription = recipe.getRecipeDescription();
            if (recipeDescription != null) {
                axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual, recipeDescription);
                manager.addAxiom(ontology, axiom);
            }
            if (rules != null) {
                for (Rule rule : rules) {
                    IRI ruleID = rule.getRuleID();
                    String ruleName = rule.getRuleName();
                    String ruleDescription = rule.getDescription();
                    String ruleURI = ruleID.toString().replace("<", "").replace(">", "");
                    String ruleNameURI = Symbols.ruleName.toString().replace("<", "").replace(">", "");
                    String ruleBodyURI = Symbols.ruleBody.toString().replace("<", "").replace(">", "");
                    String ruleHeadURI = Symbols.ruleHead.toString().replace("<", "").replace(">", "");
                    String hasRuleURI = Symbols.hasRule.toString().replace("<", "").replace(">", "");
                    String ruleContent = rule.toString();
                    String[] ruleParts = ruleContent.split("\\->");
                    org.semanticweb.owlapi.model.IRI ruleIRI = org.semanticweb.owlapi.model.IRI.create(ruleURI);
                    org.semanticweb.owlapi.model.IRI ruleNameIRI = org.semanticweb.owlapi.model.IRI.create(ruleNameURI);
                    org.semanticweb.owlapi.model.IRI ruleBodyIRI = org.semanticweb.owlapi.model.IRI.create(ruleBodyURI);
                    org.semanticweb.owlapi.model.IRI ruleHeadIRI = org.semanticweb.owlapi.model.IRI.create(ruleHeadURI);
                    org.semanticweb.owlapi.model.IRI hasRuleIRI = org.semanticweb.owlapi.model.IRI.create(hasRuleURI);
                    OWLIndividual ruleIndividual = factory.getOWLNamedIndividual(ruleIRI);
                    OWLObjectProperty hasRule = factory.getOWLObjectProperty(hasRuleIRI);
                    OWLDataProperty nameProperty = factory.getOWLDataProperty(ruleNameIRI);
                    OWLDataProperty ruleBodyProperty = factory.getOWLDataProperty(ruleBodyIRI);
                    OWLDataProperty ruleHeadProperty = factory.getOWLDataProperty(ruleHeadIRI);
                    // add the name to the rule individual
                    axiom = factory.getOWLDataPropertyAssertionAxiom(nameProperty, ruleIndividual, ruleName);
                    manager.addAxiom(ontology, axiom);
                    // add the description to the rule individual
                    if (ruleDescription != null) {
                        axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, ruleIndividual, ruleDescription);
                        manager.addAxiom(ontology, axiom);
                    }
                    // add the rule body to the rule individual
                    axiom = factory.getOWLDataPropertyAssertionAxiom(ruleBodyProperty, ruleIndividual, ruleParts[0]);
                    manager.addAxiom(ontology, axiom);
                    // add the rule head to the rule individual
                    axiom = factory.getOWLDataPropertyAssertionAxiom(ruleHeadProperty, ruleIndividual, ruleParts[1]);
                    manager.addAxiom(ontology, axiom);
                    // bind the rule to the recipe
                    axiom = factory.getOWLObjectPropertyAssertionAxiom(hasRule, recipeIndividual, ruleIndividual);
                    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.apache.clerezza.commons.rdf.IRI) ManchesterOWLSyntaxOntologyFormat(org.coode.owlapi.manchesterowlsyntax.ManchesterOWLSyntaxOntologyFormat) RuleList(org.apache.stanbol.rules.base.api.util.RuleList) 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) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) Rule(org.apache.stanbol.rules.base.api.Rule) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Aggregations

OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)20 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)10 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)9 IRI (org.semanticweb.owlapi.model.IRI)7 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)6 OWLClass (org.semanticweb.owlapi.model.OWLClass)5 OWLNamedIndividual (org.semanticweb.owlapi.model.OWLNamedIndividual)5 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 RegistryContentException (org.apache.stanbol.ontologymanager.registry.api.RegistryContentException)4 Logger (org.slf4j.Logger)4 LinkedList (java.util.LinkedList)3 Graph (org.apache.clerezza.commons.rdf.Graph)3 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)3 OWLClassExpression (org.semanticweb.owlapi.model.OWLClassExpression)3 Atom (fr.lirmm.graphik.graal.api.core.Atom)2 Term (fr.lirmm.graphik.graal.api.core.Term)2 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)2 File (java.io.File)2