示例#1
0
 public function testMixedParallelEdgesMultiple()
 {
     // 1 -> 2
     // 1 -> 2
     // 1 -- 2
     // 1 -- 2
     // 2 -> 1
     // 2 -> 1
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $e1 = $v1->createEdgeTo($v2);
     $e2 = $v1->createEdgeTo($v2);
     $e3 = $v1->createEdge($v2);
     $e4 = $v1->createEdge($v2);
     $e5 = $v2->createEdgeTo($v1);
     $e6 = $v2->createEdgeTo($v1);
     $alg = new AlgorithmParallel($graph);
     $this->assertTrue($alg->hasEdgeParallel());
     $this->assertEquals(array($e2, $e3, $e4), $alg->getEdgesParallelEdge($e1)->getVector());
     $this->assertEquals(array($e1, $e3, $e4), $alg->getEdgesParallelEdge($e2)->getVector());
     $this->assertEquals(array($e1, $e2, $e4, $e5, $e6), $alg->getEdgesParallelEdge($e3)->getVector());
     $this->assertEquals(array($e1, $e2, $e3, $e5, $e6), $alg->getEdgesParallelEdge($e4)->getVector());
     $this->assertEquals(array($e3, $e4, $e6), $alg->getEdgesParallelEdge($e5)->getVector());
     $this->assertEquals(array($e3, $e4, $e5), $alg->getEdgesParallelEdge($e6)->getVector());
 }
示例#2
0
 /**
  *
  * @param string $dir
  * @return \Fhaculty\Graph\Graph
  */
 public function createGraph()
 {
     $graph = new Graph();
     foreach ($this->dependencyGraph->getPackages() as $package) {
         $name = $package->getName();
         $start = $graph->createVertex($name, true);
         $label = $name;
         if ($package->getVersion() !== null) {
             $label .= ': ' . $package->getVersion();
         }
         $this->setLayout($start, array('label' => $label) + $this->layoutVertex);
         foreach ($package->getOutEdges() as $requires) {
             $targetName = $requires->getDestPackage()->getName();
             $target = $graph->createVertex($targetName, true);
             $label = $requires->getVersionConstraint();
             $edge = $start->createEdgeTo($target);
             $this->setLayout($edge, array('label' => $label) + $this->layoutEdge);
             if ($requires->isDevDependency()) {
                 $this->setLayout($edge, $this->layoutEdgeDev);
             }
         }
     }
     $root = $graph->getVertex($this->dependencyGraph->getRootPackage()->getName());
     $this->setLayout($root, $this->layoutVertexRoot);
     return $graph;
 }
 public function testShouldOptimizeOrderBasedOnMaxSubtreeDelay()
 {
     //     -------ROOT-------
     //    / 0   / 0    \ 0   \ 0
     //   A      B      F     H
     //       3 / \ 5   | 11  | 4
     //        C   D    G     I
     //     10 |
     //        E
     // Build graph
     $graph = new Graph();
     $root = $graph->createVertex(0);
     $root->createEdgeTo($graph->createVertex('A'))->setWeight(0);
     $root->createEdgeTo($vertexB = $graph->createVertex('B'))->setWeight(0);
     $vertexB->createEdgeTo($vertexC = $graph->createVertex('C'))->setWeight(3);
     $vertexB->createEdgeTo($graph->createVertex('D'))->setWeight(5);
     $vertexC->createEdgeTo($graph->createVertex('E'))->setWeight(10);
     $root->createEdgeTo($vertexF = $graph->createVertex('F'))->setWeight(0);
     $vertexF->createEdgeTo($graph->createVertex('G'))->setWeight(11);
     $root->createEdgeTo($vertexH = $graph->createVertex('H'))->setWeight(0);
     $vertexH->createEdgeTo($graph->createVertex('I'))->setWeight(4);
     // Check the vertices have proper order value
     $strategy = new MaxTotalDelayStrategy();
     $evaluatedOrder = $strategy->optimize(new OutTree($graph));
     $this->assertSame($evaluatedOrder['A'], 0);
     $this->assertSame($evaluatedOrder['B'], 13);
     $this->assertSame($evaluatedOrder['C'], 10);
     $this->assertSame($evaluatedOrder['D'], 0);
     $this->assertSame($evaluatedOrder['E'], 0);
     $this->assertSame($evaluatedOrder['F'], 11);
     $this->assertSame($evaluatedOrder['G'], 0);
     $this->assertSame($evaluatedOrder['H'], 4);
     $this->assertSame($evaluatedOrder['I'], 0);
 }
示例#4
0
 /**
  *
  * @param  string                $dir
  * @return \Fhaculty\Graph\Graph
  */
 public function createGraph($vendorName = null, $excludeDevDependency = true)
 {
     $graph = new Graph();
     foreach ($this->dependencyGraph->getPackages() as $package) {
         $name = $package->getName();
         if (null !== $vendorName && false === strpos($name, $vendorName)) {
             continue;
         }
         $start = $graph->createVertex($name, true);
         $label = $name;
         if ($package->getVersion() !== null) {
             $label .= ': ' . $package->getVersion();
         }
         $start->setLayout(array('label' => $label) + $this->getLayoutVertex($name));
         foreach ($package->getOutEdges() as $requires) {
             $targetName = $requires->getDestPackage()->getName();
             if (null !== $vendorName && false === strpos($targetName, $vendorName)) {
                 continue;
             }
             if ($excludeDevDependency && $requires->isDevDependency()) {
                 continue;
             }
             $target = $graph->createVertex($targetName, true);
             $label = $requires->getVersionConstraint();
             $start->createEdgeTo($target)->setLayout(array('label' => $label) + $this->layoutEdge);
         }
     }
     return $graph;
 }
示例#5
0
 /**
  * expect exception for non-bipartit graphs
  * @expectedException UnexpectedValueException
  */
 public function testInvalidBipartit()
 {
     $graph = new Graph();
     $graph->createVertex(0)->setGroup(1)->createEdge($graph->createVertex(1)->setGroup(1));
     $alg = new Flow($graph);
     $alg->getNumberOfMatches();
 }
 /**
  * Create edges between versions graph
  *
  * @param Graph  $graph
  * @param string $className
  */
 private function createEdges(Graph $graph, $className)
 {
     $migrationsAnnotations = $this->reader->getClassMigrationMethodInfo($className);
     $parentVertex = $graph->hasVertex($className) ? $graph->getVertex($className) : $graph->createVertex($className);
     foreach ($migrationsAnnotations as $migrationsAnnotation) {
         if ($migrationsAnnotation->annotation->from) {
             $fromClass = $migrationsAnnotation->annotation->from;
             $fromVertex = $graph->hasVertex($fromClass) ? $graph->getVertex($fromClass) : $graph->createVertex($fromClass);
             if (!$parentVertex->hasEdgeTo($fromVertex)) {
                 $edge = $fromVertex->createEdgeTo($parentVertex);
                 $this->annotations[$this->getEdgeId($edge)] = $migrationsAnnotation;
                 $this->createEdges($graph, $fromClass);
             }
         }
         if ($migrationsAnnotation->annotation->to) {
             $toClass = $migrationsAnnotation->annotation->to;
             $fromVertex = $graph->hasVertex($toClass) ? $graph->getVertex($toClass) : $graph->createVertex($toClass);
             if (!$parentVertex->hasEdgeTo($fromVertex)) {
                 $edge = $parentVertex->createEdgeTo($fromVertex);
                 $this->annotations[$this->getEdgeId($edge)] = $migrationsAnnotation;
                 $this->createEdges($graph, $toClass);
             }
         }
     }
 }
示例#7
0
 protected function createGraphParallelEdge()
 {
     // v1 -> v2, v1 -> v2
     $graph = new Graph();
     $graph->createVertex('v1')->createEdgeTo($graph->createVertex('v2'));
     $graph->getVertex('v1')->createEdgeTo($graph->getVertex('v2'));
     return $graph;
 }
示例#8
0
 public function setUp()
 {
     $graph = new Graph();
     $graph->createVertex(1);
     $graph->createVertex(2);
     // 1 -> 2
     $this->edge = $graph->getVertex(1)->createEdge($graph->getVertex(2));
 }
 /**
  * @expectedException UnexpectedValueException
  */
 public function testFailCycle()
 {
     $graph = new Graph();
     $graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
     $graph->getVertex(2)->createEdgeTo($graph->getVertex(1));
     $alg = new TopologicalSort($graph);
     $alg->getVertices();
 }
示例#10
0
 public function testGraphSingleUndirectedIsSymmetricr()
 {
     // 1 -- 2
     $graph = new Graph();
     $graph->createVertex(1)->createEdge($graph->createVertex(2));
     $alg = new AlgorithmSymmetric($graph);
     $this->assertTrue($alg->isSymmetric());
 }
示例#11
0
 /**
  * @expectedException UnexpectedValueException
  */
 public function testVertexWithUndirectedEdgeHasInvalidFlow()
 {
     // 1 -- 2
     $graph = new Graph();
     $graph->createVertex(1)->createEdge($graph->createVertex(2))->setFlow(10);
     $alg = new AlgorithmFlow($graph);
     $alg->getFlowVertex($graph->getVertex(1));
 }
示例#12
0
 /**
  *
  * @param Graph $graph
  * @depends testGraphSimple
  */
 public function testGraphWithUnweightedEdges(Graph $graph)
 {
     $graph->createVertex(5)->createEdgeTo($graph->createVertex(6))->setFlow(7);
     $alg = new AlgorithmWeight($graph);
     $this->assertEquals(3, $alg->getWeight());
     $this->assertEquals(12, $alg->getWeightFlow());
     $this->assertEquals(3, $alg->getWeightMin());
     $this->assertTrue($alg->isWeighted());
 }
示例#13
0
 public function testGraphMixed()
 {
     // 1 -- 2 -> 3
     $graph = new Graph();
     $graph->createVertex(1)->createEdge($graph->createVertex(2));
     $graph->getVertex(2)->createEdgeTo($graph->createVertex(3));
     $alg = new AlgorithmDirected($graph);
     $this->assertTrue($alg->hasDirected());
     $this->assertTrue($alg->hasUndirected());
     $this->assertTrue($alg->isMixed());
 }
示例#14
0
 /**
  * Draw all Transitions
  *
  * @return array
  */
 public function drawTransitions()
 {
     $transitions = array();
     foreach ($this->transitionsStorage as $transition) {
         $fromState = $this->graph->createVertex($transition['fromStateId'], true);
         $targetState = $this->graph->createVertex($transition['targetStateId'], true);
         $edge = $fromState->createEdgeTo($targetState)->setLayout($transition['styleAttributes']);
         $transitions[] = $edge;
     }
     return $transitions;
 }
示例#15
0
 /**
  * @expectedException UnexpectedValueException
  */
 public function testGraphParallelNegative()
 {
     // 1 -[10]-> 2
     // 1 -[-1]-> 2
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $e1 = $v1->createEdgeTo($v2)->setWeight(10);
     $e2 = $v1->createEdgeTo($v2)->setWeight(-1);
     $alg = $this->createAlg($v1);
     $alg->getEdges();
 }
 /**
  * Creates the graph from the commits.
  *
  * The graph represents the parent-child relationship.  Each node in the graph has the id of the commit hash.
  *
  * @param array $commits The commits.
  * @return \Fhaculty\Graph\Graph The graph of commits.
  */
 private function _buildGraph(array $commits)
 {
     $graph = new Graph();
     foreach ($commits as $commit) {
         $vertex = $graph->createVertex($commit['sha'], true);
         $vertex->setAttribute('commit', $commit);
         foreach ($commit['parents'] as $parent) {
             $vertex->createEdgeTo($graph->createVertex($parent['sha'], true));
         }
     }
     return $graph;
 }
示例#17
0
 public function testGraphTriangleCycleIsNotBipartit()
 {
     // 1 -- 2 -- 3 -- 1
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $v3 = $graph->createVertex(3);
     $v1->createEdge($v2);
     $v2->createEdge($v3);
     $v3->createEdge($v1);
     $alg = new AlgorithmEulerian($graph);
     $this->assertTrue($alg->hasCycle());
 }
示例#18
0
 public function testGraphTriangleCycleIsNotBipartit()
 {
     // 1 -> 2 --> 3 --> 1
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $v3 = $graph->createVertex(3);
     $v1->createEdgeTo($v2);
     $v2->createEdgeTo($v3);
     $v3->createEdgeTo($v1);
     $alg = new AlgorithmBipartit($graph);
     $this->assertFalse($alg->isBipartit());
     return $alg;
 }
示例#19
0
 public function testAdditionalEdgesToNotAffectCompleteness()
 {
     // 1 -> 2
     // 1 -- 2
     // 2 -> 1
     // 1 -> 1
     $graph = new Graph();
     $graph->createVertex(1)->createEdgeTo($graph->createVertex(2));
     $graph->getVertex(1)->createEdge($graph->getVertex(2));
     $graph->getVertex(2)->createEdgeTo($graph->getVertex(1));
     $graph->getVertex(1)->createEdgeTo($graph->getVertex(1));
     $alg = new AlgorithmComplete($graph);
     $this->assertTrue($alg->isComplete());
 }
示例#20
0
 public function testSimpleDirected()
 {
     // 1 -> 2
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $v1->createEdgeTo($v2);
     $output = $this->exporter->getOutput($graph);
     $xml = new SimpleXMLElement($output);
     $this->assertEquals(1, count($xml->graph->edge));
     $edgeElem = $xml->graph->edge;
     $this->assertEquals('1', (string) $edgeElem['source']);
     $this->assertEquals('2', (string) $edgeElem['target']);
     $this->assertEquals('true', (string) $edgeElem['directed']);
 }
示例#21
0
 public function testMixed()
 {
     // 1 -- 2 -> 3
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $v3 = $graph->createVertex(3);
     $v1->createEdge($v2);
     $v2->createEdgeTo($v3);
     $exporter = new Exporter();
     $output = $exporter->getOutput($graph);
     $loader = new Loader();
     $new = $loader->loadContents($output);
     $this->assertGraphEquals($graph, $new);
 }
示例#22
0
 /**
  * Build out-tree graph from defined Processes and their relations.
  *
  * @return OutTree
  */
 public function buildTree()
 {
     if (!$this->tree) {
         $root = $this->graph->createVertex(0);
         // Create edges directed from the root node
         foreach ($this->processes as $processClassName => $processObject) {
             $vertex = $this->graph->getVertex($processClassName);
             if (!$processObject->delayMinutes) {
                 // process doesn't depend on anything => link it to the root node
                 $root->createEdgeTo($vertex)->setWeight(0);
             } else {
                 // link process to its dependency
                 // Throw error if dependency is to not existing vertex
                 if (!$this->graph->hasVertex($processObject->delayAfter)) {
                     throw new \InvalidArgumentException(sprintf('Testcase "%s" has @delayAfter dependency on "%s", but this testcase was not defined.', $processClassName, $processObject->delayAfter));
                 }
                 $this->graph->getVertex($processObject->delayAfter)->createEdgeTo($vertex)->setWeight($processObject->delayMinutes);
             }
         }
     }
     $this->tree = new OutTree($this->graph);
     if (!$this->tree->isTree()) {
         throw new \InvalidArgumentException(sprintf('Cannot build tree graph from tests dependencies. Probably some cyclic dependency is present.'));
     }
     return $this->tree;
 }
示例#23
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testInvalidVertexPassedToAlgorithm()
 {
     $graph = new Graph();
     $graph2 = new Graph();
     $v2 = $graph2->createVertex(12);
     $alg = new AlgorithmConnected($graph);
     $alg->createGraphComponentVertex($v2);
 }
示例#24
0
 public function testGraphParallelNegative()
 {
     // 1 -[10]-> 2
     // 1 -[-1]-> 2
     $graph = new Graph();
     $v1 = $graph->createVertex(1);
     $v2 = $graph->createVertex(2);
     $e1 = $v1->createEdgeTo($v2)->setWeight(10);
     $e2 = $v1->createEdgeTo($v2)->setWeight(-1);
     $alg = $this->createAlg($v1);
     $this->assertEquals(1, $alg->getDistance($v2));
     $this->assertEquals(array(2 => 1), $alg->getDistanceMap());
     $this->assertEquals(array($e1), $alg->getEdges()->getVector());
     $this->assertEquals(array($e1), $alg->getEdgesTo($v2)->getVector());
     $this->assertEquals(array(2 => $v2), $alg->getVertices()->getMap());
     $this->assertEquals(array(2), $alg->getVertices()->getIds());
 }
示例#25
0
 public function testGraphTriangleCycleIsNotBipartit()
 {
     // 1 -> 2 -> 3 -> 1
     $graph = new Graph();
     $v1 = $graph->createVertex(1)->setGroup(1);
     $v2 = $graph->createVertex(2)->setGroup(2);
     $v3 = $graph->createVertex(3)->setGroup(1);
     $v1->createEdgeTo($v2);
     $v2->createEdgeTo($v3);
     $v3->createEdgeTo($v1);
     $alg = new AlgorithmGroups($graph);
     $this->assertEquals(array(1, 2), $alg->getGroups());
     $this->assertEquals(2, $alg->getNumberOfGroups());
     $this->assertTrue($alg->getVerticesGroup(123)->isEmpty());
     $this->assertEquals(array(1 => $v1, 3 => $v3), $alg->getVerticesGroup(1)->getMap());
     $this->assertFalse($alg->isBipartit());
 }
示例#26
0
 public function testOne()
 {
     $loader = new CompleteGraph(1);
     $graph = $loader->createGraph();
     $expected = new Graph();
     $expected->createVertex();
     $this->assertGraphEquals($expected, $graph);
 }
示例#27
0
 public function provideNamespacable()
 {
     $graph = new Graph();
     $vertex = $graph->createVertex();
     $bag = $vertex->getAttributeBag();
     $subNamespace = new AttributeBagNamespaced($bag, 'prefix');
     return array(array($graph), array($vertex), array($bag), array($subNamespace));
 }
示例#28
0
 /**
  * create new uml note (attached to given class vertex)
  *
  * @param  string                $note
  * @param  Vertex|NULL           $for
  * @return LoaderUmlClassDiagram $this (chainable)
  */
 public function createVertexNote($note, $for = NULL)
 {
     $vertex = $this->graph->createVertex()->setLayoutAttribute('label', $note . "\n")->setLayoutAttribute('shape', 'note')->setLayoutAttribute('fontsize', 8)->setLayoutAttribute('style', 'filled')->setLayoutAttribute('fillcolor', 'yellow');
     if ($for !== NULL) {
         $vertex->createEdgeTo($for)->setLayoutAttribute('len', 1)->setLayoutAttribute('style', 'dashed')->setLayoutAttribute('arrowhead', 'none');
     }
     return $vertex;
 }
示例#29
0
 public function testSingleVertexIsTrivial()
 {
     $graph = new Graph();
     $graph->createVertex(1);
     $alg = new GraphProperty($graph);
     $this->assertFalse($alg->isNull());
     $this->assertTrue($alg->isEdgeless());
     $this->assertTrue($alg->isTrivial());
 }
示例#30
0
文件: LoopTest.php 项目: feffi/graph
 public function testGraphDirectedLoop()
 {
     // 1 -> 1
     $graph = new Graph();
     $graph->createVertex(1)->createEdgeTo($v1 = $graph->getVertex(1));
     $alg = new AlgorithmLoop($graph);
     $this->assertTrue($alg->hasLoop());
     $this->assertTrue($alg->hasLoopVertex($v1));
 }