Skip to content

Commit 7f83a21

Browse files
authored
Merge pull request #58 from APLA-Toolbox/enhance-output
Overload __str__ #53
2 parents d39d17b + dcf9e13 commit 7f83a21

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

jupyddl/automated_planner.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,30 @@ def get_actions_from_path(self, path):
100100
return []
101101
actions = []
102102
for node in path:
103-
actions.append((node.parent_action, node.g_cost))
104-
105-
cost = self.pddl.get_value(path[-1].state, "total-cost")
106-
if not cost:
107-
return actions
108-
return (actions, cost)
103+
if not node.parent_action:
104+
break
105+
act = str(node.parent_action).replace("<PyCall.jlwrap ", "")
106+
cost = "total-cost = " + str(node.g_cost)
107+
actions.append((act, cost))
108+
109+
return actions
110+
111+
def __stringify_state(self, state):
112+
state_str = str(state).replace("<PyCall.jlwrap PDDL.State(", "")
113+
state_str = state_str.replace("Set(Julog.Term[", "")
114+
state_str = state_str.replace("])", "")
115+
state_str = state_str.replace('Dict{Symbol,Any}(Symbol("total-cost") =>', "total-cost =")
116+
state_str = state_str.replace("))>", "")
117+
return state_str
109118

110119
def get_state_def_from_path(self, path):
111120
if not path:
112121
self.logger.warning("Path is empty, can't operate...")
113122
return []
114123
trimmed_path = []
115124
for node in path:
116-
trimmed_path.append(node.state)
125+
state = self.__stringify_state(node.state)
126+
trimmed_path.append(state)
117127
return trimmed_path
118128

119129
def breadth_first_search(self):

jupyddl/node.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def __init__(
1717
self.state = state
1818
self.parent_action = parent_action
1919
self.parent = parent
20+
self.automated_planner = automated_planner
2021
temp_cost = automated_planner.pddl.get_value(state, "total-cost")
2122
if temp_cost:
2223
self.g_cost = temp_cost
@@ -51,5 +52,24 @@ def __init__(
5152
self.is_closed = is_closed
5253
self.is_open = is_open
5354

55+
def __stringify_state(self, state):
56+
state_str = str(state).replace("<PyCall.jlwrap PDDL.State(", "")
57+
state_str = state_str.replace("Set(Julog.Term[", "")
58+
state_str = state_str.replace("])", "")
59+
state_str = state_str.replace('Dict{Symbol,Any}(Symbol("total-cost") =>', "total-cost =")
60+
state_str = state_str.replace("))>", "")
61+
return state_str
62+
5463
def __lt__(self, other):
5564
return self.f_cost <= other.f_cost
65+
66+
def __str__(self):
67+
state = self.__stringify_state(self.state)
68+
return "Node { %s | g = %.2f | h = %.2f | open = %s | closed = %s }" % (state, self.g_cost, self.h_cost, self.is_open, self.is_closed)
69+
70+
class Path:
71+
def __init__(self, nodes):
72+
self.nodes = nodes
73+
74+
def __str__(self):
75+
return str([str(n) for n in self.nodes])

tests/test_dfs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import sys
2+
from os import path

tests/test_node.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
77
from jupyddl.automated_planner import AutomatedPlanner
8-
from jupyddl.node import Node
8+
from jupyddl.node import Node, Path
99

1010

1111
def test_node_equality_cost():
@@ -36,3 +36,25 @@ def test_node_equality_no_cost():
3636
assertion2 = next_node < next_node_v2
3737

3838
assert assertion and assertion2
39+
40+
def test_stringified_node():
41+
apla = AutomatedPlanner(
42+
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
43+
)
44+
actions = apla.available_actions(apla.initial_state)
45+
for act in actions:
46+
next_state = apla.transition(apla.initial_state, act)
47+
next_node = Node(next_state, apla, heuristic_based=True)
48+
assert "<PyCall.jlwrap PDDL.State" not in str(next_node) and "Set(Julog.Term" not in str(next_node)
49+
50+
def test_stringified_path():
51+
apla = AutomatedPlanner(
52+
"pddl-examples/dinner/domain.pddl", "pddl-examples/dinner/problem.pddl"
53+
)
54+
actions = apla.available_actions(apla.initial_state)
55+
path = []
56+
for act in actions:
57+
next_state = apla.transition(apla.initial_state, act)
58+
path.append(Node(next_state, apla, heuristic_based=True))
59+
60+
assert "<PyCall.jlwrap PDDL.State" not in str(Path(path)) and "Set(Julog.Term" not in str(Path(path))

0 commit comments

Comments
 (0)