Example #1
0
File: Solver.php Project: fieg/csp
 /**
  * @return mixed
  */
 public function solve()
 {
     $features = $this->features;
     $rootNode = $this->createNodes(new Node('root'), $features);
     $graph = new Graph(new DepthFirstStrategy());
     $graph->setRoot($rootNode);
     $constraint = $this->constraint;
     $goal = function (Node $n) use($constraint) {
         $values = array();
         $path = $n->getPath();
         array_shift($path);
         // shift the root
         foreach ($path as $node) {
             $values[] = $node->getValue();
         }
         foreach ($values as $v) {
             list($feature, $value) = $v;
             $feature->setValue($value);
         }
         if ($values) {
             $result = $constraint->evaluate();
             if ($result) {
                 return true;
             }
         }
         return false;
     };
     $solutions = $graph->searchAll($goal);
     return $solutions;
 }