DiaDes  0.1
DIAgnosis of Discrete-Event System
Rule.hh
Go to the documentation of this file.
1 #ifndef RULE_H_
2 #define RULE_H_
3 
4 #include<set>
5 #include<list>
6 #include<stdexcept>
8 #include<iostream>
9 #include<diades/sdmdl/Event.hh>
12 
13 namespace Diades
14 {
15 namespace Sdmdl
16 {
17 
21 class RuleInvalid : public runtime_error, public domain_error {
22 public:
26  RuleInvalid(const string& whatArg) : runtime_error(whatArg), domain_error(whatArg){
27  cerr << "EXCEPTION RuleInvalid -> " << whatArg << endl;
28  }
29 };
30 
31 class Rule
32 {
33 private:
34  vector<Formula::ConstReference> _precond;
35  vector<Formula::ConstReference> _effect;
36  const Event * _event;
37  list<const Event *> _events;
38  vector<int *> cubes;
39  int size;
40 
41 
42 
43 public:
44  Rule();
45  virtual ~Rule();
46  void makePrecondition(const Formula & precondition)
47  {
48  require(RuleInvalid,!precondition.isNull(),"makePrecondition: invalid precondition");
49  _precond.clear();
50  _precond.push_back(precondition);
51  ensure(RuleInvalid,!_precond[0].get().isNull(), "makePrecondition: assignment of formula is invalid");
52  ensure(RuleInvalid,_precond[0].get().equivalent(precondition), "makePrecondition: assignment of formula is not logiclly equivalent");
53  }
54  void makeEffect(const Formula & effect) {
55  require(RuleInvalid,!effect.isNull(),"makeEffect: invalid effect");
56  _effect.clear();
57  _effect.push_back(effect);
58  ensure(RuleInvalid,!_effect[0].get().isNull(), "makeEffect: assignment of formula is invalid");
59  ensure(RuleInvalid,!_effect[0].get().equivalent(effect), "makeEffect: assignment of formula is not logiclly equivalent");
60  }
61  bool valid() const
62  {
63  return (_event != 0) && (!_precond.empty()) && (!_precond[0].get().isNull())
64  && (!_effect.empty()) && (!_effect[0].get().isNull());
65  }
66 
67 
68  const Formula & precondition() const
69  {
70  require(RuleInvalid, (!_precond.empty()) && (!_precond[0].get().isNull()),"precondition: the return precondition is invalid");
71  return _precond[0];
72  }
73  const Formula & effect() const
74  {
75  require(RuleInvalid, (!_effect.empty()) && (!_effect[0].get().isNull()),"effect: the return effect is invalid");
76  return _effect[0];
77  }
78  const Event & inputEvent() const { return *(_event); }
79  const list<const Event *> & outputEvents() const { return _events; }
80 
81 
82 
83 
84  void setInputEvent(const Event & evt)
85  {
86  require(RuleInvalid, evt.type() == Port::Input,
87  "setInputEvent, wrong event type");
88  require(RuleInvalid, _event == 0, "setInputEvent, the rule has already an input event");
89  _event = &evt;
90  }
91  void insertOutputEvent(const Event & evt)
92  {
93  require(RuleInvalid, evt.type() == Port::Output,
94  "setOutputEvent, wrong event type");
95  _events.push_back(&evt);
96  }
97 
98  void generateCubes()
99  {
100  for(vector<int *>::size_type i = 0; i < cubes.size(); ++i)
101  {
102  if(cubes[i] != 0)
103  {
104  delete cubes[i];
105  }
106  }
107  cubes.clear();
108  CubeGenerator gen(effect());
109  size = gen.cubeSize();
110  vector<int *>::size_type index = 0;
111  while(!gen.isEmpty())
112  {
113  cubes.push_back(new int[size]);
114  for(int i = 0; i < size; ++i)
115  {
116  cubes[index][i] = gen.currentCube()[i];
117  }
118  gen.nextCube();
119  ++index;
120  }
121  }
122 
123  vector<int *>::const_iterator beginOfCubes() const { return cubes.begin(); }
124  vector<int *>::const_iterator endOfCubes() const { return cubes.end(); }
125  int cubeSize() const { return size; }
126 
127 
128  friend ostream & operator << (ostream & os, const Rule & rule);
129 
130 
131 };
132 
133 };
134 };
135 
136 #endif /*RULE_H_*/
vector< int * >::const_iterator endOfCubes() const
Definition: Rule.hh:124
void setInputEvent(const Event &evt)
Definition: Rule.hh:84
const Formula & precondition() const
Definition: Rule.hh:68
vector< Formula::ConstReference > _effect
Definition: Rule.hh:35
vector< Formula::ConstReference > _precond
Definition: Rule.hh:34
const Event * _event
Definition: Rule.hh:36
const Event & inputEvent() const
Definition: Rule.hh:78
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
Formula class using BDD.
const list< const Event * > & outputEvents() const
Definition: Rule.hh:79
bool valid() const
Definition: Rule.hh:61
void makePrecondition(const Formula &precondition)
Definition: Rule.hh:46
vector< int * > cubes
Definition: Rule.hh:38
ostream & operator<<(ostream &os, const StateNode< T > &stateNode)
void makeEffect(const Formula &effect)
Definition: Rule.hh:54
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
void generateCubes()
Definition: Rule.hh:98
int cubeSize() const
Definition: Rule.hh:125
const Formula & effect() const
Definition: Rule.hh:73
vector< int * >::const_iterator beginOfCubes() const
Definition: Rule.hh:123
void insertOutputEvent(const Event &evt)
Definition: Rule.hh:91
RuleInvalid(const string &whatArg)
Definition: Rule.hh:26
list< const Event * > _events
Definition: Rule.hh:37