libutap
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 <list>
29 #include <set>
30 #include <map>
31 #include <stack>
32 #include <string.h>
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);
154  int32_t visitExprStatement(ExprStatement *stat);
155  int32_t visitForStatement(ForStatement *stat);
157  int32_t visitWhileStatement(WhileStatement *stat);
159  int32_t visitBlockStatement(BlockStatement *stat);
160  int32_t visitSwitchStatement(SwitchStatement *stat);
161  int32_t visitCaseStatement(CaseStatement *stat);
163  int32_t visitIfStatement(IfStatement *stat);
164  int32_t visitBreakStatement(BreakStatement *stat);
166  int32_t visitReturnStatement(ReturnStatement *stat);
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:
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);
253  void printViolation(const proc_t* process, const char* variable);
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  virtual void printProcsForDot(std::ostream &os, bool erd);
295  /* overwrite how variables appear */
296  virtual void printVarsForDot(std::ostream &os, bool ranked, bool erd);
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  virtual void printForDot(std::ostream &os, bool ranked, bool erd,
313  bool cEdged);
314 
315  };
316 }
317 
318 #endif
int32_t visitDefaultStatement(DefaultStatement *stat)
Definition: signalflow.cpp:753
const char * name
Definition: signalflow.h:70
virtual void printVarsWriteForDot(std::ostream &os)
Definition: signalflow.cpp:153
std::map< const char *, procs_t, less_str > str2procs_t
Definition: signalflow.h:77
Definition: statement.h:180
int32_t visitReturnStatement(ReturnStatement *stat)
Definition: signalflow.cpp:783
void addVariableNeedle(const char *var)
adds a variable needle to I/O map
Definition: signalflow.cpp:1520
strs_t varsIUT
Definition: signalflow.h:233
Definition: statement.h:75
void updateDistances()
Recalculates the distances to needles.
Definition: signalflow.cpp:1792
int32_t visitEmptyStatement(EmptyStatement *stat)
System visitor pattern extracts read/write information from UCode.
Definition: signalflow.cpp:691
void setVerbose(int verbose)
Definition: signalflow.h:129
bool paramsExpanded
Definition: signalflow.h:90
strs_t variables
Definition: signalflow.h:85
instance_t * cP
Definition: signalflow.h:87
SignalFlow(const char *_title, TimedAutomataSystem &ta)
Analyse the system and extract I/O information:
Definition: signalflow.cpp:50
int32_t visitSwitchStatement(SwitchStatement *stat)
Definition: signalflow.cpp:741
virtual void printVarsForDot(std::ostream &os, bool ranked, bool erd)
Definition: signalflow.cpp:120
Definition: statement.h:193
virtual ~SignalFlow()
All strings are from TASystem (don't dispose TASystem before SignalFlow).
Definition: signalflow.cpp:789
A reference to a symbol.
Definition: symbols.h:107
strs_t chansBad
Definition: signalflow.h:232
std::map< const proc_t *, strs_t > proc2strs_t
Definition: signalflow.h:65
void addIntChans(const procs_t &procs, strs_t &result, strs_t &exclude)
Takes the internal channels of each process from procs list and adds them to the result list...
Definition: signalflow.cpp:850
std::ostream & operator<<(std::ostream &os, const SignalFlow::strs_t &s)
Definition: signalflow.h:189
strs_t outChans
Definition: signalflow.h:71
int32_t visitExprStatement(ExprStatement *stat)
Definition: signalflow.cpp:696
Statement class for the iterator loop-construction.
Definition: statement.h:91
int32_t visitBreakStatement(BreakStatement *stat)
Definition: signalflow.cpp:768
strs_t varsBad
Definition: signalflow.h:233
Partitions the system into environment and IUT according to TRON assumptions.
Definition: signalflow.h:228
int32_t visitForStatement(ForStatement *stat)
Definition: signalflow.cpp:708
const char * infix
Definition: signalflow.h:178
str2procs_t transmitters
Definition: signalflow.h:84
int32_t visitCaseStatement(CaseStatement *stat)
Definition: signalflow.cpp:747
print(std::ostream &out, const char *sep)
Definition: signalflow.h:180
strs_t processes
Definition: signalflow.h:85
Definition: statement.h:202
procs_t procs
Definition: signalflow.h:83
Definition: statement.h:55
Definition: statement.h:222
Partial instance of a template.
Definition: system.h:331
Definition: statement.h:152
Class SignalFlow is for analysing UPPAAL specifications and extracting the timed automata input/outpu...
Definition: signalflow.h:56
bool operator()(const char *s1, const char *s2) const
Definition: signalflow.h:60
void operator()(const T &x)
Definition: signalflow.h:182
DistanceCalculator(const char *_title, TimedAutomataSystem &ta)
Definition: signalflow.h:299
Definition: statement.h:211
bool out
Definition: signalflow.h:90
std::set< const char *, const less_str > strs_t
Definition: signalflow.h:64
std::map< const char *, strs_t > str2strs_t
Definition: signalflow.h:67
Definition: lexer.cc:585
virtual void printChansSeparateForDot(std::ostream &os, bool ranked, bool erd)
Definition: signalflow.cpp:290
procs_t procsBad
Definition: signalflow.h:231
bool inp
Definition: signalflow.h:90
Definition: statement.h:105
~Partitioner()
Definition: signalflow.cpp:797
procs_t procsEnv
Definition: signalflow.h:231
void visitProcess(instance_t &)
Definition: signalflow.cpp:420
void addChan(const std::string &, strs_t &, str2procs_t &)
Definition: signalflow.cpp:394
void addVar(const symbol_t &, str2strs_t &, str2procs_t &)
Definition: signalflow.cpp:407
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:1213
uint32_t getDistance(const char *element)
Finds a distance measure for given element.
Definition: signalflow.cpp:1772
Definition: statement.h:127
int32_t visitContinueStatement(ContinueStatement *stat)
Definition: signalflow.cpp:773
Definition: statement.h:172
Definition: statement.h:65
std::stack< exprref_t > valparams
Definition: signalflow.h:93
void popIO()
Definition: signalflow.h:103
str2strs_t rdVars
Definition: signalflow.h:72
TimedAutomataSystem & taSystem
Definition: signalflow.h:292
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
Definition: statement.h:162
int partition(const strs_t &inputs, const strs_t &outputs)
Definition: signalflow.cpp:1071
void fillWithEnvProcs(strs_t &procs)
Definition: signalflow.cpp:1507
Definition: signalflow.h:69
A reference to an expression.
Definition: expression.h:68
int32_t visitWhileStatement(WhileStatement *stat)
Definition: signalflow.cpp:716
void printForTron(std::ostream &os)
Print I/O information in TRON format into given output stream.
Definition: signalflow.cpp:66
virtual void printProcsForDot(std::ostream &os, bool erd)
Definition: signalflow.cpp:101
str2procs_t receivers
Definition: signalflow.h:84
std::map< const symbol_t, expression_t > exprref_t
Definition: signalflow.h:78
strs_t chansOut
Definition: signalflow.h:234
bool sync
Definition: signalflow.h:90
bool checkParams(const symbol_t &s)
Definition: signalflow.cpp:359
Partitioner(const char *_title, TimedAutomataSystem &ta)
Definition: signalflow.h:246
const char * cChan
Definition: signalflow.h:88
void visitExpression(const expression_t &)
Definition: signalflow.cpp:444
int32_t visitAssertStatement(UTAP::AssertStatement *stat)
Definition: signalflow.cpp:778
Definition: signalflow.h:59
std::stack< exprref_t > refparams
Definition: signalflow.h:92
virtual ~DistanceCalculator()
Definition: signalflow.cpp:1761
strs_t channels
Definition: signalflow.h:85
int32_t visitBlockStatement(BlockStatement *stat)
Definition: signalflow.cpp:729
Definition: statement.h:46
void addIntVars(const procs_t &procs, strs_t &result, strs_t &exclude)
Takes the variables of each process from procs list and adds them to the result list.
Definition: signalflow.cpp:896
proc_t(const char *_name)
Definition: signalflow.h:74
void addProcessNeedle(const char *proc)
adds a variable needle to I/O map
Definition: signalflow.cpp:1553
int verbosity
Definition: signalflow.h:81
virtual void printVarsForDot(std::ostream &os, bool ranked, bool erd)
Definition: signalflow.cpp:1726
int32_t visitDoWhileStatement(DoWhileStatement *stat)
Definition: signalflow.cpp:722
std::ostream & os
Definition: signalflow.h:177
Definition: statement.h:116
virtual void printChansOnEdgesForDot(std::ostream &os)
Definition: signalflow.cpp:197
proc2strs_t outEdges
Definition: signalflow.h:73
int32_t visitIfStatement(IfStatement *stat)
Definition: signalflow.cpp:758
bool need
Definition: signalflow.h:179
void fillWithIUTProcs(strs_t &procs)
Definition: signalflow.cpp:1513
std::stack< std::pair< bool, bool > > ioStack
Definition: signalflow.h:91
int32_t visitIterationStatement(IterationStatement *stat)
Definition: signalflow.cpp:702
DistanceCalculator is used in TargetFirst heuristic search order of Uppaal.
Definition: signalflow.h:266
virtual void printProcsForDot(std::ostream &os, bool erd)
Definition: signalflow.cpp:1699
strs_t varsEnv
Definition: signalflow.h:233
strs_t inChans
Definition: signalflow.h:71
strs_t chansIntEnv
Definition: signalflow.h:232
std::set< proc_t * > procs_t
Definition: signalflow.h:76
strs_t observable
Definition: signalflow.h:232
void printViolation(const proc_t *process, const char *variable)
Definition: signalflow.cpp:805
strs_t chansInp
Definition: signalflow.h:234
const char * rule
Definition: signalflow.h:235
procs_t procsIUT
Definition: signalflow.h:231
print – template for pretty printing lists.
Definition: signalflow.h:175
str2strs_t wtVars
Definition: signalflow.h:72
Definition: system.h:472
void addProcs(const strs_t &chans, const str2procs_t &index, procs_t &result, procs_t &exclude)
Adds processes to the result which use the channels from chans list according to index.
Definition: signalflow.cpp:817
const char * title
Definition: signalflow.h:82
strs_t chansIntIUT
Definition: signalflow.h:232
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:1784
std::string chanString
Definition: signalflow.h:89
void addProcsByVars(const strs_t &vars, procs_t &procs, procs_t &exclude)
Take all variables and add all accessing processes to the list.
Definition: signalflow.cpp:945
void pushIO()
Definition: signalflow.h:100
proc_t * cTA
Definition: signalflow.h:86