File size: 2,858 Bytes
2130399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pysoarlib.util import parse_wm_printout

class PrintoutIdentifier:
    """ Represents an identifier that was parsed from a soar print command via parse_wm_printout
        and implements the IdentifierExtensions interface for it """

    def create(client, id, depth):
        """ Will print the given identifier to the given depth and wrap the result in a PrintoutIdentifier """
        printout = client.execute_command("p " + id + " -d " + str(depth))
        if printout.strip().startswith("There is no identifier"):
            return None
        wmes = parse_wm_printout(printout)
        return PrintoutIdentifier(wmes, id) 

    def __init__(self, wmes, root_id):
        """ wmes is the result of a parse_wm_printout command,
            root_id is the str id for this identifier """
        self.wmes = wmes
        self.root_id = root_id

    def __lt__(self, other):
        return self.root_id < other.root_id

    def GetIdentifierSymbol(self):
        return self.root_id

    def GetChildString(self, attr):
        return self._get_value(attr)

    def GetChildInt(self, attr):
        val = self._get_value(attr)
        try:
            return int(val)
        except ValueError:
            return None

    def GetChildFloat(self, attr):
        val = self._get_value(attr)
        try:
            return float(val)
        except ValueError:
            return None

    def GetChildId(self, attr):
        child_id = self._get_value(attr)
        if child_id is not None:
            return PrintoutIdentifier(self.wmes, child_id)
        return None

    def GetAllChildIds(self, attr=None):
        # Get all children whose values are also identifiers in the wmes dict
        child_wmes = [ wme for wme in self.wmes.get(self.root_id, []) if wme[2] in self.wmes ]
        if attr is not None:
            child_wmes = [ wme for wme in child_wmes if wme[1] == attr ]
        return [ PrintoutIdentifier(self.wmes, wme[2]) for wme in child_wmes ]

    def GetAllChildValues(self, attr=None):
        # Get all children whose values are not identifiers in the wmes dict
        child_wmes = [ wme for wme in self.wmes.get(self.root_id, []) if wme[2] not in self.wmes ]
        if attr is not None:
            child_wmes = [ wme for wme in child_wmes if wme[1] == attr ]
        return [ wme[2] for wme in child_wmes ]

    def GetAllChildWmes(self):
        child_wmes = []
        for wme in self.wmes.get(self.root_id, []):
            if wme[2] in self.wmes:
                # Identifier
                child_wmes.append( (wme[1], PrintoutIdentifier(self.wmes, wme[2])) )
            else:
                # Value
                child_wmes.append( (wme[1], wme[2]) )
        return child_wmes

    def _get_value(self, attr):
        return next((wme[2] for wme in self.wmes.get(self.root_id, []) if wme[1] == attr), None)