-
Notifications
You must be signed in to change notification settings - Fork 5
/
IdentifierExtensions.py
79 lines (67 loc) · 3.1 KB
/
IdentifierExtensions.py
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
""" Defines additional helper methods for the Identifier class for accessing child values
This module is not intended to be imported directly,
Importing the pysoarlib module will cause these to be added to the Identifier class
Note that the methods will use CamelCase, so get_child_str => GetChildStr
"""
_INTEGER_VAL = "int"
_FLOAT_VAL = "double"
_STRING_VAL = "string"
def get_child_str(self, attribute):
""" Given id and attribute, returns value for WME as string (self ^attribute value) """
wme = self.FindByAttribute(attribute, 0)
if wme == None or len(wme.GetValueAsString()) == 0:
return None
return wme.GetValueAsString()
def get_child_int(self, attribute):
""" Given id and attribute, returns integer value for WME (self ^attribute value) """
wme = self.FindByAttribute(attribute, 0)
if wme == None or wme.GetValueType() != _INTEGER_VAL:
return None
return wme.ConvertToIntElement().GetValue()
def get_child_float(self, attribute):
""" Given id and attribute, returns float value for WME (self ^attribute value) """
wme = self.FindByAttribute(attribute, 0)
if wme == None or wme.GetValueType() != _FLOAT_VAL:
return None
return wme.ConvertToFloatElement().GetValue()
def get_child_id(self, attribute):
""" Given id and attribute, returns identifier value of WME (self ^attribute child_id) """
wme = self.FindByAttribute(attribute, 0)
if wme == None or not wme.IsIdentifier():
return None
return wme.ConvertToIdentifier()
def get_all_child_ids(self, attribute=None):
""" Given id and attribute, returns a list of child identifiers from all WME's matching (self ^attribute child_id)
If no attribute is specified, all child identifiers are returned
"""
child_ids = []
for index in range(self.GetNumberChildren()):
wme = self.GetChild(index)
if not wme.IsIdentifier():
continue
if attribute == None or wme.GetAttribute() == attribute:
child_ids.append(wme.ConvertToIdentifier())
return child_ids
def get_all_child_values(self, attribute=None):
""" Given id and attribute, returns a list of strings of non-identifier values from all WME's matching (self ^attribute value)
If no attribute is specified, all child values (non-identifiers) are returned
"""
child_values = []
for index in range(self.GetNumberChildren()):
wme = self.GetChild(index)
if wme.IsIdentifier():
continue
if attribute == None or wme.GetAttribute() == attribute:
child_values.append(wme.GetValueAsString())
return child_values
def get_all_child_wmes(self):
""" Returns a list of (attr, val) tuples representing all wmes rooted at this identifier
val will either be an Identifier or a string, depending on its type """
wmes = []
for index in range(self.GetNumberChildren()):
wme = self.GetChild(index)
if wme.IsIdentifier():
wmes.append( (wme.GetAttribute(), wme.ConvertToIdentifier()) )
else:
wmes.append( (wme.GetAttribute(), wme.GetValueAsString()) )
return wmes