Search in sources :

Example 6 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 7 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)

Example 8 with OWLIndividual

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

the class RecipeListWriter method writeTo.

@Override
public void writeTo(RecipeList recipeList, 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 descriptionURI = Symbols.description.toString().replace("<", "").replace(">", "");
        IRI descriptionIRI = IRI.create(descriptionURI);
        OWLDataProperty descriptionProperty = factory.getOWLDataProperty(descriptionIRI);
        if (recipeList != null) {
            log.info("Converting the recipe list to OWL.");
            for (Recipe recipe : recipeList) {
                String recipeURI = recipe.getRecipeID().toString().replace("<", "").replace(">", "");
                IRI recipeIRI = IRI.create(recipeURI);
                OWLIndividual recipeIndividual = factory.getOWLNamedIndividual(recipeIRI);
                OWLAxiom axiom = factory.getOWLClassAssertionAxiom(owlRecipeClass, recipeIndividual);
                manager.addAxiom(ontology, axiom);
                String description = recipe.getRecipeDescription();
                if (description != null) {
                    axiom = factory.getOWLDataPropertyAssertionAxiom(descriptionProperty, recipeIndividual, description);
                    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) Recipe(org.apache.stanbol.rules.base.api.Recipe) OWLFunctionalSyntaxOntologyFormat(org.semanticweb.owlapi.io.OWLFunctionalSyntaxOntologyFormat) RDFXMLOntologyFormat(org.semanticweb.owlapi.io.RDFXMLOntologyFormat) Logger(org.slf4j.Logger) 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) OWLAxiom(org.semanticweb.owlapi.model.OWLAxiom) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual) OWLOntologyStorageException(org.semanticweb.owlapi.model.OWLOntologyStorageException)

Example 9 with OWLIndividual

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

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

the class OntologyNetworkConfigurationUtils method getScopes.

/**
 * To get all the instances of Scope in this configuration
 *
 * @param config
 * @return
 */
public static String[] getScopes(OWLOntology config) {
    Set<OWLIndividual> scopes = cScope.getIndividuals(config);
    List<String> result = new ArrayList<String>();
    for (OWLIndividual iScope : scopes) {
        for (OWLClassExpression sce : iScope.getTypes(config)) {
            if (sce.containsConjunct(cScope)) {
                if (iScope.isNamed()) {
                    result.add(((OWLNamedIndividual) iScope).getIRI().toString());
                }
            }
        }
    }
    return result.toArray(EMPTY_IRI_ARRAY);
}
Also used : ArrayList(java.util.ArrayList) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) OWLClassExpression(org.semanticweb.owlapi.model.OWLClassExpression) OWLIndividual(org.semanticweb.owlapi.model.OWLIndividual)

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