Search in sources :

Example 1 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager in project webprotege by protegeproject.

the class RootOntologyLoader method loadRootOntology.

public OWLOntology loadRootOntology() {
    // The delegate - we use the concurrent ontology manager
    OWLOntologyManager delegateManager = WebProtegeOWLManager.createConcurrentOWLOntologyManager();
    // We only support the binary format for speed
    delegateManager.getOntologyStorers().add(new RDFXMLStorerFactory());
    delegateManager.getOntologyStorers().add(new OWLXMLStorerFactory());
    delegateManager.getOntologyStorers().add(new FunctionalSyntaxStorerFactory());
    delegateManager.getOntologyStorers().add(new ManchesterSyntaxStorerFactory());
    delegateManager.getOntologyParsers().add(new BinaryOWLOntologyDocumentParserFactory());
    // The wrapper manager
    ProjectOWLOntologyManager manager = new ProjectOWLOntologyManager();
    manager.setDelegate(delegateManager);
    int threadPriority = Thread.currentThread().getPriority();
    try {
        Thread.currentThread().setPriority(3);
        OWLOntology rootOntology = documentStore.initialiseOntologyManagerWithProject(manager.getDelegate());
        manager.sealDelegate();
        return rootOntology;
    } catch (OWLOntologyCreationException | OWLOntologyStorageException e) {
        throw new RuntimeException("Failed to load project: " + e.getMessage(), e);
    } finally {
        Thread.currentThread().setPriority(threadPriority);
    }
}
Also used : BinaryOWLOntologyDocumentParserFactory(org.semanticweb.binaryowl.owlapi.BinaryOWLOntologyDocumentParserFactory) ManchesterSyntaxStorerFactory(org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterSyntaxStorerFactory) FunctionalSyntaxStorerFactory(org.semanticweb.owlapi.functional.renderer.FunctionalSyntaxStorerFactory) RDFXMLStorerFactory(org.semanticweb.owlapi.rdf.rdfxml.renderer.RDFXMLStorerFactory) OWLXMLStorerFactory(org.semanticweb.owlapi.owlxml.renderer.OWLXMLStorerFactory) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 2 with OWLOntologyManager

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

the class DefaultGWASOWLPublisher method saveGWASDataInferredView.

public void saveGWASDataInferredView(OWLReasoner reasoner, File outputFile) throws OWLConversionException {
    try {
        // create new ontology to hold inferred axioms
        OWLOntology inferredOntology = getConverter().createConversionOntology();
        getLog().info("Saving inferred view...");
        List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
        // we require all inferred stuff except for disjoints...
        gens.add(new InferredClassAssertionAxiomGenerator());
        gens.add(new InferredDataPropertyCharacteristicAxiomGenerator());
        gens.add(new InferredEquivalentClassAxiomGenerator());
        gens.add(new InferredEquivalentDataPropertiesAxiomGenerator());
        gens.add(new InferredEquivalentObjectPropertyAxiomGenerator());
        gens.add(new InferredInverseObjectPropertiesAxiomGenerator());
        gens.add(new InferredObjectPropertyCharacteristicAxiomGenerator());
        gens.add(new InferredPropertyAssertionGenerator());
        gens.add(new InferredSubClassAxiomGenerator());
        gens.add(new InferredSubDataPropertyAxiomGenerator());
        gens.add(new InferredSubObjectPropertyAxiomGenerator());
        // now create the target ontology and save
        OWLOntologyManager inferredManager = inferredOntology.getOWLOntologyManager();
        InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens);
        iog.fillOntology(inferredManager, inferredOntology);
        inferredManager.saveOntology(inferredOntology, IRI.create(outputFile));
        getLog().info("Inferred view saved ok");
    } catch (OWLOntologyStorageException e) {
        throw new OWLConversionException("Failed to save GWAS data (inferred view)", e);
    }
}
Also used : InferredSubClassAxiomGenerator(org.semanticweb.owlapi.util.InferredSubClassAxiomGenerator) InferredDataPropertyCharacteristicAxiomGenerator(org.semanticweb.owlapi.util.InferredDataPropertyCharacteristicAxiomGenerator) InferredEquivalentDataPropertiesAxiomGenerator(org.semanticweb.owlapi.util.InferredEquivalentDataPropertiesAxiomGenerator) InferredPropertyAssertionGenerator(org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator) InferredObjectPropertyCharacteristicAxiomGenerator(org.semanticweb.owlapi.util.InferredObjectPropertyCharacteristicAxiomGenerator) InferredInverseObjectPropertiesAxiomGenerator(org.semanticweb.owlapi.util.InferredInverseObjectPropertiesAxiomGenerator) InferredAxiomGenerator(org.semanticweb.owlapi.util.InferredAxiomGenerator) ArrayList(java.util.ArrayList) InferredSubDataPropertyAxiomGenerator(org.semanticweb.owlapi.util.InferredSubDataPropertyAxiomGenerator) InferredEquivalentClassAxiomGenerator(org.semanticweb.owlapi.util.InferredEquivalentClassAxiomGenerator) OWLConversionException(uk.ac.ebi.spot.goci.exception.OWLConversionException) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) InferredClassAssertionAxiomGenerator(org.semanticweb.owlapi.util.InferredClassAssertionAxiomGenerator) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) InferredSubObjectPropertyAxiomGenerator(org.semanticweb.owlapi.util.InferredSubObjectPropertyAxiomGenerator) InferredEquivalentObjectPropertyAxiomGenerator(org.semanticweb.owlapi.util.InferredEquivalentObjectPropertyAxiomGenerator) InferredOntologyGenerator(org.semanticweb.owlapi.util.InferredOntologyGenerator) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 3 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager 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 4 with OWLOntologyManager

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

the class KBLoader method quantifyKnowledgeBase.

public Map<IRI, Integer> quantifyKnowledgeBase(URL efoLocation, URL gwasSchemaLocation, URL kbLocation) throws OWLOntologyCreationException, URISyntaxException {
    Map<IRI, Integer> results = new HashMap<IRI, Integer>();
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    // do iri mapping
    URI efoURI = efoLocation.toURI();
    URI gwasSchemaURI = gwasSchemaLocation.toURI();
    URI kbURI = kbLocation.toURI();
    getLog().info("Mapping EFO to " + efoURI);
    manager.addIRIMapper(new SimpleIRIMapper(IRI.create(OntologyConstants.EFO_ONTOLOGY_SCHEMA_IRI), IRI.create(efoURI)));
    getLog().info("Mapping GWAS schema to " + gwasSchemaURI);
    manager.addIRIMapper(new SimpleIRIMapper(IRI.create(OntologyConstants.GWAS_ONTOLOGY_SCHEMA_IRI), IRI.create(gwasSchemaURI)));
    System.out.println("Loading knowledge base " + kbURI);
    getLog().info("Loading knowledge base " + kbURI);
    // load the knowledgebase
    OWLOntology kb = manager.loadOntology(IRI.create(kbURI));
    System.out.println("Processing knowledge base");
    getLog().info("Processing knowledge base");
    // retrieve all individuals
    Set<OWLNamedIndividual> inds = kb.getIndividualsInSignature();
    System.out.println("The knowledge base contains " + inds.size() + " individuals");
    getLog().info("The knowledge base contains " + inds.size() + " individuals");
    for (OWLNamedIndividual ind : inds) {
        // for each individual, check if it is an association
        boolean isAssociation = false;
        Set<OWLClassExpression> types = ind.getTypes(kb);
        for (OWLClassExpression type : types) {
            if (type.asOWLClass().getIRI().toString().equals(OntologyConstants.TRAIT_ASSOCIATION_CLASS_IRI)) {
                isAssociation = true;
                break;
            }
        }
        if (isAssociation) {
            // get the IRI of the trait class (from EFO) this individual is associated with
            Set<IRI> traitClasses = getTraitClass(kb, ind);
            for (IRI traitClass : traitClasses) {
                // skip SNPs
                if (traitClass.toString().equals(OntologyConstants.SNP_CLASS_IRI)) {
                    continue;
                }
                // increment count
                if (results.containsKey(traitClass)) {
                    int count = results.get(traitClass) + 1;
                    results.put(traitClass, count);
                } else {
                    results.put(traitClass, 1);
                }
            }
        }
    }
    return results;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) SimpleIRIMapper(org.semanticweb.owlapi.util.SimpleIRIMapper) HashMap(java.util.HashMap) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) URI(java.net.URI) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager)

Example 5 with OWLOntologyManager

use of org.semanticweb.owlapi.model.OWLOntologyManager 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

OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)83 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)52 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)42 OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)23 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)21 OWLAxiom (org.semanticweb.owlapi.model.OWLAxiom)17 IRI (org.semanticweb.owlapi.model.IRI)14 HashSet (java.util.HashSet)13 AddImport (org.semanticweb.owlapi.model.AddImport)12 OWLClass (org.semanticweb.owlapi.model.OWLClass)12 Test (org.junit.Test)11 ReasoningServiceException (org.apache.stanbol.reasoners.servicesapi.ReasoningServiceException)10 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)10 OntModel (com.hp.hpl.jena.ontology.OntModel)9 InputStream (java.io.InputStream)9 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)9 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)9 ArrayList (java.util.ArrayList)8 InconsistentInputException (org.apache.stanbol.reasoners.servicesapi.InconsistentInputException)8 OWLOntologyChange (org.semanticweb.owlapi.model.OWLOntologyChange)8