ECF 1.5
PrimitiveSetAP.cpp
1#include "PrimitiveSetAP.h"
2#include "../../ECF/tree/Tree.h"
3#include "../../ECF/tree/Primitive.h"
4#include <ECF/ECF.h>
5
6
7namespace Tree
8{
9
10 PrimitiveSetAP::PrimitiveSetAP()
11 { }
12
13
14 PrimitiveSetAP::~PrimitiveSetAP()
15 { }
16
17
18 bool PrimitiveSetAP::initialize(StateP state)
19 {
20 state_ = state;
21
22 // register existing primitives
23 PrimitiveP prim = (PrimitiveP)(new Primitives::Add);
24 mAllPrimitives_[prim->getName()] = prim;
25 prim = (PrimitiveP)(new Primitives::Sub);
26 mAllPrimitives_[prim->getName()] = prim;
27 prim = (PrimitiveP)(new Primitives::Mul);
28 mAllPrimitives_[prim->getName()] = prim;
29 prim = (PrimitiveP)(new Primitives::Div);
30 mAllPrimitives_[prim->getName()] = prim;
31 prim = (PrimitiveP)(new Primitives::Sin);
32 mAllPrimitives_[prim->getName()] = prim;
33 prim = (PrimitiveP)(new Primitives::Cos);
34 mAllPrimitives_[prim->getName()] = prim;
35 prim = (PrimitiveP)(new Primitives::Pos);
36 mAllPrimitives_[prim->getName()] = prim;
37 prim = (PrimitiveP)(new Primitives::Neg);
38 mAllPrimitives_[prim->getName()] = prim;
39 prim = (PrimitiveP)(new Primitives::Max);
40 mAllPrimitives_[prim->getName()] = prim;
41 prim = (PrimitiveP)(new Primitives::Min);
42 mAllPrimitives_[prim->getName()] = prim;
43
44 prim_iter primIter;
45 for (primIter = mAllPrimitives_.begin(); primIter != mAllPrimitives_.end(); ++primIter)
46 primIter->second->initialize(state);
47
48 // register terminal types
49 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("DOUBLE", Primitives::Double));
50 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("INT", Primitives::Int));
51 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("BOOL", Primitives::Bool));
52 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("CHAR", Primitives::Char));
53 mTypeNames_.insert(std::pair<std::string, Primitives::terminal_type>("STRING", Primitives::String));
54
55 return true;
56 }
57
58
60 PrimitiveP PrimitiveSetAP::getGFSOneByName(std::string name)
61 {
62 prim_iter iter = mGFSOneSet_.find(name);
63 if (iter == mGFSOneSet_.end())
64 return PrimitiveP();
65
66 return iter->second;
67 }
68
70 PrimitiveP PrimitiveSetAP::getGFSTwoByName(std::string name)
71 {
72 prim_iter iter = mGFSTwoSet_.find(name);
73 if (iter == mGFSTwoSet_.end())
74 return PrimitiveP();
75
76 return iter->second;
77 }
78
79
81 PrimitiveP PrimitiveSetAP::getTerminalByName(std::string name)
82 {
83 prim_iter iter = mTerminalSet_.find(name);
84 if (iter == mTerminalSet_.end())
85 return PrimitiveP();
86
87 return iter->second;
88 }
89
90
92 PrimitiveP PrimitiveSetAP::getPrimitiveByName(std::string name)
93 {
94 prim_iter iter = mPrimitiveSet_.find(name);
95 if (iter == mPrimitiveSet_.end())
96 return PrimitiveP();
97
98 return iter->second;
99 }
100
101 PrimitiveP PrimitiveSetAP::getTerminalByIndex(uint index) {
102 return terminalSet_[index % terminalSet_.size()];
103 }
104
105 PrimitiveP PrimitiveSetAP::getGFSOneByIndex(uint index) {
106 return GFSOneSet_[index % GFSOneSet_.size()];
107 }
108
109 PrimitiveP PrimitiveSetAP::getGFSTwoByIndex(uint index) {
110 return GFSTwoSet_[index % GFSTwoSet_.size()];
111 }
112
113 PrimitiveP PrimitiveSetAP::getPrimitiveByIndex(uint index) {
114 return primitives_[index % primitives_.size()];
115 }
116
117
122 bool PrimitiveSetAP::addFunction(std::string name)
123 {
124 prim_iter iter = mAllPrimitives_.find(name);
125 if (iter == mAllPrimitives_.end())
126 return false;
127
128 int argNum = iter->second->getNumberOfArguments();
129
130 if (argNum == 0) {
131 terminalSet_.push_back(iter->second);
132 mTerminalSet_[iter->first] = iter->second;
133 }
134 else if (argNum == 1) {
135 GFSOneSet_.push_back(iter->second);
136 mGFSOneSet_[iter->first] = iter->second;
137 }
138 else if (argNum == 2) {
139 GFSTwoSet_.push_back(iter->second);
140 mGFSTwoSet_[iter->first] = iter->second;
141 }
142
143 primitives_.push_back(iter->second);
144 mPrimitiveSet_[iter->first] = iter->second;
145
146 return true;
147 }
148
149
153 void PrimitiveSetAP::addTerminal(PrimitiveP terminalPrimitive)
154 {
155 terminalSet_.push_back(terminalPrimitive);
156 mTerminalSet_[terminalPrimitive->getName()] = terminalPrimitive;
157
158 primitives_.push_back(terminalPrimitive);
159 mPrimitiveSet_[terminalPrimitive->getName()] = terminalPrimitive;
160 }
161
162
163 uint PrimitiveSetAP::getFunctionSetSize()
164 {
165 return getGFSOneSetSize() + getGFSTwoSetSize();
166 }
167
168 uint PrimitiveSetAP::getGFSOneSetSize()
169 {
170 return (uint)GFSOneSet_.size();
171 }
172
173 uint PrimitiveSetAP::getGFSTwoSetSize()
174 {
175 return (uint)GFSTwoSet_.size();
176 }
177
180 {
181 return (uint)terminalSet_.size();
182 }
183
186 {
187 return (uint)primitives_.size();
188 }
189
190}
std::vector< PrimitiveP > GFSOneSet_
vector of active (actual used) functions
PrimitiveP getTerminalByName(std::string name)
Access terminal by name (active terminals only).
std::map< std::string, PrimitiveP > mAllPrimitives_
map of all registered primitive functions
std::vector< PrimitiveP > primitives_
vector of active (actual used) primitives
void addTerminal(PrimitiveP terminalPrimitive)
Add a terminal primitive to the set of active primitives.
std::vector< PrimitiveP > GFSTwoSet_
vector of active (actual used) functions
uint getTerminalSetSize()
Get the number of active terminals.
bool addFunction(std::string name)
Add a function primitive to the set of active primitives - if found by name in collection of all prim...
PrimitiveP getPrimitiveByName(std::string name)
Access primitive by name (active functions or terminals only).
PrimitiveP getGFSTwoByName(std::string name)
Access function by name (active functions only).
std::map< std::string, PrimitiveP > mPrimitiveSet_
map of active (actual used) primitives
uint getPrimitivesSize()
Get the number of active primitives (functions and terminals).
std::vector< PrimitiveP > terminalSet_
vector of active (actual used) terminals
std::map< std::string, PrimitiveP > mGFSTwoSet_
map of active (actual used) functions
std::map< std::string, PrimitiveP > mTerminalSet_
map of active (actual used) terminals
PrimitiveP getGFSOneByName(std::string name)
Access function by name (active functions only).
std::map< std::string, PrimitiveP > mGFSOneSet_
map of active (actual used) functions