/** * Method that s if two elements of an ontology are subsumes. * * @param requestParameter - A request parameter. * @param serviceParameter - A service parameter. * @param typoOfParameter - It informs if the parameters are inputs or outputs. * @return true/false - return true if they are the same and false if they are different. */ @SuppressWarnings("unchecked") private boolean isSubsumesMatching( URI requestParameter, URI serviceParameter, char typeOfParameter) { Iterator iter = null; OntClass ontClass = null; loadOntology( requestParameter.getScheme() + ":" + requestParameter.getSchemeSpecificPart() + "#"); if (typeOfParameter == FunctionalMatcher.INPUT) { ontClass = model.getOntClass(serviceParameter.toString()); iter = ontClass.listSubClasses(); while (iter.hasNext()) { if (requestParameter.toString().equals(iter.next().toString())) { return true; } } } else if (typeOfParameter == FunctionalMatcher.OUTPUT) { ontClass = model.getOntClass(requestParameter.toString()); iter = ontClass.listSubClasses(); while (iter.hasNext()) { if (serviceParameter.toString().equals(iter.next().toString())) { return true; } } } return false; }
/** * Method that calculates if two elements of an ontology are exact. * * @param requestParameter - A request parameter. * @param serviceParameter - A service parameter. * @param typoOfParameter - It informs if the parameters are inputs or outputs. * @return true/false - return true if they are the same and false if they are different. */ @SuppressWarnings("unchecked") private boolean isExactMatching( URI requestParameter, URI serviceParameter, char typeOfParameter) { Iterator iterClass = null; NodeIterator iter = null; OntClass ontClass = null; loadOntology( requestParameter.getScheme() + ":" + requestParameter.getSchemeSpecificPart() + "#"); if (typeOfParameter == FunctionalMatcher.INPUT) { resource = model.getResource(ontology + requestParameter.getFragment()); iter = model.listObjectsOfProperty(resource, OWL.equivalentClass); while (iter.hasNext()) { if (serviceParameter.toString().equals(iter.next().toString())) { return true; } } ontClass = model.getOntClass(requestParameter.toString()); iterClass = ontClass.listSubClasses(true); while (iterClass.hasNext()) { if (serviceParameter.toString().equals(iterClass.next().toString())) { return true; } } } else if (typeOfParameter == FunctionalMatcher.OUTPUT) { resource = model.getResource(ontology + requestParameter.getFragment()); iter = model.listObjectsOfProperty(resource, OWL.equivalentClass); while (iter.hasNext()) { if (serviceParameter.toString().equals(iter.next().toString())) { return true; } } ontClass = model.getOntClass(serviceParameter.toString()); iterClass = ontClass.listSubClasses(true); while (iterClass.hasNext()) { if (requestParameter.toString().equals(iterClass.next().toString())) { return true; } } } if (requestParameter.toString().equals(serviceParameter.toString())) { return true; } return false; }
/** * Method that s if two elements of an ontology are siblings. * * @param requestParameter - A request parameter. * @param serviceParameter - A service parameter. * @param typoOfParameter - It informs if the parameters are inputs or outputs. * @return true/false - return true if they are the same and false if they are different. */ @SuppressWarnings("unchecked") private boolean isSiblingMatching( URI requestParameter, URI serviceParameter, char typeOfParameter) { boolean gotSuperClass = false; OntClass ontClass = null; Iterator iter = null; loadOntology( requestParameter.getScheme() + ":" + requestParameter.getSchemeSpecificPart() + "#"); ontClass = model.getOntClass(serviceParameter.toString()); iter = ontClass.listSuperClasses(true); if (iter.hasNext()) { ontClass = (OntClass) iter.next(); gotSuperClass = true; } if (gotSuperClass) { iter = ontClass.listSubClasses(true); } while (iter.hasNext()) { String next = iter.next().toString(); if (requestParameter.toString().equals(next)) { return true; } } return false; }
private List<OntClass> OntClassInheritanceChain(List<OntClass> _subClasses) { List<OntClass> removeSubClasses = new ArrayList<OntClass>(); for (OntClass jenaClass : _subClasses) { List<OntClass> subClassCopy = jenaClass.listSubClasses().toList(); for (OntClass subClass : subClassCopy) { if (_subClasses.contains(subClass)) { if (!removeSubClasses.contains(subClass)) removeSubClasses.add(subClass); } } } for (OntClass subClass : removeSubClasses) _subClasses.remove(_subClasses.lastIndexOf(subClass)); return _subClasses; }
public static void main(String args[]) { String filename = "example6.rdf"; // Create an empty model OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM); // Use the FileManager to find the input file InputStream in = FileManager.get().open(filename); if (in == null) throw new IllegalArgumentException("File: " + filename + " not found"); // Read the RDF/XML file model.read(in, null); // model.write(System.out,"Turtle"); ExtendedIterator<? extends OntResource> iterator; // ** TASK 7.1: List all individuals of "Person" ** OntClass person = model.getOntClass(ns + "Person"); iterator = person.listInstances(); System.out.println("Individuals"); System.out.println("------------------------------------"); while (iterator.hasNext()) { OntResource r = iterator.next(); System.out.println(r.getURI()); } System.out.println("------------------------------------"); // ** TASK 7.2: List all subclasses of "Person" ** iterator = person.listSubClasses(); System.out.println("Sublcasses"); System.out.println("------------------------------------"); while (iterator.hasNext()) { OntResource r = iterator.next(); System.out.println(r.getURI()); } System.out.println("------------------------------------"); // ** TASK 7.3: Make the necessary changes to get as well indirect instances and subclasses. // TIP: you need some inference... ** }
/* * (non-Javadoc) * @see net.java.treaty.ContractVocabulary#getSubtypes(java.net.URI) */ public Set<URI> getSubTypes(URI type) throws TreatyException { Set<URI> result; result = new HashSet<URI>(); OntModel model; model = getOntology(); OntClass clazz; clazz = model.getOntClass(type.toString()); for (Iterator<OntClass> iterator = clazz.listSubClasses(false); iterator.hasNext(); ) { try { result.add(new URI(iterator.next().getURI())); } catch (URISyntaxException e) { throw new TreatyException(e); } // end catch. } // end for. return result; }
private void ImportRelationsInheritanceOrEquivalence(int typeRelation) throws Exception { List<OntClass> classes = this.jena.listClasses().toList(); for (OntClass jenaClass : classes) { Class mobiClass = new Class(jenaClass.getLocalName()); // this.mobi.getClass(jenaClass.getLocalName()); if (mobiClass != null) { List<OntClass> subClasses = null; if (typeRelation == Relation.INHERITANCE) subClasses = this.OntClassInheritanceChain(jenaClass.listSubClasses().toList()); else if (typeRelation == Relation.EQUIVALENCE) subClasses = jenaClass.listEquivalentClasses().toList(); for (OntClass jenaSubClass : subClasses) { if (jenaSubClass != null && !jenaSubClass.getLocalName().equals(mobiClass.getUri()) && jenaSubClass.listInstances().hasNext()) { // this.mobi.addConcept(mobiClass); Class mobiSubClass = new Class(jenaSubClass.getLocalName()); Relation relation = null; if (typeRelation == Relation.INHERITANCE) relation = this.mobi.createInheritanceRelation("isSuper"); else if (typeRelation == Relation.EQUIVALENCE) relation = this.mobi.createEquivalenceRelation("equals"); relation.setClassA(mobiClass); relation.setClassB(mobiSubClass); List<? extends OntResource> individualsClassA = jenaClass.listInstances().toList(); List<? extends OntResource> individualsClassB = jenaSubClass.listInstances().toList(); for (OntResource resourceA : individualsClassA) { Individual individualA = resourceA.asIndividual(); Instance instanceA = new Instance(individualA.getLocalName()); // this.mobi.getInstance(individualA.getLocalName()); for (OntResource resourceB : individualsClassB) { Individual individualB = resourceB.asIndividual(); Instance instanceB = new Instance(individualB.getLocalName()); // this.mobi.getInstance(individualB.getLocalName()); if (instanceB != null && instanceA.getUri().equals(instanceB.getUri())) { this.mobi.addConcept(mobiClass); this.mobi.addConcept(mobiSubClass); this.mobi.addConcept(instanceA); this.mobi.addConcept(instanceB); try { this.mobi.isOneOf(instanceA, mobiClass); this.mobi.isOneOf(instanceB, mobiSubClass); } catch (ExceptionURI e) { } this.mobi.addConcept(instanceB); relation.addInstanceRelation(instanceA, instanceB); } } } relation.processCardinality(); if (relation.getInstanceRelationMapA().size() > 0) this.mobi.addConcept(relation); } } } } }