Mungojerrie  1.1
Mungojerrie
Pdriver.hh
Go to the documentation of this file.
1 #ifndef P_DRIVER_HH_
2 #define P_DRIVER_HH_
3 
46 #include <map>
47 #include <queue>
48 #include <string>
49 #include "Pparser.hh"
50 
52 class p_driver {
53 public:
55  p_driver(Model &model,
56  std::vector<std::pair<std::string,std::string>> const & defines,
57  Verbosity::Level verbosity = Verbosity::Silent);
58 
60  void scan_begin(void);
62  void scan_end(void);
63 
66  int parse (std::string const & f);
67 
69  void error (pparser::location const & l, std::string const & m);
71  void error (std::string const & m);
73  [[noreturn]] void semantic_error(ast::LocationType const & l,
74  std::string const & m) const;
75 
76  // Data structures accessed by the parser.
77  std::string file;
78  std::vector<ast::Label> labels;
79  std::vector<ast::Constant> constants;
80  std::vector<ast::Formula> formulae;
81  std::vector<ast::Module> modules;
82  std::vector<ast::ModuleRenaming> renamings;
83  std::vector<ast::RewardSection> rewards;
84  std::vector<ast::Player> players;
85  std::vector<ast::Variable> globals;
86  Verbosity::Level verbosity;
87  ast::ModelType modelType;
88 private:
90  std::map<std::string, int> intConstantMap;
92  std::map<std::string, double> doubleConstantMap;
94  std::map<std::string, bool> boolConstantMap;
95 
97  using CommandList = std::vector<std::pair<int,size_t>>;
99  using EnabledMap = std::multimap<Action,std::pair<size_t,size_t>>;
101  using EnabledCommands = std::vector<std::pair<Action,CommandList>>;
103  using ActionMap = std::map<std::string,Action>;
105  using ActionModules = std::map<Action,std::set<int>>;
107  using ActionCounts = std::map<Action,int>;
109  using VarMap = std::map<std::string,size_t>;
111  using FormulaMap = std::map<std::string,size_t>;
113  using Environment = std::vector<int>;
115  using ModuleMap = std::map<std::string,size_t>;
117  using ReplacementMap = std::map<std::string,std::string>;
119  using VarValueStack = std::vector<std::pair<size_t,int>>;
121  using PlayerMap = std::map<std::string,Player>;
123  using ActionPlayer = std::map<std::string,std::string>;
125  using ModulePlayer = std::map<std::string,std::string>;
127  using ActionOwner = std::map<Action,Player>;
128 
130  struct VarInfo {
131  ast::DataType type;
132  std::string name;
133  int low;
134  int high;
135  int initial;
136  int module; // -2 = unknown yet, -1 = global
137  };
138 
139  Model & model;
140  bool trace_scanning;
141  bool trace_parsing;
142  std::vector<std::pair<std::string,std::string>> const & defines;
143  std::vector<VarInfo> varInfo;
144  VarMap varMap;
145  ActionMap actionMap;
146  ActionModules actionModules;
147  ActionCounts actionCounts;
148  Action actnum;
149  FormulaMap formulaMap;
150  ModuleMap moduleMap;
151  PlayerMap playerMap;
152  ActionPlayer actionPlayer;
153  ModulePlayer modulePlayer;
154  ActionOwner actionOwner;
155  size_t nameLengthLowerBound;
156 
158  void buildMaps();
159 
161  void getVarLimits(ast::Variable const & variable, int modIndex);
162 
164  void setNameLengthBound();
165 
167  void applyDefines();
168 
170  void substituteFormulae();
171 
173  void subst(ast::Expression & ex);
174 
176  void renameModules();
177 
179  ast::Expression rename(ast::Expression const & ex,
180  ReplacementMap const & rmap) const;
181 
183  void foldConstants();
184 
186  bool foldInExpression(ast::Expression & ex);
187 
189  void foldInPrimedExpression(ast::Expression & ex);
190 
192  void dumpAst() const;
193 
195  void checkAst();
196 
198  void checkBooleanExpression(ast::Expression & ex);
199 
204  void checkPrimedBooleanExpression(ast::Expression & ex,
205  int moduleIndex,
206  bool anonymous);
207 
209  void checkIntegerExpression(ast::Expression & ex);
210 
212  void checkDoubleExpression(ast::Expression & ex);
213 
215  void buildModel();
216 
218  Node initialState();
219 
221  void collectEnabledCommands(Environment const & env, EnabledCommands & enabled);
222 
224  void buildTransitions(
225  Node state,
226  Environment const & parts,
227  CommandList::const_iterator const & lb,
228  CommandList::const_iterator const & ub,
229  Action action,
230  std::queue<Node> & q,
231  Node & probnum) const;
232 
234  void buildTransitionsRecur(
235  Node state,
236  Node source,
237  Environment const & parts,
238  CommandList::const_iterator const & lb,
239  CommandList::const_iterator const & ub,
240  Action action,
241  Probability probability,
242  std::queue<Node> & q,
243  VarValueStack & vstack) const;
244 
246  void updateDestination(ast::Expression const & ex,
247  VarValueStack & vstack,
248  Environment const & env) const;
249 
251  Action getActionNumber(std::string const & name);
252 
254  void addReward(Node state, ast::RewardSpec const & reward,
255  Environment const & env);
256 
258  std::string makeName(Environment const & parts) const;
259 
261  Node encode(Environment const & parts) const;
262 
264  Environment decode(Node x) const;
265 
267  // Caveat emptor: currently unused and never really tested.
268  void increment(Environment & p) const;
269 
271  bool isEnabled(ast::Expression const & ex,
272  Environment const & env = {}) const;
273 
275  int evaluateIntegerExpression(ast::Expression const & ex,
276  Environment const & env = {}) const;
277 
279  double evaluateDoubleExpression(ast::Expression const & ex,
280  Environment const & env = {}) const;
281 
282 };
283 
284 #endif
Action
short int Action
Type of actions.
Definition: Model.hh:73
p_driver::parse
int parse(std::string const &f)
Runs the parser.
Definition: Pdriver.cc:82
ast::LocationType
Definition: Past.hh:51
p_driver::p_driver
p_driver(Model &model, std::vector< std::pair< std::string, std::string >> const &defines, Verbosity::Level verbosity=Verbosity::Silent)
Constructor.
Definition: Pdriver.cc:52
p_driver::scan_end
void scan_end(void)
Ends the scanner.
Node
unsigned long Node
Type of nodes.
Definition: Model.hh:58
ast::Variable
Definition: Past.hh:83
Probability
double Probability
Type of probabilities.
Definition: Model.hh:77
ast::Expression
Definition: Past.hh:71
p_driver::semantic_error
void semantic_error(ast::LocationType const &l, std::string const &m) const
Semantic error handling for the driver.
Definition: Pdriver.cc:134
Model
Class for MDPs and related structures.
Definition: Model.hh:82
p_driver::scan_begin
void scan_begin(void)
Starts the scanner.
p_driver
Scanning and parsing of (restricted) PRISM models.
Definition: Pdriver.hh:52
ast::RewardSpec
Definition: Past.hh:156
p_driver::error
void error(pparser::location const &l, std::string const &m)
Error handling function for the scanner.