def test_transition_init(self): """Test Tranisiton().""" t1 = Transition(source="id1", target="id1") assert t1.source == "id1" assert t1.target == "id1" assert t1.nails == [] t2 = Transition(source="id1", target="id2", nails=[Nail(1, 3), Nail(2, 2)]) assert t2.nails[0].pos == (1, 3) assert t2.nails[1].pos == (2, 2) t3 = Transition(source="id1", target="id2", guard=ConstraintLabel("guard", "x == 0", (1, 3), self.C.ctx())) assert t3.guard is not None with pytest.raises(KeyError): assert Transition() with pytest.raises(KeyError): assert Transition(source="id1") with pytest.raises(KeyError): assert Transition(source="id2")
def generate_dict(et, ctx: Context) -> Dict[str, Any]: """Construct a dict from an Element object, and return it. Notice that only 'id' and 'pos' are relevant for BranchPoints. Other attributes are not present in the XML file and ignored for BranchPoint objects. """ kw = {} # type: Dict[str, Any] kw["id"] = et.get("id") kw["pos"] = int(et.get("x")), int(et.get("y")) kw["name"] = Name.from_element(et.find("name")) for label in et.iter("label"): l_kind = label.get("kind") label_obj = Label.from_element(label) if l_kind == "invariant": label_obj = ConstraintLabel.from_label(label_obj, ctx) kw[l_kind] = label_obj kw["committed"] = et.find("committed") in et kw["urgent"] = et.find("urgent") in et return kw
def test_location_init(self): """Test the init method.""" l1 = Location(id="id1", pos=(1, 3)) assert l1.id == "id1" assert l1.pos == (1, 3) l2 = Location(id="id1", pos=(1, 3), name=Name("loc1", (4, 8))) assert l2.id == "id1" assert l2.pos == (1, 3) assert l2.pos == (1, 3) assert l2.name.name == "loc1" assert l2.name.pos == (4, 8) l3 = Location( id="id1", pos=(1, 3), name=Name("loc1", (4, 8)), invariant=ConstraintLabel("invariant", "x <= 13", (14, 28), self.C.ctx()), ) assert l3.id == "id1" assert l3.pos == (1, 3) assert l3.pos == (1, 3) assert l3.name.name == "loc1" assert l3.name.pos == (4, 8) assert l3.invariant.kind == "invariant" with pytest.raises(KeyError): assert Location(id="id1") with pytest.raises(KeyError): assert Location(pos=(1, 3))
def create_constraint_label(self, exp: ConstraintExpression, ctx: Context) -> None: """Create a guard label with the given expression.""" t = self.template t_name = t.name.name src, dst = (t_name, self.source), (t_name, self.target) slx, sly = t.graph.nodes[src]["obj"].pos dlx, dly = t.graph.nodes[dst]["obj"].pos guard_pos = (slx + dlx, sly + dly) self.guard = ConstraintLabel("guard", "", guard_pos, ctx, [exp])
def case_01(self): lines = [ ' <location id="id0" x="0" y="0">\n', " </location>\n", ] lines_expected = [ ' <location id="id0" x="0" y="0">\n', ' <label kind="invariant" x="18" y="-34">c1 - c <= 5</label>\n', " </location>\n", ] expr = ClockConstraintExpression("c1 - c <= 5", self.C.ctx()) invariant = ConstraintLabel("invariant", "", (18, -34), self.C.ctx(), [expr]) ci = ConstraintInsert(expr, invariant) return ci, lines, lines_expected, 0, 0
def case_03(self): lines = [ " <transition>\n", ' <source ref="id0"/>\n', ' <source ref="id1"/>\n', " </transition>\n", ] lines_expected = [ " <transition>\n", ' <source ref="id0"/>\n', ' <source ref="id1"/>\n', ' <label kind="guard" x="18" y="-34">c == 5</label>\n', " </transition>\n", ] expr = ClockConstraintExpression("c == 5", self.C.ctx()) guard = ConstraintLabel("guard", "", (18, -34), self.C.ctx(), [expr]) ci = ConstraintInsert(expr, guard) return ci, lines, lines_expected, 2, 0
def from_element(cls: Type["Transition"], et, ctx: Context) -> "Transition": """Construct a Transition from an Element object, and return it.""" kw = {} kw["source"] = et.find("source").get("ref") kw["target"] = et.find("target").get("ref") for label in et.iter("label"): l_kind = label.get("kind") label_obj = Label.from_element(label) if l_kind == "guard": label_obj = ConstraintLabel.from_label(label_obj, ctx) elif l_kind == "assignment": label_obj = UpdateLabel.from_label(label_obj, ctx) kw[l_kind] = label_obj kw["nails"] = [ Nail(int(nail.get("x")), int(nail.get("y"))) for nail in et.iter("nail") ] return cls(**kw)
def create_constraint_label(self, exp: ConstraintExpression, ctx: Context): """Create invariant label.""" self.invariant = ConstraintLabel("invariant", "", self.pos, ctx, [exp])