|
24 | 24 | (defrule cx-pddl-clips-agent-pddl-add-instance |
25 | 25 | " Setup PDDL instance with an active goal to plan for " |
26 | 26 | (pddl-manager (ros-comm-init TRUE)) |
27 | | - => |
| 27 | +=> |
28 | 28 | (bind ?share-dir (ament-index-get-package-share-directory "cx_pddl_bringup")) |
29 | 29 | (assert |
30 | 30 | (pddl-instance |
|
38 | 38 | (pddl-goal-fluent (instance test) (goal active-goal) (name on) (params a b)) |
39 | 39 | (pddl-goal-fluent (instance test) (goal active-goal) (name on) (params b c)) |
40 | 40 | (pddl-set-goals (instance test) (goal active-goal)) |
41 | | - (pddl-plan (instance test) (goal active-goal) (plan-type TEMPORAL)) |
| 41 | + (pddl-plan (id test-plan) (instance test) (goal active-goal) (plan-type TEMPORAL)) |
42 | 42 | ) |
43 | 43 | ) |
44 | 44 |
|
45 | 45 |
|
46 | 46 | (defrule cx-pddl-clips-agent-select-action |
47 | 47 | " Start executing the first action of the resulting plan " |
48 | | - (not (plan-start ?t)) |
49 | | - ?pa <- (pddl-action (planned-start-time ?t) (state IDLE)) |
50 | | - (not (pddl-action (planned-start-time ?ot&:(< ?ot ?t)))) |
51 | | - => |
| 48 | + ?plan <- (pddl-plan (id ?plan-id) (plan-start ?p-start)) |
| 49 | + (not (pddl-action (state EXECUTING|SELECTED))) |
| 50 | + ?pa <- (pddl-action (plan ?plan-id) (planned-start-time ?t) (state IDLE)) |
| 51 | + (not (pddl-action (plan ?plan-id) (state IDLE) (planned-start-time ?ot&:(< ?ot ?t)))) |
| 52 | +=> |
| 53 | + (if (= ?p-start 0.0) then (modify ?plan (plan-start (now)))) |
52 | 54 | (modify ?pa (state SELECTED)) |
53 | | - (assert (plan-start (now))) |
54 | 55 | ) |
55 | 56 |
|
56 | 57 | (defrule cx-pddl-clips-agent-check-action |
57 | 58 | " Before executing an action check the condition to make sure it is feasible " |
58 | 59 | (pddl-action (id ?id) (state SELECTED) (name ?name) (params $?params)) |
59 | 60 | (not (pddl-action-condition (action ?id))) |
60 | | - => |
| 61 | +=> |
61 | 62 | (assert (pddl-action-condition (instance test) (action ?id))) |
62 | 63 | ) |
63 | 64 |
|
64 | 65 | (defrule cx-pddl-clips-agent-executable-action |
65 | 66 | " Condition is satisfied, go ahead with execution " |
66 | | - (plan-start ?t) |
| 67 | + (pddl-plan (id ?plan-id) (plan-start ?t)) |
67 | 68 | (pddl-action-condition (action ?action-id) (state CONDITION-SAT)) |
68 | | - ?pa <- (pddl-action (id ?action-id) (name ?name) (params $?params) (state SELECTED)) |
69 | | - => |
| 69 | + ?pa <- (pddl-action (id ?action-id) (plan ?plan-id) (name ?name) (params $?params) (state SELECTED)) |
| 70 | +=> |
70 | 71 | (modify ?pa (state EXECUTING) (actual-start-time (- (now) ?t))) |
71 | 72 | ) |
72 | 73 |
|
73 | 74 | (defrule cx-pddl-clips-agent-execution-done |
74 | 75 | " After the duration has elapsed, the action is done " |
75 | 76 | (time ?now) |
76 | | - (plan-start ?t) |
77 | | - ?pa <- (pddl-action (id ?id) (state EXECUTING) (planned-duration ?d) (name ?name) |
| 77 | + (pddl-plan (id ?plan-id) (plan-start ?t)) |
| 78 | + ?pa <- (pddl-action (id ?id) (plan ?plan-id) (state EXECUTING) (planned-duration ?d) (name ?name) |
78 | 79 | (actual-start-time ?s&:(< (+ ?s ?d ?t) ?now))) |
79 | | - => |
| 80 | +=> |
80 | 81 | (bind ?duration (- (now) (+ ?s ?t))) |
81 | 82 | (printout info "Executed action " ?name " in " ?duration " seconds" crlf) |
82 | 83 | (modify ?pa (state DONE) (actual-duration ?duration)) |
83 | 84 | (assert (pddl-action-get-effect (action ?id) (apply TRUE))) |
84 | 85 | ) |
85 | 86 |
|
86 | | -(defrule cx-pddl-clips-agent-select-next-action |
87 | | -" Once an action is done, select one with lowest planned start time next " |
88 | | - (not (pddl-action (state EXECUTING|SELECTED))) |
89 | | - (not (pddl-action-get-effect (state ~DONE))) |
90 | | - (not (pddl-fluent-change)) |
91 | | - ?pa <- (pddl-action (planned-start-time ?t) (state IDLE)) |
92 | | - (not (pddl-action (state IDLE) (planned-start-time ?ot&:(< ?ot ?t)))) |
93 | | - => |
94 | | - (modify ?pa (state SELECTED)) |
95 | | -) |
96 | | - |
97 | 87 | (defrule cx-pddl-clips-agent-print-exec-times |
98 | 88 | " Once everything is done, print out planned vs actual times " |
99 | 89 | (pddl-action) |
100 | 90 | (not (pddl-action (state ~DONE))) |
101 | 91 | (not (printed)) |
102 | | - => |
| 92 | +=> |
103 | 93 | (printout blue "Execution done" crlf) |
104 | 94 | (do-for-all-facts ((?pa pddl-action)) TRUE |
105 | 95 | (printout green "action " ?pa:name " " |
|
0 commit comments