Skip to content

Commit

Permalink
Use generic attributes instead of mathematical attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 5, 2020
1 parent b4ba1ad commit 91e24a5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 36 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ $graph = new Graphp\Graph\Graph();
$blue = $graph->createVertex('blue');
```

If you assign a vertex balance, this library will automatically include a
`label` attribute that includes the balance value. The following example will
automatically assign `blue (+10)` as the label:
If you assign a `balance` attribute to a vertex, this library will automatically
include a `label` attribute that includes the balance value. The following
example will automatically assign `blue (+10)` as the label:

```php
$graph = new Graphp\Graph\Graph();

$blue = $graph->createVertex('blue');
$blue->setBalance(10);
$blue->setAttribute('balance', 10);
```

You can use [vertex attributes](#vertex-attributes) to explicitly assign a
Expand Down Expand Up @@ -247,9 +247,10 @@ $b = $graph->createVertex('b');
$edge = $graph->createEdgeDirected($a, $b);
```

If you assign an edge flow, capacity or weight, this library will automatically
include a `label` attribute that includes these values. The following example
will automatically assign `100` as the label for the weighted edge:
If you assign a `flow`, `capacity` or `weight` attribute to an edge, this library
will automatically include a `label` attribute that includes these values. The
following example will automatically assign `100` as the label for the weighted
edge:

```php
$graph = new Graphp\Graph\Graph();
Expand All @@ -258,7 +259,7 @@ $a = $graph->createVertex('a');
$b = $graph->createVertex('b');

$edge = $graph->createEdgeDirected($a, $b);
$edge->setWeight(100);
$edge->setAttribute('weight', 100);
```

The following example will automatically assign `4/10` as the label for an edge
Expand All @@ -271,8 +272,8 @@ $a = $graph->createVertex('a');
$b = $graph->createVertex('b');

$edge = $graph->createEdgeDirected($a, $b);
$edge->setFlow(4);
$edge->setCapacity(10);
$edge->setAttribute('flow', 4);
$edge->setAttribute('capacity', 10);
```

The following example will automatically assign `4/∞/100` as the label for a
Expand All @@ -285,9 +286,9 @@ $a = $graph->createVertex('a');
$b = $graph->createVertex('b');

$edge = $graph->createEdgeDirected($a, $b);
$edge->setFlow(4);
$edge->setCapacity(null);
$edge->setWeight(100);
$edge->setAttribute('flow', 4);
$edge->setAttribute('capacity', null);
$edge->setAttribute('weight', 100);
```

You can use [edge attributes](#edge-attributes) to explicitly assign any
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"require": {
"php": ">=5.3.0",
"graphp/graph": "dev-master#fb198e4 as 1.0.0"
"graphp/graph": "dev-master#908fd49 as 1.0.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35"
Expand Down
26 changes: 17 additions & 9 deletions src/GraphViz.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class GraphViz
*/
private $formatIndent = ' ';

private $attributeFlow = 'flow';
private $attributeCapacity = 'capacity';
private $attributeWeight = 'weight';

private $attributeGroup = 'group';
private $attributeBalance = 'balance';

const DELAY_OPEN = 2.0;

const EOL = PHP_EOL;
Expand Down Expand Up @@ -259,15 +266,16 @@ public function createScript(Graph $graph)

$groups = array();
foreach ($graph->getVertices()->getMap() as $vid => $vertex) {
$groups[$vertex->getGroup()][$vid] = $vertex;
$groups[$vertex->getAttribute('group', 0)][$vid] = $vertex;
}

// only cluster vertices into groups if there are at least 2 different groups
if (count($groups) > 1) {
$indent = str_repeat($this->formatIndent, 2);
$gid = 0;
// put each group of vertices in a separate subgraph cluster
foreach ($groups as $group => $vertices) {
$script .= $this->formatIndent . 'subgraph cluster_' . $group . ' {' . self::EOL .
$script .= $this->formatIndent . 'subgraph cluster_' . $gid++ . ' {' . self::EOL .
$indent . 'label = ' . $this->escape($group) . self::EOL;
foreach ($vertices as $vid => $vertex) {
$layout = $this->getLayoutVertex($vertex);
Expand Down Expand Up @@ -391,12 +399,12 @@ protected function getLayoutVertex(Vertex $vertex)
$bag = new AttributeBagNamespaced($vertex, 'graphviz.');
$layout = $bag->getAttributes();

$balance = $vertex->getBalance();
if($balance !== NULL){
if($balance > 0){
$balance = $vertex->getAttribute($this->attributeBalance);
if ($balance !== NULL) {
if ($balance > 0) {
$balance = '+' . $balance;
}
if(!isset($layout['label'])){
if (!isset($layout['label'])) {
$layout['label'] = $vertex->getId();
}
$layout['label'] .= ' (' . $balance . ')';
Expand All @@ -413,8 +421,8 @@ protected function getLayoutEdge(Edge $edge)
// use flow/capacity/weight as edge label
$label = NULL;

$flow = $edge->getFlow();
$capacity = $edge->getCapacity();
$flow = $edge->getAttribute($this->attributeFlow);
$capacity = $edge->getAttribute($this->attributeCapacity);
// flow is set
if ($flow !== NULL) {
// NULL capacity = infinite capacity
Expand All @@ -424,7 +432,7 @@ protected function getLayoutEdge(Edge $edge)
$label = '0/' . $capacity;
}

$weight = $edge->getWeight();
$weight = $edge->getAttribute($this->attributeWeight);
// weight is set
if ($weight !== NULL) {
if ($label === NULL) {
Expand Down
26 changes: 13 additions & 13 deletions tests/GraphVizTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public function testGraphIsolatedVertices()
public function testGraphIsolatedVerticesWithGroupsWillBeAddedToClusters()
{
$graph = new Graph();
$graph->createVertex('a')->setGroup(0);
$graph->createVertex('b')->setGroup(1)->setAttribute('graphviz.label', 'second');
$graph->createVertex('a')->setAttribute('group', 0);
$graph->createVertex('b')->setAttribute('group', 'foo bar')->setAttribute('graphviz.label', 'second');

$expected = <<<VIZ
graph {
Expand All @@ -84,7 +84,7 @@ public function testGraphIsolatedVerticesWithGroupsWillBeAddedToClusters()
"a"
}
subgraph cluster_1 {
label = 1
label = "foo bar"
"b" [label="second"]
}
}
Expand Down Expand Up @@ -285,11 +285,11 @@ public function testGraphUndirectedWithIsolatedVerticesFirst()
public function testVertexLabels()
{
$graph = new Graph();
$graph->createVertex('a')->setBalance(1);
$graph->createVertex('b')->setBalance(0);
$graph->createVertex('c')->setBalance(-1);
$graph->createVertex('a')->setAttribute('balance', 1);
$graph->createVertex('b')->setAttribute('balance', 0);
$graph->createVertex('c')->setAttribute('balance', -1);
$graph->createVertex('d')->setAttribute('graphviz.label', 'test');
$graph->createVertex('e')->setBalance(2)->setAttribute('graphviz.label', 'unnamed');
$graph->createVertex('e')->setAttribute('balance', 2)->setAttribute('graphviz.label', 'unnamed');

$expected = <<<VIZ
graph {
Expand Down Expand Up @@ -332,12 +332,12 @@ public function testEdgeLabels()
{
$graph = new Graph();
$graph->createEdgeUndirected($graph->createVertex('1a'), $graph->createVertex('1b'));
$graph->createEdgeUndirected($graph->createVertex('2a'), $graph->createVertex('2b'))->setWeight(20);
$graph->createEdgeUndirected($graph->createVertex('3a'), $graph->createVertex('3b'))->setCapacity(30);
$graph->createEdgeUndirected($graph->createVertex('4a'), $graph->createVertex('4b'))->setFlow(40);
$graph->createEdgeUndirected($graph->createVertex('5a'), $graph->createVertex('5b'))->setFlow(50)->setCapacity(60);
$graph->createEdgeUndirected($graph->createVertex('6a'), $graph->createVertex('6b'))->setFlow(60)->setCapacity(70)->setWeight(80);
$graph->createEdgeUndirected($graph->createVertex('7a'), $graph->createVertex('7b'))->setFlow(70)->setAttribute('graphviz.label', 'prefixed');
$graph->createEdgeUndirected($graph->createVertex('2a'), $graph->createVertex('2b'))->setAttribute('weight', 20);
$graph->createEdgeUndirected($graph->createVertex('3a'), $graph->createVertex('3b'))->setAttribute('capacity', 30);
$graph->createEdgeUndirected($graph->createVertex('4a'), $graph->createVertex('4b'))->setAttribute('flow', 40);
$graph->createEdgeUndirected($graph->createVertex('5a'), $graph->createVertex('5b'))->setAttribute('flow', 50)->setAttribute('capacity', 60);
$graph->createEdgeUndirected($graph->createVertex('6a'), $graph->createVertex('6b'))->setAttribute('flow', 60)->setAttribute('capacity', 70)->setAttribute('weight', 80);
$graph->createEdgeUndirected($graph->createVertex('7a'), $graph->createVertex('7b'))->setAttribute('flow', 70)->setAttribute('graphviz.label', 'prefixed');

$expected = <<<VIZ
graph {
Expand Down

0 comments on commit 91e24a5

Please sign in to comment.