Last active
December 18, 2015 10:59
-
-
Save clue/5772483 to your computer and use it in GitHub Desktop.
thejit graph transformer prototype/mockup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Fhaculty\Graph\Graph; | |
use Fhaculty\Graph\Vertex; | |
use Fhaculty\Graph\Algorithm\Tree\OutTree as Tree; | |
class TheJit | |
{ | |
private $graph; | |
public function __construct(Graph $graph) | |
{ | |
$this->graph = $graph; | |
} | |
// http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.code.html | |
public function exportTree() | |
{ | |
$tree = new Tree($this->graph); | |
if (!$tree->isTree()) { | |
throw new UnexpectedValueException('Graph does not represent a valid tree'); | |
} | |
$root = $tree->getVertexRoot(); | |
return $this->exportVertexTree($root, $tree); | |
} | |
public function exportJsonTree() | |
{ | |
return json_encode($this->exportTree()); | |
} | |
// http://philogb.github.io/jit/static/v20/Docs/files/Loader/Loader-js.html#Loader.loadJSON | |
public function exportGraph() | |
{ | |
$ret = array(); | |
foreach ($this->graph->getVertices() as $vertex) { | |
$ret []= $this->exportVertex($vertex); | |
} | |
return $ret; | |
} | |
public function exportJsonGraph() | |
{ | |
return json_encode($this->exportGraph()); | |
} | |
private function exportVertexAdjacencies(Vertex $vertex) | |
{ | |
return array( | |
'id' => $vertex->getId(), | |
'name' => $vertex->getId(), | |
'data' => array(), | |
'adjacencies' => array_keys($vertex->getVerticesEdgeTo()) | |
); | |
} | |
// http://philogb.github.io/jit/static/v20/Docs/files/Loader/Loader-js.html#Loader.loadJSON | |
private function exportVertexTree(Vertex $vertex, Tree $tree) | |
{ | |
$children = array(); | |
// recursively export vertex children | |
foreach ($tree->getVertexChildren($vertex) as $child) { | |
$children []= $this->exportVertexTree($child); | |
} | |
return array( | |
'id' => $vertex->getId(), | |
'name' => $vertex->getId(), | |
'data' => array(), | |
'children' => $children | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment