|
2 | 2 |
|
3 | 3 | namespace Graphp\GraphViz; |
4 | 4 |
|
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; |
11 | 5 | 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; |
12 | 9 | use Fhaculty\Graph\Graph; |
13 | 10 | use Fhaculty\Graph\Vertex; |
14 | 11 |
|
@@ -225,8 +222,13 @@ public function createImageFile(Graph $graph) |
225 | 222 | */ |
226 | 223 | public function createScript(Graph $graph) |
227 | 224 | { |
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 | + } |
230 | 232 |
|
231 | 233 | /* |
232 | 234 | * The website [http://www.graphviz.org/content/dot-language] uses the term `ID` when displaying |
@@ -255,39 +257,38 @@ public function createScript(Graph $graph) |
255 | 257 | } |
256 | 258 | } |
257 | 259 |
|
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 | + } |
261 | 264 |
|
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) { |
264 | 267 | $indent = str_repeat($this->formatIndent, 2); |
265 | 268 | // 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 . |
268 | 271 | $indent . 'label = ' . $this->escape($group) . self::EOL; |
269 | | - foreach($alg->getVerticesGroup($group)->getMap() as $vid => $vertex) { |
| 272 | + foreach ($vertices as $vid => $vertex) { |
270 | 273 | $layout = $this->getLayoutVertex($vertex); |
271 | 274 |
|
272 | 275 | $script .= $indent . $this->escapeId($vid); |
273 | | - if($layout){ |
| 276 | + if ($layout) { |
274 | 277 | $script .= ' ' . $this->escapeAttributes($layout); |
275 | 278 | } |
276 | 279 | $script .= self::EOL; |
277 | 280 | } |
278 | 281 | $script .= ' }' . self::EOL; |
279 | 282 | } |
280 | 283 | } else { |
281 | | - $alg = new Degree($graph); |
282 | | - |
283 | 284 | // explicitly add all isolated vertices (vertices with no edges) and vertices with special layout set |
284 | 285 | // other vertices wil be added automatically due to below edge definitions |
285 | 286 | foreach ($graph->getVertices()->getMap() as $vid => $vertex){ |
286 | 287 | $layout = $this->getLayoutVertex($vertex); |
287 | 288 |
|
288 | | - if($layout || $alg->isVertexIsolated($vertex)){ |
| 289 | + if ($layout || $vertex->getEdges()->isEmpty()) { |
289 | 290 | $script .= $this->formatIndent . $this->escapeId($vid); |
290 | | - if($layout){ |
| 291 | + if ($layout) { |
291 | 292 | $script .= ' ' . $this->escapeAttributes($layout); |
292 | 293 | } |
293 | 294 | $script .= self::EOL; |
@@ -337,7 +338,7 @@ private function escapeId($id) |
337 | 338 | public static function escape($id) |
338 | 339 | { |
339 | 340 | // see raw() |
340 | | - if ($id instanceof stdClass && isset($id->string)) { |
| 341 | + if ($id instanceof \stdClass && isset($id->string)) { |
341 | 342 | return $id->string; |
342 | 343 | } |
343 | 344 | // see @link: There is no semantic difference between abc_2 and "abc_2" |
@@ -377,7 +378,7 @@ private function escapeAttributes($attrs) |
377 | 378 | * create a raw string representation, i.e. do NOT escape the given string when used in graphviz output |
378 | 379 | * |
379 | 380 | * @param string $string |
380 | | - * @return StdClass |
| 381 | + * @return \stdClass |
381 | 382 | * @see GraphViz::escape() |
382 | 383 | */ |
383 | 384 | public static function raw($string) |
|
0 commit comments