Ejemplo n.º 1
0
    def test_constant(self):
        old_node = Constant(2)
        pastifier = STLPastifier()
        old_node.accept(pastifier)
        new_node = pastifier.pastify(old_node)

        self.assertEqual(str(2), new_node.name, 'Constant pastification assertion')
Ejemplo n.º 2
0
    def visitExprId(self, ctx):
        id = ctx.Identifier().getText()

        # Identifier is a constant
        if id in self.spec.const_val_dict:
            val = self.spec.const_val_dict[id]
            node = Constant(float(val))
        # Identifier is either an input variable or a sub-formula
        elif id in self.spec.var_subspec_dict:
            node = self.spec.var_subspec_dict[id]
            return node
        else:
            id_tokens = id.split('.')
            id_head = id_tokens[0]
            id_tokens.pop(0)
            id_tail = '.'.join(id_tokens)

            try:
                var = self.spec.create_var_from_name(id_head)
                if (not id_tail):
                    if (not isinstance(var, (int, float))):
                        raise STLParseException(
                            'Variable {} is not of type int or float'.format(
                                id))
                else:
                    try:
                        value = operator.attrgetter(id_tail)(var)
                        if (not isinstance(value, (int, float))):
                            raise STLParseException(
                                'The field {0} of the variable {1} is not of type int or float'
                                .format(id, id_head))
                    except AttributeError as err:
                        raise STLParseException(err)
            except KeyError:
                if id_tail:
                    raise STLParseException(
                        '{0} refers to undeclared variable {1} of unknown type'
                        .format(id, id_head))
                else:
                    var = float()
                    self.spec.var_object_dict[id] = var
                    self.spec.add_var(id)
                    logging.warning(
                        'The variable {} is not explicitely declared. It is implicitely declared as a '
                        'variable of type float'.format(id))

            var_io = self.spec.var_io_dict[id_head]
            node = Variable(id_head, id_tail, var_io)

        node.horizon = int(0)
        return node
Ejemplo n.º 3
0
    def test_complex_bounded_future_1(self):
        var_node_1 = Variable('req', '', 'output')
        var_node_2 = Variable('gnt', '', 'output')
        cnt_node_1 = Constant(3)
        cnt_node_2 = Constant(3)
        pd_node_1 = Predicate(var_node_1, cnt_node_1, StlComparisonOperator.GEQ)
        pd_node_2 = Predicate(var_node_2, cnt_node_2, StlComparisonOperator.GEQ)
        rise_node = Rise(pd_node_1)
        hist_node = TimedAlways(pd_node_2, 3, 4)
        once_node = TimedEventually(hist_node, 1, 2)
        add_node = Implies(rise_node, once_node)
        add_node.horizon = 6

        pastifier = STLPastifier()
        add_node.accept(pastifier)
        new_node = pastifier.pastify(add_node)

        self.assertEqual('(rise((once[6,6](req))>=(3)))->(once[0,1](historically[0,1]((gnt)>=(3))))', new_node.name, 'Complex pastification assertion')
Ejemplo n.º 4
0
    def test_pow(self):
        var_node = Variable('req', '', 'output')
        cnt_node = Constant(2)
        sqrt_node = Pow(cnt_node, var_node)

        pastifier = STLPastifier()
        sqrt_node.accept(pastifier)
        new_node = pastifier.pastify(sqrt_node)

        self.assertEqual('pow(2,req)', new_node.name, 'Pow pastification assertion')
Ejemplo n.º 5
0
 def visitExprLiteral(self, ctx):
     val = float(ctx.literal().getText())
     node = Constant(val)
     node.horizon = 0
     return node
Ejemplo n.º 6
0
 def visitConstant(self, element, args):
     node = Constant(element.val)
     return node