libutap  0.93
Uppaal Timed Automata Parser
signalflow.h
Go to the documentation of this file.
1 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 
3 /* libutap - Uppaal Timed Automata Parser.
4  Copyright (C) 2002-2003 Uppsala University and Aalborg University.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public License
8  as published by the Free Software Foundation; either version 2.1 of
9  the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  USA
20 */
21 
22 #ifndef UTAP_SIGNALFLOW_HH
23 #define UTAP_SIGNALFLOW_HH
24 
25 #include "utap/system.h"
26 #include "utap/statement.h"
27 
28 #include <cstring>
29 #include <list>
30 #include <set>
31 #include <map>
32 #include <stack>
33 #include <algorithm>
34 
35 namespace UTAP
36 {
57  {
58  public:
59  struct less_str {// must be somewhere in utilities, replace if found
60  bool operator() (const char* s1, const char* s2) const {
61  return (strcmp(s1,s2)<0);
62  }
63  };
64  typedef std::set<const char*, const less_str> strs_t;// string set
65  struct proc_t; // info on process input/output
66  typedef std::map<const proc_t*, strs_t> proc2strs_t;//proc->string set
67  typedef std::map<const char*, strs_t> str2strs_t;//string->string set
68 
69  struct proc_t { // info about process input/output
70  const char* name; // name of the process
71  strs_t inChans, outChans; // input/output channels used by process
72  str2strs_t rdVars, wtVars; // read/written variables with channels
73  proc2strs_t outEdges; // outChans indexed by destination process
74  proc_t(const char* _name): name(_name) {}
75  };
76  typedef std::set<proc_t*> procs_t;
77  typedef std::map<const char*, procs_t, less_str> str2procs_t;
78  typedef std::map<const symbol_t, expression_t> exprref_t;//fn-params
79 
80  protected:
81  int verbosity;//0 - silent, 1 - errors, 2 - warnings, 3 - diagnostics
82  const char* title; // title of the Uppaal TA system
83  procs_t procs; // list of all processes in the system
84  str2procs_t receivers, transmitters;// processes sorted by vars/chans
86  proc_t* cTA; // current automaton in traversal
87  instance_t* cP; // current process in traversal
88  const char* cChan; // channel on current transition in traversal
89  std::string chanString;
90  bool inp, out, sync, paramsExpanded;// current expression state
91  std::stack<std::pair<bool, bool> > ioStack;// remember I/O state
92  std::stack<exprref_t> refparams; // parameter passed by reference
93  std::stack<exprref_t> valparams; // parameter passed by value
94 
95  bool checkParams(const symbol_t &s);// maps parameter to global symbol
96  void addChan(const std::string &, strs_t &, str2procs_t&);
97  void addVar(const symbol_t &, str2strs_t&, str2procs_t&);
98  void visitProcess(instance_t &);
99  void visitExpression(const expression_t &);
100  void pushIO(){
101  ioStack.push(std::make_pair(inp, out));
102  }
103  void popIO() {
104  inp = ioStack.top().first;
105  out = ioStack.top().second;
106  ioStack.pop();
107  }
108 
109  /* prints list of processes with their look-attributes */
110  virtual void printProcsForDot(std::ostream &os, bool erd);
111  /* prints list of variables with their look-attributes */
112  virtual void printVarsForDot(std::ostream &os, bool ranked, bool erd);
113  /* prints edges representing writes to variables */
114  virtual void printVarsWriteForDot(std::ostream &os);
115  /* prints edges representing reads from variables */
116  virtual void printVarsReadForDot(std::ostream &os);
117  /* prints channel communication as labels on I/O edges */
118  virtual void printChansOnEdgesForDot(std::ostream &os);
119  /* prints channels as separate nodes (similar to variables) */
120  virtual void printChansSeparateForDot(std::ostream &os, bool ranked,
121  bool erd);
122 
123  public:
127  SignalFlow(const char* _title, TimedAutomataSystem& ta);
128 
129  void setVerbose(int verbose) { verbosity = verbose; }
133  virtual ~SignalFlow();
134 
138  void printForTron(std::ostream &os);
139 
146  virtual void printForDot(std::ostream &os, bool ranked, bool erd,
147  bool cEdged);
148 
153  int32_t visitEmptyStatement(EmptyStatement *stat) override;
154  int32_t visitExprStatement(ExprStatement *stat) override;
155  int32_t visitForStatement(ForStatement *stat) override;
156  int32_t visitIterationStatement(IterationStatement *stat) override;
157  int32_t visitWhileStatement(WhileStatement *stat) override;
158  int32_t visitDoWhileStatement(DoWhileStatement *stat) override;
159  int32_t visitBlockStatement(BlockStatement *stat) override;
160  int32_t visitSwitchStatement(SwitchStatement *stat) override;
161  int32_t visitCaseStatement(CaseStatement *stat) override;
162  int32_t visitDefaultStatement(DefaultStatement *stat) override;
163  int32_t visitIfStatement(IfStatement *stat) override;
164  int32_t visitBreakStatement(BreakStatement *stat) override;
165  int32_t visitContinueStatement(ContinueStatement *stat) override;
166  int32_t visitReturnStatement(ReturnStatement *stat) override;
167  int32_t visitAssertStatement(UTAP::AssertStatement *stat) override;
168  };
169 
174  template <class T>
175  struct print: public std::unary_function<T, void>
176  {
177  std::ostream& os;
178  const char *infix;
179  bool need;
180  print(std::ostream& out, const char* sep):
181  os(out), infix(sep), need(false) {}
182  void operator()(const T& x)
183  {
184  if (need) os << infix;
185  os << x; need = true;
186  }
187  };
188 
189  inline std::ostream& operator<<(std::ostream& os, const SignalFlow::strs_t& s)
190  {
191  for_each(s.begin(), s.end(), print<const char*>(os, ", "));
192  return os;
193  }
194 
195  inline std::ostream& operator<<(std::ostream& os, const SignalFlow::procs_t& ps)
196  {
197  SignalFlow::procs_t::const_iterator p=ps.begin(), e=ps.end();
198  if (p!=e) { os << (*p)->name; ++p; }
199  while (p!=e) { os << ", " << (*p)->name; ++p; }
200  return os;
201  }
202 
228  class Partitioner: public SignalFlow
229  {
230  protected:
231  procs_t procsEnv, procsIUT, procsBad;
232  strs_t chansIntEnv, chansIntIUT, observable, chansBad;
233  strs_t varsEnv, varsIUT, varsBad;
234  strs_t chansInp, chansOut;
235  const char* rule;
236 
237  void addProcs(const strs_t& chans, const str2procs_t& index,
238  procs_t& result, procs_t& exclude);
239  void addIntChans(const procs_t& procs,
240  strs_t& result, strs_t& exclude);
241  void addIntVars(const procs_t& procs, strs_t& result,
242  strs_t& exclude);
243  void addProcsByVars(const strs_t& vars, procs_t& procs,
244  procs_t& exclude);
245  public:
246  Partitioner(const char* _title, TimedAutomataSystem& ta):
247  SignalFlow(_title, ta) {}
248  ~Partitioner();
249 
250  int partition(const strs_t& inputs, const strs_t& outputs);
251  int partition(std::istream& ioinfo);
252  void printForDot(std::ostream &os, bool ranked, bool erd, bool cEdged) override;
253  void printViolation(const proc_t* process, const char* variable);
254  void fillWithEnvProcs(strs_t& procs);
255  void fillWithIUTProcs(strs_t& procs);
256  };
257 
267  {
268  strs_t varNeedles; // set of variables of interest
269  strs_t procNeedles; // set of processes of interest
270  bool distancesUpToDate;
271 
272  struct dist_t // distance structure
273  {
274  uint32_t hops; // number of hops to closest needle
275  uint32_t complexity; // complexity of this entity
276  uint32_t distance; // accumulated complexity|hops to closest needle
277  dist_t(int h, int c, int d): hops(h), complexity(c), distance(d) {}
278  dist_t(): hops(0), complexity(1), distance(0) {}
279  };
280 
281  typedef std::map<const char*, dist_t, less_str> str2dist_t;
282 
283  str2dist_t distances;
284 
285  void updateDistancesFromVariable(const char* name,
286  const dist_t* distance);
287  void updateDistancesFromProcess(const char* name,
288  const dist_t* distance);
289  int calcComplexity(const char* process);
290 
291  protected:
293  /* overwrite how processes appear */
294  void printProcsForDot(std::ostream &os, bool erd) override;
295  /* overwrite how variables appear */
296  void printVarsForDot(std::ostream &os, bool ranked, bool erd) override;
297 
298  public:
299  DistanceCalculator(const char* _title, TimedAutomataSystem& ta):
300  SignalFlow(_title, ta), distancesUpToDate(false), taSystem(ta) {}
301  virtual ~DistanceCalculator();
303  void addVariableNeedle(const char* var);
305  void addProcessNeedle(const char* proc);
307  void updateDistances();
309  uint32_t getDistance(const char* element);
310 
311  /* overwritten to update the distances on demand. */
312  void printForDot(std::ostream &os, bool ranked, bool erd,
313  bool cEdged) override;
314 
315  };
316 }
317 
318 #endif
print(std::ostream &out, const char *sep)
Definition: signalflow.h:180
virtual void printChansOnEdgesForDot(std::ostream &os)
Definition: signalflow.cpp:197
Class SignalFlow is for analysing UPPAAL specifications and extracting the timed automata input/outpu...
Definition: signalflow.h:56
const char * cChan
Definition: signalflow.h:88
Partial instance of a template.
Definition: system.h:329
int32_t visitWhileStatement(WhileStatement *stat) override
Definition: signalflow.cpp:716
int32_t visitBreakStatement(BreakStatement *stat) override
Definition: signalflow.cpp:768
void visitExpression(const expression_t &)
Definition: signalflow.cpp:444
DistanceCalculator is used in TargetFirst heuristic search order of Uppaal.
Definition: signalflow.h:266
int32_t visitCaseStatement(CaseStatement *stat) override
Definition: signalflow.cpp:747
virtual void printVarsReadForDot(std::ostream &os)
Definition: signalflow.cpp:175
virtual void printForDot(std::ostream &os, bool ranked, bool erd, bool cEdged)
Print I/O information in DOT format into given output stream.
Definition: signalflow.cpp:326
virtual ~SignalFlow()
All strings are from TASystem (don&#39;t dispose TASystem before SignalFlow).
Definition: signalflow.cpp:789
int32_t visitIfStatement(IfStatement *stat) override
Definition: signalflow.cpp:758
A reference to a symbol.
Definition: symbols.h:107
void visitProcess(instance_t &)
Definition: signalflow.cpp:420
const char * infix
Definition: signalflow.h:178
int32_t visitContinueStatement(ContinueStatement *stat) override
Definition: signalflow.cpp:773
Statement class for the iterator loop-construction.
Definition: statement.h:91
void addChan(const std::string &, strs_t &, str2procs_t &)
Definition: signalflow.cpp:394
int32_t visitDefaultStatement(DefaultStatement *stat) override
Definition: signalflow.cpp:753
std::stack< std::pair< bool, bool > > ioStack
Definition: signalflow.h:91
int32_t visitEmptyStatement(EmptyStatement *stat) override
System visitor pattern extracts read/write information from UCode.
Definition: signalflow.cpp:691
virtual void printProcsForDot(std::ostream &os, bool erd)
Definition: signalflow.cpp:101
std::set< proc_t * > procs_t
Definition: signalflow.h:76
bool operator()(const char *s1, const char *s2) const
Definition: signalflow.h:60
int32_t visitBlockStatement(BlockStatement *stat) override
Definition: signalflow.cpp:729
Partitions the system into environment and IUT according to TRON assumptions.
Definition: signalflow.h:228
std::map< const char *, strs_t > str2strs_t
Definition: signalflow.h:67
Partitioner(const char *_title, TimedAutomataSystem &ta)
Definition: signalflow.h:246
strs_t variables
Definition: signalflow.h:85
std::ostream & operator<<(std::ostream &os, const SignalFlow::strs_t &s)
Definition: signalflow.h:189
std::set< const char *, const less_str > strs_t
Definition: signalflow.h:64
virtual void printChansSeparateForDot(std::ostream &os, bool ranked, bool erd)
Definition: signalflow.cpp:290
std::stack< exprref_t > valparams
Definition: signalflow.h:93
std::stack< exprref_t > refparams
Definition: signalflow.h:92
str2procs_t receivers
Definition: signalflow.h:84
virtual void printVarsForDot(std::ostream &os, bool ranked, bool erd)
Definition: signalflow.cpp:120
int32_t visitIterationStatement(IterationStatement *stat) override
Definition: signalflow.cpp:702
std::map< const proc_t *, strs_t > proc2strs_t
Definition: signalflow.h:65
print – template for pretty printing lists.
Definition: signalflow.h:175
std::ostream & os
Definition: signalflow.h:177
bool checkParams(const symbol_t &s)
Definition: signalflow.cpp:359
str2procs_t transmitters
Definition: signalflow.h:84
int32_t visitReturnStatement(ReturnStatement *stat) override
Definition: signalflow.cpp:783
std::map< const symbol_t, expression_t > exprref_t
Definition: signalflow.h:78
proc_t(const char *_name)
Definition: signalflow.h:74
SignalFlow(const char *_title, TimedAutomataSystem &ta)
Analyse the system and extract I/O information:
Definition: signalflow.cpp:50
const char * rule
Definition: signalflow.h:235
proc_t * cTA
Definition: signalflow.h:86
void addVar(const symbol_t &, str2strs_t &, str2procs_t &)
Definition: signalflow.cpp:407
void printForTron(std::ostream &os)
Print I/O information in TRON format into given output stream.
Definition: signalflow.cpp:66
int32_t visitSwitchStatement(SwitchStatement *stat) override
Definition: signalflow.cpp:741
void operator()(const T &x)
Definition: signalflow.h:182
strs_t processes
Definition: signalflow.h:85
A reference to an expression.
Definition: expression.h:70
DistanceCalculator(const char *_title, TimedAutomataSystem &ta)
Definition: signalflow.h:299
int32_t visitForStatement(ForStatement *stat) override
Definition: signalflow.cpp:708
std::string chanString
Definition: signalflow.h:89
Definition: lexer.cc:817
std::map< const char *, procs_t, less_str > str2procs_t
Definition: signalflow.h:77
int32_t visitExprStatement(ExprStatement *stat) override
Definition: signalflow.cpp:696
const char * title
Definition: signalflow.h:82
void setVerbose(int verbose)
Definition: signalflow.h:129
int32_t visitDoWhileStatement(DoWhileStatement *stat) override
Definition: signalflow.cpp:722
virtual void printVarsWriteForDot(std::ostream &os)
Definition: signalflow.cpp:153
instance_t * cP
Definition: signalflow.h:87
int32_t visitAssertStatement(UTAP::AssertStatement *stat) override
Definition: signalflow.cpp:778
TimedAutomataSystem & taSystem
Definition: signalflow.h:292