public Collection<OntView> getCandidates() { OntResource r = verb.getRange(); r = m().getOntClass(r.getURI()); return new each(r.asClass().listInstances()) { void $() { if (!m().contains(subject, verb, item)) add(OntView.$(item)); } }.result; }
protected boolean ontResourceExists(Iterator it, String uri) { while (it.hasNext()) { OntResource ontResource = (OntResource) it.next(); String otherURI = ontResource.getURI(); if (otherURI.equals(uri)) { return true; } } return false; }
private void buildPropertyClasses(Frame frame) { for (Field field : frame.getDeclaredFields()) { OntResource type = field.getType(); if (type.canAs(OntProperty.class)) { String uri = type.getURI(); UmlClass umlClass = addPropertyClass(type, true); } } }
public void pushEdgesInDia() { StmtIterator stmt = ontmod.getBaseModel().listStatements(null, hierarchyPattern, (Resource) null); while (stmt.hasNext()) { Statement test = stmt.next(); OntResource source = ontmod.getOntResource(test.getSubject()); OntResource target = ontmod.getOntResource(test.getObject().asResource()); if (target.isURIResource() && source.isURIResource() && dia.containsNode(source) && dia.containsNode(target)) { dia.addEdge(source, target); } } }
private void addFields(OntProperty p, OntProperty ancestor) { OntResource domainResource = ancestor.getDomain(); if (domainResource == null) { handleNullDomain(p, ancestor); return; } OntClass domain = domainResource.as(OntClass.class); List<OntResource> domainList = listUnionMembers(p, domain); if (domainList.isEmpty()) { domainList.add(domain); } for (OntResource type : domainList) { addField(type, p, ancestor); } }
public Collection<DataPropertyStatement> getDataPropertyStatementsForIndividualByDataPropertyURI( Individual entity, String datapropURI) { Collection<DataPropertyStatement> edList = new ArrayList<DataPropertyStatement>(); if (entity.getURI() == null) { return edList; } try { getOntModel().enterCriticalSection(Lock.READ); OntResource ontRes = (VitroVocabulary.PSEUDO_BNODE_NS.equals(entity.getNamespace())) ? (OntResource) getOntModel() .createResource(new AnonId(entity.getLocalName())) .as(OntResource.class) : getOntModel().getOntResource(entity.getURI()); if (ontRes == null) { return edList; } ClosableIterator stmtIt; stmtIt = (datapropURI != null) ? ontRes.listProperties(getOntModel().getProperty(datapropURI)) : ontRes.listProperties(); try { while (stmtIt.hasNext()) { Statement st = (Statement) stmtIt.next(); if (st.getObject().isLiteral()) { DataPropertyStatement ed = new DataPropertyStatementImpl(); Literal lit = (Literal) st.getObject(); fillDataPropertyStatementWithJenaLiteral(ed, lit); ed.setIndividualURI(entity.getURI()); ed.setIndividual(entity); ed.setDatapropURI(st.getPredicate().getURI()); edList.add(ed); } } } finally { stmtIt.close(); } } finally { getOntModel().leaveCriticalSection(); } return edList; }
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... ** }
private List<OntResource> listUnionMembers(OntProperty p, OntResource domain) { List<OntResource> list = new ArrayList<OntResource>(); Resource union = domain.getPropertyResourceValue(OWL.unionOf); if (union != null && union.canAs(RDFList.class)) { RDFList rdfList = union.as(RDFList.class); Iterator<RDFNode> sequence = rdfList.iterator(); while (sequence.hasNext()) { list.add(sequence.next().as(OntResource.class)); } } return list; }
/** * Call this method to convert a value (v) as a Java object to a typed Literal matching the range * of the property. * * @param m * @param prop * @param v * @return * @throws Exception */ public static synchronized Literal getLiteralMatchingDataPropertyRange( OntModel m, OntProperty prop, Object v) throws Exception { Literal val = null; String errMsg = null; if (prop.isAnnotationProperty()) { return m.createTypedLiteral(v); } // SADL only has DoubleLiterals--if this property has range float convert v to Float. OntResource rng = prop.getRange(); String rnguri = rng != null ? rng.getURI() : null; if (rng == null) { errMsg = "Range not given."; } else if (rng.isAnon()) { // this is a complex range--needs work. Try to do something with it.... // If value is a String if (v instanceof String) { v = stripQuotes((String) v); val = m.createTypedLiteral(v); } else { val = m.createTypedLiteral(v); if (val == null) { errMsg = "Range is an unsupported complex type, failed to create a Literal value for '" + v.toString() + "'."; } } } else { val = getLiteralMatchingDataPropertyRange(m, rnguri, v); } if (errMsg != null) { errMsg += " (Property is '" + prop.getLocalName() + "'.)"; throw new Exception(errMsg); } return val; }
private Field createListField(Frame frame, OntProperty p, OntResource range) { Resource intersection = range.getPropertyResourceValue(OWL.intersectionOf); if (intersection == null) return null; if (intersection.canAs(RDFList.class)) { List<RDFNode> intersectionList = intersection.as(RDFList.class).asJavaList(); for (RDFNode node : intersectionList) { if (node.canAs(OntClass.class)) { OntClass intersectionMember = node.as(OntClass.class); if (RDF.first.equals(intersectionMember.getPropertyResourceValue(OWL.onProperty))) { // The intersectionMember has an owl:onProperty property whose value is rdf:first Resource elementRdfType = intersectionMember.getPropertyResourceValue(OWL.allValuesFrom); if (elementRdfType != null) { String elementTypeURI = elementRdfType.getURI(); if (elementTypeURI != null) { RdfType elementType = manager.getTypeByURI(elementTypeURI); if (elementType != null) { ListType listType = manager.getListTypeByElementUri(elementTypeURI); if (listType == null) { listType = new ListType(manager, intersectionMember, elementType); manager.add(listType); } return new Field(frame, p, listType); } } } } } } } return null; }
private UmlClass addPropertyClass(OntResource type, boolean addSuperProperties) { String uri = type.getURI(); UmlClass umlClass = uri2Class.get(uri); if (umlClass == null) { Frame propertyFrame = typeManager.getFrameByUri(uri); if (propertyFrame == null) { if (!type.canAs(OntClass.class)) { type.addProperty(RDF.type, OWL.Class); } propertyFrame = new Frame(typeManager, type.asClass()); typeManager.add(propertyFrame); } umlClass = new UmlClass(propertyFrame, this); uri2Class.put(uri, umlClass); addSubProperties(umlClass, type.asProperty()); } if (addSuperProperties) { addSuperProperties(umlClass, type.asProperty()); } return umlClass; }
private void addField(OntResource type, OntProperty p, OntProperty ancestor) { int minCardinality = 0; int maxCardinality = -1; OntResource range = null; String typeURI = type.getURI(); if (typeURI == null) { // We only add fields to named types. return; } // Do not add abstract properties. if (isAbstract(p)) return; Frame frame = manager.getFrameByUri(typeURI); if (frame == null) { if (isStandard(typeURI)) return; logger.warn( "Ignoring property " + p.getLocalName() + " on class " + type.getLocalName() + ": frame not found"); return; } if (frame.getDeclaredFieldByPropertyURI(p.getURI()) != null) return; if (p.hasRDFType(OWL.FunctionalProperty)) { maxCardinality = 1; } OntClass restriction = frame.getRestriction(p.getURI()); range = p.getRange(); if (range == null && ancestor != null) { range = ancestor.getRange(); } if (range == null) { // logger.warn("Ignoring property " + p.getLocalName() + " on class " + // type.getLocalName() + ": range not defined"); // return; range = THING; } if (restriction != null) { Resource onClass = restriction.getPropertyResourceValue(OWL2.onClass); if (onClass != null) { range = onClass.as(OntResource.class); if (restriction.hasProperty(OWL2.minQualifiedCardinality)) { minCardinality = restriction.getProperty(OWL2.minQualifiedCardinality).getInt(); } if (restriction.hasProperty(OWL2.maxQualifiedCardinality)) { maxCardinality = restriction.getProperty(OWL2.maxQualifiedCardinality).getInt(); } } else { if (restriction.hasProperty(OWL.minCardinality)) { minCardinality = restriction.getProperty(OWL.minCardinality).getInt(); } if (restriction.hasProperty(OWL.maxCardinality)) { maxCardinality = restriction.getProperty(OWL.maxCardinality).getInt(); } } } Field field = null; String rangeURI = range.getURI(); if (rangeURI == null) { field = createListField(frame, p, range); if (field == null) { logger.warn( "Ignoring property " + p.getLocalName() + " on class " + type.getLocalName() + ": range has no URI"); return; } } else { field = new Field(frame, p, range, minCardinality, maxCardinality); if (field.getRdfType() == null) { logger.warn( "Failed to create RdfType for field " + field.getLocalName() + " of type " + field.getType().getURI()); } } Resource rawInverse = p.getPropertyResourceValue(OWL.inverseOf); if (rawInverse != null && rawInverse.canAs(OntProperty.class)) { field.setInverseOf(rawInverse.as(OntProperty.class)); } frame.getDeclaredFields().add(field); }
/** * Returns the natural language representation of the given resource * * @param r Ontology resource * @return String NL-representation */ public String getNLExpression(OntResource r) { String str = reader.getNLExpression(r); if (str != null) // if the nl-expr. has been defined in the ontology, use that return str; return getNLExpression(r.getLocalName()); }
/** * Sorts the properties in compulsory and optional by submenu and alphabetical order. Also * specifies which properties can be removed. */ private void sort(boolean query) { comp = new String[compulsory.size()]; compNL = new String[compulsory.size()]; Map<String, String> map = new HashMap<String, String>(); for (int i = 0; i < compulsory.size(); i++) { Object o = compulsory.get(i); if (o instanceof String) { comp[i] = (String) o; compNL[i] = getNLExpression(comp[i]); } else if (o instanceof OntResource) { OntResource r = (OntResource) o; comp[i] = r.getLocalName(); compNL[i] = getNLExpression(r); } map.put(compNL[i], comp[i]); } Arrays.sort( compNL); // sort the nl-expressions, then make sure the original names are in the same // sequence for (int i = 0; i < compNL.length; i++) comp[i] = map.get(compNL[i]); Map<String, List<OntProperty>> menuMap = getMenus(); String[] menuOrder = (String[]) menuMap.keySet().toArray(new String[0]); Arrays.sort(menuOrder); // sort the menu's alphabetically op = new ArrayList<String>(); opNL = new ArrayList<String>(); optionalPropType = new ArrayList<String>(); for (int i = 0; i < menuOrder.length; i++) { if (!menuMap.containsKey(menuOrder[i])) continue; List<OntProperty> submenu = menuMap.get(menuOrder[i]); map = new HashMap<String, String>(); String[] nlexpr = new String[submenu.size()]; for (int j = 0; j < submenu.size(); j++) { String name = submenu.get(j).getLocalName(); nlexpr[j] = getNLExpression(submenu.get(j)); map.put(nlexpr[j], name); } Arrays.sort(nlexpr); for (int j = 0; j < nlexpr.length; j++) { opNL.add(nlexpr[j]); op.add(map.get(nlexpr[j])); optionalPropType.add(menuOrder[i]); } } // collect all edges the node has (and their values) to offer them as 'undo' options removableMap = new HashMap<String, List<String>>(); map = new HashMap<String, String>(); for (Iterator it = node.getOutgoingEdges(); it.hasNext(); ) { SGEdge edge = (SGEdge) it.next(); if (!edge.isRemovable()) continue; String property = edge.getLabel(); List<String> values = new ArrayList<String>(); if (edge.getTarget() instanceof QueryValueNode) values = ((QueryValueNode) edge.getTarget()).getChoiceLabels(reader); else values.add(edge.getTarget().getChoiceLabel(query, reader)); if (removableMap.containsKey(property)) removableMap.get(property).addAll(values); else { map.put(getNLExpression(property), property); removableMap.put(property, values); } } removableNL = new String[map.size()]; int cntr = 0; for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { removableNL[cntr] = (String) it.next(); cntr++; } Arrays.sort(removableNL); removable = new String[removableNL.length]; for (int i = 0; i < removableNL.length; i++) removable[i] = map.get(removableNL[i]); }
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); } } } } }
private void ImportRelationsComposition() throws Exception { List<ObjectProperty> properties = this.jena.listObjectProperties().toList(); for (ObjectProperty property : properties) { OntClass domain = property.listDomain().next().asClass(); if (domain.isUnionClass()) domain = domain.asUnionClass().listOperands().toList().get(0); OntClass range = property.listRange().next().asClass(); if (range.isUnionClass()) range = range.asUnionClass().listOperands().toList().get(0); Relation r = null; if (property.isSymmetricProperty()) r = this.mobi.createSymmetricRelation( this.getNameObjectProperty( property.getLocalName(), domain.getLocalName().length(), range.getLocalName().length())); else if (property.getInverse() == null) r = this.mobi.createUnidirecionalCompositionRelationship( this.getNameObjectProperty( property.getLocalName(), domain.getLocalName().length(), range.getLocalName().length())); else if (property.getInverse() != null) r = this.mobi.createBidirecionalCompositionRelationship( this.getNameObjectProperty( property.getLocalName(), domain.getLocalName().length(), range.getLocalName().length()), this.getNameObjectProperty( property.getInverse().getLocalName(), range.getLocalName().length(), domain.getLocalName().length())); Class mobiDomain = new Class(domain.getLocalName()); // this.mobi.getClass(domain.getLocalName()); Class mobiRange = new Class(range.getLocalName()); // this.mobi.getClass(range.getLocalName()); if (mobiDomain != null && mobiRange != null) { r.setClassA(mobiDomain); r.setClassB(mobiRange); List<? extends OntResource> individuals = domain.listInstances().toList(); for (OntResource resourceIndividual : individuals) { Individual individualDomain = resourceIndividual.asIndividual(); NodeIterator propertyValues = this.jena.getIndividual(individualDomain.getURI()).listPropertyValues(property); while (propertyValues.hasNext()) { RDFNode node = propertyValues.next(); Individual individualValue = node.as(Individual.class); this.mobi.addConcept(mobiDomain); this.mobi.addConcept(mobiRange); Instance instanceDomain = new Instance(individualDomain.getLocalName()); Instance instanceRange = new Instance(individualValue.getLocalName()); try { this.mobi.isOneOf(instanceDomain, mobiDomain); this.mobi.isOneOf(instanceRange, mobiRange); } catch (ExceptionURI e) { } r.addInstanceRelation(instanceDomain, instanceRange); } } r.processCardinality(); if (r.getInstanceRelationMapA().size() > 0) this.mobi.addConcept(r); } } }