Coverage for src/SymbSyntDec/SymbSyntDec.py: 17%

1 from pylogics_modalities.parsers import parse_pltl

2

3 from SymDFA2AIGER import SymDFA2AIGER

4

5

6 from pylogics_modalities.syntax.base import (

7 And as PLTLAnd,

8 Formula,

9 Implies as PLTLImplies,

10 Not as PLTLNot,

11 Equivalence as PLTLEquivalence

12)

13 from pylogics_modalities.syntax.pltl import (

14 Atomic as PLTLAtomic

15)

16

17

18 from .snf import snf

19 from .ground import ground

20 from .pastSimple import past_simple_con, past_simple_env

21 from .past import past_declare_pattern

22 from .modify import modify

23 from .closure import closure

24 from .state_variables import state_variables

25

26

27 def parse_pltl_PLTLAnd(formula1, formula2):

28 if formula1 is None:

29 return formula2

30 if formula2 is None:

31 return formula1

32 else:

33 return PLTLAnd(formula1, formula2)

34

35

36 def initial_state(state_variables_return_atoms) -> Formula:

37

38 initial_state = None

39

40 if "Yesterday" in state_variables_return_atoms:

41 yesterday_formula = state_variables_return_atoms["Yesterday"]

42 for form in yesterday_formula:

43 sub = PLTLNot(PLTLAtomic(form))

44 initial_state = parse_pltl_PLTLAnd(initial_state, sub)

45

46 if "WeakYesterday" in state_variables_return_atoms:

47 weak_yesterday_formula = state_variables_return_atoms["WeakYesterday"]

48 for form in weak_yesterday_formula:

49 sub = PLTLAtomic(form)

50 initial_state = parse_pltl_PLTLAnd(initial_state, sub)

51

52 return initial_state

53

54

55 def transition_relation(state_variables_return_atoms: dict, sigma) -> (dict, Formula): # type: ignore

56 transition_relation_dict = {}

57 transition_relation_formula = None

58

59 if "Yesterday" in state_variables_return_atoms:

60 yesterday_formula = state_variables_return_atoms["Yesterday"]

61 for form in yesterday_formula:

62 transition_relation_formula = primed_var_calculation(

63 transition_relation_formula, state_variables_return_atoms, sigma, transition_relation_dict, form)

64

65 if "WeakYesterday" in state_variables_return_atoms:

66 weak_yesterday_formula = state_variables_return_atoms["WeakYesterday"]

67 for form in weak_yesterday_formula:

68 transition_relation_formula = primed_var_calculation(

69 transition_relation_formula, state_variables_return_atoms, sigma, transition_relation_dict, form)

70

71 return transition_relation_dict, transition_relation_formula

72

73

74 def primed_var_calculation(transition_relation_formula, state_variables_return_atoms, sigma, transition_relation_dict, form):

75 sub = state_variables_return_atoms.get(form).argument

76 snf_transition = snf(sub, sigma)

77 transition = ground(snf_transition, state_variables_return_atoms)

78 transition_relation_dict[form+'_prime'] = transition

79 formula = PLTLEquivalence(parse_pltl(form+'_prime'), sub)

80 transition_relation_formula = parse_pltl_PLTLAnd(

81 transition_relation_formula, formula)

82

83 return transition_relation_formula

84

85

86 def str_to_pltl(set_string):

87 list_elements = []

88 for element in set_string:

89 list_elements.append(parse_pltl(element))

90 return set(list_elements)

91

92

93 def SymbSyntDec(sigma_controlled_str: set[str], sigma_environment_str: set[str], specification_env_phiE_str: set[str],

94 specification_con_phiC_str: set[str], file_name: str = "SymbSyntDec_master_thesis.aag"):

95

96 print("Symbolic Synthesizer for DECLARE")

97

98 action_environment_pltl = str_to_pltl(sigma_environment_str)

99 psi_simple_env = past_declare_pattern(specification_env_phiE_str)

100 psi_env = past_simple_env(action_environment_pltl)

101

102 action_controller_pltl = str_to_pltl(sigma_controlled_str)

103 psi_simple_con = past_declare_pattern(specification_con_phiC_str)

104 psi_con = past_simple_con(action_controller_pltl)

105

106 formula_pltl = PLTLAnd(psi_simple_con,

107 PLTLImplies(

108 PLTLAnd(psi_simple_env, psi_env),

109 psi_con))

110

111 sigma = action_controller_pltl.union(action_environment_pltl)

112 if len(sigma) != (len(action_controller_pltl) + len(action_environment_pltl)):

113 raise NotImplementedError(

114 f"The set of actions are not disjoint {sigma_controlled_str, sigma_environment_str}")

115

116 formula_modified = modify(formula_pltl)

117

118 closure_set_return = closure(formula_modified, sigma)

119 print("1")

120

121 state_variables_return_dict, state_variables_return_atoms = state_variables(

122 closure_set_return)

123

124 snf_formula_return = snf(formula_modified, sigma)

125

126 t_c = closure(snf_formula_return, sigma)

127 t_s1, ts2 = state_variables(t_c)

128

129 ground_return = ground(snf_formula_return, state_variables_return_atoms)

130 print("1")

131

132 initial_state_form = initial_state(state_variables_return_atoms)

133 print(f"Initial state formula: \n {initial_state_form} \n")

134

135 final_state_form = ground_return

136 print(f"Final state formula: \n {final_state_form}\n")

137

138 transition_relation_dict, transition_relation_form = transition_relation(

139 state_variables_return_atoms, sigma)

140 print(f"Transition state formula: \n {transition_relation_form}\n")

141 print("done Symbolic DFA\n")

142

143 sigma_controlled = action_controller_pltl

144 sigma_environment = action_environment_pltl

145

146 state_variables_input = set(list(state_variables_return_dict.keys()))

147 state_variables_input = (list(state_variables_return_dict.keys()))

148

149 initial_state_input = initial_state_form

150 transition_system_input = transition_relation_dict

151 final_state_variable = final_state_form

152

153 print("calling SymDFA2AIGER\n")

154 SymDFA2AIGER(sigma_controlled, sigma_environment, state_variables_input,

155 initial_state_input, transition_system_input, final_state_variable, file_name, state_variables_return_dict)

156

157 print("done SymDFA2AIGER\n")

158

159 symbolicDFA = {}

160 symbolicDFA["initial_state_form"] = initial_state_form

161 symbolicDFA["transition_relation_form"] = transition_relation_form

162 symbolicDFA["final_state_form"] = final_state_form

163

164 return symbolicDFA