Skip to content

Commit df42153

Browse files
committed
Replace layout attributes (Layoutable) with general purpose Attributes
1 parent 028b282 commit df42153

9 files changed

Lines changed: 102 additions & 143 deletions

File tree

lib/Fhaculty/Graph/Algorithm/MaximumMatching/Flow.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function getEdges()
3030
// create temporary flow graph with supersource and supersink
3131
$graphFlow = $this->graph->createGraphCloneEdgeless();
3232

33-
$superSource = $graphFlow->createVertex()->setLayoutAttribute('label', 's*');
34-
$superSink = $graphFlow->createVertex()->setLayoutAttribute('label', 't*');
33+
$superSource = $graphFlow->createVertex();
34+
$superSink = $graphFlow->createVertex();
3535

3636
$groups = $alg->getGroups();
3737
$groupA = $groups[0];

lib/Fhaculty/Graph/Algorithm/MinimumCostFlow/CycleCanceling.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function createGraph()
1919
// create resulting graph with supersource and supersink
2020
$resultGraph = $this->graph->createGraphClone();
2121

22-
$superSource = $resultGraph->createVertex()->setLayoutAttribute('label', 's*');
23-
$superSink = $resultGraph->createVertex()->setLayoutAttribute('label', 't*');
22+
$superSource = $resultGraph->createVertex();
23+
$superSink = $resultGraph->createVertex();
2424

2525
$sumBalance = 0;
2626

lib/Fhaculty/Graph/Edge/Base.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Fhaculty\Graph\Edge;
44

5-
use Fhaculty\Graph\Layoutable;
65
use Fhaculty\Graph\Vertex;
76
use Fhaculty\Graph\Set\Edges;
87
use Fhaculty\Graph\Set\Vertices;
@@ -13,8 +12,10 @@
1312
use Fhaculty\Graph\Exception\UnderflowException;
1413
use Fhaculty\Graph\Exception\InvalidArgumentException;
1514
use Fhaculty\Graph\Exception\BadMethodCallException;
15+
use Fhaculty\Graph\Attribute\AttributeAware;
16+
use Fhaculty\Graph\Attribute\AttributeBagReference;
1617

17-
abstract class Base extends Layoutable implements VerticesAggregate
18+
abstract class Base implements VerticesAggregate, AttributeAware
1819
{
1920
/**
2021
* weight of this edge
@@ -40,6 +41,8 @@ abstract class Base extends Layoutable implements VerticesAggregate
4041
*/
4142
protected $flow = NULL;
4243

44+
protected $attributes = array();
45+
4346
/**
4447
* get Vertices that are a target of this edge
4548
*
@@ -289,4 +292,19 @@ private function __clone()
289292
throw new BadMethodCallException();
290293
// @codeCoverageIgnoreEnd
291294
}
295+
296+
public function getAttribute($name)
297+
{
298+
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
299+
}
300+
301+
public function setAttribute($name, $value)
302+
{
303+
$this->attributes[$name] = $value;
304+
}
305+
306+
public function getAttributeBag()
307+
{
308+
return new AttributeBagReference($this->attributes);
309+
}
292310
}

lib/Fhaculty/Graph/Graph.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
use Fhaculty\Graph\Set\VerticesMap;
2222
use Fhaculty\Graph\Set\Edges;
2323
use Fhaculty\Graph\Set\DualAggregate;
24+
use Fhaculty\Graph\Attribute\AttributeAware;
25+
use Fhaculty\Graph\Attribute\AttributeBagReference;
2426

25-
class Graph implements DualAggregate
27+
class Graph implements DualAggregate, AttributeAware
2628
{
2729
/**
2830
* @var ExporterInterface|null
@@ -36,6 +38,8 @@ class Graph implements DualAggregate
3638
protected $edgesStorage = array();
3739
protected $edges;
3840

41+
protected $attributes = array();
42+
3943
public function __construct()
4044
{
4145
$this->vertices = VerticesMap::factoryArrayReference($this->verticesStorage);
@@ -100,7 +104,7 @@ public function createVertexClone(Vertex $originalVertex)
100104
}
101105
$newVertex = new Vertex($this, $id);
102106
// TODO: properly set attributes of vertex
103-
$newVertex->setLayout($originalVertex->getLayout());
107+
$newVertex->getAttributeBag()->setAttributes($originalVertex->getAttributeBag()->getAttributes());
104108
$newVertex->setBalance($originalVertex->getBalance());
105109
$newVertex->setGroup($originalVertex->getGroup());
106110

@@ -117,7 +121,7 @@ public function createVertexClone(Vertex $originalVertex)
117121
public function createGraphCloneEdgeless()
118122
{
119123
$graph = new Graph();
120-
// $graph->setLayout($this->getLayout());
124+
$graph->getAttributeBag()->setAttributes($this->getAttributeBag()->getAttributes());
121125
// TODO: set additional graph attributes
122126
foreach ($this->getVertices() as $originalVertex) {
123127
$vertex = $graph->createVertexClone($originalVertex);
@@ -236,7 +240,7 @@ private function createEdgeCloneInternal(Edge $originalEdge, $ia, $ib)
236240
$newEdge = $a->createEdge($b);
237241
}
238242
// TODO: copy edge attributes
239-
$newEdge->setLayout($originalEdge->getLayout());
243+
$newEdge->getAttributeBag()->setAttributes($originalEdge->getAttributeBag()->getAttributes());
240244
$newEdge->setWeight($originalEdge->getWeight());
241245
$newEdge->setFlow($originalEdge->getFlow());
242246
$newEdge->setCapacity($originalEdge->getCapacity());
@@ -497,8 +501,18 @@ public function __toString()
497501
return $this->getExporter()->getOutput($this);
498502
}
499503

500-
public function getLayout()
504+
public function getAttribute($name)
505+
{
506+
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
507+
}
508+
509+
public function setAttribute($name, $value)
510+
{
511+
$this->attributes[$name] = $value;
512+
}
513+
514+
public function getAttributeBag()
501515
{
502-
return array();
516+
return new AttributeBagReference($this->attributes);
503517
}
504518
}

lib/Fhaculty/Graph/GraphViz.php

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Fhaculty\Graph\Exception\InvalidArgumentException;
1010
use Fhaculty\Graph\Edge\Base as Edge;
1111
use \stdClass;
12+
use Fhaculty\Graph\Attribute\AttributeBagNamespaced;
1213

1314
class GraphViz
1415
{
@@ -26,9 +27,6 @@ class GraphViz
2627
*/
2728
private $format = 'png';
2829

29-
private $layoutVertex = array();
30-
private $layoutEdge = array();
31-
3230
/**
3331
* Either the name of full path to GraphViz layout.
3432
*
@@ -151,21 +149,6 @@ public function display()
151149
const LAYOUT_EDGE = 2;
152150
const LAYOUT_VERTEX = 3;
153151

154-
private function mergeLayout(&$old, $new)
155-
{
156-
if ($new === NULL) {
157-
$old = array();
158-
} else {
159-
foreach ($new as $key => $value) {
160-
if ($value === NULL) {
161-
unset($old[$key]);
162-
} else {
163-
$old[$key] = $value;
164-
}
165-
}
166-
}
167-
}
168-
169152
public function setLayout($where, $layout, $value = NULL)
170153
{
171154
if (!is_array($where)) {
@@ -174,13 +157,17 @@ public function setLayout($where, $layout, $value = NULL)
174157
if (func_num_args() > 2) {
175158
$layout = array($layout => $value);
176159
}
160+
161+
$map = array(
162+
self::LAYOUT_GRAPH => 'graphviz.graph.',
163+
self::LAYOUT_EDGE => 'graphviz.edge.',
164+
self::LAYOUT_VERTEX => 'graphviz.node',
165+
);
166+
177167
foreach ($where as $where) {
178-
if ($where === self::LAYOUT_GRAPH) {
179-
$this->graph->setLayout($layout, $value);
180-
} elseif ($where === self::LAYOUT_EDGE) {
181-
$this->mergeLayout($this->layoutEdge, $layout);
182-
} elseif ($where === self::LAYOUT_VERTEX) {
183-
$this->mergeLayout($this->layoutVertex, $layout);
168+
if (isset($map[$where])) {
169+
$bag = new AttributeBagNamespaced($this->graph, $map[$where]);
170+
$bag->setAttributes($layout);
184171
} else {
185172
throw new InvalidArgumentException('Invalid layout identifier');
186173
}
@@ -285,15 +272,18 @@ public function createScript()
285272
$script = ($directed ? 'di':'') . 'graph G {' . self::EOL;
286273

287274
// add global attributes
288-
$layout = $this->graph->getLayout();
289-
if ($layout) {
290-
$script .= $this->formatIndent . 'graph ' . $this->escapeAttributes($layout) . self::EOL;
291-
}
292-
if ($this->layoutVertex) {
293-
$script .= $this->formatIndent . 'node ' . $this->escapeAttributes($this->layoutVertex) . self::EOL;
294-
}
295-
if ($this->layoutEdge) {
296-
$script .= $this->formatIndent . 'edge ' . $this->escapeAttributes($this->layoutEdge) . self::EOL;
275+
$globals = array(
276+
'graph' => 'graphviz.graph.',
277+
'node' => 'graphviz.node.',
278+
'edge' => 'graphviz.edge.',
279+
);
280+
281+
foreach ($globals as $key => $prefix) {
282+
$bag = new AttributeBagNamespaced($this->graph, $prefix);
283+
284+
if ($layout = $bag->getAttributes()) {
285+
$script .= $this->formatIndent . $key . ' ' . $this->escapeAttributes($layout) . self::EOL;
286+
}
297287
}
298288

299289
$alg = new Groups($this->graph);
@@ -428,7 +418,8 @@ public static function raw($string)
428418

429419
protected function getLayoutVertex(Vertex $vertex)
430420
{
431-
$layout = $vertex->getLayout();
421+
$bag = new AttributeBagNamespaced($vertex, 'graphviz.');
422+
$layout = $bag->getAttributes();
432423

433424
$balance = $vertex->getBalance();
434425
if($balance !== NULL){
@@ -446,7 +437,8 @@ protected function getLayoutVertex(Vertex $vertex)
446437

447438
protected function getLayoutEdge(Edge $edge)
448439
{
449-
$layout = $edge->getLayout();
440+
$bag = new AttributeBagNamespaced($edge, 'graphviz.');
441+
$layout = $bag->getAttributes();
450442

451443
// use flow/capacity/weight as edge label
452444
$label = NULL;

lib/Fhaculty/Graph/Layoutable.php

Lines changed: 0 additions & 84 deletions
This file was deleted.

lib/Fhaculty/Graph/Vertex.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use Fhaculty\Graph\Exception\BadMethodCallException;
1212
use Fhaculty\Graph\Exception\UnexpectedValueException;
1313
use Fhaculty\Graph\Exception\InvalidArgumentException;
14+
use Fhaculty\Graph\Attribute\AttributeAware;
15+
use Fhaculty\Graph\Attribute\AttributeBagReference;
1416

15-
class Vertex extends Layoutable implements EdgesAggregate
17+
class Vertex implements EdgesAggregate, AttributeAware
1618
{
1719
private $id;
1820

@@ -42,6 +44,8 @@ class Vertex extends Layoutable implements EdgesAggregate
4244
*/
4345
private $group = 0;
4446

47+
private $attributes = array();
48+
4549
/**
4650
* Create a new Vertex
4751
*
@@ -367,4 +371,19 @@ private function __clone()
367371
throw new BadMethodCallException();
368372
// @codeCoverageIgnoreEnd
369373
}
374+
375+
public function getAttribute($name)
376+
{
377+
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
378+
}
379+
380+
public function setAttribute($name, $value)
381+
{
382+
$this->attributes[$name] = $value;
383+
}
384+
385+
public function getAttributeBag()
386+
{
387+
return new AttributeBagReference($this->attributes);
388+
}
370389
}

0 commit comments

Comments
 (0)