1818import py_trees
1919from py_trees .common import Access , Status
2020from pkg_resources import iter_entry_points
21-
2221import inspect
2322
24- from scenario_execution .model .types import ActionDeclaration , EventReference , FunctionApplicationExpression , ModifierInvocation , ScenarioDeclaration , DoMember , WaitDirective , EmitDirective , BehaviorInvocation , EventCondition , EventDeclaration , RelationExpression , LogicalExpression , ElapsedExpression , PhysicalLiteral , ModifierDeclaration
23+ from scenario_execution .model .types import KeepConstraintDeclaration , visit_expression , ActionDeclaration , BinaryExpression , EventReference , Expression , FunctionApplicationExpression , ModifierInvocation , ScenarioDeclaration , DoMember , WaitDirective , EmitDirective , BehaviorInvocation , EventCondition , EventDeclaration , RelationExpression , LogicalExpression , ElapsedExpression , PhysicalLiteral , ModifierDeclaration
2524from scenario_execution .model .model_base_visitor import ModelBaseVisitor
2625from scenario_execution .model .error import OSC2ParsingError
2726from scenario_execution .actions .base_action import BaseAction
@@ -103,6 +102,20 @@ def update(self):
103102 return Status .SUCCESS
104103
105104
105+ class ExpressionBehavior (py_trees .behaviour .Behaviour ):
106+
107+ def __init__ (self , name : "ExpressionBehavior" , expression : Expression ):
108+ super ().__init__ (name )
109+
110+ self .expression = expression
111+
112+ def update (self ):
113+ if self .expression .eval ():
114+ return Status .SUCCESS
115+ else :
116+ return Status .RUNNING
117+
118+
106119class ModelToPyTree (object ):
107120
108121 def __init__ (self , logger ):
@@ -122,6 +135,7 @@ class BehaviorInit(ModelBaseVisitor):
122135 def __init__ (self , logger , tree ) -> None :
123136 super ().__init__ ()
124137 self .logger = logger
138+ self .blackboard = None
125139 if not isinstance (tree , py_trees .composites .Sequence ):
126140 raise ValueError ("ModelToPyTree requires a py-tree sequence as input" )
127141 self .tree = tree
@@ -348,19 +362,25 @@ def visit_event_reference(self, node: EventReference):
348362 def visit_event_condition (self , node : EventCondition ):
349363 expression = ""
350364 for child in node .get_children ():
351- if isinstance (child , RelationExpression ):
352- raise NotImplementedError ()
353- elif isinstance (child , LogicalExpression ):
354- raise NotImplementedError ()
365+ if isinstance (child , (RelationExpression , LogicalExpression )):
366+ expression = ExpressionBehavior (name = node .get_ctx ()[2 ], expression = self .visit (child ))
355367 elif isinstance (child , ElapsedExpression ):
356368 elapsed_condition = self .visit_elapsed_expression (child )
357- expression = py_trees .timers .Timer (
358- name = f"wait { elapsed_condition } s" , duration = float (elapsed_condition ))
369+ expression = py_trees .timers .Timer (name = f"wait { elapsed_condition } s" , duration = float (elapsed_condition ))
359370 else :
360371 raise OSC2ParsingError (
361372 msg = f'Invalid event condition { child } ' , context = node .get_ctx ())
362373 return expression
363374
375+ def visit_relation_expression (self , node : RelationExpression ):
376+ return visit_expression (node , self .blackboard )
377+
378+ def visit_logical_expression (self , node : LogicalExpression ):
379+ return visit_expression (node , self .blackboard )
380+
381+ def visit_binary_expression (self , node : BinaryExpression ):
382+ return visit_expression (node , self .blackboard )
383+
364384 def visit_elapsed_expression (self , node : ElapsedExpression ):
365385 elem = node .find_first_child_of_type (PhysicalLiteral )
366386 if not elem :
@@ -389,3 +409,7 @@ def visit_modifier_invocation(self, node: ModifierInvocation):
389409 self .create_decorator (node .modifier , resolved_values )
390410 except ValueError as e :
391411 raise OSC2ParsingError (msg = f'ModifierDeclaration { e } .' , context = node .get_ctx ()) from e
412+
413+ def visit_keep_constraint_declaration (self , node : KeepConstraintDeclaration ):
414+ # skip relation-expression
415+ pass
0 commit comments