Search in sources :

Example 56 with OWLDataFactory

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

the class RegistryManagerImpl method populateLibrary.

protected Library populateLibrary(OWLNamedObject ind, Set<OWLOntology> registries) throws RegistryContentException {
    IRI libId = ind.getIRI();
    RegistryItem lib = null;
    if (population.containsKey(libId)) {
        // We are not allowing multityping either.
        lib = population.get(libId);
        if (!(lib instanceof Library))
            throw new RegistryContentException("Inconsistent multityping: for item " + libId + " : {" + Library.class + ", " + lib.getClass() + "}");
    } else {
        lib = riFactory.createLibrary(ind);
        try {
            population.put(lib.getIRI(), lib);
        } catch (Exception e) {
            log.error("Invalid identifier for library item " + lib, e);
            return null;
        }
    }
    // EXIT nodes.
    Set<OWLNamedObject> ironts = new HashSet<OWLNamedObject>();
    OWLDataFactory df = OWLManager.getOWLDataFactory();
    for (OWLOntology o : registries) {
        if (ind instanceof OWLIndividual) {
            // Get usages of hasOntology as an object property
            for (OWLIndividual value : ((OWLIndividual) ind).getObjectPropertyValues(hasOntology, o)) if (value.isNamed())
                ironts.add(value.asOWLNamedIndividual());
            // Get usages of hasOntology as an annotation property
            for (OWLAnnotationAssertionAxiom ann : o.getAnnotationAssertionAxioms(ind.getIRI())) if (hasOntologyAnn.equals(ann.getProperty())) {
                OWLAnnotationValue value = ann.getValue();
                if (value instanceof OWLNamedObject)
                    ironts.add((OWLNamedObject) value);
                else if (value instanceof IRI)
                    ironts.add(df.getOWLNamedIndividual((IRI) value));
            }
        }
    }
    for (OWLNamedObject iront : ironts) {
        IRI childId = iront.getIRI();
        // If some populate*() method has created it, it will be there.
        RegistryItem ront = population.get(childId);
        // Otherwise populating it will also put it in population.
        if (ront == null)
            ront = populateOntology(iront, registries);
        lib.addChild(ront);
        if (ontologyIndex.get(childId) == null)
            ontologyIndex.put(childId, new HashSet<IRI>());
        ontologyIndex.get(childId).add(libId);
    }
    return (Library) lib;
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) OWLAnnotationAssertionAxiom(org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom) 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) Library(org.apache.stanbol.ontologymanager.registry.api.model.Library) 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 57 with OWLDataFactory

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

the class TestOntologySpaces method setup.

@BeforeClass
public static void setup() throws Exception {
    factory = onManager.getOntologySpaceFactory();
    if (factory == null)
        fail("Could not instantiate ontology space factory");
    OWLOntologyManager mgr = OWLOntologyManagerFactory.createOWLOntologyManager(onManager.getOfflineConfiguration().getOntologySourceLocations().toArray(new IRI[0]));
    OWLDataFactory df = mgr.getOWLDataFactory();
    ont = mgr.createOntology(baseIri);
    inMemorySrc = new RootOntologySource(ont);
    // Let's state that Linus is a human being
    OWLClass cHuman = df.getOWLClass(IRI.create(baseIri + "/" + Constants.humanBeing));
    OWLIndividual iLinus = df.getOWLNamedIndividual(IRI.create(baseIri + "/" + Constants.linus));
    linusIsHuman = df.getOWLClassAssertionAxiom(cHuman, iLinus);
    mgr.applyChange(new AddAxiom(ont, linusIsHuman));
    ont2 = mgr.createOntology(baseIri2);
    minorSrc = new RootOntologySource(ont2);
    dropSrc = getLocalSource("/ontologies/droppedcharacters.owl", mgr);
    nonexSrc = getLocalSource("/ontologies/nonexistentcharacters.owl", mgr);
    minorSrc = new RootOntologySource(ont2);
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) RootOntologySource(org.apache.stanbol.ontologymanager.sources.owlapi.RootOntologySource) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) BeforeClass(org.junit.BeforeClass)

Example 58 with OWLDataFactory

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

OWLDataFactory (org.semanticweb.owlapi.model.OWLDataFactory)58 OWLOntologyCreationException (org.semanticweb.owlapi.model.OWLOntologyCreationException)24 OWLOntologyManager (org.semanticweb.owlapi.model.OWLOntologyManager)23 OWLOntology (org.semanticweb.owlapi.model.OWLOntology)21 ArgumentSWRLAtom (org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom)18 ArrayList (java.util.ArrayList)16 SWRLAtom (org.semanticweb.owlapi.model.SWRLAtom)16 RuleAtomCallExeption (org.apache.stanbol.rules.base.api.RuleAtomCallExeption)15 SWRLDArgument (org.semanticweb.owlapi.model.SWRLDArgument)15 SWRLRule (org.semanticweb.owlapi.model.SWRLRule)15 HigherOrderSWRLAtom (org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom)12 OWLClass (org.semanticweb.owlapi.model.OWLClass)12 SWRLArgument (org.semanticweb.owlapi.model.SWRLArgument)12 IRI (org.semanticweb.owlapi.model.IRI)11 OWLLiteral (org.semanticweb.owlapi.model.OWLLiteral)11 OWLIndividual (org.semanticweb.owlapi.model.OWLIndividual)10 OWLObjectProperty (org.semanticweb.owlapi.model.OWLObjectProperty)10 HashSet (java.util.HashSet)9 ExpressionAtom (org.apache.stanbol.rules.manager.atoms.ExpressionAtom)9 OWLDataProperty (org.semanticweb.owlapi.model.OWLDataProperty)9