OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ConcreteFun.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: ConcreteFun.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: ConcreteFun
10 // This class is the concrete class for a single constrained value
11 // for matching.
12 //
13 // ------------------------------------------------------------------------
14 //
15 // $Date: 2000/03/27 09:33:43 $
16 // $Author: Andreas Adelmann $
17 //
18 // ------------------------------------------------------------------------
19 
20 #include "Match/ConcreteFun.h"
21 #include "Attributes/Attributes.h"
22 #include "Utilities/LogicalError.h"
23 #include <iomanip>
24 #include <iostream>
25 
26 
27 // Character form of relational operators.
28 static const char *relString[] = { " == ", " > ", " < " };
29 
30 
31 // Class ConcreteFun
32 // ------------------------------------------------------------------------
33 
35 (Attribute &lhs, int rel, Attribute &rhs, Attribute &wgt):
36  itsLhs(lhs),
37  itsRhs(rhs),
38  relation(rel),
39  itsWeight(Attributes::getRealArray(wgt))
40 {}
41 
42 
44 {}
45 
46 
48  return itsWeight.size();
49 }
50 
51 
52 void ConcreteFun::evaluate(Vector<double> &fun, int &index) const {
55  std::vector<double>::size_type size = itsWeight.size();
56 
57  if(lValue.size() != size || rValue.size() != size) {
58  throw LogicalError("ConcreteFun::evaluate()",
59  "Matching constraint is inconsistent, size_of(LHS), "
60  "size_of(RHS), size_of(WGT) should all be equal.");
61  }
62 
63  value.erase(value.begin(), value.end());
64  value.resize(size, 0.0);
65 
66  for(std::vector<double>::size_type i = 0; i < size; ++i) {
67  switch(relation) {
68 
69  case 0:
70  value[i] = lValue[i] - rValue[i];
71  break;
72 
73  case 1:
74  if(lValue[i] < rValue[i]) value[i] = lValue[i] - rValue[i];
75  break;
76 
77  case 2:
78  if(lValue[i] > rValue[i]) value[i] = lValue[i] - rValue[i];
79  }
80 
81  fun[index++] = value[i] * itsWeight[i];
82  }
83 }
84 
85 
86 void ConcreteFun::print(std::ostream &os) const {
87  // Print the symbolic representation of the constraint.
88  os << "\nCondition: " << itsLhs << relString[relation] << itsRhs
89  << std::endl;
90 
91  // Print the value(s) of the constraint.
92  std::streamsize old_prec = os.precision(9);
93  os.setf(std::ios::scientific, std::ios::floatfield);
94 
95  for(std::vector<double>::size_type i = 0; i < lValue.size(); ++i) {
96  os << " lhs = " << std::setw(16) << lValue[i]
97  << ", rhs = " << std::setw(16) << rValue[i]
98  << ", wgt = " << std::setw(16) << itsWeight[i]
99  << ", val = " << std::setw(16) << value[i]
100  << std::endl;
101  }
102 
103  os.setf(std::ios::fixed, std::ios::floatfield);
104  os.precision(old_prec);
105 }
std::vector< double > value
Definition: ConcreteFun.h:75
Attribute itsLhs
Definition: ConcreteFun.h:67
FmtFlags_t setf(FmtFlags_t setbits, FmtFlags_t field)
Definition: Inform.h:104
virtual void evaluate(Vector< double > &f, int &) const
Evaluate the matching function(s).
Definition: ConcreteFun.cpp:52
A representation of an Object attribute.
Definition: Attribute.h:55
virtual ~ConcreteFun()
Definition: ConcreteFun.cpp:43
std::vector< double > itsWeight
Definition: ConcreteFun.h:70
Attribute itsRhs
Definition: ConcreteFun.h:68
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
Definition: Attributes.cpp:258
int precision() const
Definition: Inform.h:115
std::vector< double > lValue
Definition: ConcreteFun.h:73
std::vector< double > rValue
Definition: ConcreteFun.h:74
virtual int countConstraints() const
Get the number of constrained values.
Definition: ConcreteFun.cpp:47
Logical error exception.
Definition: LogicalError.h:33
virtual void print(std::ostream &) const
Print the function name and value(s).
Definition: ConcreteFun.cpp:86
Inform & endl(Inform &inf)
Definition: Inform.cpp:42