@@ -5,19 +5,86 @@ def __init__(self, *args, **kwargs):
5
5
super ().__init__ (* args , ** kwargs )
6
6
7
7
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
+ """
9
13
14
+ # Rules for 'free-text' fields
10
15
__value_rules = []
11
16
17
+ # Rules for fields with 'option' constraints
12
18
__option_rules = []
13
19
14
20
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
+ """
15
49
self .__value_rules .append ((domain , type , value ))
16
50
17
51
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
+ """
18
81
self .__option_rules .append ((domain , type ))
19
82
20
83
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
+ """
21
88
domain = ns_repo .resolve (key )
22
89
type = ns_repo .resolve (value )
23
90
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):
31
98
return None
32
99
33
100
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
+ """
34
105
for option in options :
35
106
for key , value in option .items ():
36
107
if key != 'value' :
@@ -42,6 +113,12 @@ def __dispatch_options_field(self, ns_repo, options):
42
113
return None
43
114
44
115
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
+ """
45
122
if it ['type' ] != 'object' :
46
123
for key , value in it .items ():
47
124
if key == 'options' :
@@ -68,6 +145,12 @@ def __dispatch_type_description(self, ns_repo, it):
68
145
return o
69
146
70
147
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
+ """
71
154
it = action .input_type ()
72
155
ns_repo = action .get_td ().namespace_repository ()
73
156
0 commit comments