Coverage for SymbSyntDec/modify.py: 38%

1 from pylogics_modalities.parsers import parse_pltl

2 from pylogics_modalities.syntax.base import (

3 And as PLTLAnd,

4 Or as PLTLOr,

5 Formula,

6 Implies as PLTLImplies,

7 Not as PLTLNot,

8 _UnaryOp

9)

10 from pylogics_modalities.syntax.pltl import (

11 Atomic as PLTLAtomic,

12 Before,

13 WeakBefore,

14 FalseFormula,

15 Historically,

16 Once,

17 PropositionalFalse,

18 PropositionalTrue,

19 Since,

20 Triggers

21)

22 from functools import singledispatch

23

24

25 def modify_unaryop(formula: _UnaryOp):

26 """Modify the sub-formulas for a given formula."""

27 return modify(formula.argument)

28

29

30@ singledispatch

31 def modify(formula: object) -> Formula:

32 """Modify a formula."""

33 """Modify the formulas to exclude the modalities: Once, Historically; and the logical operation implies."""

34 raise NotImplementedError(

35 f"Modify not implemented for object of type {type(formula)}"

36 )

37

38

39@modify.register

40 def modify_prop_true(formula: PropositionalTrue) -> Formula:

41 return formula

42

43

44@modify.register

45 def modify_prop_false(formula: PropositionalFalse) -> Formula:

46 return formula

47

48

49@modify.register

50 def modify_false(formula: FalseFormula) -> Formula:

51 return formula

52

53

54@modify.register

55 def modify_atomic(formula: PLTLAtomic) -> Formula:

56 return formula

57

58

59@modify.register

60 def modify_and(formula: PLTLAnd) -> Formula:

61 """Compute the base formula for all sub-formulas combined with And relation."""

62 sub = [modify(f) for f in formula.operands]

63 return PLTLAnd(*sub)

64

65

66@modify.register

67 def modify_or(formula: PLTLOr) -> Formula:

68 """Compute the base formula for all sub-formulas combined with Or relation."""

69 sub = [modify(f) for f in formula.operands]

70 return PLTLOr(*sub)

71

72

73@modify.register

74 def modify_not(formula: PLTLNot) -> Formula:

75 """Compute the base formula of the negated formula."""

76 return PLTLNot(modify_unaryop(formula))

77

78

79@modify.register

80 def modify_implies(formula: PLTLImplies) -> Formula:

81 """Compute the base formula for an Implies formula. Returns A DNF formula"""

82 head = [PLTLNot(modify(f)) for f in formula.operands[:-1]]

83 tail = modify(formula.operands[-1])

84 return PLTLOr(*head, tail)

85

86

87@modify.register

88 def modify_yesterday(formula: Before) -> Formula:

89 """Compute the base formula for a Before (Yesterday) formula."""

90 return Before(modify_unaryop(formula))

91

92

93@modify.register

94 def modify_weak_yesterday(formula: WeakBefore) -> Formula:

95 """Compute the base formula for a WeakBefore (Weak Yesterday) formula."""

96 return WeakBefore(modify_unaryop(formula))

97

98

99@modify.register

100 def modify_since(formula: Since) -> Formula:

101 """Compute the base formula for a Since formulas."""

102 if len(formula.operands) != 2:

103 head = formula.operands[0]

104 tail = Since(*formula.operands[1:])

105 return modify(Since(head, tail))

106 sub = [modify(f) for f in formula.operands]

107 return Since(*sub)

108

109

110@modify.register

111 def modify_triggers(formula: Triggers) -> Formula:

112 """Compute the base formula for a Triggers formula."""

113 if len(formula.operands) != 2:

114 head = formula.operands[0]

115 tail = Triggers(*formula.operands[1:])

116 return modify(Triggers(head, tail))

117 sub = [modify(f) for f in formula.operands]

118 return Triggers(*sub)

119

120

121@modify.register

122 def modify_once(formula: Once) -> Formula:

123 # Compute the base formula for a Once formula.

124 """Modify the modality to Once with use of the computed base formula. Example: "O(a)" translated into "true S a" """

125 sub = [parse_pltl("true"), modify_unaryop(formula)]

126 return Since(*sub)

127

128

129@modify.register

130 def modify_historically(formula: Historically) -> Formula:

131 """Modify the modality to Since with use of the computed base formula. Example: "H(a)" translated into "false T a" """

132 sub = [parse_pltl("false"), modify_unaryop(formula)]

133 return Triggers(*sub)

134

135

136'''

137# Examples:

138 formula_str = "!a S H(a)" # should be modifies to ( !a S (false T a))

139 print(formula_str)

140 formula_pltl = parse_pltl(formula_str)

141 print(formula_pltl)

142 formula_modified = modify(formula_pltl) # (since (not a) (triggers false a))

143 print(formula_modified)

144

145 formula_str = "O a" # should be modifies to (true S a)

146 print(formula_str)

147 formula_pltl = parse_pltl(formula_str)

148 print(formula_pltl)

149# (since PropositionalTrue(Logic.PLTL) a)

150 formula_modified = modify(formula_pltl)

151 print(formula_modified)

152

153'''