DiaDes  0.1
DIAgnosisofDiscrete-EventSystem
Expression.hh
Go to the documentation of this file.
1 #ifndef __DIADES__ALTARICA_EXPRESSION__HH
2 #define __DIADES__ALTARICA_EXPRESSION__HH
3 
4 #include<vector>
5 #include<arsyntax/altarica-tree.h>
11 
12 namespace Diades
13 {
14  namespace Altarica
15  {
16 
17  /*****************************************************/
18  class AltaricaModel;
19  class NodeDecl;
20 
26  class Expression {
27  public:
34  typedef enum {IfThenElse=0,Case=1,Or=2,And=3,Eq=4,Neq=5,Imply=6,Lt=7,Gt=8,Leq=9,Geq=10,Add=11,Sub=12,Mul=13,Div=14,Mod=15,Neg=16,Not=17,Paren=18,StructMember=19,ArrayMember=20,Var=21,Id=22,Min=23,Max=24,True=25,False=26,Integer=27,ForAll=28,Exist=29,FunctionCall=30,Struct=31,Array=32} Category;
35 
36 
48  typedef reference_wrapper<Expression const> ConstReference;
49  typedef reference_wrapper<Expression> Reference;
54  typedef std::vector<ConstReference> ExprVector;
55 
56 
57 
58  public:
60  static string typeName() { return "Altarica::Expression"; }
61 
62  public:
63  typedef std::vector<VariableDecl::ConstReference>::const_iterator VariableIterator;
64  typedef std::vector<VariableDecl::ConstReference>::size_type SizeType;
65  typedef std::vector<VariableDecl::ConstReference> Support;
66  protected:
67  Support _varSupport;
68 
73  const NodeDecl & _owner;
74  unsigned _id;
75  //mutable Type::ConstReference _returnedType;
76  public:
77 
82  virtual ~Expression()
83  {
84  _varSupport.clear();
85  }
86 
91  const AltaricaModel & model() const;
92 
97  const NodeDecl & owner() const;
98 
99 
100  bool operator==(const Expression & e) const
101  {
102  return this == &e;
103  }
104 
105 
112  Expression(const NodeDecl & owner,unsigned id);
113 
114 
115  // /**
116  // Expression evaluation: this operation evaluates the expression with
117  // the current value associated to the variables that support the Expression.
118  // The evaluation is failing if there is a variable of the support that is
119  // still free (that is an unassigned variable).
120  // @param value the Value resulting from the evaluation of the current
121  // Expression.
122  // @return true if the operation succeeds, false otherwise.
123  // **/
124  // virtual bool eval(Value & value) const = 0;
125 
131  virtual Category category() const = 0;
132 
133 
140  unsigned id() const { return _id; }
141 
142 
146  virtual bool valid() const = 0;
147 
152  VariableIterator variableBegin() const { return _varSupport.begin(); }
153 
158  VariableIterator variableEnd() const { return _varSupport.end(); }
159 
164  SizeType supportSize() const
165  {
166  return _varSupport.size();
167  }
168 
175  const Support & support() const
176  {
177  return _varSupport;
178  }
179 
184  virtual const Type & valueType() const = 0;
185 
189  virtual bool isConstant() const = 0;
190 
197  friend ostream & operator<<(ostream & os, const Expression & expr);
198 
199 
200  //friend class NodeInstance;
201  };
202 
211  vector<VariableDecl::ConstReference> & mergedSupport);
212 
219  vector<VariableDecl::ConstReference> & appendedSupport);
220 
221 
222 
223  class NullExpression : public Expression
224  {
225 
226 
227  public:
229  virtual bool isConstant() const { return true; }
230  virtual const Type & valueType() const;
231  virtual Category category() const { return False; }
232  virtual bool valid() const { return false; }
233  virtual ~NullExpression(){}
234  };
235 
236  /*****************************************************/
237 
238 
239  class IteExpression: public Expression
240  {
241  private:
242  const Expression & _if;
243  const Expression & _then;
244  const Expression & _else;
245  public:
246  IteExpression(const NodeDecl & owner,unsigned id, const Expression & ifE, const Expression & thenE, const Expression & elseE);
247  virtual bool valid() const { return _if.valid() && _then.valid() && _else.valid(); }
248  Category category() const { return IfThenElse; }
249  //bool eval(Value & result) const;
250 
251  virtual const Type & valueType() const
252  {
253  return _then.valueType();
254  }
255  const Expression & ifExpr() const { return _if; }
256  const Expression & thenExpr() const { return _then; }
257  const Expression & elseExpr() const { return _else; }
258 
262  virtual bool isConstant() const
263  {
264  return ifExpr().isConstant() && thenExpr().isConstant() && elseExpr().isConstant();
265  }
266 
267  virtual ~IteExpression(){}
268 
269  friend ostream & operator<<(ostream & os, const Expression & expression);
270  //friend class NodeInstance;
271  };
272 
273  /*****************************************************/
274 
275 
284  {
285  private:
288  public:
289  typedef ExprVector::size_type AlternativeIndex;
290  public:
291  CaseExpression(const NodeDecl & owner,unsigned id,const ExprVector & choices,
292  const ExprVector & expressions);
293  AlternativeIndex first() const {
294  return 0;
295  }
296 
297  AlternativeIndex last() const {
298  return _choices.size() - 1;
299  }
300 
301  const Expression & getAlternative(AlternativeIndex index) const
302  {
303  return _choices[index];
304  }
305  const Expression & getAlternativeExpression(AlternativeIndex index) const
306  {
307  return _expressions[index];
308  }
309 
310  const Expression & defaultCase() const { return _expressions.back(); }
311  virtual bool valid() const
312  {
313  bool result = !_choices.empty() && (_choices.size() == _expressions.size()-1);
314  unsigned index = 0;
315  while(result && (index < _choices.size()))
316  {
317  result = _choices[index].get().valid() && _expressions[index].get().valid();
318  ++index;
319  }
320  if(result)
321  {
322  result = _expressions[_expressions.size()-1].get().valid();
323  }
324  return result;
325  }
326  Category category() const { return Case; }
327  //bool eval(Value & result) const;
328  virtual const Type & valueType() const
329  {
330  return defaultCase().valueType();
331  }
335  virtual bool isConstant() const
336  {
337  bool result = true;
338  for(AlternativeIndex index = first(); index<= last(); ++index)
339  {
340  result = result && getAlternative(index).isConstant()
341  && getAlternativeExpression(index).isConstant();
342  }
343  result = result && defaultCase().isConstant();
344  return result;
345  }
346 
347  virtual ~CaseExpression()
348  {
349  _choices.clear();
350  _expressions.clear();
351  }
352 
353  friend ostream & operator<<(ostream & os, const Expression & expression);
354  //friend class NodeInstance;
355  };
356 
357  /*****************************************************/
358 
359 
360 
362  {
363  public:
365 
366  private:
367  const Expression & _left;
370  Operator _op;
371  public:
372  BinaryExpression(const NodeDecl & owner,unsigned id,
373  const Expression & left,
374  const Expression & right,
376  virtual bool valid() const
377  {
378  return _left.valid() && _right.valid();
379  }
380  Operator op() const { return _op; }
381  const Expression & left() const { return _left; }
382  const Expression & right() const { return _right; }
383  Category category() const { return _category; }
384  virtual const Type & valueType() const;
385  virtual bool isConstant() const
386  {
387  return left().isConstant() && right().isConstant();
388  }
389  virtual ~BinaryExpression(){}
390  };
391 
392 
393  /*****************************************************/
394 
395 
397  {
398  private:
399  const Expression & _expr;
401  public:
402  UnaryExpression(const NodeDecl & owner, unsigned id,const Expression & expr,Category category);
403  Category category() const { return _category; }
404  virtual bool valid() const
405  {
406  return _expr.valid();
407  }
408  virtual const Type & valueType() const
409  {
410  return _expr.valueType();
411  }
412  const Expression & expression() const
413  {
414  return _expr;
415  }
416  virtual bool isConstant() const
417  {
418  return expression().isConstant();
419  }
420  virtual ~UnaryExpression(){}
421  };
422 
423 
424 
425 
426 
427 
428 
429 
430 
431 
432 
433 
434  // /*****************************************************/
435 
436 
437 
438  /*****************************************************/
439 
441  {
442  private:
445  public:
446  StructMemberExpression(const NodeDecl & owner, unsigned id, const Expression & structure,
447  const Expression & field);
448  Category category() const { return StructMember; }
449 
451  //VariableDecl getVariableDecl() const;
452  virtual bool valid() const
453  {
454  return _structure.valid() && _field.valid();
455  }
456  // bool eval(Value & result) const;
457  const Expression & getStructure() const
458  {
459  return _structure;
460  }
461  const Expression & getMember() const
462  {
463  return _field;
464  }
466  virtual const Type & valueType() const;
467  virtual bool isConstant() const
468  {
469  return false;
470  }
471  };
472 
473 
474  /*****************************************************/
475 
477  {
478  private:
481 
482  public:
483  ArrayMemberExpression(const NodeDecl & owner, unsigned id,const Expression & array, const Expression & exprField);
484  const Expression & arrayExpr() const
485  {
486  return _array;
487  }
488  const Expression & indexExpr() const
489  {
490  return _exprField;
491  }
492  const Expression & left() const
493  {
494  return arrayExpr();
495  }
496  const Expression & right() const
497  {
498  return indexExpr();
499  }
500  Category category() const { return ArrayMember; }
501  // bool eval(Value & result) const;
502  virtual bool valid() const
503  {
504  return (_array.valid() && _exprField.valid());
505  }
507  //VariableDecl getVariableDecl() const;
509  virtual const Type & valueType() const;
510  virtual bool isConstant() const
511  {
512  return false;
513  }
514  friend ostream & operator<<(ostream & os, const Expression & expression);
515  //friend class NodeInstance;
516  };
517 
518 
519  /*****************************************************/
520 
521  class VarExpression: public Expression
522  {
523  public:
524  VarExpression(const NodeDecl & owner, unsigned id,const VariableDecl & var);
525  Category category() const { return Var; }
526  virtual bool valid() const { return !_varSupport.begin()->get().isNull(); }
527  const VariableDecl & getVariableDecl() const { return *variableBegin(); }
528  // bool eval(Value & result) const;
529 
530  virtual const Type & valueType() const
531  {
532  return getVariableDecl().type();
533  }
534  virtual bool isConstant() const
535  {
536  return false;
537  }
538  virtual ~VarExpression(){}
539  friend ostream & operator<<(ostream & os, const Expression & expression);
540  };
541 
542 
543  /*****************************************************/
544 
545 
546  class IdExpression: public Expression
547  {
548  private:
550 
551  public:
552  IdExpression(const NodeDecl & owner, unsigned id,const Identifier & identifier);
553  const Identifier & identifier() const { return _ident; }
554  virtual bool valid() const
555  {
556  return !_ident.str().empty();
557  }
558  Category category() const { return Id; }
559  // bool eval(Value & result) const;
560  virtual const Type & valueType() const;
561  virtual bool isConstant() const
562  {
563  return true;
564  }
565  virtual ~IdExpression(){}
566  friend ostream & operator<<(ostream & os, const Expression & expression);
567  //friend class NodeInstance;
568  };
569 
570 
571  /*****************************************************/
572 
574  {
575  private:
578  public:
579  MinMaxExpression(const NodeDecl & owner, unsigned id,const ExprVector & body,Category category);
580  Category category() const { return _category; }
581  ExprVector::const_iterator bodyBegin() const
582  {
583  return _body.begin();
584  }
585  ExprVector::const_iterator bodyEnd() const
586  {
587  return _body.end();
588  }
589  virtual bool valid() const
590  {
591  bool result = !_body.empty();
592  unsigned index = 0;
593  while(result && (index < _body.size()))
594  {
595  result = _body[index].get().valid();
596  ++index;
597  }
598  return result;
599  }
600  // bool eval(Value & result) const;
601  virtual const Type & valueType() const;
602  virtual bool isConstant() const
603  {
604  bool result = true;
605  ExprVector::size_type index = 0;
606  while(result && (index < _body.size()-1))
607  {
608  result = _body[index].get().isConstant();
609  ++index;
610  }
611  return result;
612  }
614  {
615  _body.clear();
616  }
617  friend ostream & operator<<(ostream & os, const Expression & expression);
618  };
619 
620 
621 
622  /*****************************************************/
623 
624  class TrueExpression : public Expression
625  {
626 
627 
628  public:
629  TrueExpression(const NodeDecl & owner, unsigned id);
630  Category category() const { return True; }
631  virtual bool valid() const { return true; }
632  // bool eval(Value & result) const;
633  virtual const Type & valueType() const;
634  virtual bool isConstant() const
635  {
636  return true;
637  }
638  virtual ~TrueExpression(){}
639  friend ostream & operator<<(ostream & os, const Expression & expression);
640  //friend class NodeInstance;
641  };
642 
643 
644 
645  /*****************************************************/
646 
648  {
649  private:
650 
651  public:
652  FalseExpression(const NodeDecl & owner, unsigned id);
653  Category category() const { return False; }
654  // bool eval(Value & result) const;
655  virtual const Type & valueType() const;
656  virtual bool isConstant() const
657  {
658  return true;
659  }
660  virtual bool valid() const { return true; }
661  virtual ~FalseExpression(){}
662  friend ostream & operator<<(ostream & os, const Expression & expression);
663  };
664 
665 
666 
667  /*****************************************************/
668 
669 
671  {
672  private:
673  int _value;
674  public:
675  IntegerExpression(const NodeDecl & owner, unsigned id, int value);
676  Category category() const { return Integer; }
677  // bool eval(Value & result) const;
678  virtual bool valid() const { return true; }
679  virtual const Type & valueType() const;
680 
681  virtual bool isConstant() const
682  {
683  return true;
684  }
685  int integer() const { return _value; }
686  virtual ~IntegerExpression(){}
687  friend ostream & operator<<(ostream & os, const Expression & expression);
688  };
689 
690 
691  /*****************************************************/
692 
693 
695  {
696  private:
697  public:
698  Category category() const { return ForAll; }
699  virtual const Type & valueType() const;
700  virtual bool isConstant() const
701  {
702  return false;
703  }
704  virtual ~ForAllExpression(){}
705  friend ostream & operator<<(ostream & os, const Expression & expression);
706  };
707 
708 
709  /*****************************************************/
710 
711 
713  {
714  private:
715  public:
716  Category category() const { return Exist; }
717  virtual const Type & valueType() const;
718  virtual bool isConstant() const
719  {
720  return false;
721  }
722  virtual ~ExistExpression(){}
723  friend ostream & operator<<(ostream & os, const Expression & expression);
724  };
725 
726  /*****************************************************/
727 
729  {
730  private:
731  //Function * _func;
732  vector<VariableDecl> _param;
733  public:
734  Category category() const { return FunctionCall; }
735  // bool eval(Value & result) const;
739  virtual const Type & valueType() const;
740  virtual bool isConstant() const
741  {
742  return false;
743  }
745  friend ostream & operator<<(ostream & os, const Expression & expression);
746  //friend class NodeInstance;
747  };
748 
749 
750  /*****************************************************/
751 
752 
754  {
755  private:
756  vector<Identifier> _identifiers;
758  public:
759  StructExpression(const NodeDecl & owner, unsigned id,const vector<Identifier> & identifiers,
760  const ExprVector & expressions);
761  vector<Identifier>::size_type size() const
762  {
763  return _identifiers.size();
764  }
765 
766 
767  const Identifier & identifierOfIndex(vector<Identifier>::size_type index) const
768  {
769  return _identifiers[index];
770  }
771  const Expression & expressionOfIndex(ExprVector::size_type index) const
772  {
773  return _expressions[index];
774  }
775 
776 
777  Category category() const { return Struct; }
778  // bool eval(Value & result) const;
779  virtual bool valid() const
780  {
781  bool result = !_identifiers.empty() && (_identifiers.size() == _expressions.size());
782  unsigned index = 0;
783  while(result && (index < _identifiers.size()))
784  {
785  result = (!_identifiers[index].str().empty()) && (_expressions[index].get().valid());
786  ++index;
787  }
788  return result;
789  }
790  virtual const Type & valueType() const;
791  virtual bool isConstant() const
792  {
793  bool result = true;
794  ExprVector::size_type index = 0;
795  while(result && (index != size()))
796  {
797  result = _expressions[index].get().isConstant();
798  ++index;
799  }
800  return result;
801  }
802  virtual ~StructExpression(){ _identifiers.clear(); _expressions.clear(); }
803  };
804 
805 
806  /*****************************************************/
807 
808 
810  {
811  private:
813  public:
814  ArrayExpression(const NodeDecl & owner, unsigned id,const ExprVector & array);
815  Category category() const { return Array; }
816  // bool eval(Value & result) const;
817 
818  ExprVector::const_iterator bodyBegin() const
819  {
820  return _array.begin();
821  }
822  ExprVector::const_iterator bodyEnd() const
823  {
824  return _array.end();
825  }
826  ExprVector::size_type size() const
827  {
828  return _array.size();
829  }
830 
831 
832  virtual bool valid() const
833  {
834  bool result = !_array.empty();
835  unsigned index = 0;
836  while(result && (index < _array.size()))
837  {
838  result = _array[index].get().valid();
839  ++index;
840  }
841  return result;
842  }
843  virtual const Type & valueType() const;
844  virtual bool isConstant() const
845  {
846  bool result = true;
847  ExprVector::size_type index = 0;
848  while(result && (index != _array.size()))
849  {
850  result = _array[index].get().isConstant();
851  ++index;
852  }
853  return result;
854  }
855  virtual ~ArrayExpression(){ _array.clear(); }
856  };
857 
858 
865  const Expression & nullExpression();
866 
867  };
868 };
869 
870 
871 
872 
873 #endif
ExprVector::const_iterator bodyEnd() const
Definition: Expression.hh:822
Expression(const NodeDecl &owner, unsigned id)
const Expression & right() const
Definition: Expression.hh:382
An Identifier is a reference to a string (IdentifierData) that only contains alpha-numeric characters...
Definition: Identifier.hh:121
virtual bool valid() const
Definition: Expression.hh:247
virtual bool isConstant() const
Definition: Expression.hh:634
const Expression & expression() const
Definition: Expression.hh:412
virtual bool valid() const
Definition: Expression.hh:832
VariableIterator variableBegin() const
Definition: Expression.hh:152
virtual const Type & valueType() const
Definition: Expression.hh:328
const Expression & getMember() const
Definition: Expression.hh:461
virtual const Type & valueType() const
Definition: Expression.hh:251
virtual bool isConstant() const
Definition: Expression.hh:656
friend ostream & operator<<(ostream &os, const Expression &expr)
const Expression & left() const
Definition: Expression.hh:381
ExprVector::const_iterator bodyBegin() const
Definition: Expression.hh:581
const NodeDecl & owner() const
virtual bool isConstant() const
Definition: Expression.hh:534
virtual bool isConstant() const
Definition: Expression.hh:602
virtual bool isConstant() const
Definition: Expression.hh:262
const NodeDecl & _owner
VariableDecls that are supporting the Expression.
Definition: Expression.hh:73
const string & str() const
Definition: Identifier.hh:409
virtual Category category() const =0
const Identifier & identifier() const
Definition: Expression.hh:553
std::vector< VariableDecl::ConstReference > Support
Definition: Expression.hh:65
ExprVector::size_type size() const
Definition: Expression.hh:826
const Expression & getStructure() const
Definition: Expression.hh:457
virtual bool valid() const
Definition: Expression.hh:660
reference_wrapper< Expression const > ConstReference
Definition: Expression.hh:48
AlternativeIndex last() const
Definition: Expression.hh:297
virtual const Type & valueType() const =0
const Expression & expressionOfIndex(ExprVector::size_type index) const
Definition: Expression.hh:771
const Expression & right() const
Definition: Expression.hh:496
const VariableDecl & getVariableDecl() const
Definition: Expression.hh:527
bool operator==(const Expression &e) const
Definition: Expression.hh:100
virtual bool valid() const
Definition: Expression.hh:526
const Expression & nullExpression()
const Expression & elseExpr() const
Definition: Expression.hh:257
const Expression & arrayExpr() const
Definition: Expression.hh:484
virtual bool valid() const
Definition: Expression.hh:311
virtual const Type & valueType() const
Definition: Expression.hh:408
Utils::Exception< Expression > Exception
Definition: Expression.hh:59
virtual bool isConstant() const
Definition: Expression.hh:700
void appendExpressionSupports(const Expression::ExprVector &expr, vector< VariableDecl::ConstReference > &appendedSupport)
virtual bool valid() const
Definition: Expression.hh:631
reference_wrapper< Expression > Reference
Definition: Expression.hh:49
const Identifier & identifierOfIndex(vector< Identifier >::size_type index) const
Definition: Expression.hh:767
virtual bool valid() const
Definition: Expression.hh:232
virtual bool isConstant() const
Definition: Expression.hh:561
SizeType supportSize() const
Definition: Expression.hh:164
virtual bool isConstant() const
Definition: Expression.hh:335
bool mergeExpressionSupports(const Expression::ExprVector &expr, vector< VariableDecl::ConstReference > &mergedSupport)
virtual bool isConstant() const
Definition: Expression.hh:385
ExprVector::const_iterator bodyEnd() const
Definition: Expression.hh:585
AlternativeIndex first() const
Definition: Expression.hh:293
Namespace of the Diades project.
const Expression & getAlternativeExpression(AlternativeIndex index) const
Definition: Expression.hh:305
const Expression & left() const
Definition: Expression.hh:492
virtual bool valid() const =0
virtual bool isConstant() const
Definition: Expression.hh:844
virtual bool isConstant() const
Definition: Expression.hh:791
const Expression & getAlternative(AlternativeIndex index) const
Definition: Expression.hh:301
VariableIterator variableEnd() const
Definition: Expression.hh:158
virtual bool isConstant() const =0
ExprVector::size_type AlternativeIndex
Definition: Expression.hh:289
virtual bool valid() const
Definition: Expression.hh:404
virtual bool valid() const
Definition: Expression.hh:554
virtual bool isConstant() const
Definition: Expression.hh:229
Value::BinaryOperator Operator
Definition: Expression.hh:364
vector< Identifier > _identifiers
Definition: Expression.hh:756
const AltaricaModel & model() const
const Expression & thenExpr() const
Definition: Expression.hh:256
virtual bool isConstant() const
Definition: Expression.hh:718
virtual Category category() const
Definition: Expression.hh:231
vector< Identifier >::size_type size() const
Definition: Expression.hh:761
virtual bool isConstant() const
Definition: Expression.hh:416
virtual bool isConstant() const
Definition: Expression.hh:681
std::vector< VariableDecl::ConstReference >::size_type SizeType
Definition: Expression.hh:64
const Expression & ifExpr() const
Definition: Expression.hh:255
static string typeName()
Definition: Expression.hh:60
std::vector< ConstReference > ExprVector
Definition: Expression.hh:54
ExprVector::const_iterator bodyBegin() const
Definition: Expression.hh:818
const Expression & indexExpr() const
Definition: Expression.hh:488
virtual const Type & valueType() const
Definition: Expression.hh:530
const Expression & defaultCase() const
Definition: Expression.hh:310
std::vector< VariableDecl::ConstReference >::const_iterator VariableIterator
Definition: Expression.hh:63
const Support & support() const
Definition: Expression.hh:175