YARP
Yet Another Robot Platform
binexparser.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef YARP_MANAGER_BINEXPARSER
7 #define YARP_MANAGER_BINEXPARSER
8 
9 #include <string>
10 #include <cstdio>
11 
12 #include <yarp/manager/ymm-types.h>
13 #include <yarp/manager/utility.h>
14 #include <yarp/manager/graph.h>
15 #include <yarp/conf/api.h>
16 
17 namespace yarp {
18 namespace manager {
19 
20 
21 typedef enum __BinNodeType {
23  OPERAND
25 
26 
27 static int node_id = 0;
28 
29 class BinaryNode : public Node
30 {
31 
32 public:
33  BinaryNode(const char* opd) : Node(static_cast<NodeType>(OPERAND)) {
34  char str[128];
35  sprintf(str, "%s%d", opd, node_id++);
36  setLabel(str);
37  strName = opd;
38  value = false;
39  }
40 
41  BinaryNode(const char* opt,
42  BinaryNode* left,
43  BinaryNode* right ) : Node(static_cast<NodeType>(OPERATOR)) {
44 
45  char str[128];
46  sprintf(str, "%s%d", opt, node_id++);
47  setLabel(str);
48  strName = opt;
49  value = false;
50  if(left) {
51  addSuc(left, 0);
52  }
53  if(right) {
54  addSuc(right, 0);
55  }
56  }
57 
58  ~BinaryNode() override = default;
59 
60  Node* clone() override {
61  BinaryNode* binode = new BinaryNode(*this);
62  return binode;
63  }
64 
66  if(sucCount()<1) {
67  return nullptr;
68  }
69  return (BinaryNode*) getLinkAt(0).to();
70  }
71 
73  if(sucCount()<2) {
74  return nullptr;
75  }
76  return (BinaryNode*) getLinkAt(1).to();
77  }
78 
79  bool getValue() { return value; }
80  void setValue(bool val) { value = val; }
81 
82  const char* getName() {return strName.c_str(); }
83 
84 protected:
85 
86 private:
87  bool value;
88  std::string strName;
89 
90 };
91 
93 
95 {
96 
97 public:
99  virtual ~BinaryExpParser();
100 
101  bool parse(std::string _exp);
102  bool exportDotGraph(const char* szFileName);
103  void addRestrictedOperand(const char* opnd) {
104  if(opnd) {
105  validOperands.push_back(opnd);
106  }
107  }
108 
109  const std::map<std::string, bool> &getOperands() {
110  return operands;
111  }
112  const std::vector<std::vector<int> > &getTruthTable() {
113  return truthTable;
114  }
115 
116 private:
117 
118  bool evalTree(BinaryNodePtr node, std::map<std::string, bool>& opnd);
119 
120  bool checkExpression(std::string& strexp);
121  void parseExpression(std::string &strexp, BinaryNodePtr& node);
122  void parseNot(std::string &strexp, BinaryNodePtr& node);
123  void parseFactor(std::string &strexp, BinaryNodePtr& node);
124  std::string getNextOperand(std::string &strexp);
125  std::string popNextOperand(std::string &strexp);
126  void createTruthTable(const int n);
127  void printTruthTable(std::string lopr);
128  //bool train(int max_itr=1000, double train_rate=1.0);
129 
130 private:
131  std::string expression;
132  std::string leftOpr;
133  Graph binTree;
134  // mapping operands to their real value
135  std::map<std::string, bool> operands;
136  std::vector<std::string> validOperands;
137  std::vector<std::string> invalidOperands;
138  std::vector<std::vector<int> > truthTable;
139  //std::vector<double> alphas;
140  //std::vector<double> errors;
141  //double bias;
142 };
143 
145 {
146 public:
147  LinkTrainer(int max_itr=1000, double train_rate=1.0) :
148  maxIteration(max_itr),
149  trainRate(train_rate),
150  bias(0.0)
151  {}
152 
153  virtual ~LinkTrainer() {}
154 
155  bool train(const std::vector<std::vector<int> > &truthTable);
156 
157  const std::vector<double> &getAlphas() { return alphas; }
158  const std::vector<double> &getErrors() { return errors; }
159  double getBias() {return bias; }
160 
161 private:
162  int maxIteration;
163  double trainRate;
164  std::vector<double> alphas;
165  std::vector<double> errors;
166  double bias;
167 
168 };
169 
170 } // namespace yarp
171 } // namespace manager
172 
173 
174 #endif // __YARP_MANAGER_BINEXPARSER____
void addRestrictedOperand(const char *opnd)
Definition: binexparser.h:103
bool parse(std::string _exp)
Definition: binexparser.cpp:35
const std::vector< std::vector< int > > & getTruthTable()
Definition: binexparser.h:112
const std::map< std::string, bool > & getOperands()
Definition: binexparser.h:109
bool exportDotGraph(const char *szFileName)
Definition: binexparser.cpp:80
const char * getName()
Definition: binexparser.h:82
~BinaryNode() override=default
BinaryNode * rightOf()
Definition: binexparser.h:72
Node * clone() override
Definition: binexparser.h:60
BinaryNode * leftOf()
Definition: binexparser.h:65
BinaryNode(const char *opd)
Definition: binexparser.h:33
void setValue(bool val)
Definition: binexparser.h:80
BinaryNode(const char *opt, BinaryNode *left, BinaryNode *right)
Definition: binexparser.h:41
Class Graph.
Definition: graph.h:28
const std::vector< double > & getErrors()
Definition: binexparser.h:158
bool train(const std::vector< std::vector< int > > &truthTable)
const std::vector< double > & getAlphas()
Definition: binexparser.h:157
LinkTrainer(int max_itr=1000, double train_rate=1.0)
Definition: binexparser.h:147
a Node of a Graph
Definition: node.h:65
int sucCount()
Definition: node.h:95
bool addSuc(Node *node, float weight, bool _virtual=false)
class Node
Definition: node.cpp:15
Link & getLinkAt(int index)
Definition: node.h:96
void setLabel(const char *szLabel)
Definition: node.h:93
BinaryNode * BinaryNodePtr
Definition: binexparser.h:92
enum yarp::manager::__BinNodeType BinNodeType
enum yarp::manager::__NodeType NodeType
static int node_id
Definition: binexparser.h:27
The main, catch-all namespace for YARP.
Definition: dirs.h:16