Example #1
0
 /**
  * Rewind the iterator.
  *
  * @return  void
  */
 public function rewind()
 {
     $this->_key = -1;
     $this->_current = null;
     if (null === $this->_coverage) {
         if (empty($this->_variables)) {
             $this->_variables = $this->_declaration->getLocalVariables();
         }
         $this->_coverage = new Coverage\Domain($this->_variables);
     }
     $this->_coverage->rewind();
     $this->_current();
     return;
 }
Example #2
0
 /**
  * Check a clause.
  *
  * @param   \Hoa\Praspel\Model\Declaration   $clause        Clause.
  * @param   array                           &$data          Data.
  * @param   \Hoa\Praspel\Exception\Group     $exceptions    Exceptions group.
  * @param   string                           $exception     Exception to
  *                                                          throw.
  * @param   bool                             $assign        Assign data to
  *                                                          variable.
  * @param   \Hoa\Praspel\Trace               $trace         Trace.
  * @return  bool
  * @throws  \Hoa\Praspel\Exception
  */
 protected function checkClause(Praspel\Model\Declaration $clause, array &$data, \Hoa\Praspel\Exception\Group $exceptions, $exception, $assign = false, $trace = false)
 {
     $verdict = true;
     $traceClause = null;
     if (!empty($trace)) {
         $traceClause = clone $clause;
     }
     foreach ($clause as $name => $variable) {
         if (false === array_key_exists($name, $data)) {
             $exceptions[] = new $exception('Variable %s in @%s is required and has no value.', 9, [$name, $clause->getName()]);
             continue;
         }
         $datum =& $data[$name];
         $_verdict = false;
         $traceVariable = null;
         if (null !== $traceClause) {
             $traceVariable = clone $variable;
             $traceVariableDomains = $traceVariable->getDomains();
         }
         $i = 0;
         foreach ($variable->getDomains() as $realdom) {
             if (false === $_verdict && true === $realdom->predicate($datum)) {
                 $_verdict = true;
             } elseif (null !== $traceClause) {
                 unset($traceVariableDomains[$i--]);
             }
             ++$i;
         }
         if (false === $_verdict) {
             if (null !== $traceClause) {
                 unset($traceClause[$name]);
             }
             $exceptions[] = new $exception('Variable %s does not verify the constraint @%s %s.', 10, [$name, $clause->getName(), $this->getVisitorPraspel()->visit($variable)]);
         } else {
             if (true === $assign) {
                 $variable->setValue($datum);
             }
             if (null !== $traceClause) {
                 unset($traceClause[$name]);
                 $traceClause->addVariable($name, $traceVariable);
             }
         }
         $verdict &= $_verdict;
     }
     $predicateEvaluator = function ($__hoa_arguments, $__hoa_code) {
         extract($__hoa_arguments);
         return true == eval('return ' . $__hoa_code . ';');
     };
     foreach ($clause->getPredicates() as $predicate) {
         $_predicate = $predicate;
         preg_match_all('#(?<!\\\\)\\$([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)#', $_predicate, $matches);
         $predicateArguments = [];
         foreach ($matches[1] as $variable) {
             if (true === array_key_exists($variable, $data)) {
                 $predicateArguments[$variable] = $data[$variable];
             }
         }
         if (false !== strpos($_predicate, '\\result')) {
             if (!$clause instanceof Praspel\Model\Ensures && !$clause instanceof Praspel\Model\Throwable) {
                 $verdict &= false;
                 $exceptions[] = new $exception('Illegal \\result in the following predicate: %s.', 11, $predicate);
                 continue;
             }
             $placeholder = '__hoa_praspel_' . uniqid();
             $_predicate = str_replace('\\result', '$' . $placeholder, $_predicate);
             $predicateArguments[$placeholder] = $data['\\result'];
         }
         $_verdict = $predicateEvaluator($predicateArguments, $_predicate);
         if (false === $_verdict) {
             $exceptions[] = new $exception('Violation of the following predicate: %s.', 11, $predicate);
         }
         $verdict &= $_verdict;
     }
     if (!empty($trace)) {
         $trace->addClause($traceClause);
     }
     return (bool) $verdict;
 }