def __dispatch_value_field(self, ns_repo, key, value, datatype, sparql_endpoint=sparql.DEFAULT_SPARQL_ENDPOINT): """ Determines the value of a 'free-text' field using the rules given. @param sparql_endpoint The URL of the NanoSPARQLServer REST-endpoint. @return Returns the value imposed by an applicable rule or None if no rule was applicable. """ if key != 'type': domain = ns_repo.resolve(key) type = ns_repo.resolve(value) for rule_domain, rule_type, rule_value in self.__value_rules: dt_match = (isinstance(rule_value, int) and datatype == 'integer') \ or (isinstance(rule_value, float) and (datatype == 'float' or datatype == 'number')) \ or (isinstance(rule_value, str) and datatype == 'string') \ or (isinstance(rule_value, bool) and datatype == 'boolean') if dt_match and sparql.classes_equivalent( domain, rule_domain, endpoint=sparql_endpoint ) and sparql.classes_equivalent( type, rule_type, endpoint=sparql_endpoint): return rule_value return None
def test_classes_equivalent(self): self.assertTrue( sparql.classes_equivalent( 'http://purl.org/iot/vocab/m3-lite#Thermometer', 'http://purl.oclc.org/NET/ssnx/meteo/aws#TemperatureSensor')) self.assertFalse( sparql.classes_equivalent( 'http://purl.org/iot/vocab/m3-lite#Thermometer', 'http://elite.polito.it/ontologies/dogont.owl#DeepFreezer'))
def __dispatch_oneof_field(self, ns_repo, options, sparql_endpoint=sparql.DEFAULT_SPARQL_ENDPOINT): """ Determines the value of a 'oneOf' constrained field using the rules given. @param sparql_endpoint The URL of the NanoSPARQLServer REST-endpoint. @return Returns the value imposed by an applicable rule or None if no rule was applicable. """ for option in options: for key, value in option.items(): if key != 'constant': for rule_domain, rule_type in self.__oneof_rules: domain = ns_repo.resolve(key) type = ns_repo.resolve(value) if sparql.classes_equivalent( domain, rule_domain, endpoint=sparql_endpoint ) and sparql.classes_equivalent( type, rule_type, endpoint=sparql_endpoint): return option['constant'] return None
def type_equivalent_to(self, types, sparql_endpoint=sparql.DEFAULT_SPARQL_ENDPOINT): """ Checks whether the @type of this thing is equvalent to any of the given types. @param types List of IRIs (either full or shorthands with prefixes defined in this TD) @param sparql_endpoint The URL of the NanoSPARQLServer REST-endpoint. @return Returns True if any of the types given is equivalent to @type of this TD. Returns False if none of them is or @type is not set in this TD. """ if '@type' in self.__td.keys(): for type in types: if sparql.classes_equivalent( self.__ns_repo.resolve(self.__td['@type']), self.__ns_repo.resolve(type), sparql_endpoint): return True return False else: return False
def get_event_by_types(self, types, sparql_endpoint=sparql.DEFAULT_SPARQL_ENDPOINT): """ Returns events equivalent to any of the given types. @param types List of IRIs. @param sparql_endpoint The URL of the NanoSPARQLServer REST-endpoint. @return Any event equivalent to any given type or None if none found. """ ns_repo = self.namespace_repository() for event in self.__td['events']: if '@type' in event.keys(): if ns_repo.resolve(event['@type']) in types: return TDEvent(self, event) else: for type in types: if sparql.classes_equivalent( type, ns_repo.resolve(event['@type']), sparql_endpoint): return TDEvent(self, event) return None
def get_property_by_types(self, types, sparql_endpoint=sparql.DEFAULT_SPARQL_ENDPOINT): """ Returns properties equivalent to any of the given types. @param types List of IRIs. @param sparql_endpoint The URL of the NanoSPARQLServer REST-endpoint. @return Any property equivalent to any given type or None if none found. """ ns_repo = self.namespace_repository() for prop in self.__td['properties']: if '@type' in prop.keys(): if ns_repo.resolve(prop['@type']) in types: return TDProperty(self, prop) else: for type in types: if sparql.classes_equivalent( type, ns_repo.resolve(prop['@type']), sparql_endpoint): return TDProperty(self, prop) return None
def has_all_events_of(self, types, sparql_endpoint=sparql.DEFAULT_SPARQL_ENDPOINT): """ Checks if this thing has an equivalent event for any of the given types. @param types List of IRIs. @param sparql_endpoint The URL of the NanoSPARQLServer REST-endpoint. @rtype bool @return Returns True iff there is an equivalent event for every given type. """ ns_repo = self.namespace_repository() for type in types: found_matching_event = False for event in self.__td['events']: if '@type' in event.keys(): if sparql.classes_equivalent( ns_repo.resolve(type), ns_repo.resolve(event['@type']), sparql_endpoint): found_matching_event = True if not found_matching_event: return False return True