dynrules
 All Classes Functions Variables
LearnSystem.cpp
1 /*
2  * dynrules - Python dynamic rules engine
3  *
4  * Authors: Marcus von Appen
5  *
6  * This file is distributed under the Public Domain.
7  */
8 
9 #include <ctime>
10 #include <stdexcept>
11 #include "LearnSystem.h"
12 
13 namespace dynrules
14 {
15 
17  _maxtries (100),
18  _maxscriptsize(1024),
19  _ruleset (new RuleSet(0,0))
20 {
21 }
22 
23 LearnSystem::LearnSystem (double minweight, double maxweight) :
24  _maxtries (100),
25  _maxscriptsize(1024),
26  _ruleset(new RuleSet (minweight, maxweight))
27 {
28 }
29 
31  _maxtries(100),
32  _maxscriptsize(1024),
33  _ruleset(ruleset)
34 {
35 }
36 
38  _maxtries(lsystem.getMaxTries ()),
39  _maxscriptsize(lsystem.getMaxScriptSize ()),
40  _ruleset(new RuleSet (*(lsystem.getRuleSet())))
41 {
42 }
43 
45 {
46  delete this->_ruleset;
47 }
48 
50 {
51  return this->_ruleset;
52 }
53 
55 {
56  if (ruleset == 0)
57  throw new std::invalid_argument ("ruleset must not be NULL");
58  this->_ruleset = ruleset;
59 }
60 
61 unsigned int LearnSystem::getMaxTries () const
62 {
63  return this->_maxtries;
64 }
65 
66 void LearnSystem::setMaxTries (unsigned int maxtries)
67 {
68  this->_maxtries = maxtries;
69 }
70 
71 unsigned int LearnSystem::getMaxScriptSize () const
72 {
73  return this->_maxscriptsize;
74 }
75 
76 void LearnSystem::setMaxScriptSize (unsigned int maxscriptsize)
77 {
78  this->_maxscriptsize = maxscriptsize;
79 }
80 
81 std::string LearnSystem::createHeader () const
82 {
83  std::string retval = "";
84  return retval;
85 }
86 
87 std::string LearnSystem::createFooter () const
88 {
89  std::string retval = "";
90  return retval;
91 }
92 
93 std::string LearnSystem::createRules (unsigned int maxrules) const
94 {
95  std::string buf, retval = "";
96  std::vector<Rule*> rules;
97  Rule *rule;
98  unsigned int tries, i;
99  unsigned long j;
100  int added = 0, selected;
101  size_t len, count, written = 0;
102  double wsum, fraction, weights = this->_ruleset->getWeight ();
103 
104  if (weights == 0 || maxrules == 0)
105  return retval;
106 
107  rules = this->_ruleset->getRules ();
108  count = rules.size ();
109 
110  /* Initialise the random number generator */
111  srand (static_cast<unsigned int>(time (0)));
112 
113  for (i = 0; i < maxrules; i++)
114  {
115  if (written >= static_cast<size_t>(this->_maxscriptsize))
116  break;
117 
118  tries = added = 0;
119  while (tries < this->_maxtries && !added)
120  {
121  j = 0;
122  selected = -1;
123  wsum = 0.;
124  fraction = ((static_cast<double>(rand())) / RAND_MAX) * weights;
125  while (selected == -1)
126  {
127  rule = rules.at (j);
128  wsum += rule->getWeight ();
129  if (wsum > fraction)
130  {
131  selected = j;
132  break;
133  }
134  j = ((j + 1) % count);
135  }
136 
137  rule = rules.at (static_cast<unsigned int>(selected));
138 
139  /* Write the rule code */
140  buf = rule->getCode ();
141  len = buf.size ();
142  /* Buffer acquired, write the raw data. */
143  if (written + len > static_cast<size_t>(this->_maxscriptsize))
144  goto finish;
145  retval.append (buf);
146  written += len;
147  added = 1;
148 
149  tries++;
150  break;
151  }
152  }
153 
154 finish:
155  return retval;
156 }
157 
158 void LearnSystem::createScript (std::ostream& stream, unsigned int maxrules)
159 {
160  stream << this->createHeader () << std::endl;
161  stream << this->createRules (maxrules) << std::endl;
162  stream << this->createFooter () << std::endl;
163  return;
164 }
165 
166 } // namespace