-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.go
294 lines (235 loc) · 6.98 KB
/
evaluation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package wotlib
// contains function for evaluation based on expanded thing descriptions
// GetPropertyAffordances searches within a set for all property affordances where constraints match
func (s *ExpandedThingDescriptionSet) GetPropertyAffordances(constraint ThingConstraint) []ExpandedPropertyAffordance {
// some optimization: remove unwanted constraints for filtering things
strippedThingConstraints := constraint
strippedThingConstraints.PropertyConstraint = nil
var result []ExpandedPropertyAffordance
for _, currTD := range *s {
if currTD.Fulfills(strippedThingConstraints) {
if constraint.PropertyConstraint == nil {
result = append(result, currTD.GetPropertyAffordances(PropertyConstraint{})...)
} else {
result = append(result, currTD.GetPropertyAffordances(*constraint.PropertyConstraint)...)
}
}
}
return result
}
// GetActionAffordances searches within a set for all actions affordances where constraints match
func (s *ExpandedThingDescriptionSet) GetActionAffordances(constraint ThingConstraint) []ExpandedActionAffordance {
// some optimization: remove unwanted constraints for filtering things
strippedThingConstraints := constraint
strippedThingConstraints.ActionConstraint = nil
var result []ExpandedActionAffordance
for _, currTD := range *s {
if currTD.Fulfills(strippedThingConstraints) {
if constraint.ActionConstraint == nil {
result = append(result, currTD.GetActionAffordances(ActionConstraint{})...)
} else {
result = append(result, currTD.GetActionAffordances(*constraint.ActionConstraint)...)
}
}
}
return result
}
// GetPropertyAffordances searches for a property affordance with specific criteria
func (t *ExpandedThingDescription) GetPropertyAffordances(constraint PropertyConstraint) []ExpandedPropertyAffordance {
var result []ExpandedPropertyAffordance
if len(t.Properties) == 0 {
return result
}
for _, currProperty := range t.Properties {
if currProperty.Fulfills(constraint) {
result = append(result, currProperty)
}
}
return result
}
// GetActionAffordances searches for an action affordance with specific criteria
func (t *ExpandedThingDescription) GetActionAffordances(constraint ActionConstraint) []ExpandedActionAffordance {
var result []ExpandedActionAffordance
if len(t.Actions) == 0 {
return result
}
for _, currAction := range t.Actions {
if currAction.Fulfills(constraint) {
result = append(result, currAction)
}
}
return result
}
// ThingConstraint defines a thing constraint
type ThingConstraint struct {
ID *string
Type *[]string
Name *string
PropertyConstraint *PropertyConstraint
ActionConstraint *ActionConstraint
}
// Fulfills checks if constraint matches with given element
// A thing description matches if ID, type and name match (if given)
// and if at least one of the PropertyConstraints and one of
// the ActionConstraints matches (if given)
func (t ExpandedThingDescription) Fulfills(c ThingConstraint) bool {
if c.ID != nil && *c.ID != t.ID {
return false
}
if c.Type != nil && !allTypesContained(*c.Type, t.Type) {
return false
}
if c.Name != nil && *c.Name != t.Name.Value() {
return false
}
if c.PropertyConstraint != nil {
matchFound := false
// at least one property has to match
for _, currProperty := range t.Properties {
if currProperty.Fulfills(*c.PropertyConstraint) {
matchFound = true
break
}
}
if !matchFound {
return false
}
}
if c.ActionConstraint != nil {
matchFound := false
// at least one action has to match
for _, currAction := range t.Actions {
if currAction.Fulfills(*c.ActionConstraint) {
matchFound = true
break
}
}
if !matchFound {
return false
}
}
return true
}
// PropertyConstraint defines a property constraint
type PropertyConstraint struct {
Name *string
Type *[]string
DataType *string
DataPropertyConstraint *DataPropertyConstraint
IsObservable *bool
}
// Fulfills checks if PropertyConstraint is fulfilled by given ExpandedPropertyAffordance
func (t ExpandedPropertyAffordance) Fulfills(c PropertyConstraint) bool {
if c.Name != nil && *c.Name != t.Name.Value() {
return false
}
if c.Type != nil && !allTypesContained(*c.Type, t.Type) {
return false
}
if c.DataType != nil && *c.DataType != t.DataType.Value() {
return false
}
if c.IsObservable != nil && *c.IsObservable != t.IsObservable.Value() {
return false
}
if c.DataPropertyConstraint != nil {
matchFound := false
// at least one property has to match
for _, currProperty := range t.Properties {
if currProperty.Fulfills(*c.DataPropertyConstraint) {
matchFound = true
break
}
}
if !matchFound {
return false
}
}
return true
}
// ActionConstraint defines an action constraint
type ActionConstraint struct {
Name *string
Type *[]string
InputConstraint *InputConstraint
IsIdempotent *bool
IsSafe *bool
}
// Fulfills checks if ActionConstraint is fulfilled by given ExpandedActionAffordance
func (t ExpandedActionAffordance) Fulfills(c ActionConstraint) bool {
if c.Name != nil && *c.Name != t.Name.Value() {
return false
}
if c.Type != nil && !allTypesContained(*c.Type, t.Type) {
return false
}
if c.IsIdempotent != nil && *c.IsIdempotent != t.IsIdempotent.Value() {
return false
}
if c.IsSafe != nil && *c.IsSafe != t.IsSafe.Value() {
return false
}
if c.InputConstraint != nil && !t.Input.Fulfills(*c.InputConstraint) {
return false
}
return true
}
// InputConstraint defines an input constraint
type InputConstraint struct {
DataType *string
DataPropertyConstraint *DataPropertyConstraint
}
// Fulfills checks if ExpandedInputConstraint matches with given ExpandedDataProperty
func (t ExpandedDataSchemaNode) Fulfills(c InputConstraint) bool {
elem := t.Value()
if c.DataType != nil && *c.DataType != elem.DataType.Value() {
return false
}
if c.DataPropertyConstraint != nil {
matchFound := false
for _, currProperty := range elem.Properties {
if currProperty.Fulfills(*c.DataPropertyConstraint) {
matchFound = true
break
}
}
if !matchFound {
return false
}
}
return true
}
// DataPropertyConstraint defines a data property constraint
type DataPropertyConstraint struct {
Name *string
Type *[]string
DataType *string
}
// Fulfills checks if DataPropertyConstraint is fulfilled by given ExpandedDataProperty
func (t ExpandedDataProperty) Fulfills(c DataPropertyConstraint) bool {
if c.Name != nil && *c.Name != t.Name.Value() {
return false
}
if c.Type != nil && !allTypesContained(*c.Type, t.Type) {
return false
}
if c.DataType != nil && *c.DataType != t.DataType.Value() {
return false
}
return true
}
func allTypesContained(requiredTypes []string, givenTypes []string) bool {
for _, r := range requiredTypes {
found := false
for _, g := range givenTypes {
if r == g {
found = true
break
}
}
if !found {
return false
}
}
return true
}