Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Compiler\Llk\Sampler\Uniform Class Reference
Inheritance diagram for Hoa\Compiler\Llk\Sampler\Uniform:

Public Member Functions

 __construct (Compiler\Llk\Parser $compiler, Visitor\Visit $tokenSampler, $length=5)
 
 uniform (Compiler\Llk\Rule $rule=null, $n=-1)
 
 count (Compiler\Llk\Rule $rule=null, $n=-1)
 
 setLength ($length)
 
 getLength ()
 
- Public Member Functions inherited from Hoa\Compiler\Llk\Sampler\Sampler
 __construct (Compiler\Llk\Parser $compiler, Visitor\Visit $tokenSampler)
 
 getCompiler ()
 

Protected Attributes

 $_data = []
 
 $_length = 5
 
- Protected Attributes inherited from Hoa\Compiler\Llk\Sampler\Sampler
 $_compiler = null
 
 $_tokens = null
 
 $_rules = null
 
 $_tokenSampler = null
 
 $_rootRuleName = null
 
 $_currentNamespace = 'default'
 

Additional Inherited Members

- Protected Member Functions inherited from Hoa\Compiler\Llk\Sampler\Sampler
 completeToken (Compiler\Llk\Rule\Token $token)
 
 setCurrentNamespace ($namespace)
 
 generateToken (Compiler\Llk\Rule\Token $token)
 

Detailed Description

Class .

This generator aims at producing random and uniform a sequence of a fixed size. We use the recursive method to count all possible sub-structures of size n. The counting helps to compute cumulative distribution functions, which guide the exploration. Repetition unfolding: upper bound of + and * is set to n.

Definition at line 55 of file Uniform.php.

Constructor & Destructor Documentation

Hoa\Compiler\Llk\Sampler\Uniform::__construct ( Compiler\Llk\Parser  $compiler,
Visitor\Visit  $tokenSampler,
  $length = 5 
)

Construct a generator.

Parameters
\Hoa\Compiler\Llk\Parser$compilerCompiler/parser.
\Hoa\Visitor\Visit$tokenSamplerToken sampler.
Returns
void

Definition at line 80 of file Uniform.php.

84  {
85  parent::__construct($compiler, $tokenSampler);
86 
87  foreach ($this->_rules as $name => $_) {
88  $this->_data[$name] = [];
89  }
90 
91  $this->setLength($length);
92  $this->_sampler = new Math\Sampler\Random();
93 
94  return;
95  }

Here is the call graph for this function:

Member Function Documentation

Hoa\Compiler\Llk\Sampler\Uniform::count ( Compiler\Llk\Rule  $rule = null,
  $n = -1 
)

Recursive method applied to our problematic.

Parameters
\Hoa\Compiler\Llk\Rule$ruleRule to start.
int$nSize.
Returns
int

Definition at line 179 of file Uniform.php.

180  {
181  if (null === $rule || -1 === $n) {
182  return 0;
183  }
184 
185  $ruleName = $rule->getName();
186 
187  if (isset($this->_data[$ruleName][$n])) {
188  return $this->_data[$ruleName][$n]['n'];
189  }
190 
191  $this->_data[$ruleName][$n] = ['n' => 0];
192  $out = &$this->_data[$ruleName][$n]['n'];
193  $rule = $this->_rules[$ruleName];
194 
195  if ($rule instanceof Compiler\Llk\Rule\Choice) {
196  foreach ($rule->getContent() as $child) {
197  $out += $this->count($this->_rules[$child], $n);
198  }
199  } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
200  $children = $rule->getContent();
201  $Γ = new Math\Combinatorics\Combination\Gamma(
202  count($children),
203  $n
204  );
205  $this->_data[$ruleName][$n]['Γ'] = [];
206  $handle = &$this->_data[$ruleName][$n]['Γ'];
207 
208  foreach ($Γ as $γ) {
209  $oout = 1;
210 
211  foreach ($γ as $α => $_γ) {
212  $oout *= $this->count($this->_rules[$children[$α]], $_γ);
213  }
214 
215  if (0 !== $oout) {
216  $handle[] = $γ;
217  }
218 
219  $out += $oout;
220  }
221  } elseif ($rule instanceof Compiler\Llk\Rule\Repetition) {
222  $this->_data[$ruleName][$n]['xy'] = [];
223  $handle = &$this->_data[$ruleName][$n]['xy'];
224  $child = $this->_rules[$rule->getContent()];
225  $x = $rule->getMin();
226  $y = $rule->getMax();
227 
228  if (-1 === $y) {
229  $y = $n;
230  } else {
231  $y = min($n, $y);
232  }
233 
234  if (0 === $x && $x === $y) {
235  $out = 1;
236  } else {
237  for ($α = $x; $α <= $y; ++$α) {
238  $ut = 0;
239  $handle[$α] = ['n' => 0, 'Γ' => []];
240  $Γ = new Math\Combinatorics\Combination\Gamma($α, $n);
241 
242  foreach ($Γ as $γ) {
243  $oout = 1;
244 
245  foreach ($γ as $β => $_γ) {
246  $oout *= $this->count($child, $_γ);
247  }
248 
249  if (0 !== $oout) {
250  $handle[$α]['Γ'][] = $γ;
251  }
252 
253  $ut += $oout;
254  }
255 
256  $handle[$α]['n'] = $ut;
257  $out += $ut;
258  }
259  }
260  } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
261  $out = Math\Util::δ($n, 1);
262  }
263 
264  return $out;
265  }
static δ($i, $j)
Definition: Util.php:55
count(Compiler\Llk\Rule $rule=null, $n=-1)
Definition: Uniform.php:179

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Compiler\Llk\Sampler\Uniform::getLength ( )

Get upper-bound.

Returns
int

Definition at line 299 of file Uniform.php.

300  {
301  return $this->_length;
302  }

Here is the caller graph for this function:

Hoa\Compiler\Llk\Sampler\Uniform::setLength (   $length)

Set upper-bound, the maximum data length.

Parameters
int$lengthLength.
Returns
int
Exceptions

Definition at line 274 of file Uniform.php.

275  {
276  if (0 >= $length) {
277  throw new Exception(
278  'Length must be greater than 0, given %d.',
279  0,
280  $length
281  );
282  }
283 
284  $old = $this->_length;
285  $this->_length = $length;
286  $this->count(
287  $this->_compiler->getRule($this->_rootRuleName),
288  $length
289  );
290 
291  return $old;
292  }
count(Compiler\Llk\Rule $rule=null, $n=-1)
Definition: Uniform.php:179

Here is the call graph for this function:

Here is the caller graph for this function:

Hoa\Compiler\Llk\Sampler\Uniform::uniform ( Compiler\Llk\Rule  $rule = null,
  $n = -1 
)

The random and uniform algorithm.

Parameters
\Hoa\Compiler\Llk\Rule$ruleRule to start.
int$nSize.
Returns
string

Definition at line 104 of file Uniform.php.

105  {
106  if (null === $rule && -1 === $n) {
107  $rule = $this->_rules[$this->_rootRuleName];
108  $n = $this->getLength();
109  }
110 
111  $data = &$this->_data[$rule->getName()][$n];
112  $computed = $data['n'];
113 
114  if (0 === $n || 0 === $computed) {
115  return null;
116  }
117 
118  if ($rule instanceof Compiler\Llk\Rule\Choice) {
119  $children = $rule->getContent();
120  $stat = [];
121 
122  foreach ($children as $c => $child) {
123  $stat[$c] = $this->_data[$child][$n]['n'];
124  }
125 
126  $i = $this->_sampler->getInteger(1, $computed);
127 
128  for ($e = 0, $b = $stat[$e], $max = count($stat) - 1;
129  $e < $max && $i > $b;
130  $b += $stat[++$e]);
131 
132  return $this->uniform($this->_rules[$children[$e]], $n);
133  } elseif ($rule instanceof Compiler\Llk\Rule\Concatenation) {
134  $children = $rule->getContent();
135  $out = null;
136  $Γ = $data['Γ'];
137  $γ = $Γ[$this->_sampler->getInteger(0, count($Γ) - 1)];
138 
139  foreach ($children as $i => $child) {
140  $out .= $this->uniform($this->_rules[$child], $γ[$i]);
141  }
142 
143  return $out;
144  } elseif ($rule instanceof Compiler\Llk\Rule\Repetition) {
145  $out = null;
146  $stat = &$data['xy'];
147  $child = $this->_rules[$rule->getContent()];
148  $b = 0;
149  $i = $this->_sampler->getInteger(1, $computed);
150 
151  foreach ($stat as $α => $st) {
152  if ($i <= $b += $st['n']) {
153  break;
154  }
155  }
156 
157  $Γ = &$st['Γ'];
158  $γ = &$Γ[$this->_sampler->getInteger(0, count($Γ) - 1)];
159 
160  for ($j = 0; $j < $α; ++$j) {
161  $out .= $this->uniform($child, $γ[$j]);
162  }
163 
164  return $out;
165  } elseif ($rule instanceof Compiler\Llk\Rule\Token) {
166  return $this->generateToken($rule);
167  }
168 
169  return null;
170  }
generateToken(Compiler\Llk\Rule\Token $token)
Definition: Sampler.php:188
uniform(Compiler\Llk\Rule $rule=null, $n=-1)
Definition: Uniform.php:104
count(Compiler\Llk\Rule $rule=null, $n=-1)
Definition: Uniform.php:179

Here is the call graph for this function:

Member Data Documentation

Hoa\Compiler\Llk\Sampler\Uniform::$_data = []
protected

Definition at line 62 of file Uniform.php.

Hoa\Compiler\Llk\Sampler\Uniform::$_length = 5
protected

Definition at line 69 of file Uniform.php.


The documentation for this class was generated from the following file: