Skip to content

Commit e5def8a

Browse files
committed
Added documentation.
1 parent c42ff33 commit e5def8a

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

src/semantics/__init__.py

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,86 @@ def __init__(self, *args, **kwargs):
55
super().__init__(*args, **kwargs)
66

77

8-
class InputTypeBuilder(object):
8+
class TDInputBuilder(object):
9+
"""
10+
Builds the input for TD actions.
11+
Therefore rules what values are desired can be defined and afterwards the input can be automatically determined.
12+
"""
913

14+
# Rules for 'free-text' fields
1015
__value_rules = []
1116

17+
# Rules for fields with 'option' constraints
1218
__option_rules = []
1319

1420
def add_value_rule(self, domain, type, value):
21+
"""
22+
Adds an rule how to process 'free-text' fields.
23+
Note that values datatype must also match the one of the type description.
24+
25+
Example:
26+
ib = TDInputBuilder()
27+
ib.add_value_rule('http://someont.de/#Duration', 'http://someont.de/#Second', 5)
28+
ib.add_value_rule('http://someont.de/#Duration', 'http://someont.de/#Millisecond', 5000)
29+
input_params = ib.build(some_action)
30+
31+
This e.g. sets the value accordingly for:
32+
{
33+
"type": "integer",
34+
"dbo:Duration": "dbr:Second"
35+
}
36+
or for
37+
{
38+
"type": "integer",
39+
"dbo:Duration": "dbr:Millisecond"
40+
}
41+
given the according mappings at the SPARQL endpoint used.
42+
43+
@type domain str
44+
@param domain Full IRI the fields value domain must be equivalent to.
45+
@type type str
46+
@param type Full IRI the fields value must be equivalent to.
47+
@param value The value to set in this specific situation.
48+
"""
1549
self.__value_rules.append((domain, type, value))
1650

1751
def add_option_rule(self, domain, type):
52+
"""
53+
Adds an rule how to process fields with 'options' constraints.
54+
55+
Example:
56+
ib = TDInputBuilder()
57+
ib.add_option_rule('http://someont.de/#Color', 'http://someont.de/#Red')
58+
input_params = ib.build(some_action)
59+
60+
This sets the value of the field with the following type description to "#ff0000"
61+
{
62+
"type": "string",
63+
"options": [
64+
{
65+
"value": "#0000ff",
66+
"dbo:Colour": "dbr:Blue"
67+
},
68+
{
69+
"value": "#ff0000",
70+
"dbo:Colour": "dbr:Red"
71+
}
72+
]
73+
}
74+
given the according mappings at the SPARQL endpoint used.
75+
76+
@type domain str
77+
@param domain Full IRI of the fields value domain.
78+
@type type str
79+
@param type Full IRI of the fields value type.
80+
"""
1881
self.__option_rules.append((domain, type))
1982

2083
def __dispatch_value_field(self, ns_repo, key, value, datatype):
84+
"""
85+
Determines the value of a 'free-text' field using the rules given.
86+
@return Returns the value imposed by an applicable rule or None if no rule was applicable.
87+
"""
2188
domain = ns_repo.resolve(key)
2289
type = ns_repo.resolve(value)
2390
for rule_domain, rule_type, rule_value in self.__value_rules:
@@ -31,6 +98,10 @@ def __dispatch_value_field(self, ns_repo, key, value, datatype):
3198
return None
3299

33100
def __dispatch_options_field(self, ns_repo, options):
101+
"""
102+
Determines the value of a 'options' constrained field using the rules given.
103+
@return Returns the value imposed by an applicable rule or None if no rule was applicable.
104+
"""
34105
for option in options:
35106
for key, value in option.items():
36107
if key != 'value':
@@ -42,6 +113,12 @@ def __dispatch_options_field(self, ns_repo, options):
42113
return None
43114

44115
def __dispatch_type_description(self, ns_repo, it):
116+
"""
117+
Determines the values of the fields using the given type description and the rules registered.
118+
@return The determined input. (Either primitive type or dict if it['type'] is 'object')
119+
@raise UnknownSemanticsException If 'it' describes a field of a primitive type and the value for it could
120+
not be determined or if 'it' describes an object and the latter case is given for any required property.
121+
"""
45122
if it['type'] != 'object':
46123
for key, value in it.items():
47124
if key == 'options':
@@ -68,6 +145,12 @@ def __dispatch_type_description(self, ns_repo, it):
68145
return o
69146

70147
def build(self, action):
148+
"""
149+
Determines the values of the fields using the actions type description and the rules registered.
150+
@return The determined input. (Either primitive type or dict if it['type'] is 'object')
151+
@raise UnknownSemanticsException If 'it' describes a field of a primitive type and the value for it could
152+
not be determined or if 'it' describes an object and the latter case is given for any required property.
153+
"""
71154
it = action.input_type()
72155
ns_repo = action.get_td().namespace_repository()
73156

0 commit comments

Comments
 (0)