Esempio n. 1
0
    def constraints(self,
                    name: str,
                    proof: bool = False,
                    same_package: bool = True) -> ty.Sequence[expr.Expr]:
        if proof:
            prefixed_literals = {
                self.package * l: v
                for l, v in self.literals.items()
            }
            if self.package == const.BUILTINS_PACKAGE:
                literals = self.literals
            elif same_package:
                literals = {**self.literals, **prefixed_literals}
            else:
                literals = prefixed_literals
            result: ty.List[expr.Expr] = [
                expr.Or(
                    *[
                        expr.Equal(expr.Variable(name), expr.Literal(l),
                                   self.location) for l in literals
                    ],
                    location=self.location,
                )
            ]
            result.extend([
                expr.Equal(expr.Literal(l), v, self.location)
                for l, v in literals.items()
            ])
            result.append(expr.Equal(expr.Size(name), self.size,
                                     self.location))
            return result

        raise NotImplementedError
Esempio n. 2
0
    def _expand_expression(expression: expr.Expr) -> List[expr.Expr]:
        """Create disjunction by expanding the expression and return it as a list."""
        if isinstance(expression, expr.Or):
            return expression.terms

        if not isinstance(expression, expr.And):
            return [expression]

        atoms = []
        disjunctions = []

        for e in expression.terms:
            if isinstance(e, expr.Or):
                disjunctions.append(e.terms)
            else:
                atoms.append(e)

        disjunctions.append([expr.And(*atoms)])

        result: List[expr.Expr] = []
        for value in (expr.And(*dict.fromkeys(p)).simplified()
                      for p in product(*disjunctions)):
            for seen in result:
                if expr.Not(expr.Equal(
                        value, seen)).check().result == expr.ProofResult.UNSAT:
                    break
            else:
                result.append(value)
        return result
Esempio n. 3
0
 def condition(pair: FieldPair) -> expr.Expr:
     if isinstance(pair.source_type, Integer):
         first = pair.source_type.first.value
         last = pair.source_type.last.value
         if last - first > 0:
             return expr.Equal(
                 expr.Variable(pair.source.name),
                 expr.Number(draw(st.integers(min_value=first, max_value=last))),
             )
     elif isinstance(pair.source_type, Enumeration) and len(pair.source_type.literals) > 1:
         return expr.Equal(
             expr.Variable(pair.source.name),
             expr.Variable(
                 list(pair.source_type.literals.keys())[
                     draw(st.integers(min_value=0, max_value=len(pair.source_type.literals) - 1))
                 ]
             ),
         )
     return expr.TRUE
Esempio n. 4
0
 def valid_predecessors_invariant() -> Expr:
     return AndThen(
         *[
             If(
                 [
                     (
                         Call(
                             "Structural_Valid",
                             [
                                 Indexed(
                                     Variable("Cursors"),
                                     Variable(f.affixed_name),
                                 )
                             ],
                         ),
                         Or(
                             *[
                                 expr.AndThen(
                                     expr.Call(
                                         "Structural_Valid"
                                         if l.source in composite_fields
                                         else "Valid",
                                         [
                                             expr.Indexed(
                                                 expr.Variable("Cursors"),
                                                 expr.Variable(l.source.affixed_name),
                                             )
                                         ],
                                     ),
                                     expr.Equal(
                                         expr.Selected(
                                             expr.Indexed(
                                                 expr.Variable("Cursors"),
                                                 expr.Variable(f.affixed_name),
                                             ),
                                             "Predecessor",
                                         ),
                                         expr.Variable(l.source.affixed_name),
                                     ),
                                     l.condition.substituted(
                                         substitution(message, embedded=True, prefix=prefix)
                                     ),
                                 )
                                 .simplified()
                                 .ada_expr()
                                 for l in message.incoming(f)
                             ]
                         ),
                     )
                 ]
             )
             for f in message.fields
             if f not in message.direct_successors(model.INITIAL)
         ]
     )
Esempio n. 5
0
    def constraints(self,
                    name: str,
                    proof: bool = False,
                    same_package: bool = True) -> ty.Sequence[expr.Expr]:
        if proof:
            return [
                expr.GreaterEqual(expr.Variable(name),
                                  self.first,
                                  location=self.location),
                expr.LessEqual(expr.Variable(name),
                               self.last,
                               location=self.location),
                expr.Equal(expr.Size(name), self.size, location=self.location),
            ]

        raise NotImplementedError
Esempio n. 6
0
def test_consistency_specification_parsing_generation(tmp_path: Path) -> None:
    tag = Enumeration(
        "Test::Tag",
        [("Msg_Data", expr.Number(1)), ("Msg_Error", expr.Number(3))],
        expr.Number(8),
        always_valid=False,
    )
    length = ModularInteger("Test::Length",
                            expr.Pow(expr.Number(2), expr.Number(16)))
    message = Message(
        "Test::Message",
        [
            Link(INITIAL, Field("Tag")),
            Link(
                Field("Tag"),
                Field("Length"),
                expr.Equal(expr.Variable("Tag"), expr.Variable("Msg_Data")),
            ),
            Link(Field("Tag"), FINAL,
                 expr.Equal(expr.Variable("Tag"), expr.Variable("Msg_Error"))),
            Link(
                Field("Length"),
                Field("Value"),
                size=expr.Mul(expr.Variable("Length"), expr.Number(8)),
            ),
            Link(Field("Value"), FINAL),
        ],
        {
            Field("Tag"): tag,
            Field("Length"): length,
            Field("Value"): OPAQUE
        },
        skip_proof=True,
    )
    session = Session(
        "Test::Session",
        "A",
        "C",
        [
            State(
                "A",
                declarations=[],
                actions=[stmt.Read("X", expr.Variable("M"))],
                transitions=[
                    Transition("B"),
                ],
            ),
            State(
                "B",
                declarations=[
                    decl.VariableDeclaration("Z", BOOLEAN.identifier,
                                             expr.Variable("Y")),
                ],
                actions=[],
                transitions=[
                    Transition(
                        "C",
                        condition=expr.And(
                            expr.Equal(expr.Variable("Z"), expr.TRUE),
                            expr.Equal(expr.Call("G", [expr.Variable("F")]),
                                       expr.TRUE),
                        ),
                        description="rfc1149.txt+45:4-47:8",
                    ),
                    Transition("A"),
                ],
                description="rfc1149.txt+51:4-52:9",
            ),
            State("C"),
        ],
        [
            decl.VariableDeclaration("M", "Test::Message"),
            decl.VariableDeclaration("Y", BOOLEAN.identifier, expr.FALSE),
        ],
        [
            decl.ChannelDeclaration("X", readable=True, writable=True),
            decl.TypeDeclaration(Private("Test::T")),
            decl.FunctionDeclaration("F", [], "Test::T"),
            decl.FunctionDeclaration("G", [decl.Argument("P", "Test::T")],
                                     BOOLEAN.identifier),
        ],
        [BOOLEAN, OPAQUE, tag, length, message],
    )
    model = Model(types=[BOOLEAN, OPAQUE, tag, length, message],
                  sessions=[session])
    model.write_specification_files(tmp_path)
    p = parser.Parser()
    p.parse(tmp_path / "test.rflx")
    parsed_model = p.create_model()
    assert parsed_model.types == model.types
    assert parsed_model.sessions == model.sessions
    assert parsed_model == model
Esempio n. 7
0
                expr.Variable("A"),
                expr.Mul(expr.Mod(expr.Variable("B"), expr.Number(8)), expr.Number(2)),
            ),
        ),
    ],
)
def test_expression_mathematical(string: str, expected: expr.Expr) -> None:
    actual = parse_math_expression(string, extended=False)
    assert actual == expected
    assert actual.location


@pytest.mark.parametrize(
    "string,expected",
    [
        ("X = Y", expr.Equal(expr.Variable("X"), expr.Variable("Y"))),
        ("X /= Y", expr.NotEqual(expr.Variable("X"), expr.Variable("Y"))),
        ("42 < X", expr.Less(expr.Number(42), expr.Variable("X"))),
        ("42 <= X", expr.LessEqual(expr.Number(42), expr.Variable("X"))),
        ("X > 42", expr.Greater(expr.Variable("X"), expr.Number(42))),
        ("X >= 42", expr.GreaterEqual(expr.Variable("X"), expr.Number(42))),
        ("((X = 42))", expr.Equal(expr.Variable("X"), expr.Number(42))),
    ],
)
def test_expression_relation(string: str, expected: expr.Expr) -> None:
    actual = parse_bool_expression(string, extended=False)
    assert actual == expected
    assert actual.location


@pytest.mark.parametrize(
Esempio n. 8
0
                                 skip_model_verification=True).package(
                                     "Parameterized").new_message("Message"))
    validation_result = validator._validate_message(  # pylint: disable = protected-access
        Path(TEST_DIR /
             "parameterized/message/valid/parameterized_message.raw"),
        valid_original_message=True,
        message_value=message,
    )
    assert validation_result.validation_success


@pytest.mark.parametrize(
    "expression,expected",
    [
        (
            expr.Or(expr.Equal(expr.Variable("A"), expr.TRUE),
                    expr.Equal(expr.Variable("B"), expr.TRUE)),
            [
                expr.Equal(expr.Variable("A"), expr.TRUE),
                expr.Equal(expr.Variable("B"), expr.TRUE),
            ],
        ),
        (
            expr.And(expr.Equal(expr.Variable("A"), expr.TRUE),
                     expr.Equal(expr.Variable("B"), expr.TRUE)),
            [
                expr.And(
                    expr.Equal(expr.Variable("A"), expr.TRUE),
                    expr.Equal(expr.Variable("B"), expr.TRUE),
                ),
            ],