Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requirement Boolean Negation Fix #289

Merged
merged 7 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/scenic/core/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import rv_ltl
import trimesh

from scenic.core.distributions import Samplable, needsSampling
from scenic.core.distributions import Samplable, needsSampling, toDistribution
from scenic.core.errors import InvalidScenarioError
from scenic.core.lazy_eval import needsLazyEvaluation
from scenic.core.propositions import Atomic, PropositionNode
Expand Down Expand Up @@ -71,6 +71,10 @@ def compile(self, namespace, scenario, syntax=None):
bindings, ego, line = self.bindings, self.egoObject, self.line
condition, ty = self.condition, self.ty

# Convert bound values to distributions as needed
for name, value in bindings.items():
bindings[name] = toDistribution(value)

# Check whether requirement implies any relations used for pruning
canPrune = condition.check_constrains_sampling()
if canPrune:
Expand Down
11 changes: 10 additions & 1 deletion src/scenic/syntax/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class PropositionTransformer(Transformer):
def __init__(self, filename="<unknown>") -> None:
super().__init__(filename)
self.nextSyntaxId = 0
self.in_atomic = False
Eric-Vin marked this conversation as resolved.
Show resolved Hide resolved

def transform(
self, node: ast.AST, nextSyntaxId=0
Expand All @@ -260,6 +261,14 @@ def transform(
newNode = self._create_atomic_proposition_factory(node)
return newNode, self.nextSyntaxId

def generic_visit(self, node):
# Don't recurse inside atomics.
old_in_atomic = self.in_atomic
self.in_atomic = True
super_val = super().generic_visit(node)
self.in_atomic = old_in_atomic
return super_val

def _register_requirement_syntax(self, syntax):
"""register requirement syntax for later use
returns an ID for retrieving the syntax
Expand Down Expand Up @@ -337,7 +346,7 @@ def visit_BoolOp(self, node: ast.BoolOp) -> ast.AST:

def visit_UnaryOp(self, node):
# rewrite `not` in requirements into a proposition factory
if not isinstance(node.op, ast.Not):
if not isinstance(node.op, ast.Not) or self.in_atomic:
return self.generic_visit(node)

lineNum = ast.Constant(node.lineno)
Expand Down
11 changes: 11 additions & 0 deletions tests/syntax/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,14 @@ def test_random_occlusion():
hasattr(obj, "name") and obj.name == "wall" and (not obj.occluding)
for obj in scene.objects
)


def test_deep_not():
"""Test that a not deep inside a requirement is interpreted correctly."""
with pytest.raises(RejectionException):
sampleSceneFrom(
"""
objs = [new Object at 10@10, new Object at 20@20]
require all(not o.x > 0 for o in objs)
"""
)
Loading