private float execute(OntClass c1, OntClass c2) { int max = 0, min = 0; Collection<OntProperty> listaProps2 = OntologyCache.getOrAddListProperties(c2); for (Iterator i = OntologyCache.getOrAddListProperties(c1).iterator(); i.hasNext(); ) { OntProperty p1 = (OntProperty) i.next(); if (p1.isDatatypeProperty()) { max += 1; for (OntProperty p2 : listaProps2) { if (p2.isDatatypeProperty()) { SimilarityVO similarityVO = new SimilarityVO(); similarityVO.setElementA(p1); similarityVO.setElementB(p2); similarityVO.setSimilarity( strategyEditDistance.compute(p1.getLocalName(), p2.getLocalName())); similaridade.add(similarityVO); } } } } // Loop para pegar o numero de datatypeproperty de c2 for (OntProperty p2 : listaProps2) if (p2.isDatatypeProperty()) min += 1; similaridade = strategyCalculation.explore(similaridade); return averageWithPenalty(sumSimilarities(), max, min); }
private float execute(OntProperty c1, OntProperty c2) { float similarity = strategyEditDistance.compute(c1.getLocalName(), c2.getLocalName()); SimilarityVO similarityVO = new SimilarityVO(); similarityVO.setElementA(c1); similarityVO.setElementB(c2); similarityVO.setSimilarity(similarity); similaridade.add(similarityVO); return similarity; }
/** * List of all subproperties of the predicate parameter * * @param prop prefix namespace : name of the propoety * @return list<prefix:prop_name> */ public List<String> listSubProp(String prop) { List<String> l = new ArrayList<String>(); ExtendedIterator<OntProperty> listAllOntProperties = ontologie.listAllOntProperties(); while (listAllOntProperties.hasNext()) { OntProperty next = listAllOntProperties.next(); if (next.getLocalName().equals(prop.split(":")[1]) && next.getNameSpace().equals(prop.split(":")[0])) { ExtendedIterator<? extends OntProperty> listSubProperties = next.listSubProperties(); while (listSubProperties.hasNext()) { OntProperty next2 = listSubProperties.next(); l.add(next2.getNameSpace() + ":" + next2.getLocalName()); } } } return l; }
/** * Checks whether adding p to this node would violate cardinality constraints * * @param p OntProperty * @return true if adding the property would violate a cardinality constraint */ public boolean violatesCardinality(OntProperty p) { if (p.isInverseFunctionalProperty()) { if (node.getIncomingEdges(p.getLocalName()).size() > 0) return true; } OntProperty inverse = reader.getInverse(p); if (inverse == null) return false; // if the inverse property does not exist there are no cardinal constraints on // it... if (optional.contains(inverse) || compulsory.contains(inverse)) return false; if (addShowOrHideOption() == SGNode.INCOMPLETE) return false; return true; }
/** * Initialises the anchor, finding the node's compulsory and optional properties and their NL * representations and ordering it all in lists and maps, using the submenus. * * @param c OntClass corresponding to this anchor * @param query true if this is an anchor for a node in the QueryGraph * @throws BadAnchorException if the given SGNode does not need an anchor */ public void init(OntClass c, boolean query) throws BadAnchorException { Map<String, Integer[]> map = reader.getCardinalities(c.getLocalName()); List<OntProperty> list = reader.getDomainProperties(c.getLocalName()); for (int i = 0; i < list.size(); i++) { OntProperty p = list.get(i); String name = p.getLocalName(); int nr = node.getOutgoingEdges(name).size(); // number of times the node already has this property String inverse = reader.getInverse(name); // , n.getLabel(), null); if (inverse != null) nr += node.getIncomingEdges(inverse) .size(); // plus the number of times the inverse has this node as range if (map.containsKey(name)) { int min = map.get(name)[0]; // if the minimum cardinality is not satisfied, add to 'compulsory' int max = map.get(name)[1]; if (query) optional.add(p); // for the query, cardinality constraints do not matter else if ((min != 0) && (min > nr)) // if the minimum cardinality > 0, add the property to compulsory compulsory.add(p); else if ((max == 0) || (max > nr)) // if the maximum cardinality will not be violated, add to optional optional.add(p); } else optional.add(p); // no cardinality constraints, so add to optional } if (!compulsory.isEmpty()) // if there are compulsory properties, this is a red anchor redAnchor = true; else if (optional.isEmpty()) // compulsory and optional both empty - means this is not an anchor throw new BadAnchorException( "Node " + node.getLabel() + " is not an anchor anymore; all its relations are specified"); sort(query); }
/** * 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 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); }
public void setSubPropertyOf(Properties Properties, OntModel ontologyModel) { Iterator<OntProperty> IteratorExtractedProperties = Properties.getExtractedProperty().iterator(); while (IteratorExtractedProperties.hasNext()) { OntProperty property = (OntProperty) IteratorExtractedProperties.next(); String URI = property.getURI(); if (URI != null) { try { ExtendedIterator<OntProperty> itSup = (ExtendedIterator<OntProperty>) property.listSuperProperties(true); while (itSup.hasNext()) { OntProperty propertySup = itSup.next(); String URISUP = propertySup.getURI(); if (URISUP != null) { addSubPropertyOfRelation(property, propertySup); } } } catch (Exception e) { // SPARQL Query for SubProperties String queryString = "PREFIX rdfs:<" + RDFS.getURI() + ">" + "PREFIX ont:<" + property.getNameSpace() + ">" + "SELECT ?obj " + "WHERE {" + " ont:" + property.getLocalName() + " rdfs:subPropertyOf ?obj" + " }"; // Execute Query Query query = QueryFactory.create(queryString); QueryExecution qexec = QueryExecutionFactory.create(query, ontologyModel); try { ResultSet results = qexec.execSelect(); // Temporary Model in Order to Construct Node for External Property OntModel ontologyTempModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, null); // Extract Relation for (; results.hasNext(); ) { QuerySolution soln = results.nextSolution(); Resource obj = soln.getResource("obj"); String URIObj = obj.getURI(); // Get SubPropertyOf all Property different from the current one if (URIObj != null && property.getURI() != URIObj) { OntProperty propertySup = ontologyTempModel.createOntProperty(URIObj); // Save SubPropertyOf Relation (property SubPropertyOf PropertySub) addSubPropertyOfRelation(property, propertySup); } } } finally { qexec.close(); } } } } }
@Override public UpdateContainer doIt(VWorkspace vWorkspace) throws CommandException { OntologyManager ontMgr = vWorkspace.getWorkspace().getOntologyManager(); JSONArray classesList = new JSONArray(); JSONArray classesMap = new JSONArray(); JSONArray propertiesList = new JSONArray(); JSONArray propertiesMap = new JSONArray(); Map<String, String> prefixMap = vWorkspace.getWorkspace().getOntologyManager().getPrefixMap(); ExtendedIterator<OntClass> iter = ontMgr.getOntModel().listNamedClasses(); // ExtendedIterator<DatatypeProperty> propsIter = ontMgr.getOntModel() // .listDatatypeProperties(); ExtendedIterator<OntProperty> propsIter = ontMgr.getOntModel().listAllOntProperties(); final JSONObject outputObj = new JSONObject(); try { while (iter.hasNext()) { OntClass cls = iter.next(); String pr = prefixMap.get(cls.getNameSpace()); String classLabel = cls.getLocalName(); // if (cls.getLabel(null) != null && !cls.getLabel(null).equals("")) // classLabel = cls.getLabel(null); String clsStr = (pr != null && !pr.equals("")) ? pr + ":" + classLabel : classLabel; classesList.put(clsStr); JSONObject classKey = new JSONObject(); classKey.put(clsStr, cls.getURI()); classesMap.put(classKey); } while (propsIter.hasNext()) { // DatatypeProperty prop = propsIter.next(); OntProperty prop = propsIter.next(); if (prop.isObjectProperty() && !prop.isDatatypeProperty()) continue; String pr = prefixMap.get(prop.getNameSpace()); String propLabel = prop.getLocalName(); // if (prop.getLabel(null) != null && !prop.getLabel(null).equals("")) // propLabel = prop.getLabel(null); String propStr = (pr != null && !pr.equals("")) ? pr + ":" + propLabel : propLabel; propertiesList.put(propStr); JSONObject propKey = new JSONObject(); propKey.put(propStr, prop.getURI()); propertiesMap.put(propKey); } // Populate the JSON object that will hold everything in output outputObj.put(JsonKeys.classList.name(), classesList); outputObj.put(JsonKeys.classMap.name(), classesMap); outputObj.put(JsonKeys.propertyList.name(), propertiesList); outputObj.put(JsonKeys.propertyMap.name(), propertiesMap); } catch (JSONException e) { logger.error("Error populating JSON!"); } UpdateContainer upd = new UpdateContainer( new AbstractUpdate() { @Override public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) { pw.print(outputObj.toString()); } }); return upd; }
public void createJenaModel(RegisterContextRequest rcr) { OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model entityOnt = FileManager.get().loadModel(ONT_FILE); ontModel.addSubModel(entityOnt); ontModel.setNsPrefixes(entityOnt.getNsPrefixMap()); // ontModel.loadImports(); ExtendedIterator<OntProperty> iter = ontModel.listAllOntProperties(); while (iter.hasNext()) { OntProperty ontProp = iter.next(); System.out.println(ontProp.getLocalName()); // if (formParams.containsKey(ontProp.getLocalName())) { // regIndividual.addProperty(ontProp, // ontModel.getcreateTypedLiteral(formParams.get(ontProp.getLocalName())[0])); // } } // OntClass regClass = ontModel.getOntClass(ONT_URL + "iotReg"); // OntClass entClass = ontModel.createOntClass(ONT_URL + "entity"); // OntClass regClass = (OntClass) ontModel.createOntResource(OntClass.class, // null,ONT_URL+"Registration" ); // OntClass regClass = (OntClass) ontModel.createClass(ONT_URL + "Registration"); // OntClass entityClass = (OntClass) ontModel.createClass(ONT_URL + "Entity"); OntClass entityGroup = (OntClass) ontModel.getOntClass(ONT_URL + "EntityGroup"); OntClass entity = (OntClass) ontModel.getOntClass(ONT_URL + "Entity"); OntClass attribute = (OntClass) ontModel.getOntClass(ONT_URL + "Attribute"); OntClass metadata = (OntClass) ontModel.getOntClass(ONT_URL + "Metadata"); OntClass location = (OntClass) ontModel.getOntClass(entityOnt.getNsPrefixURI("geo") + "Point"); // System.out.println("Class type is: " + regClass.getLocalName()); String ngsiValue = ""; ngsiValue = rcr.getRegistrationId(); Individual entityGroupIndiv = ontModel.createIndividual(ONT_URL + ngsiValue, entityGroup); entityGroupIndiv.setPropertyValue( ontModel.getProperty(ONT_URL + "registrationId"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getTimestamp().toString(); entityGroupIndiv.setPropertyValue( ontModel.getProperty(ONT_URL + "regTimeStamp"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getDuration(); entityGroupIndiv.setPropertyValue( ontModel.getProperty(ONT_URL + "duration"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration().get(0).getEntityId().get(0).getId(); Individual entity1 = ontModel.createIndividual(ONT_URL + ngsiValue, entity); entityGroupIndiv.setPropertyValue(ontModel.getProperty(ONT_URL + "hasEntity"), entity1); ngsiValue = rcr.getContextRegistration().get(0).getEntityId().get(0).getId(); entity1.setPropertyValue( ontModel.getProperty(ONT_URL + "id"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration().get(0).getEntityId().get(0).getType(); entity1.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getName(); Individual attribute1 = ontModel.createIndividual(ONT_URL + ngsiValue, attribute); entity1.setPropertyValue(ontModel.getProperty(ONT_URL + "hasAttribute"), attribute1); ngsiValue = rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getType(); attribute1.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(0) .getName(); Individual metadata1 = ontModel.createIndividual(ONT_URL + ngsiValue, metadata); attribute1.setPropertyValue(ontModel.getProperty(ONT_URL + "hasMetadata"), metadata1); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(0) .getType(); metadata1.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(0) .getValue() .toString(); metadata1.setPropertyValue( ontModel.getProperty(ONT_URL + "value"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(1) .getName(); Individual metadata2 = ontModel.createIndividual(ONT_URL + ngsiValue, metadata); attribute1.addProperty(ontModel.getProperty(ONT_URL + "hasMetadata"), metadata2); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(1) .getType(); metadata2.setPropertyValue( ontModel.getProperty(ONT_URL + "type"), ontModel.createLiteral(ngsiValue)); ngsiValue = rcr.getContextRegistration() .get(0) .getContextRegistrationAttribute() .get(0) .getContextMetadata() .get(1) .getValue() .toString(); metadata2.setPropertyValue( ontModel.getProperty(ONT_URL + "value"), ontModel.createLiteral(ngsiValue)); // ngsiValue = // rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getContextMetadata().get(2).getName(); // Individual metadata3 = ontModel.createIndividual(ONT_URL + ngsiValue, metadata); // attribute1.addProperty(ontModel.getProperty(ONT_URL + "hasMetadata"), metadata3); // // ngsiValue = // rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getContextMetadata().get(2).getType(); // metadata3.setPropertyValue(ontModel.getProperty(ONT_URL + "type"), // ontModel.createLiteral(ngsiValue)); // ngsiValue = // rcr.getContextRegistration().get(0).getContextRegistrationAttribute().get(0).getContextMetadata().get(2).getValue().toString(); // metadata3.setPropertyValue(ontModel.getProperty(ONT_URL + "value"), // ontModel.createLiteral(ngsiValue)); // System.out.println("has propertry // \"expiry\":"+entityIndiv.hasProperty(ontModel.getProperty(ONT_URL, "expiry"))); ontModel.write(System.out, "TURTLE"); // ontModel.write(System.out, "RDF/XML"); // ontModel.write(System.out, "JSON-LD"); }