Skip to content

Commit a00aeaf

Browse files
committed
Skeleton for TrivialGraphFormat (TGF) exporter
0 parents  commit a00aeaf

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/TrivialGraphFormat.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Fhaculty\Graph\Exporter;
4+
5+
use Fhaculty\Graph\Edge\Directed;
6+
use Fhaculty\Graph\Edge\Base as Edge;
7+
use Fhaculty\Graph\Vertex;
8+
use Fhaculty\Graph\Graph;
9+
10+
/**
11+
*
12+
* @author clue
13+
* @link http://en.wikipedia.org/wiki/Trivial_Graph_Format
14+
*/
15+
class TrivialGraphFormat implements ExporterInterface
16+
{
17+
const EOL = PHP_EOL;
18+
19+
public function getOutput(Graph $graph)
20+
{
21+
$output = '';
22+
23+
// build an array to map vertex IDs (which may contain complex strings) to temporary numeric IDs for output
24+
$tid = 1;
25+
$tids = array();
26+
27+
foreach ($graph->getVertices() as $vid => $vertex) {
28+
$output .= $tid . ' ' . $this->getVertexLabel($vertex) . self::EOL;
29+
$tids[$vid] = $tid++;
30+
}
31+
32+
// end of vertex list, start of edge list
33+
$output .= '#' . self::EOL;
34+
35+
foreach ($graph->getEdges() as $edge) {
36+
$ids = $edge->getVerticesId();
37+
$a = $tids[$ids[0]];
38+
$b = $tids[$ids[1]];
39+
40+
$label = $this->getEdgeLabel($edge);
41+
if ($label !== '') {
42+
$label = ' ' . $label;
43+
}
44+
45+
$output .= $a . ' ' . $b . $label . self::EOL;
46+
47+
// this is not a directed edge => also add back-edge with same label
48+
if (!($edge instanceof Directed)) {
49+
$output .= $b . ' ' . $a . $label . self::EOL;
50+
}
51+
}
52+
return $output;
53+
}
54+
55+
protected function getVertexLabel(Vertex $vertex)
56+
{
57+
// TODO: dump additional vertex attributes, such as group, balance, etc.
58+
return $vertex->getId();
59+
}
60+
61+
protected function getEdgeLabel(Edge $edge)
62+
{
63+
// TODO: dump additional edge attributes, such as flow, capacity, weight, etc.
64+
return '';
65+
}
66+
}

0 commit comments

Comments
 (0)