Skip to content

Commit

Permalink
Merge branch 'cpp_refactoring' into refactoring-dejan
Browse files Browse the repository at this point in the history
  • Loading branch information
TomyYamy committed Mar 28, 2022
2 parents d1536b9 + 8875c89 commit c920445
Show file tree
Hide file tree
Showing 9 changed files with 1,022 additions and 13 deletions.
1 change: 1 addition & 0 deletions rtamt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from rtamt.spec.stl.discrete_time.specification import STLDiscreteTimeSpecification as STLSpecification
from rtamt.spec.stl.discrete_time.specification import StlDiscreteTimeOfflineSpecification
from rtamt.spec.stl.discrete_time.specification import StlDiscreteTimeOnlineSpecification
from rtamt.spec.stl.discrete_time.specification import StlDiscreteTimeOnlineSpecificationCpp
from rtamt.spec.stl.dense_time.specification import STLDenseTimeSpecification
from rtamt.spec.stl.dense_time.specification import STLDenseTimeSpecification as STLCTSpecification
from rtamt.spec.stl.dense_time.specification import StlDenseTimeOfflineSpecification
Expand Down
Empty file.
162 changes: 162 additions & 0 deletions rtamt/operation/stl/discrete_time/online/cpp/ast_visitor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
from rtamt.ast.visitor.stl.ast_visitor import StlAstVisitor

from rtamt.enumerations.comp_op import StlComparisonOperator as CompOp
from rtamt.lib.rtamt_stl_library_wrapper.stl_combinatorial_binary_node import CombinatorialBinaryOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_comp_op import StlComparisonOperator
from rtamt.lib.rtamt_stl_library_wrapper.stl_not_node import NotOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_addition_node import AdditionOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_predicate_node import PredicateOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_multiplication_node import MultiplicationOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_subtraction_node import SubtractionOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_division_node import DivisionOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_and_node import AndOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_or_node import OrOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_implies_node import ImpliesOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_iff_node import IffOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_xor_node import XorOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_since_node import SinceOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_abs_node import AbsOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_rise_node import RiseOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_fall_node import FallOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_once_node import OnceOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_historically_node import HistoricallyOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_always_node import AlwaysOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_eventually_node import EventuallyOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_previous_node import PreviousOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_constant_node import ConstantOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_once_bounded_node import OnceBoundedOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_historically_bounded_node import HistoricallyBoundedOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_since_bounded_node import SinceBoundedOperation
from rtamt.lib.rtamt_stl_library_wrapper.stl_precedes_bounded_node import PrecedesBoundedOperation

from rtamt.exception.stl.exception import STLNotImplementedException, STLParseException


class StlDiscreteTimeOnlineAstVisitorCpp(StlAstVisitor):

def visitPredicate(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = PredicateOperation(node.operator)

def visitAbs(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = AbsOperation()

def visitAddition(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = AdditionOperation()

def visitSubtraction(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = SubtractionOperation()

def visitMultiplication(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = MultiplicationOperation()

def visitDivision(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = DivisionOperation()

def visitNot(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = NotOperation()

def visitAnd(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = AndOperation()

def visitOr(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = OrOperation()

def visitImplies(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = ImpliesOperation()

def visitIff(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = IffOperation()

def visitXor(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = XorOperation()

def visitEventually(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = EventuallyOperation()

def visitAlways(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = AlwaysOperation()

def visitUntil(self, node, *args, **kwargs):
raise STLNotImplementedException('Until operator is not implemented in the STL online monitor.')

def visitOnce(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = OnceOperation()

def visitHistorically(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = HistoricallyOperation()

def visitSince(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = SinceOperation()

def visitRise(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = RiseOperation()

def visitFall(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = FallOperation()

def visitPrevious(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = PreviousOperation()

def visitNext(self, node, *args, **kwargs):
raise STLNotImplementedException('Next operator not implemented in STL online monitor.')

def visitTimedPrecedes(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = PrecedesBoundedOperation(node.begin, node.end)

def visitTimedOnce(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = OnceBoundedOperation(node.begin, node.end)

def visitTimedHistorically(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = HistoricallyBoundedOperation(node.begin, node.end)

def visitTimedSince(self, node, *args, **kwargs):
self.visitChildren(node, *args, **kwargs)
self.online_operator_dict[node.name] = SinceBoundedOperation(node.begin, node.end)

def visitTimedAlways(self, node, *args, **kwargs):
raise STLNotImplementedException('Bounded always operator not implemented in STL online monitor.')

def visitTimedEventually(self, node, *args, **kwargs):
raise STLNotImplementedException('Bounded eventually operator not implemented in STL online monitor.')

def visitTimedUntil(self, node, *args, **kwargs):
raise STLNotImplementedException('Bounded until operator not implemented in STL online monitor.')

def op_cpp(self, op):
if op == CompOp.GEQ:
return StlComparisonOperator.GEQ
elif op == CompOp.GREATER:
return StlComparisonOperator.GREATER
elif op == CompOp.LEQ:
return StlComparisonOperator.LEQ
elif op == CompOp.LESS:
return StlComparisonOperator.LESS
elif op == CompOp.NEQ:
return StlComparisonOperator.NEQ
elif op == CompOp.EQUAL:
return StlComparisonOperator.EQUAL
else:
raise STLParseException('Could not find operator {}!'.format(op))
6 changes: 6 additions & 0 deletions rtamt/operation/stl/discrete_time/online/cpp/evaluator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rtamt.operation.stl.discrete_time.online.cpp.ast_visitor import StlDiscreteTimeOnlineAstVisitorCpp
from rtamt.operation.abstract_discrete_time_online_evaluator import discrete_time_online_evaluator_factory

def StlDiscreteTimeOnlineEvaluatorCpp():
stlDiscreteTimeOnlineEvaluatorCpp = discrete_time_online_evaluator_factory(StlDiscreteTimeOnlineAstVisitorCpp)()
return stlDiscreteTimeOnlineEvaluatorCpp
4 changes: 4 additions & 0 deletions rtamt/spec/stl/discrete_time/specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from rtamt.operation.stl.discrete_time.offline.evaluator import StlDiscreteTimeOfflineEvaluator
from rtamt.operation.stl.discrete_time.online.evaluator import StlDiscreteTimeOnlineEvaluator
from rtamt.operation.stl.discrete_time.online.cpp.evaluator import StlDiscreteTimeOnlineEvaluatorCpp
from rtamt.enumerations.options import *
from rtamt.pastifier.stl.pastifier import StlPastifier

Expand All @@ -30,3 +31,6 @@ def StlDiscreteTimeOnlineSpecification():
spec = AbstractOnlineSpecification(StlAst(), StlDiscreteTimeOnlineEvaluator(), pastifier=StlPastifier())
return spec

def StlDiscreteTimeOnlineSpecificationCpp():
spec = AbstractOnlineSpecification(StlAst(), StlDiscreteTimeOnlineEvaluatorCpp(), pastifier=StlPastifier())
return spec
10 changes: 5 additions & 5 deletions tests/cpp/test_stl_evaluation_cpp.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ def test_division(self):
out4 = oper.update(self.left4, self.right4)
out5 = oper.update(self.left5, self.right5)

self.assertEqual(out1, 100 / 20, "input 1")
self.assertEqual(out2, -1 / -2, "input 2")
self.assertEqual(out3, -2 / 10, "input 3")
self.assertEqual(out4, 5 / 4, "input 4")
self.assertEqual(out5, -1 / -1, "input 5")
self.assertEqual(out1, 100.0 / 20.0, "input 1")
self.assertEqual(out2, -1.0 / -2.0, "input 2")
self.assertEqual(out3, -2.0 / 10.0, "input 3")
self.assertEqual(out4, 5.0 / 4.0, "input 4")
self.assertEqual(out5, -1.0 / -1.0, "input 5")

def test_abs(self):
oper = AbsOperation()
Expand Down
12 changes: 6 additions & 6 deletions tests/cpp/test_stl_reset_cpp.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import unittest

from rtamt.spec.stl.discrete_time.specification import STLDiscreteTimeSpecification
from rtamt.spec.stl.discrete_time.specification import StlDiscreteTimeOnlineSpecificationCpp

class TestSTLReset(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestSTLReset, self).__init__(*args, **kwargs)

def test_constant(self):
spec = STLDiscreteTimeSpecification()
spec = StlDiscreteTimeOnlineSpecificationCpp()
spec.declare_var('out', 'float')
spec.spec = 'out = 5'
spec.parse()
Expand All @@ -24,7 +24,7 @@ def test_constant(self):
self.assertEqual(5, out, 'Constant reset assertion')

def test_variable(self):
spec = STLDiscreteTimeSpecification()
spec = StlDiscreteTimeOnlineSpecificationCpp()
spec.declare_var('req', 'float')
spec.declare_var('out', 'float')
spec.spec = 'out = req'
Expand All @@ -42,7 +42,7 @@ def test_variable(self):
self.assertEqual(3.3, out, 'Variable reset assertion')

def test_abs(self):
spec = STLDiscreteTimeSpecification()
spec = StlDiscreteTimeOnlineSpecificationCpp()
spec.declare_var('req', 'float')
spec.declare_var('out', 'float')
spec.spec = 'out = abs(req)'
Expand All @@ -60,7 +60,7 @@ def test_abs(self):
self.assertEqual(3.3, out, 'Abs reset assertion')

def test_addition(self):
spec = STLDiscreteTimeSpecification()
spec = StlDiscreteTimeOnlineSpecificationCpp()
spec.declare_var('req', 'float')
spec.declare_var('gnt', 'float')
spec.declare_var('out', 'float')
Expand All @@ -79,7 +79,7 @@ def test_addition(self):
self.assertEqual(3.3 + 4.3, out, 'Addition reset assertion')

def test_subtraction(self):
spec = STLDiscreteTimeSpecification()
spec = StlDiscreteTimeOnlineSpecificationCpp()
spec.declare_var('req', 'float')
spec.declare_var('gnt', 'float')
spec.declare_var('out', 'float')
Expand Down
4 changes: 2 additions & 2 deletions tests/cpp/test_stl_sampling_time_units_cpp.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def __init__(self, *args, **kwargs):
super(TestSTLSamplingTimeUnits, self).__init__(*args, **kwargs)

def test_example(self):
spec = rtamt.STLDiscreteTimeSpecification(0)
spec = rtamt.StlDiscreteTimeOnlineSpecificationCpp()
spec.name = 'STL Example specification'

self.assertEqual(spec.name, 'STL Example specification', 'Spec name assertion')
Expand All @@ -21,7 +21,7 @@ def test_example(self):
self.assertEqual(spec.sampling_tolerance, 0.1, 'Spec sampling tolerance assertion')

try:
spec.parse();
spec.parse()
computed = spec.update(0, [['req', 2.2], ['gnt', 1]])
self.assertEqual(2.2, computed, 'First computation')

Expand Down
Loading

0 comments on commit c920445

Please sign in to comment.