/** * Just check the method returns properly. No need to test * all type combinations, that is done in the engine test */ public function testCompareReturnsResult() { $one = new IntType(1); $two = new IntType(2); $c = new Comparator(); //use default engine $this->assertEquals(0, $c->compare($one, $one)); $this->assertEquals(1, $c->compare($two, $one)); $this->assertEquals(-1, $c->compare($one, $two)); }
/** * Format the matrix contents for outputting * * @param Matrix $mA Matrix to format * @param array $options Options for formatter * - attribs => Collection of VertexDescription * - optional: edgeFunc => function($weight){return $newWeight;} * - optional: output:string script|object default = script * * If output is script, return the graphviz dot file contents * If output is object then return Fhaculty\Graph\Graph for your own * processing via Graphp\GraphViz\GraphViz * * @return Graph|string */ public function format(Matrix $mA, array $options = array()) { if (!$mA instanceof NumericMatrix) { throw new \InvalidArgumentException('Matrix is not NumericMatrix'); } return FFor::create(['mA' => $mA, 'graph' => new Graph(), 'options' => $options])->attribs(function ($options) { $attribs = array_key_exists('attribs', $options) ? $options['attribs'] : new Collection([], 'string'); if ($attribs instanceof Collection) { return $attribs; } throw new \InvalidArgumentException('options[attribs]'); })->edgeFunc(function ($options) { $edgeFunc = array_key_exists('edgeFunc', $options) ? $options['edgeFunc'] : function ($w) { return $w; }; if ($edgeFunc instanceof \Closure) { return $edgeFunc; } throw new \InvalidArgumentException('pptions[edgeFunc]'); })->output(function ($options) { return array_key_exists('output', $options) ? $options['output'] : 'script'; })->vertices(function (Collection $attribs, Matrix $mA, Graph $graph) { $vertices = []; foreach (range(0, $mA->rows() - 1) as $idx) { if (array_key_exists($idx, $attribs)) { $attribute = $attribs[$idx]; $vertices[$idx + 1] = $graph->createVertex($attribute->getName()); foreach ($attribute->getAttributes() as $key => $val) { $vertices[$idx + 1]->setAttribute($key, $val); } } else { $vertices[$idx + 1] = $graph->createVertex(); } } return $vertices; })->graphViz(function (Graph $graph, Matrix $mA, array $vertices, \Closure $edgeFunc, $output) { $comp = new Comparator(); $zero = TypeFactory::createInt(0); $rows = $mA->rows(); for ($row = 1; $row <= $rows; $row++) { for ($col = 1; $col <= $rows; $col++) { if ($comp->compare($zero, $mA->get($row, $col)) != 0) { $vertices[$row]->createEdgeTo($vertices[$col])->setWeight($edgeFunc($mA->get($row, $col)->get())); } } } return $output == 'script' ? (new GraphViz())->createScript($graph) : $graph; })->fyield('graphViz'); }