Search in sources :

Example 11 with OWLDataFactory

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

the class SameAtom method adapt.

@SuppressWarnings("unchecked")
@Override
public <T> T adapt(RuleAtom ruleAtom) throws RuleAtomCallExeption, UnavailableRuleObjectException, UnsupportedTypeForExportException {
    org.apache.stanbol.rules.manager.atoms.SameAtom tmp = (org.apache.stanbol.rules.manager.atoms.SameAtom) ruleAtom;
    ExpressionAtom argument1 = tmp.getStringFunctionAtom1();
    ExpressionAtom argument2 = tmp.getStringFunctionAtom2();
    OWLDataFactory factory = OWLManager.getOWLDataFactory();
    SWRLAtom swrlAtom1 = (SWRLAtom) adapter.adaptTo(argument1, SWRLRule.class);
    SWRLAtom swrlAtom2 = (SWRLAtom) adapter.adaptTo(argument2, SWRLRule.class);
    SWRLDArgument swrldArgument1;
    SWRLDArgument swrldArgument2;
    List<SWRLAtom> listOfArguments = new ArrayList<SWRLAtom>();
    if (swrlAtom1 instanceof HigherOrderSWRLAtom) {
        swrldArgument1 = ((HigherOrderSWRLAtom) swrlAtom1).getBindableArgument();
        listOfArguments.addAll(((HigherOrderSWRLAtom) swrlAtom1).getAtoms());
    } else if (swrlAtom1 instanceof ArgumentSWRLAtom) {
        SWRLArgument swrlArgument = ((ArgumentSWRLAtom) swrlAtom1).getSwrlArgument();
        swrldArgument1 = (SWRLDArgument) swrlArgument;
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    if (swrlAtom2 instanceof HigherOrderSWRLAtom) {
        swrldArgument2 = ((HigherOrderSWRLAtom) swrlAtom2).getBindableArgument();
        listOfArguments.addAll(((HigherOrderSWRLAtom) swrlAtom2).getAtoms());
    } else if (swrlAtom2 instanceof ArgumentSWRLAtom) {
        SWRLArgument swrlArgument = ((ArgumentSWRLAtom) swrlAtom2).getSwrlArgument();
        swrldArgument2 = (SWRLDArgument) swrlArgument;
    } else {
        throw new org.apache.stanbol.rules.base.api.RuleAtomCallExeption(getClass());
    }
    List<SWRLDArgument> swrldArguments = new ArrayList<SWRLDArgument>();
    swrldArguments.add(swrldArgument1);
    swrldArguments.add(swrldArgument2);
    SWRLBuiltInAtom swrlBuiltInAtom = factory.getSWRLBuiltInAtom(SWRLBuiltInsVocabulary.EQUAL.getIRI(), swrldArguments);
    return (T) swrlBuiltInAtom;
}
Also used : ArrayList(java.util.ArrayList) ArgumentSWRLAtom(org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom) RuleAtomCallExeption(org.apache.stanbol.rules.base.api.RuleAtomCallExeption) ExpressionAtom(org.apache.stanbol.rules.manager.atoms.ExpressionAtom) HigherOrderSWRLAtom(org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom) SWRLDArgument(org.semanticweb.owlapi.model.SWRLDArgument) SWRLAtom(org.semanticweb.owlapi.model.SWRLAtom) ArgumentSWRLAtom(org.apache.stanbol.rules.adapters.swrl.ArgumentSWRLAtom) HigherOrderSWRLAtom(org.apache.stanbol.rules.adapters.swrl.HigherOrderSWRLAtom) SWRLBuiltInAtom(org.semanticweb.owlapi.model.SWRLBuiltInAtom) SWRLRule(org.semanticweb.owlapi.model.SWRLRule) SWRLArgument(org.semanticweb.owlapi.model.SWRLArgument) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 12 with OWLDataFactory

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

the class DifferentAtom method getOWLTypedLiteral.

private OWLLiteral getOWLTypedLiteral(Object argument) {
    OWLDataFactory factory = OWLManager.createOWLOntologyManager().getOWLDataFactory();
    OWLLiteral owlLiteral;
    if (argument instanceof String) {
        owlLiteral = factory.getOWLTypedLiteral((String) argument);
    } else if (argument instanceof Integer) {
        owlLiteral = factory.getOWLTypedLiteral(((Integer) argument).intValue());
    } else if (argument instanceof Double) {
        owlLiteral = factory.getOWLTypedLiteral(((Double) argument).doubleValue());
    } else if (argument instanceof Float) {
        owlLiteral = factory.getOWLTypedLiteral(((Float) argument).floatValue());
    } else if (argument instanceof Boolean) {
        owlLiteral = factory.getOWLTypedLiteral(((Boolean) argument).booleanValue());
    } else {
        owlLiteral = factory.getOWLStringLiteral(argument.toString());
    }
    return owlLiteral;
}
Also used : OWLLiteral(org.semanticweb.owlapi.model.OWLLiteral) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory)

Example 13 with OWLDataFactory

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

the class SessionManagerResource method listSessions.

@GET
@Produces(value = { RDF_XML, OWL_XML, TURTLE, X_TURTLE, FUNCTIONAL_OWL, MANCHESTER_OWL, RDF_JSON, N3, N_TRIPLE, TEXT_PLAIN })
public Response listSessions(@Context UriInfo uriInfo, @Context HttpHeaders headers) {
    OWLOntologyManager ontMgr = OWLManager.createOWLOntologyManager();
    OWLDataFactory df = ontMgr.getOWLDataFactory();
    OWLClass cSession = df.getOWLClass(IRI.create("http://stanbol.apache.org/ontologies/meta/Session"));
    OWLOntology o;
    try {
        o = ontMgr.createOntology(IRI.create(uriInfo.getRequestUri()));
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        for (String id : sessionManager.getRegisteredSessionIDs()) {
            IRI sessionid = IRI.create(sessionManager.getDefaultNamespace() + sessionManager.getID() + "/" + id);
            OWLNamedIndividual ind = df.getOWLNamedIndividual(sessionid);
            changes.add(new AddAxiom(o, df.getOWLClassAssertionAxiom(cSession, ind)));
        }
        ontMgr.applyChanges(changes);
    } catch (OWLOntologyCreationException e) {
        throw new WebApplicationException(e, INTERNAL_SERVER_ERROR);
    }
    ResponseBuilder rb = Response.ok(o);
    MediaType mediaType = MediaTypeUtil.getAcceptableMediaType(headers, null);
    if (mediaType != null)
        rb.header(HttpHeaders.CONTENT_TYPE, mediaType);
    //        addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IRI(org.semanticweb.owlapi.model.IRI) AddAxiom(org.semanticweb.owlapi.model.AddAxiom) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLNamedIndividual(org.semanticweb.owlapi.model.OWLNamedIndividual) MediaType(javax.ws.rs.core.MediaType) OWLClass(org.semanticweb.owlapi.model.OWLClass) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 14 with OWLDataFactory

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

the class AbstractOntologyCollectorImpl method getOntologyAsOWLOntology.

protected OWLOntology getOntologyAsOWLOntology(OWLOntologyID ontologyId, boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    // if (merge) throw new UnsupportedOperationException("Merge not implemented yet for OWLOntology.");
    // Remove the check below. It might be an unmanaged dependency (TODO remove from collector and
    // reintroduce check?).
    // if (!hasOntology(ontologyIri)) return null;
    OWLOntology o;
    o = ontologyProvider.getStoredOntology(ontologyId, OWLOntology.class, merge);
    if (merge) {
        final Set<OWLOntology> set = new HashSet<OWLOntology>();
        log.debug("Merging {} with its imports, if any.", o);
        set.add(o);
        // Actually, if the provider already performed the merge, this won't happen
        for (OWLOntology impo : o.getImportsClosure()) {
            log.debug("Imported ontology {} will be merged with {}.", impo, o);
            set.add(impo);
        }
        OWLOntologySetProvider provider = new OWLOntologySetProvider() {

            @Override
            public Set<OWLOntology> getOntologies() {
                return set;
            }
        };
        OWLOntologyMerger merger = new OWLOntologyMerger(provider);
        try {
            o = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), ontologyId.getOntologyIRI());
        } catch (OWLOntologyCreationException e) {
            log.error("Failed to merge imports for ontology " + ontologyId, e);
        // do not reassign the root ontology
        }
    } else {
        // Rewrite import statements
        List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>();
        OWLDataFactory df = OWLManager.getOWLDataFactory();
        /*
             * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
             * going upOne() add "session" or "ontology" if needed). But only do this if we keep considering
             * imported ontologies as *not* managed.
             */
        for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) {
            changes.add(new RemoveImport(o, oldImp));
            String s = oldImp.getIRI().toString();
            // FIXME Ugly way to check, but we'll get through with it
            if (s.contains("::"))
                s = s.substring(s.indexOf("::") + 2, s.length());
            boolean managed = managedOntologies.contains(oldImp.getIRI());
            // For space, always go up at least one
            String tid = getID();
            if (backwardPathLength > 0)
                tid = tid.split("/")[0];
            org.semanticweb.owlapi.model.IRI target = org.semanticweb.owlapi.model.IRI.create((managed ? universalPrefix + "/" + tid + "/" : URIUtils.upOne(universalPrefix) + "/") + s);
            changes.add(new AddImport(o, df.getOWLImportsDeclaration(target)));
        }
        o.getOWLOntologyManager().applyChanges(changes);
    }
    return o;
}
Also used : ArrayList(java.util.ArrayList) OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) OWLImportsDeclaration(org.semanticweb.owlapi.model.OWLImportsDeclaration) AddImport(org.semanticweb.owlapi.model.AddImport) RemoveImport(org.semanticweb.owlapi.model.RemoveImport) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

Example 15 with OWLDataFactory

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

the class AbstractOntologyCollectorImpl method exportToOWLOntology.

/**
     * This method has no conversion calls, to it can be invoked by subclasses that wish to modify it
     * afterwards.
     * 
     * FIXME not merging yet FIXME not including imported ontologies unless they are merged *before* storage.
     * 
     * @param merge
     * @return
     */
protected OWLOntology exportToOWLOntology(boolean merge, org.semanticweb.owlapi.model.IRI prefix) {
    long before = System.currentTimeMillis();
    // Create a new ontology
    OWLOntology root;
    OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
    org.semanticweb.owlapi.model.IRI iri = org.semanticweb.owlapi.model.IRI.create(prefix + _id);
    try {
        root = ontologyManager.createOntology(iri);
    } catch (OWLOntologyAlreadyExistsException e) {
        // It should be impossible, but just in case.
        ontologyManager.removeOntology(ontologyManager.getOntology(iri));
        try {
            root = ontologyManager.createOntology(iri);
        } catch (OWLOntologyAlreadyExistsException e1) {
            root = ontologyManager.getOntology(iri);
        } catch (OWLOntologyCreationException e1) {
            log.error("Failed to assemble root ontology for scope " + iri, e);
            root = null;
        }
    } catch (OWLOntologyCreationException e) {
        log.error("Failed to assemble root ontology for scope " + _id, e);
        root = null;
    }
    // Add the import declarations for directly managed ontologies.
    if (root != null) {
        if (merge) {
            final Set<OWLOntology> set = new HashSet<OWLOntology>();
            log.debug("Merging {} with its imports.", root);
            set.add(root);
            for (OWLOntologyID ontologyId : managedOntologies) {
                log.debug("Merging {} with {}.", ontologyId, root);
                set.add(getOntology(ontologyId, OWLOntology.class, true));
            }
            OWLOntologySetProvider provider = new OWLOntologySetProvider() {

                @Override
                public Set<OWLOntology> getOntologies() {
                    return set;
                }
            };
            OWLOntologyMerger merger = new OWLOntologyMerger(provider);
            try {
                root = merger.createMergedOntology(OWLManager.createOWLOntologyManager(), iri);
            } catch (OWLOntologyCreationException e) {
                log.error("Failed to merge imports for ontology " + iri, e);
                root = null;
            }
        } else {
            // Add the import declarations for directly managed ontologies.
            List<OWLOntologyChange> changes = new LinkedList<OWLOntologyChange>();
            OWLDataFactory df = ontologyManager.getOWLDataFactory();
            String base = prefix + getID();
            for (int i = 0; i < backwardPathLength; i++) base = URIUtils.upOne(URI.create(base)).toString();
            base += "/";
            // The key set of managedOntologies contains the ontology IRIs, not their storage keys.
            for (OWLOntologyID ontologyId : managedOntologies) {
                // XXX some day the versionIRI will be the only physical reference for the ontology
                org.semanticweb.owlapi.model.IRI physIRI = org.semanticweb.owlapi.model.IRI.create(base + OntologyUtils.encode(ontologyId));
                changes.add(new AddImport(root, df.getOWLImportsDeclaration(physIRI)));
            }
            ontologyManager.applyChanges(changes);
        }
    }
    log.debug("OWL export of {} completed in {} ms.", getID(), System.currentTimeMillis() - before);
    return root;
}
Also used : OWLOntologyMerger(org.semanticweb.owlapi.util.OWLOntologyMerger) AddImport(org.semanticweb.owlapi.model.AddImport) OWLOntologyAlreadyExistsException(org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException) LinkedList(java.util.LinkedList) OWLOntologySetProvider(org.semanticweb.owlapi.model.OWLOntologySetProvider) OWLOntologyCreationException(org.semanticweb.owlapi.model.OWLOntologyCreationException) OWLOntologyChange(org.semanticweb.owlapi.model.OWLOntologyChange) OWLOntology(org.semanticweb.owlapi.model.OWLOntology) OWLOntologyID(org.semanticweb.owlapi.model.OWLOntologyID) OWLOntologyManager(org.semanticweb.owlapi.model.OWLOntologyManager) OWLDataFactory(org.semanticweb.owlapi.model.OWLDataFactory) HashSet(java.util.HashSet)

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