OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ConstraintCmd.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: ConstraintCmd.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.2 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: ConstraintCmd
10 // The class for the OPAL CONSTRAINT command.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2001/08/13 15:10:01 $
15 // $Author: jowett $
16 //
17 // ------------------------------------------------------------------------
18 
19 #include "Match/ConstraintCmd.h"
20 #include "Attributes/Attributes.h"
22 #include "Match/ConcreteFun.h"
23 #include "Match/Match.h"
24 #include "Parser/Statement.h"
26 #include "Utilities/Options.h"
27 #include "Utilities/ParseError.h"
28 #include "Utilities/Round.h"
29 #include <vector>
30 
31 
32 // Class ConstraintCmd
33 // ------------------------------------------------------------------------
34 
35 // The attributes of class ConstraintCmd.
36 namespace {
37  enum {
38  LHS, // the left-hand side of the constraint array
39  RHS, // the right-hand side of the constraint array
40  WGT, // the matching weight array
41  SIZE
42  };
43 }
44 
45 
47  Action(SIZE, "CONSTRAINT", "") {
49  ("LHS", "left-hand side");
51  ("RHS", "right-hand side");
53  ("WGT", "the weight(s) for this constraint");
54  relation = 0;
55 
57 }
58 
59 
60 ConstraintCmd::ConstraintCmd(const std::string &name, ConstraintCmd *parent):
61  Action(name, parent) {
62  relation = 0;
63 }
64 
65 
67 {}
68 
69 
70 ConstraintCmd *ConstraintCmd::clone(const std::string &name) {
71  return new ConstraintCmd(name, this);
72 }
73 
74 
76  ConcreteFun *fun =
77  new ConcreteFun(itsAttr[LHS], relation, itsAttr[RHS], itsAttr[WGT]);
79 
80  if(Options::info) {
81  std::cerr << "\n 1 constraint has been added.\n" << std::endl;
82  }
83 }
84 
85 
87  Expressions::parseDelimiter(stat, ',');
88 
89  // Constraint expressions.
90  itsAttr[LHS].parse(stat, false);
91 
92  if(stat.delimiter("==")) {
93  relation = 0;
94  } else if(stat.delimiter('>')) {
95  relation = 1;
96  } else if(stat.delimiter('<')) {
97  relation = 2;
98  } else {
99  throw ParseError("ConstraintCmd::parse()",
100  "Expected relational operator: "
101  "\"==\", \">\", or \"<\".");
102  }
103 
104  itsAttr[RHS].parse(stat, false);
105 
106  // Weight array.
107  Expressions::parseDelimiter(stat, ',');
108  std::string name = Expressions::parseString(stat, "Attribute name expected.");
109 
110  if(name == "WGT") {
111  if(stat.delimiter('=')) {
112  itsAttr[WGT].parse(stat, true);
113  } else if(stat.delimiter(":=")) {
114  throw ParseError("ConstraintCmd::parse()",
115  "The delimiter \":=\" is not allowed here.");
116  } else {
117  throw ParseError("ConstraintCmd::parse()",
118  "The attribute \"WGT\" has no default.");
119  }
120  } else {
121  throw ParseError("ConstraintCmd::parse()",
122  "Object \"CONSTRAINT\" has no attribute \"" +
123  name + "\".");
124  }
125 }
126 
127 
128 void ConstraintCmd::print(std::ostream &os) const {
129  os << getOpalName();
130  Object *parent = getParent();
131  if(parent != 0 && ! parent->getOpalName().empty()) {
132  if(! getOpalName().empty()) os << ':';
133  os << parent->getOpalName();
134  }
135 
136  os << "," << itsAttr[LHS]
137  << ((relation == 0) ? "==" : ((relation > 0) ? ">" : "<"))
138  << itsAttr[RHS] << ",WGT=" << itsAttr[WGT];
139  os << ';';
140  os << std::endl;
141 }
142 
143 
144 void ConstraintCmd::printHelp(std::ostream &os) const {
145  os << "\nThe \"CONSTRAINT\" sub-command defines a constraint.\n"
146  << "Its format is:\n"
147  << "\tCONSTRAINT,<lhs><relop><rhs>,W=<wgt>"
148  << "where <lhs>, <rhs>, and <wgt> are array expressions,"
149  << "and <relop> is one of \"<\", \"==\", or \">\"." << std::endl;
150 }
virtual ~ConstraintCmd()
virtual void print(std::ostream &) const
Print the command.
Parse exception.
Definition: ParseError.h:32
The base class for all OPAL actions.
Definition: Action.h:30
virtual ConstraintCmd * clone(const std::string &name)
Make clone.
Object * getParent() const
Return parent pointer.
Definition: Object.cpp:289
static Match * block
The block of match data.
Definition: Match.h:100
std::vector< Attribute > itsAttr
The object attributes (see Attribute.hh).
Definition: Object.h:214
bool info
Info flag.
Definition: Options.cpp:8
A single matching constraints or an array of matching constraints.
Definition: ConcreteFun.h:32
void parseDelimiter(Statement &stat, char delim)
Test for one-character delimiter.
const std::string & getOpalName() const
Return object name.
Definition: Object.cpp:284
virtual void parse(Statement &)
Parse the command.
Interface for statements.
Definition: Statement.h:38
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition: Object.cpp:194
void addFunction(AbstractFun *)
Add a set of matching function(s).
Definition: Match.cpp:111
ConstraintCmd()
Exemplar constructor.
The CONSTRAINT command.
Definition: ConstraintCmd.h:29
virtual void printHelp(std::ostream &) const
Print help for the command.
The base class for all OPAL objects.
Definition: Object.h:48
const std::string name
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
Definition: Attributes.cpp:253
virtual void execute()
Execute the command.
std::string parseString(Statement &, const char msg[])
Parse string value.
bool delimiter(char c)
Test for delimiter.
Definition: Statement.cpp:103
Inform & endl(Inform &inf)
Definition: Inform.cpp:42