Skip to content

Commit

Permalink
Changed 'options' to 'oneOf' to implement proposal json-schema-org/js…
Browse files Browse the repository at this point in the history
  • Loading branch information
fischmat committed Jan 17, 2017
1 parent 063f7d9 commit c4744a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/controller/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def on_door_opened(self, is_opened):
if secs <= self.auth_ttl_secs: # Permission case
welcome_action = self.alarm_source.get_action_by_types(['http://www.matthias-fisch.de/ontologies/wot#PlaybackAction'])
if welcome_action:
pb = TDInputBuilder()
pb.add_option_rule('http://www.matthias-fisch.de/ontologies/wot#SoundFile', 'http://www.matthias-fisch.de/ontologies/wot#WelcomeSound')
ib = TDInputBuilder()
ib.add_oneof_rule('http://www.matthias-fisch.de/ontologies/wot#SoundFile', 'http://www.matthias-fisch.de/ontologies/wot#WelcomeSound')

try:
params = pb.build(welcome_action)
params = ib.build(welcome_action)
except UnknownSemanticsException:
print("Wanted to say 'Welcome', but semantics of playback action could not be determined :(")
return
Expand All @@ -55,15 +55,15 @@ def on_door_opened(self, is_opened):
alarm_action = self.alarm_source.get_action_by_types(
['http://www.matthias-fisch.de/ontologies/wot#AlarmAction'])

pb = TDInputBuilder()
pb.add_value_rule('http://www.matthias-fisch.de/ontologies/wot#Duration',
ib = TDInputBuilder()
ib.add_value_rule('http://www.matthias-fisch.de/ontologies/wot#Duration',
'http://dbpedia.org/resource/Second', self.alarm_duration_secs)
pb.add_value_rule('http://www.matthias-fisch.de/ontologies/wot#Duration',
ib.add_value_rule('http://www.matthias-fisch.de/ontologies/wot#Duration',
'http://dbpedia.org/resource/Millisecond', self.alarm_duration_secs * 1000)
pb.add_option_rule('http://dbpedia.org/ontology/Colour', 'http://dbpedia.org/resource/Red')
ib.add_oneof_rule('http://dbpedia.org/ontology/Colour', 'http://dbpedia.org/resource/Red')

try:
params = pb.build(alarm_action)
params = ib.build(alarm_action)
except UnknownSemanticsException as e:
print("Cannot determine semantics of alarm actions input type.")
return
Expand Down
30 changes: 15 additions & 15 deletions src/semantics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class TDInputBuilder(object):
# Rules for 'free-text' fields
__value_rules = []

# Rules for fields with 'option' constraints
__option_rules = []
# Rules for fields with 'oneof' constraints
__oneof_rules = []

def add_value_rule(self, domain, type, value):
"""
Expand Down Expand Up @@ -48,19 +48,19 @@ def add_value_rule(self, domain, type, value):
"""
self.__value_rules.append((domain, type, value))

def add_option_rule(self, domain, type):
def add_oneof_rule(self, domain, type):
"""
Adds an rule how to process fields with 'options' constraints.
Adds an rule how to process fields with 'oneOf' constraints.
Example:
ib = TDInputBuilder()
ib.add_option_rule('http://someont.de/#Color', 'http://someont.de/#Red')
ib.add_oneof_rule('http://someont.de/#Color', 'http://someont.de/#Red')
input_params = ib.build(some_action)
This sets the value of the field with the following type description to "#ff0000"
{
"type": "string",
"options": [
"oneOf": [
{
"value": "#0000ff",
"dbo:Colour": "dbr:Blue"
Expand All @@ -78,7 +78,7 @@ def add_option_rule(self, domain, type):
@type type str
@param type Full IRI of the fields value type.
"""
self.__option_rules.append((domain, type))
self.__oneof_rules.append((domain, type))

def __dispatch_value_field(self, ns_repo, key, value, datatype):
"""
Expand All @@ -97,19 +97,19 @@ def __dispatch_value_field(self, ns_repo, key, value, datatype):
return rule_value
return None

def __dispatch_options_field(self, ns_repo, options):
def __dispatch_oneof_field(self, ns_repo, options):
"""
Determines the value of a 'options' constrained field using the rules given.
Determines the value of a 'oneOf' constrained field using the rules given.
@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 != 'value':
for rule_domain, rule_type in self.__option_rules:
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) and sparql.classes_equivalent(type, rule_type):
return option['value']
return option['constant']
return None

def __dispatch_type_description(self, ns_repo, it):
Expand All @@ -121,12 +121,12 @@ def __dispatch_type_description(self, ns_repo, it):
"""
if it['type'] != 'object':
for key, value in it.items():
if key == 'options':
v = self.__dispatch_options_field(ns_repo, it['options'])
if key == 'oneOf':
v = self.__dispatch_oneof_field(ns_repo, it['oneOf'])
if v:
return v
else:
raise UnknownSemanticsException('The semantics of none of the options could be determined.')
raise UnknownSemanticsException('The semantics of none of the oneOf options could be determined.')

elif isinstance(key, str) and isinstance(value, str):
v = self.__dispatch_value_field(ns_repo, key, value, it['type'])
Expand Down

0 comments on commit c4744a7

Please sign in to comment.