pddl-pyvalidator 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ Metadata-Version: 2.4
2
+ Name: pddl-pyvalidator
3
+ Version: 0.1.0
4
+ Summary: Pure Python PDDL plan validator
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ License-File: LICENSE
8
+ Requires-Dist: unified-planning>=1.1.0
9
+ Provides-Extra: dev
10
+ Requires-Dist: pytest>=7.0; extra == "dev"
11
+ Provides-Extra: copilot
12
+ Requires-Dist: pddl-plus-parser; extra == "copilot"
13
+ Dynamic: license-file
@@ -0,0 +1,15 @@
1
+ pddl_pyvalidator-0.1.0.dist-info/licenses/LICENSE,sha256=eppAqBLLzP6SKT_c_8v_9yvey-bVjR0iH1d8ZThQVxY,1064
2
+ pyval/__init__.py,sha256=6W25DWrNGhTWSQeVnbuk1Jp5RRfHFfbO4ipepOhaGwc,403
3
+ pyval/cli.py,sha256=Qksw3sBZnrxadzNV57KxE0c2JyD55_e_cajZWVMGt2k,2073
4
+ pyval/diagnostics.py,sha256=phlJ8hNg_7j3qdgmbiJgyZlOfYqBLB1ErYC9ZZc4IPg,8552
5
+ pyval/models.py,sha256=aMmk0AzFAcU1OTovqL-CwJJ3TAhZpiGXl1A6bXeElU0,1893
6
+ pyval/numeric_tracker.py,sha256=uRM6NH9wfNwe1vxqW4EtlLrcriWRVBvSkVj8SjqduzQ,2711
7
+ pyval/plan_simulator.py,sha256=_cJy_dtEM264JaxbpHC0OnK0FL_Z_-4fWDtrWua_DVg,4246
8
+ pyval/report_formatter.py,sha256=Kyjy6IIdsg4zkRM_DjmhDOfNxZ-KgVDHXLQKN2rFvdQ,8990
9
+ pyval/syntax_checker.py,sha256=htxMjaTq9uZk89tPi2_GwS7D_2bguCe7wT7Hc84RxHA,2246
10
+ pyval/validator.py,sha256=7ri7fzkaJKE57gC5d-ZyByWMadR-gekTMs88ePqoFKQ,8265
11
+ pddl_pyvalidator-0.1.0.dist-info/METADATA,sha256=-E8GO_mo8Rt3md4-bs6EYcs1acU4iI5gJs7H6FjT_7I,371
12
+ pddl_pyvalidator-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
13
+ pddl_pyvalidator-0.1.0.dist-info/entry_points.txt,sha256=XG6ZbaynFjtJwl48-nN-6xpzq06jbRo-sAClHw8Mi3A,41
14
+ pddl_pyvalidator-0.1.0.dist-info/top_level.txt,sha256=FsnZuyQIfXng5Gjl4gTGgNfEJTAXVAEwyDKpZIVPaA0,6
15
+ pddl_pyvalidator-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ pyval = pyval.cli:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 SPL@BGU
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ pyval
pyval/__init__.py ADDED
@@ -0,0 +1,21 @@
1
+ """PyVAL — Pure Python PDDL plan validator."""
2
+
3
+ from pyval.models import (
4
+ GoalResult,
5
+ NumericChange,
6
+ PreconditionFailure,
7
+ StateSnapshot,
8
+ StepResult,
9
+ ValidationResult,
10
+ )
11
+ from pyval.validator import PDDLValidator
12
+
13
+ __all__ = [
14
+ "PDDLValidator",
15
+ "ValidationResult",
16
+ "StepResult",
17
+ "NumericChange",
18
+ "PreconditionFailure",
19
+ "GoalResult",
20
+ "StateSnapshot",
21
+ ]
pyval/cli.py ADDED
@@ -0,0 +1,78 @@
1
+ """CLI entry point — mirrors VAL's Validate interface."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import sys
7
+
8
+ from pyval.report_formatter import format_json, format_plain_text, format_trajectory
9
+ from pyval.validator import PDDLValidator
10
+
11
+
12
+ def main() -> None:
13
+ parser = argparse.ArgumentParser(
14
+ prog="pyval",
15
+ description="PyVAL — Pure Python PDDL plan validator",
16
+ )
17
+ parser.add_argument(
18
+ "files",
19
+ nargs="+",
20
+ metavar="FILE",
21
+ help="PDDL files: domain [problem [plan]]",
22
+ )
23
+ parser.add_argument(
24
+ "-v", "--verbose", action="store_true", help="Verbose output"
25
+ )
26
+ parser.add_argument(
27
+ "--json", action="store_true", help="Output as structured JSON"
28
+ )
29
+ parser.add_argument(
30
+ "--trajectory", action="store_true", help="Output numeric fluent trajectory"
31
+ )
32
+ parser.add_argument(
33
+ "--track",
34
+ action="append",
35
+ metavar="FLUENT",
36
+ help="Track specific numeric fluent (repeatable)",
37
+ )
38
+ parser.add_argument(
39
+ "--version", action="version", version="pyval 0.1.0"
40
+ )
41
+
42
+ args = parser.parse_args()
43
+ files = args.files
44
+
45
+ if len(files) > 3:
46
+ parser.error("Expected 1-3 files: domain [problem [plan]]")
47
+
48
+ validator = PDDLValidator()
49
+
50
+ if len(files) == 1:
51
+ result = validator.validate_syntax(domain_path=files[0])
52
+ elif len(files) == 2:
53
+ result = validator.validate_syntax(
54
+ domain_path=files[0], problem_path=files[1]
55
+ )
56
+ else:
57
+ result = validator.validate(
58
+ domain_path=files[0],
59
+ problem_path=files[1],
60
+ plan_path=files[2],
61
+ tracked_fluents=args.track,
62
+ )
63
+
64
+ # Output
65
+ import json
66
+
67
+ if args.json:
68
+ print(json.dumps(format_json(result), indent=2))
69
+ elif args.trajectory:
70
+ print(format_trajectory(result, tracked=args.track))
71
+ else:
72
+ print(format_plain_text(result, verbose=args.verbose))
73
+
74
+ sys.exit(0 if result.is_valid else 1)
75
+
76
+
77
+ if __name__ == "__main__":
78
+ main()
pyval/diagnostics.py ADDED
@@ -0,0 +1,253 @@
1
+ """Diagnostic message generation — precondition decomposition and goal checking."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unified_planning.model import FNode, OperatorKind
6
+
7
+ from pyval.models import GoalResult, PreconditionFailure
8
+
9
+
10
+ def decompose_preconditions(
11
+ action, parameters: tuple, state, problem
12
+ ) -> list[PreconditionFailure]:
13
+ """Walk action preconditions, evaluate each against state, return failures.
14
+
15
+ Parameters are the grounded objects substituted into the action schema.
16
+ """
17
+ failures: list[PreconditionFailure] = []
18
+ param_map = dict(zip(action.parameters, parameters))
19
+
20
+ for prec in action.preconditions:
21
+ _evaluate_expr(prec, state, param_map, failures)
22
+
23
+ return failures
24
+
25
+
26
+ def check_goals(problem, state) -> list[GoalResult]:
27
+ """Evaluate all goals against a state, return results for each."""
28
+ results: list[GoalResult] = []
29
+ for goal in problem.goals:
30
+ _evaluate_goal(goal, state, results)
31
+ return results
32
+
33
+
34
+ def _evaluate_expr(
35
+ expr: FNode,
36
+ state,
37
+ param_map: dict,
38
+ failures: list[PreconditionFailure],
39
+ ) -> None:
40
+ """Recursively evaluate a precondition expression, collecting failures."""
41
+ node_type = expr.node_type
42
+
43
+ if node_type == OperatorKind.AND:
44
+ for arg in expr.args:
45
+ _evaluate_expr(arg, state, param_map, failures)
46
+ return
47
+
48
+ if node_type == OperatorKind.OR:
49
+ # Check if any disjunct is satisfied
50
+ sub_failures: list[PreconditionFailure] = []
51
+ for arg in expr.args:
52
+ _evaluate_expr(arg, state, param_map, sub_failures)
53
+ if len(sub_failures) == len(expr.args):
54
+ # None satisfied — report the OR as failed
55
+ failures.append(PreconditionFailure(
56
+ expression=_expr_to_pddl(expr, param_map),
57
+ type="boolean",
58
+ current_values={},
59
+ explanation="None of the disjuncts are satisfied",
60
+ ))
61
+ return
62
+
63
+ if node_type == OperatorKind.NOT:
64
+ inner = expr.args[0]
65
+ grounded = _substitute(inner, param_map)
66
+ try:
67
+ val = state.get_value(grounded)
68
+ if val.is_true():
69
+ failures.append(PreconditionFailure(
70
+ expression=_expr_to_pddl(expr, param_map),
71
+ type="boolean",
72
+ current_values={_expr_to_pddl(inner, param_map): True},
73
+ explanation=f"{_expr_to_pddl(inner, param_map)} is true but should be false",
74
+ ))
75
+ except Exception:
76
+ pass
77
+ return
78
+
79
+ # Comparison operators: LE, LT, EQUALS (UPF normalizes >= to <= with swapped args)
80
+ if node_type in (OperatorKind.LE, OperatorKind.LT, OperatorKind.EQUALS):
81
+ _evaluate_comparison(expr, state, param_map, failures)
82
+ return
83
+
84
+ # Fluent expression (boolean)
85
+ if expr.is_fluent_exp():
86
+ grounded = _substitute(expr, param_map)
87
+ try:
88
+ val = state.get_value(grounded)
89
+ if val.is_false():
90
+ failures.append(PreconditionFailure(
91
+ expression=_expr_to_pddl(expr, param_map),
92
+ type="boolean",
93
+ current_values={_expr_to_pddl(expr, param_map): False},
94
+ explanation=f"{_expr_to_pddl(expr, param_map)} is not true in the current state",
95
+ ))
96
+ except Exception:
97
+ failures.append(PreconditionFailure(
98
+ expression=_expr_to_pddl(expr, param_map),
99
+ type="boolean",
100
+ current_values={},
101
+ explanation=f"Could not evaluate {_expr_to_pddl(expr, param_map)}",
102
+ ))
103
+ return
104
+
105
+
106
+ def _evaluate_comparison(
107
+ expr: FNode,
108
+ state,
109
+ param_map: dict,
110
+ failures: list[PreconditionFailure],
111
+ ) -> None:
112
+ """Evaluate a numeric comparison, report failure with deficit."""
113
+ left_expr, right_expr = expr.args[0], expr.args[1]
114
+ left_grounded = _substitute(left_expr, param_map)
115
+ right_grounded = _substitute(right_expr, param_map)
116
+
117
+ try:
118
+ left_val = state.get_value(left_grounded).constant_value()
119
+ right_val = state.get_value(right_grounded).constant_value()
120
+ except Exception:
121
+ failures.append(PreconditionFailure(
122
+ expression=_expr_to_pddl(expr, param_map),
123
+ type="numeric",
124
+ current_values={},
125
+ explanation=f"Could not evaluate numeric comparison",
126
+ ))
127
+ return
128
+
129
+ op = expr.node_type
130
+ satisfied = _check_comparison(op, left_val, right_val)
131
+
132
+ if not satisfied:
133
+ current_values = {}
134
+ if left_expr.is_fluent_exp():
135
+ current_values[_expr_to_pddl(left_expr, param_map)] = left_val
136
+ if right_expr.is_fluent_exp():
137
+ current_values[_expr_to_pddl(right_expr, param_map)] = right_val
138
+
139
+ deficit = _compute_deficit(op, left_val, right_val)
140
+ op_symbol = _op_symbol(op)
141
+
142
+ failures.append(PreconditionFailure(
143
+ expression=_expr_to_pddl(expr, param_map),
144
+ type="numeric",
145
+ current_values=current_values,
146
+ explanation=(
147
+ f"Required: {_expr_to_pddl(left_expr, param_map)} "
148
+ f"{op_symbol} {_expr_to_pddl(right_expr, param_map)}"
149
+ ),
150
+ deficit=deficit,
151
+ ))
152
+
153
+
154
+ def _check_comparison(op: OperatorKind, left: float, right: float) -> bool:
155
+ eps = 1e-6
156
+ if op == OperatorKind.LE:
157
+ return left <= right + eps
158
+ if op == OperatorKind.LT:
159
+ return left < right
160
+ if op == OperatorKind.EQUALS:
161
+ return abs(left - right) < eps
162
+ return False
163
+
164
+
165
+ def _compute_deficit(op: OperatorKind, left: float, right: float) -> float:
166
+ if op == OperatorKind.LE:
167
+ return left - right
168
+ if op == OperatorKind.LT:
169
+ return left - right
170
+ if op == OperatorKind.EQUALS:
171
+ return abs(left - right)
172
+ return 0.0
173
+
174
+
175
+ def _op_symbol(op: OperatorKind) -> str:
176
+ return {
177
+ OperatorKind.LE: "<=",
178
+ OperatorKind.LT: "<",
179
+ OperatorKind.EQUALS: "=",
180
+ }.get(op, "?")
181
+
182
+
183
+ def _evaluate_goal(expr: FNode, state, results: list[GoalResult]) -> None:
184
+ """Evaluate a single goal expression, decomposing ANDs."""
185
+ if expr.node_type == OperatorKind.AND:
186
+ for arg in expr.args:
187
+ _evaluate_goal(arg, state, results)
188
+ return
189
+
190
+ try:
191
+ val = state.get_value(expr)
192
+ if val.is_bool_constant():
193
+ satisfied = val.is_true()
194
+ results.append(GoalResult(
195
+ expression=str(expr),
196
+ satisfied=satisfied,
197
+ current_values={str(expr): satisfied},
198
+ ))
199
+ else:
200
+ # Numeric goal (comparison)
201
+ satisfied = val.is_true() if val.is_bool_constant() else False
202
+ results.append(GoalResult(
203
+ expression=str(expr),
204
+ satisfied=satisfied,
205
+ current_values={str(expr): val.constant_value() if not val.is_bool_constant() else satisfied},
206
+ ))
207
+ except Exception:
208
+ # For complex expressions (comparisons), try to evaluate directly
209
+ satisfied = _evaluate_goal_expr(expr, state)
210
+ results.append(GoalResult(
211
+ expression=str(expr),
212
+ satisfied=satisfied,
213
+ current_values={},
214
+ ))
215
+
216
+
217
+ def _evaluate_goal_expr(expr: FNode, state) -> bool:
218
+ """Evaluate a goal expression that may be a comparison."""
219
+ if expr.node_type in (OperatorKind.LE, OperatorKind.LT, OperatorKind.EQUALS):
220
+ try:
221
+ left_val = state.get_value(expr.args[0]).constant_value()
222
+ right_val = state.get_value(expr.args[1]).constant_value()
223
+ return _check_comparison(expr.node_type, left_val, right_val)
224
+ except Exception:
225
+ return False
226
+
227
+ if expr.node_type == OperatorKind.NOT:
228
+ inner = expr.args[0]
229
+ try:
230
+ val = state.get_value(inner)
231
+ return val.is_false()
232
+ except Exception:
233
+ return False
234
+
235
+ try:
236
+ val = state.get_value(expr)
237
+ return val.is_true()
238
+ except Exception:
239
+ return False
240
+
241
+
242
+ def _substitute(expr: FNode, param_map: dict) -> FNode:
243
+ """Substitute action parameters with grounded objects in an expression."""
244
+ if not param_map:
245
+ return expr
246
+ return expr.substitute(param_map)
247
+
248
+
249
+ def _expr_to_pddl(expr: FNode, param_map: dict | None = None) -> str:
250
+ """Convert an FNode expression to a PDDL-like string."""
251
+ if param_map:
252
+ expr = _substitute(expr, param_map)
253
+ return str(expr)
pyval/models.py ADDED
@@ -0,0 +1,67 @@
1
+ """Data models for PyVAL validation results."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from typing import Any, Literal
7
+
8
+
9
+ @dataclass
10
+ class NumericChange:
11
+ before: float
12
+ after: float
13
+
14
+
15
+ @dataclass
16
+ class PreconditionFailure:
17
+ expression: str
18
+ type: Literal["boolean", "numeric"]
19
+ current_values: dict[str, Any]
20
+ explanation: str
21
+ deficit: float | None = None
22
+
23
+
24
+ @dataclass
25
+ class GoalResult:
26
+ expression: str
27
+ satisfied: bool
28
+ current_values: dict[str, Any] = field(default_factory=dict)
29
+
30
+
31
+ @dataclass
32
+ class StateSnapshot:
33
+ step: int
34
+ action: str | None
35
+ boolean_fluents: dict[str, bool] = field(default_factory=dict)
36
+ numeric_fluents: dict[str, float] = field(default_factory=dict)
37
+
38
+
39
+ @dataclass
40
+ class StepResult:
41
+ index: int
42
+ action: str
43
+ status: Literal["OK", "FAILED"]
44
+ boolean_changes: dict[str, bool] = field(default_factory=dict)
45
+ numeric_changes: dict[str, NumericChange] = field(default_factory=dict)
46
+ unsatisfied: list[PreconditionFailure] = field(default_factory=list)
47
+
48
+
49
+ @dataclass
50
+ class ValidationResult:
51
+ status: Literal["VALID", "INVALID", "SYNTAX_ERROR", "STRUCTURE_ERROR"]
52
+ is_valid: bool
53
+ phases: dict = field(default_factory=dict)
54
+ steps: list[StepResult] = field(default_factory=list)
55
+ trajectory: list[StateSnapshot] = field(default_factory=list)
56
+ numeric_trajectory: dict[str, list] = field(default_factory=dict)
57
+ failed_step: int | None = None
58
+ unsatisfied_goals: list[GoalResult] = field(default_factory=list)
59
+ warnings: list[str] = field(default_factory=list)
60
+
61
+ def report(self, verbose: bool = False) -> str:
62
+ from pyval.report_formatter import format_plain_text
63
+ return format_plain_text(self, verbose=verbose)
64
+
65
+ def to_json(self) -> dict:
66
+ from pyval.report_formatter import format_json
67
+ return format_json(self)
@@ -0,0 +1,77 @@
1
+ """Numeric fluent value tracking across plan steps."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unified_planning.model import Problem
6
+
7
+ from pyval.models import StateSnapshot
8
+
9
+
10
+ class NumericTracker:
11
+ """Tracks numeric (and boolean) fluent values across plan execution steps."""
12
+
13
+ def __init__(
14
+ self, problem: Problem, tracked_fluents: list[str] | None = None
15
+ ):
16
+ self._problem = problem
17
+ self._snapshots: list[StateSnapshot] = []
18
+
19
+ # Identify all fluent expressions from initial values
20
+ self._boolean_exprs = []
21
+ self._numeric_exprs = []
22
+ for fluent_expr, val in problem.initial_values.items():
23
+ if val.is_bool_constant():
24
+ self._boolean_exprs.append(fluent_expr)
25
+ else:
26
+ self._numeric_exprs.append(fluent_expr)
27
+
28
+ # Filter numeric fluents if tracking specific ones
29
+ if tracked_fluents is not None:
30
+ tracked_set = set(tracked_fluents)
31
+ self._numeric_exprs = [
32
+ e for e in self._numeric_exprs
33
+ if _fluent_display_name(e) in tracked_set
34
+ ]
35
+
36
+ def record(self, step: int, action: str | None, state) -> StateSnapshot:
37
+ """Record a state snapshot at a given step."""
38
+ boolean_fluents = {}
39
+ for expr in self._boolean_exprs:
40
+ val = state.get_value(expr)
41
+ boolean_fluents[str(expr)] = val.is_true()
42
+
43
+ numeric_fluents = {}
44
+ for expr in self._numeric_exprs:
45
+ val = state.get_value(expr)
46
+ numeric_fluents[str(expr)] = float(val.constant_value())
47
+
48
+ snapshot = StateSnapshot(
49
+ step=step,
50
+ action=action,
51
+ boolean_fluents=boolean_fluents,
52
+ numeric_fluents=numeric_fluents,
53
+ )
54
+ self._snapshots.append(snapshot)
55
+ return snapshot
56
+
57
+ def get_numeric_trajectory(self) -> dict[str, list[float]]:
58
+ """Return {fluent_name: [val_step0, val_step1, ...]}."""
59
+ if not self._snapshots:
60
+ return {}
61
+
62
+ # Use the first snapshot's numeric keys as the canonical set
63
+ keys = list(self._snapshots[0].numeric_fluents.keys())
64
+ trajectory: dict[str, list[float]] = {k: [] for k in keys}
65
+ for snap in self._snapshots:
66
+ for k in keys:
67
+ trajectory[k].append(snap.numeric_fluents.get(k, 0.0))
68
+ return trajectory
69
+
70
+
71
+ def _fluent_display_name(expr) -> str:
72
+ """Extract a display name like 'fuel truck1' from a fluent expression."""
73
+ if expr.is_fluent_exp():
74
+ name = expr.fluent().name
75
+ args = " ".join(str(a) for a in expr.args)
76
+ return f"{name} {args}".strip() if args else name
77
+ return str(expr)
@@ -0,0 +1,121 @@
1
+ """Phase 3: Step-by-step plan execution with diagnostics."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import warnings
6
+
7
+ from unified_planning.engines import UPSequentialSimulator
8
+ from unified_planning.model import Problem
9
+ from unified_planning.plans import ActionInstance
10
+
11
+ from pyval.diagnostics import check_goals, decompose_preconditions
12
+ from pyval.models import GoalResult, NumericChange, StateSnapshot, StepResult
13
+ from pyval.numeric_tracker import NumericTracker
14
+
15
+
16
+ def simulate(
17
+ problem: Problem,
18
+ action_instances: list[ActionInstance],
19
+ original_names: dict[str, str],
20
+ tracked_fluents: list[str] | None = None,
21
+ ) -> tuple[list[StepResult], list[StateSnapshot], list[GoalResult], dict[str, list]]:
22
+ """Simulate plan execution step-by-step.
23
+
24
+ Returns (steps, trajectory, goal_results, numeric_trajectory).
25
+ """
26
+ # Suppress UPF warnings about unsupported problem kinds
27
+ with warnings.catch_warnings():
28
+ warnings.simplefilter("ignore")
29
+ sim = UPSequentialSimulator(problem=problem, error_on_failed_checks=False)
30
+
31
+ state = sim.get_initial_state()
32
+ tracker = NumericTracker(problem, tracked_fluents)
33
+ tracker.record(0, None, state)
34
+
35
+ steps: list[StepResult] = []
36
+
37
+ for idx, ai in enumerate(action_instances, start=1):
38
+ action_display = _format_action(ai, original_names)
39
+
40
+ if sim.is_applicable(state, ai):
41
+ new_state = sim.apply(state, ai)
42
+ if new_state is None:
43
+ # apply() returned None — treat as inapplicable (per upf-gotchas.md)
44
+ failures = decompose_preconditions(
45
+ ai.action, ai.actual_parameters, state, problem
46
+ )
47
+ steps.append(StepResult(
48
+ index=idx,
49
+ action=action_display,
50
+ status="FAILED",
51
+ unsatisfied=failures,
52
+ ))
53
+ break
54
+
55
+ boolean_changes, numeric_changes = _compute_changes(
56
+ problem, state, new_state
57
+ )
58
+ steps.append(StepResult(
59
+ index=idx,
60
+ action=action_display,
61
+ status="OK",
62
+ boolean_changes=boolean_changes,
63
+ numeric_changes=numeric_changes,
64
+ ))
65
+ state = new_state
66
+ tracker.record(idx, action_display, state)
67
+ else:
68
+ failures = decompose_preconditions(
69
+ ai.action, ai.actual_parameters, state, problem
70
+ )
71
+ steps.append(StepResult(
72
+ index=idx,
73
+ action=action_display,
74
+ status="FAILED",
75
+ unsatisfied=failures,
76
+ ))
77
+ # Record snapshot at failure point too
78
+ tracker.record(idx, action_display, state)
79
+ break
80
+
81
+ goal_results = check_goals(problem, state)
82
+
83
+ return (
84
+ steps,
85
+ tracker._snapshots,
86
+ goal_results,
87
+ tracker.get_numeric_trajectory(),
88
+ )
89
+
90
+
91
+ def _compute_changes(
92
+ problem: Problem, old_state, new_state
93
+ ) -> tuple[dict[str, bool], dict[str, NumericChange]]:
94
+ """Compute fluent changes between two states."""
95
+ boolean_changes: dict[str, bool] = {}
96
+ numeric_changes: dict[str, NumericChange] = {}
97
+
98
+ for fluent_expr in problem.initial_values:
99
+ old_val = old_state.get_value(fluent_expr)
100
+ new_val = new_state.get_value(fluent_expr)
101
+
102
+ if str(old_val) != str(new_val):
103
+ name = str(fluent_expr)
104
+ if old_val.is_bool_constant():
105
+ boolean_changes[name] = new_val.is_true()
106
+ else:
107
+ numeric_changes[name] = NumericChange(
108
+ before=float(old_val.constant_value()),
109
+ after=float(new_val.constant_value()),
110
+ )
111
+
112
+ return boolean_changes, numeric_changes
113
+
114
+
115
+ def _format_action(ai: ActionInstance, original_names: dict[str, str]) -> str:
116
+ """Format an ActionInstance as a PDDL plan line using original names."""
117
+ action_name = original_names.get(ai.action.name, ai.action.name)
118
+ params = " ".join(
119
+ original_names.get(str(p), str(p)) for p in ai.actual_parameters
120
+ )
121
+ return f"({action_name} {params})" if params else f"({action_name})"
@@ -0,0 +1,249 @@
1
+ """Output formatting: plain text, structured JSON, and trajectory table."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from typing import TYPE_CHECKING
7
+
8
+ if TYPE_CHECKING:
9
+ from pyval.models import ValidationResult
10
+
11
+
12
+ def format_plain_text(result: ValidationResult, verbose: bool = False) -> str:
13
+ """Format validation result as human-readable plain text."""
14
+ lines: list[str] = []
15
+
16
+ # Phase 1: Syntax
17
+ if "syntax" in result.phases:
18
+ phase = result.phases["syntax"]
19
+ if phase["errors"] or phase["warnings"] or verbose:
20
+ lines.append("=== Syntax & Semantic Validation ===")
21
+ for err in phase["errors"]:
22
+ lines.append(f"[ERROR] {err}")
23
+ for warn in phase["warnings"]:
24
+ lines.append(f"[WARNING] {warn}")
25
+ if not phase["errors"] and not phase["warnings"]:
26
+ lines.append("All checks passed.")
27
+ lines.append("")
28
+
29
+ if result.status == "SYNTAX_ERROR":
30
+ lines.append(f"Validation halted: {result.status}")
31
+ return "\n".join(lines)
32
+
33
+ # Phase 2: Structure
34
+ if "structure" in result.phases:
35
+ phase = result.phases["structure"]
36
+ if phase["errors"] or phase["warnings"] or verbose:
37
+ lines.append("=== Plan Structure Validation ===")
38
+ for err in phase["errors"]:
39
+ lines.append(f"[ERROR] {err}")
40
+ for warn in phase["warnings"]:
41
+ lines.append(f"[WARNING] {warn}")
42
+ if not phase["errors"] and not phase["warnings"]:
43
+ lines.append("All checks passed.")
44
+ lines.append("")
45
+
46
+ if result.status == "STRUCTURE_ERROR":
47
+ lines.append(f"Validation halted: {result.status}")
48
+ return "\n".join(lines)
49
+
50
+ # Phase 3: Execution
51
+ if result.steps:
52
+ lines.append("=== Plan Execution ===")
53
+ for step in result.steps:
54
+ if step.status == "OK":
55
+ lines.append(f"Step {step.index}: {step.action} \u2713")
56
+ changes = _format_changes(step)
57
+ if changes:
58
+ lines.append(f" Changed: {changes}")
59
+ else:
60
+ lines.append(f"Step {step.index}: {step.action} \u2717 PRECONDITION FAILURE")
61
+ for failure in step.unsatisfied:
62
+ lines.append(f" Unsatisfied: {failure.expression}")
63
+ for k, v in failure.current_values.items():
64
+ lines.append(f" Current value: {k} = {v}")
65
+ if failure.explanation:
66
+ lines.append(f" {failure.explanation}")
67
+ if failure.deficit is not None and failure.deficit > 0:
68
+ lines.append(f" Deficit: {failure.deficit} units")
69
+ lines.append("")
70
+
71
+ # Goal check
72
+ lines.append("=== Goal Check ===")
73
+ if result.is_valid:
74
+ lines.append("All goals satisfied. Plan is VALID.")
75
+ else:
76
+ if result.failed_step is not None:
77
+ total = result.phases.get("execution", {}).get("total_steps", "?")
78
+ lines.append(
79
+ f"Plan is INVALID. Failed at step {result.failed_step} of {total}."
80
+ )
81
+ remaining = int(total) - result.failed_step if isinstance(total, int) else "?"
82
+ lines.append(f"Remaining actions not executed: {remaining}")
83
+ elif result.unsatisfied_goals:
84
+ lines.append("Plan executed but goals are NOT satisfied.")
85
+ for goal in result.unsatisfied_goals:
86
+ lines.append(f" Unmet goal: {goal.expression}")
87
+ for k, v in goal.current_values.items():
88
+ lines.append(f" Current value: {v}")
89
+ else:
90
+ lines.append(f"Plan is {result.status}.")
91
+
92
+ # Summary
93
+ if result.steps:
94
+ lines.append(f"Plan length: {len(result.steps)} actions")
95
+
96
+ # Numeric summary
97
+ if result.numeric_trajectory:
98
+ numeric_finals = {}
99
+ for fluent, values in result.numeric_trajectory.items():
100
+ if values:
101
+ numeric_finals[fluent] = values[-1]
102
+ if numeric_finals:
103
+ parts = [f"{k} = {v}" for k, v in numeric_finals.items()]
104
+ lines.append(f"Final state numeric values: {', '.join(parts)}")
105
+
106
+ return "\n".join(lines)
107
+
108
+
109
+ def format_json(result: ValidationResult) -> dict:
110
+ """Format validation result as structured JSON dict."""
111
+ output: dict = {
112
+ "status": result.status,
113
+ "phases": {},
114
+ }
115
+
116
+ # Syntax phase
117
+ if "syntax" in result.phases:
118
+ output["phases"]["syntax"] = result.phases["syntax"]
119
+
120
+ # Structure phase
121
+ if "structure" in result.phases:
122
+ output["phases"]["structure"] = result.phases["structure"]
123
+
124
+ # Execution phase
125
+ if "execution" in result.phases:
126
+ exec_phase: dict = {
127
+ "status": result.phases["execution"]["status"],
128
+ "failed_step": result.failed_step,
129
+ "total_steps": result.phases["execution"].get("total_steps"),
130
+ "steps": [],
131
+ }
132
+
133
+ for step in result.steps:
134
+ step_dict: dict = {
135
+ "index": step.index,
136
+ "action": step.action,
137
+ "status": step.status,
138
+ }
139
+
140
+ if step.status == "OK":
141
+ step_dict["changes"] = {
142
+ "boolean": step.boolean_changes,
143
+ "numeric": {
144
+ k: {"before": v.before, "after": v.after}
145
+ for k, v in step.numeric_changes.items()
146
+ },
147
+ }
148
+ else:
149
+ step_dict["unsatisfied_preconditions"] = [
150
+ {
151
+ "expression": f.expression,
152
+ "type": f.type,
153
+ "current_values": f.current_values,
154
+ "explanation": f.explanation,
155
+ **({"deficit": f.deficit} if f.deficit is not None else {}),
156
+ }
157
+ for f in step.unsatisfied
158
+ ]
159
+
160
+ exec_phase["steps"].append(step_dict)
161
+ output["phases"]["execution"] = exec_phase
162
+
163
+ # Goals
164
+ if result.unsatisfied_goals is not None:
165
+ goals = [
166
+ {
167
+ "expression": g.expression,
168
+ "satisfied": g.satisfied,
169
+ "current_values": g.current_values,
170
+ }
171
+ for g in result.unsatisfied_goals
172
+ ]
173
+ output["phases"]["goals"] = goals if goals else None
174
+ else:
175
+ output["phases"]["goals"] = None
176
+
177
+ return output
178
+
179
+
180
+ def _format_changes(step) -> str:
181
+ """Format boolean and numeric changes for a step as a compact string."""
182
+ parts: list[str] = []
183
+ for name, val in step.boolean_changes.items():
184
+ parts.append(f"{name} = {str(val).lower()}")
185
+ for name, change in step.numeric_changes.items():
186
+ parts.append(f"{name} = {change.after} (was {change.before})")
187
+ return ", ".join(parts)
188
+
189
+
190
+ def format_trajectory(
191
+ result: ValidationResult, tracked: list[str] | None = None
192
+ ) -> str:
193
+ """Format numeric fluent trajectory as a table."""
194
+ traj = result.numeric_trajectory
195
+ if not traj:
196
+ return "No numeric fluents to display."
197
+
198
+ # Filter if specific fluents requested
199
+ if tracked:
200
+ tracked_set = set(tracked)
201
+ traj = {k: v for k, v in traj.items() if k in tracked_set}
202
+ if not traj:
203
+ return "No matching numeric fluents found."
204
+
205
+ fluent_names = list(traj.keys())
206
+ num_steps = max(len(v) for v in traj.values()) if traj else 0
207
+
208
+ # Build action labels from trajectory snapshots
209
+ action_labels = []
210
+ for snap in result.trajectory:
211
+ action_labels.append(snap.action or "[initial state]")
212
+
213
+ # Column widths
214
+ step_width = max(4, len(str(num_steps)))
215
+ action_width = max(6, max((len(a) for a in action_labels), default=6))
216
+ col_widths = {
217
+ name: max(len(name), max((len(str(v)) for v in vals), default=1))
218
+ for name, vals in traj.items()
219
+ }
220
+
221
+ lines: list[str] = []
222
+ lines.append("=== Numeric Fluent Trajectory ===")
223
+
224
+ # Header
225
+ header = (
226
+ f"{'Step':>{step_width}} | {'Action':<{action_width}}"
227
+ )
228
+ for name in fluent_names:
229
+ header += f" | {name:>{col_widths[name]}}"
230
+ lines.append(header)
231
+
232
+ # Separator
233
+ sep = "-" * (step_width + 1) + "|" + "-" * (action_width + 2)
234
+ for name in fluent_names:
235
+ sep += "|" + "-" * (col_widths[name] + 1) + ":"
236
+ lines.append(sep)
237
+
238
+ # Data rows
239
+ for i in range(num_steps):
240
+ action = action_labels[i] if i < len(action_labels) else ""
241
+ row = f"{i:>{step_width}} | {action:<{action_width}}"
242
+ for name in fluent_names:
243
+ vals = traj[name]
244
+ val = vals[i] if i < len(vals) else ""
245
+ val_str = str(int(val)) if isinstance(val, float) and val == int(val) else str(val)
246
+ row += f" | {val_str:>{col_widths[name]}}"
247
+ lines.append(row)
248
+
249
+ return "\n".join(lines)
@@ -0,0 +1,63 @@
1
+ """Phase 1: PDDL syntax and semantic validation via UPF's PDDLReader."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unified_planning.io import PDDLReader
6
+ from unified_planning.model import Problem
7
+
8
+
9
+ def check_syntax(
10
+ domain_path: str, problem_path: str | None = None
11
+ ) -> tuple[dict, Problem | None]:
12
+ """Parse and validate PDDL domain (and optionally problem).
13
+
14
+ Returns a tuple of (phase_result, parsed_problem).
15
+ phase_result has keys: status ("PASS"|"FAIL"), errors (list[str]), warnings (list[str]).
16
+ parsed_problem is None if parsing failed.
17
+ """
18
+ errors: list[str] = []
19
+ warnings: list[str] = []
20
+
21
+ reader = PDDLReader()
22
+ try:
23
+ problem = reader.parse_problem(domain_path, problem_path)
24
+ except Exception as exc:
25
+ errors.append(_format_parse_error(exc, domain_path, problem_path))
26
+ return {"status": "FAIL", "errors": errors, "warnings": warnings}, None
27
+
28
+ # Post-parse checks: warn on uninitialized numeric functions
29
+ warnings.extend(_check_numeric_initialization(problem))
30
+
31
+ return {"status": "PASS", "errors": errors, "warnings": warnings}, problem
32
+
33
+
34
+ def _format_parse_error(
35
+ exc: Exception, domain_path: str, problem_path: str | None
36
+ ) -> str:
37
+ """Format a UPF parse exception into a readable error message."""
38
+ msg = str(exc).strip()
39
+ if problem_path:
40
+ return f"Failed to parse domain '{domain_path}' with problem '{problem_path}': {msg}"
41
+ return f"Failed to parse domain '{domain_path}': {msg}"
42
+
43
+
44
+ def _check_numeric_initialization(problem: Problem) -> list[str]:
45
+ """Warn about numeric fluents with no explicit initial value (default 0)."""
46
+ warnings = []
47
+ declared_numeric_fluents = set()
48
+ initialized_fluents = set()
49
+
50
+ for fluent in problem.fluents:
51
+ if fluent.type.is_int_type() or fluent.type.is_real_type():
52
+ declared_numeric_fluents.add(fluent.name)
53
+
54
+ for fluent_expr in problem.initial_values:
55
+ if fluent_expr.is_fluent_exp():
56
+ initialized_fluents.add(fluent_expr.fluent().name)
57
+
58
+ for name in declared_numeric_fluents - initialized_fluents:
59
+ warnings.append(
60
+ f"Numeric function '{name}' has no initial value assigned — defaults to 0"
61
+ )
62
+
63
+ return warnings
pyval/validator.py ADDED
@@ -0,0 +1,250 @@
1
+ """Main validation orchestrator — coordinates the 3-phase pipeline."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from unified_planning.model import Problem
6
+ from unified_planning.plans import ActionInstance
7
+
8
+ from pyval.models import ValidationResult
9
+ from pyval.syntax_checker import check_syntax
10
+
11
+
12
+ class PDDLValidator:
13
+ """Pure-Python PDDL plan validator."""
14
+
15
+ def validate_syntax(
16
+ self, domain_path: str, problem_path: str | None = None
17
+ ) -> ValidationResult:
18
+ """Run Phase 1 only: syntax and semantic validation."""
19
+ phase_result, problem = check_syntax(domain_path, problem_path)
20
+
21
+ if phase_result["status"] == "FAIL":
22
+ return ValidationResult(
23
+ status="SYNTAX_ERROR",
24
+ is_valid=False,
25
+ phases={"syntax": phase_result},
26
+ warnings=phase_result["warnings"],
27
+ )
28
+
29
+ return ValidationResult(
30
+ status="VALID",
31
+ is_valid=True,
32
+ phases={"syntax": phase_result},
33
+ warnings=phase_result["warnings"],
34
+ )
35
+
36
+ def validate(
37
+ self,
38
+ domain_path: str,
39
+ problem_path: str,
40
+ plan_path: str,
41
+ tracked_fluents: list[str] | None = None,
42
+ ) -> ValidationResult:
43
+ """Run full 3-phase validation pipeline."""
44
+ # Phase 1: Syntax & Semantic
45
+ phase1_result, problem = check_syntax(domain_path, problem_path)
46
+ phases: dict = {"syntax": phase1_result}
47
+
48
+ if phase1_result["status"] == "FAIL":
49
+ return ValidationResult(
50
+ status="SYNTAX_ERROR",
51
+ is_valid=False,
52
+ phases=phases,
53
+ warnings=phase1_result["warnings"],
54
+ )
55
+
56
+ assert problem is not None
57
+
58
+ # Phase 2: Plan Structure
59
+ phase2_result, action_instances, original_names = _validate_plan_structure(
60
+ problem, plan_path
61
+ )
62
+ phases["structure"] = phase2_result
63
+
64
+ if phase2_result["status"] == "FAIL":
65
+ return ValidationResult(
66
+ status="STRUCTURE_ERROR",
67
+ is_valid=False,
68
+ phases=phases,
69
+ warnings=phase1_result["warnings"] + phase2_result["warnings"],
70
+ )
71
+
72
+ # Phase 3: Plan Execution
73
+ from pyval.plan_simulator import simulate
74
+
75
+ steps, trajectory, goal_results, numeric_traj = simulate(
76
+ problem, action_instances, original_names, tracked_fluents
77
+ )
78
+
79
+ failed_step = next(
80
+ (s.index for s in steps if s.status == "FAILED"), None
81
+ )
82
+ unsatisfied_goals = [g for g in goal_results if not g.satisfied]
83
+ is_valid = failed_step is None and len(unsatisfied_goals) == 0
84
+
85
+ exec_status = "PASS" if is_valid else "FAIL"
86
+ phases["execution"] = {
87
+ "status": exec_status,
88
+ "failed_step": failed_step,
89
+ "total_steps": len(action_instances),
90
+ }
91
+
92
+ all_warnings = phase1_result["warnings"] + phase2_result["warnings"]
93
+
94
+ return ValidationResult(
95
+ status="VALID" if is_valid else "INVALID",
96
+ is_valid=is_valid,
97
+ phases=phases,
98
+ steps=steps,
99
+ trajectory=trajectory,
100
+ numeric_trajectory=numeric_traj,
101
+ failed_step=failed_step,
102
+ unsatisfied_goals=unsatisfied_goals,
103
+ warnings=all_warnings,
104
+ )
105
+
106
+
107
+ def _validate_plan_structure(
108
+ problem: Problem, plan_path: str
109
+ ) -> tuple[dict, list[ActionInstance], dict[str, str]]:
110
+ """Phase 2: parse plan file and validate action/object/type references.
111
+
112
+ Returns (phase_result, action_instances, original_names).
113
+ original_names maps normalized (underscore) names to original PDDL names.
114
+ """
115
+ errors: list[str] = []
116
+ warnings: list[str] = []
117
+ action_instances: list[ActionInstance] = []
118
+ original_names: dict[str, str] = {}
119
+
120
+ lines = _read_plan_lines(plan_path)
121
+
122
+ if not lines:
123
+ # Empty plan — valid only if goals already satisfied
124
+ # (goal checking is Phase 3's job, so we pass it through)
125
+ return (
126
+ {"status": "PASS", "errors": errors, "warnings": warnings},
127
+ action_instances,
128
+ original_names,
129
+ )
130
+
131
+ for step_idx, line in enumerate(lines, start=1):
132
+ tokens = _parse_plan_line(line)
133
+ if tokens is None:
134
+ continue
135
+
136
+ action_name_raw = tokens[0]
137
+ param_names_raw = tokens[1:]
138
+
139
+ # Look up action schema — try original name, then normalized
140
+ action_schema = _lookup_action(problem, action_name_raw)
141
+ if action_schema is None:
142
+ errors.append(
143
+ f"Step {step_idx}: Action '{action_name_raw}' is not declared in domain"
144
+ )
145
+ continue
146
+ original_names[action_schema.name] = action_name_raw
147
+
148
+ # Check parameter count
149
+ expected_count = len(action_schema.parameters)
150
+ if len(param_names_raw) != expected_count:
151
+ errors.append(
152
+ f"Step {step_idx}: Action '{action_name_raw}' expects "
153
+ f"{expected_count} parameters, got {len(param_names_raw)}"
154
+ )
155
+ continue
156
+
157
+ # Look up each parameter object and check types
158
+ objects = []
159
+ step_ok = True
160
+ for i, param_name_raw in enumerate(param_names_raw):
161
+ obj = _lookup_object(problem, param_name_raw)
162
+ if obj is None:
163
+ errors.append(
164
+ f"Step {step_idx}: Action '{action_name_raw}' — "
165
+ f"object '{param_name_raw}' is not declared in problem"
166
+ )
167
+ step_ok = False
168
+ continue
169
+ original_names[obj.name] = param_name_raw
170
+
171
+ expected_type = action_schema.parameters[i].type
172
+ if not obj.type.is_compatible(expected_type):
173
+ errors.append(
174
+ f"Step {step_idx}: Action '{action_name_raw}' — "
175
+ f"parameter {i + 1} expects type '{expected_type}', "
176
+ f"got '{obj.type}' (object '{param_name_raw}')"
177
+ )
178
+ step_ok = False
179
+ continue
180
+
181
+ objects.append(obj)
182
+
183
+ if step_ok:
184
+ action_instances.append(ActionInstance(action_schema, tuple(objects)))
185
+
186
+ status = "FAIL" if errors else "PASS"
187
+ return (
188
+ {"status": status, "errors": errors, "warnings": warnings},
189
+ action_instances,
190
+ original_names,
191
+ )
192
+
193
+
194
+ def _read_plan_lines(plan_path: str) -> list[str]:
195
+ """Read plan file, strip comments and blank lines."""
196
+ with open(plan_path) as f:
197
+ lines = []
198
+ for line in f:
199
+ line = line.strip()
200
+ if not line or line.startswith(";"):
201
+ continue
202
+ # Strip inline cost comments
203
+ if ";" in line:
204
+ line = line[: line.index(";")].strip()
205
+ if line:
206
+ lines.append(line)
207
+ return lines
208
+
209
+
210
+ def _parse_plan_line(line: str) -> list[str] | None:
211
+ """Parse a plan line into [action_name, param1, param2, ...].
212
+
213
+ Handles both `(action p1 p2)` and `action p1 p2` formats.
214
+ """
215
+ line = line.strip()
216
+ if line.startswith("(") and line.endswith(")"):
217
+ line = line[1:-1]
218
+ tokens = line.split()
219
+ if not tokens:
220
+ return None
221
+ return tokens
222
+
223
+
224
+ def _lookup_action(problem: Problem, raw_name: str):
225
+ """Look up an action schema, trying original name then hyphen-normalized."""
226
+ for candidate in _name_candidates(raw_name):
227
+ try:
228
+ return problem.action(candidate)
229
+ except Exception:
230
+ continue
231
+ return None
232
+
233
+
234
+ def _lookup_object(problem: Problem, raw_name: str):
235
+ """Look up a problem object, trying original name then hyphen-normalized."""
236
+ for candidate in _name_candidates(raw_name):
237
+ try:
238
+ return problem.object(candidate)
239
+ except Exception:
240
+ continue
241
+ return None
242
+
243
+
244
+ def _name_candidates(name: str) -> list[str]:
245
+ """Return lookup candidates: original, then normalized (if different)."""
246
+ candidates = [name]
247
+ normalized = name.replace("-", "_")
248
+ if normalized != name:
249
+ candidates.append(normalized)
250
+ return candidates