Skip to content

Commit d80a356

Browse files
authored
Merge pull request #39 from clue-labs/no-algorithms
Remove unneeded dependency on graphp/algorithms
2 parents 80fb738 + 650c5e5 commit d80a356

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
},
1111
"require": {
1212
"php": ">=5.3.0",
13-
"clue/graph": "~0.9.0|~0.8.0",
14-
"graphp/algorithms": "~0.8.0"
13+
"clue/graph": "~0.9.0|~0.8.0"
1514
},
1615
"require-dev": {
1716
"phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35"

src/GraphViz.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace Graphp\GraphViz;
44

5-
use Graphp\Algorithms\Directed;
6-
use Graphp\Algorithms\Groups;
7-
use Graphp\Algorithms\Degree;
8-
use Fhaculty\Graph\Exception\UnexpectedValueException;
9-
use Fhaculty\Graph\Edge\Base as Edge;
10-
use \stdClass;
115
use Fhaculty\Graph\Attribute\AttributeBagNamespaced;
6+
use Fhaculty\Graph\Edge\Base as Edge;
7+
use Fhaculty\Graph\Edge\Directed as EdgeDirected;
8+
use Fhaculty\Graph\Exception\UnexpectedValueException;
129
use Fhaculty\Graph\Graph;
1310
use Fhaculty\Graph\Vertex;
1411

@@ -225,8 +222,13 @@ public function createImageFile(Graph $graph)
225222
*/
226223
public function createScript(Graph $graph)
227224
{
228-
$alg = new Directed($graph);
229-
$directed = $alg->hasDirected();
225+
$directed = false;
226+
foreach ($graph->getEdges() as $edge) {
227+
if ($edge instanceof EdgeDirected) {
228+
$directed = true;
229+
break;
230+
}
231+
}
230232

231233
/*
232234
* The website [http://www.graphviz.org/content/dot-language] uses the term `ID` when displaying
@@ -255,39 +257,38 @@ public function createScript(Graph $graph)
255257
}
256258
}
257259

258-
$alg = new Groups($graph);
259-
// only append group number to vertex label if there are at least 2 different groups
260-
$showGroups = ($alg->getNumberOfGroups() > 1);
260+
$groups = array();
261+
foreach ($graph->getVertices()->getMap() as $vid => $vertex) {
262+
$groups[$vertex->getGroup()][$vid] = $vertex;
263+
}
261264

262-
if ($showGroups) {
263-
$gid = 0;
265+
// only cluster vertices into groups if there are at least 2 different groups
266+
if (count($groups) > 1) {
264267
$indent = str_repeat($this->formatIndent, 2);
265268
// put each group of vertices in a separate subgraph cluster
266-
foreach ($alg->getGroups() as $group) {
267-
$script .= $this->formatIndent . 'subgraph cluster_' . $gid++ . ' {' . self::EOL .
269+
foreach ($groups as $group => $vertices) {
270+
$script .= $this->formatIndent . 'subgraph cluster_' . $group . ' {' . self::EOL .
268271
$indent . 'label = ' . $this->escape($group) . self::EOL;
269-
foreach($alg->getVerticesGroup($group)->getMap() as $vid => $vertex) {
272+
foreach ($vertices as $vid => $vertex) {
270273
$layout = $this->getLayoutVertex($vertex);
271274

272275
$script .= $indent . $this->escapeId($vid);
273-
if($layout){
276+
if ($layout) {
274277
$script .= ' ' . $this->escapeAttributes($layout);
275278
}
276279
$script .= self::EOL;
277280
}
278281
$script .= ' }' . self::EOL;
279282
}
280283
} else {
281-
$alg = new Degree($graph);
282-
283284
// explicitly add all isolated vertices (vertices with no edges) and vertices with special layout set
284285
// other vertices wil be added automatically due to below edge definitions
285286
foreach ($graph->getVertices()->getMap() as $vid => $vertex){
286287
$layout = $this->getLayoutVertex($vertex);
287288

288-
if($layout || $alg->isVertexIsolated($vertex)){
289+
if ($layout || $vertex->getEdges()->isEmpty()) {
289290
$script .= $this->formatIndent . $this->escapeId($vid);
290-
if($layout){
291+
if ($layout) {
291292
$script .= ' ' . $this->escapeAttributes($layout);
292293
}
293294
$script .= self::EOL;
@@ -337,7 +338,7 @@ private function escapeId($id)
337338
public static function escape($id)
338339
{
339340
// see raw()
340-
if ($id instanceof stdClass && isset($id->string)) {
341+
if ($id instanceof \stdClass && isset($id->string)) {
341342
return $id->string;
342343
}
343344
// see @link: There is no semantic difference between abc_2 and "abc_2"
@@ -377,7 +378,7 @@ private function escapeAttributes($attrs)
377378
* create a raw string representation, i.e. do NOT escape the given string when used in graphviz output
378379
*
379380
* @param string $string
380-
* @return StdClass
381+
* @return \stdClass
381382
* @see GraphViz::escape()
382383
*/
383384
public static function raw($string)

tests/GraphVizTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,29 @@ public function testGraphIsolatedVertices()
6666
"b"
6767
}
6868
69+
VIZ;
70+
71+
$this->assertEquals($expected, $this->graphViz->createScript($graph));
72+
}
73+
74+
public function testGraphIsolatedVerticesWithGroupsWillBeAddedToClusters()
75+
{
76+
$graph = new Graph();
77+
$graph->createVertex('a')->setGroup(0);
78+
$graph->createVertex('b')->setGroup(1)->setAttribute('graphviz.label', 'second');
79+
80+
$expected = <<<VIZ
81+
graph {
82+
subgraph cluster_0 {
83+
label = 0
84+
"a"
85+
}
86+
subgraph cluster_1 {
87+
label = 1
88+
"b" [label="second"]
89+
}
90+
}
91+
6992
VIZ;
7093

7194
$this->assertEquals($expected, $this->graphViz->createScript($graph));

0 commit comments

Comments
 (0)