Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add consistent Layout + interface #40

Closed
wants to merge 13 commits into from
Closed
4 changes: 2 additions & 2 deletions lib/Fhaculty/Graph/Algorithm/MaximumMatching/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function getEdges()
// create temporary flow graph with supersource and supersink
$graphFlow = $this->graph->createGraphCloneEdgeless();

$superSource = $graphFlow->createVertex()->setLayoutAttribute('label', 's*');
$superSink = $graphFlow->createVertex()->setLayoutAttribute('label', 't*');
$superSource = $graphFlow->createVertex()/*->getLayout()->setAttribute('label', 's*')*/;
$superSink = $graphFlow->createVertex()/*->getLayout()->setAttribute('label', 't*')*/;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines have commented code which should not be. Maybe add @todo to make sure this gets fixed later on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting. I've removed them as they were only ever used when this algorithm has been created.


$groups = $alg->getGroups();
$groupA = $groups[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function createGraph()
// create resulting graph with supersource and supersink
$resultGraph = $this->graph->createGraphClone();

$superSource = $resultGraph->createVertex()->setLayoutAttribute('label', 's*');
$superSink = $resultGraph->createVertex()->setLayoutAttribute('label', 't*');
$superSource = $resultGraph->createVertex()/*->getLayout()->setAttribute('label', 's*')*/;
$superSink = $resultGraph->createVertex()/*->getLayout()->setAttribute('label', 't*')*/;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ditto) These lines have commented code which should not be. Maybe add @todo to make sure this gets fixed later on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, also removed.


$sumBalance = 0;

Expand Down
16 changes: 14 additions & 2 deletions lib/Fhaculty/Graph/Edge/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Fhaculty\Graph\Edge;

use Fhaculty\Graph\Layoutable;
use Fhaculty\Graph\Vertex;
use Fhaculty\Graph\Set\Edges;
use Fhaculty\Graph\Set\Vertices;
Expand All @@ -13,8 +12,10 @@
use Fhaculty\Graph\Exception\UnderflowException;
use Fhaculty\Graph\Exception\InvalidArgumentException;
use Fhaculty\Graph\Exception\BadMethodCallException;
use Fhaculty\Graph\Renderer\Layout;
use Fhaculty\Graph\Renderer\LayoutAggregate;

abstract class Base extends Layoutable implements VerticesAggregate
abstract class Base implements VerticesAggregate, LayoutAggregate
{
/**
* weight of this edge
Expand All @@ -40,6 +41,8 @@ abstract class Base extends Layoutable implements VerticesAggregate
*/
protected $flow = NULL;

protected $layout = null;

/**
* get Vertices that are a target of this edge
*
Expand Down Expand Up @@ -289,4 +292,13 @@ private function __clone()
throw new BadMethodCallException();
// @codeCoverageIgnoreEnd
}

public function getLayout()
{
if ($this->layout === null) {
$this->layout = new Layout();
}

return $this->layout;
}
}
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Exporter/Dot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Fhaculty\Graph\Exporter;

use Fhaculty\Graph\GraphViz;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Renderer\GraphViz;

class Dot implements ExporterInterface
{
Expand Down
2 changes: 1 addition & 1 deletion lib/Fhaculty/Graph/Exporter/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Fhaculty\Graph\Exporter;

use Fhaculty\Graph\GraphViz;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Renderer\GraphViz;

class Image implements ExporterInterface
{
Expand Down
51 changes: 45 additions & 6 deletions lib/Fhaculty/Graph/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
use Fhaculty\Graph\Set\Vertices;
use Fhaculty\Graph\Set\VerticesMap;
use Fhaculty\Graph\Set\Edges;
use Fhaculty\Graph\Renderer\LayoutAggregate;
use Fhaculty\Graph\Renderer\Layout;

class Graph extends Set
class Graph extends Set implements LayoutAggregate
{
/**
* @var ExporterInterface|null
Expand All @@ -35,6 +37,10 @@ class Graph extends Set
protected $edgesStorage = array();
protected $edges;

protected $layout = null;
protected $layoutVertex = null;
protected $layoutEdge = null;

public function __construct()
{
$this->vertices = VerticesMap::factoryArrayReference($this->verticesStorage);
Expand Down Expand Up @@ -106,7 +112,7 @@ public function createVertexClone(Vertex $originalVertex)
}
$newVertex = new Vertex($id, $this);
// TODO: properly set attributes of vertex
$newVertex->setLayout($originalVertex->getLayout());
$newVertex->getLayout()->setLayout($originalVertex->getLayout());
$newVertex->setBalance($originalVertex->getBalance());
$newVertex->setGroup($originalVertex->getGroup());
$this->verticesStorage[$id] = $newVertex;
Expand All @@ -124,7 +130,7 @@ public function createVertexClone(Vertex $originalVertex)
public function createGraphCloneEdgeless()
{
$graph = new Graph();
// $graph->setLayout($this->getLayout());
$graph->getLayout()->setLayout($this->getLayout());
// TODO: set additional graph attributes
foreach ($this->getVertices() as $originalVertex) {
$vertex = $graph->createVertexClone($originalVertex);
Expand Down Expand Up @@ -243,7 +249,7 @@ private function createEdgeCloneInternal(Edge $originalEdge, $ia, $ib)
$newEdge = $a->createEdge($b);
}
// TODO: copy edge attributes
$newEdge->setLayout($originalEdge->getLayout());
$newEdge->getLayout()->setLayout($originalEdge->getLayout());
$newEdge->setWeight($originalEdge->getWeight());
$newEdge->setFlow($originalEdge->getFlow());
$newEdge->setCapacity($originalEdge->getCapacity());
Expand Down Expand Up @@ -505,7 +511,40 @@ public function __toString()
return $this->getExporter()->getOutput($this);
}

public function getLayout(){
return array();
public function getLayout()
{
if ($this->layout === null) {
$this->layout = new Layout();
}

return $this->layout;
}

/**
* get the global default layout for all vertices
*
* @return Layout
*/
public function getLayoutVertexDefault()
{
if ($this->layoutVertex === null) {
$this->layoutVertex = new Layout();
}

return $this->layoutVertex;
}

/**
* get the global default layout for all edges
*
* @return Layout
*/
public function getLayoutEdgeDefault()
{
if ($this->layoutEdge === null) {
$this->layoutEdge = new Layout();
}

return $this->layoutEdge;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

namespace Fhaculty\Graph;
namespace Fhaculty\Graph\Renderer;

use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Algorithm\Directed;
use Fhaculty\Graph\Algorithm\Groups;
use Fhaculty\Graph\Algorithm\Degree;
use Fhaculty\Graph\Exception\UnexpectedValueException;
use Fhaculty\Graph\Exception\InvalidArgumentException;
use Fhaculty\Graph\Edge\Base as Edge;
use \stdClass;
use Fhaculty\Graph\Vertex;
use Fhaculty\Graph\Edge\Base as Edge;


class GraphViz
{
Expand All @@ -26,9 +29,6 @@ class GraphViz
*/
private $format = 'png';

private $layoutVertex = array();
private $layoutEdge = array();

/**
* Either the name of full path to GraphViz layout.
*
Expand Down Expand Up @@ -147,50 +147,6 @@ public function display()
// echo "... done\n";
}

const LAYOUT_GRAPH = 1;
const LAYOUT_EDGE = 2;
const LAYOUT_VERTEX = 3;

private function mergeLayout(&$old, $new)
{
if ($new === NULL) {
$old = array();
} else {
foreach ($new as $key => $value) {
if ($value === NULL) {
unset($old[$key]);
} else {
$old[$key] = $value;
}
}
}
}

public function setLayout($where, $layout, $value = NULL)
{
if (!is_array($where)) {
$where = array($where);
}
if (func_num_args() > 2) {
$layout = array($layout => $value);
}
foreach ($where as $where) {
if ($where === self::LAYOUT_GRAPH) {
$this->graph->setLayout($layout, $value);
} elseif ($where === self::LAYOUT_EDGE) {
$this->mergeLayout($this->layoutEdge, $layout);
} elseif ($where === self::LAYOUT_VERTEX) {
$this->mergeLayout($this->layoutVertex, $layout);
} else {
throw new InvalidArgumentException('Invalid layout identifier');
}
}

return $this;
}

// end

/**
* create image file data contents for this graph
*
Expand Down Expand Up @@ -285,15 +241,19 @@ public function createScript()
$script = ($directed ? 'di':'') . 'graph G {' . self::EOL;

// add global attributes
$layout = $this->graph->getLayout();
$layout = $this->graph->getLayout()->getAttributes();
if ($layout) {
$script .= $this->formatIndent . 'graph ' . $this->escapeAttributes($layout) . self::EOL;
}
if ($this->layoutVertex) {
$script .= $this->formatIndent . 'node ' . $this->escapeAttributes($this->layoutVertex) . self::EOL;

$layout = $this->graph->getLayoutVertexDefault()->getAttributes();
if ($layout) {
$script .= $this->formatIndent . 'node ' . $this->escapeAttributes($layout) . self::EOL;
}
if ($this->layoutEdge) {
$script .= $this->formatIndent . 'edge ' . $this->escapeAttributes($this->layoutEdge) . self::EOL;

$layout = $this->graph->getLayoutEdgeDefault()->getAttributes();
if ($layout) {
$script .= $this->formatIndent . 'edge ' . $this->escapeAttributes($layout) . self::EOL;
}

$alg = new Groups($this->graph);
Expand Down Expand Up @@ -428,7 +388,7 @@ public static function raw($string)

protected function getLayoutVertex(Vertex $vertex)
{
$layout = $vertex->getLayout();
$layout = $vertex->getLayout()->getAttributes();

$balance = $vertex->getBalance();
if($balance !== NULL){
Expand All @@ -446,7 +406,7 @@ protected function getLayoutVertex(Vertex $vertex)

protected function getLayoutEdge(Edge $edge)
{
$layout = $edge->getLayout();
$layout = $edge->getLayout()->getAttributes();

// use flow/capacity/weight as edge label
$label = NULL;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

namespace Fhaculty\Graph;
namespace Fhaculty\Graph\Renderer;

use Fhaculty\Graph\Exception\OutOfBoundsException;
use Fhaculty\Graph\LayoutableInterface;

abstract class Layoutable
class Layout
{
/**
* associative array of layout settings
Expand All @@ -18,7 +19,7 @@ abstract class Layoutable
*
* @return array
*/
public function getLayout()
public function getAttributes()
{
return $this->layout;
}
Expand All @@ -28,9 +29,9 @@ public function getLayout()
*
* @param array $attributes
* @return self $this (chainable)
* @see Layoutable::setLayoutAttribute()
* @see Layoutable::setAttribute()
*/
public function setLayout(array $attributes)
public function setAttributes(array $attributes)
{
foreach ($attributes as $key => $value) {
if ($value === NULL) {
Expand All @@ -44,14 +45,14 @@ public function setLayout(array $attributes)
}

/**
* set a single layouto attribute
* set a single layout attribute
*
* @param string $name
* @param string $value
* @return self
* @see Layoutable::setLayout()
* @see Layoutable::setAttributes()
*/
public function setLayoutAttribute($name, $value)
public function setAttribute($name, $value)
{
if ($value === NULL) {
unset($this->layout[$name]);
Expand All @@ -68,17 +69,24 @@ public function setLayoutAttribute($name, $value)
* @param string $name
* @return boolean
*/
public function hasLayoutAttribute($name)
public function hasAttribute($name)
{
return isset($this->layout[$name]);
}

public function getLayoutAttribute($name)
public function getAttribute($name)
{
if (!isset($this->layout[$name])) {
throw new OutOfBoundsException('Given layout attribute is not set');
}

return $this->layout[$name];
}

public function setLayout(self $layout)
{
$this->layout = $layout->getAttributes();

return $this;
}
}
14 changes: 14 additions & 0 deletions lib/Fhaculty/Graph/Renderer/LayoutAggregate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Fhaculty\Graph\Renderer;

use Fhaculty\Graph\Exception\OutOfBoundsException;

interface LayoutAggregate
{
/**
*
* @return Layout
*/
public function getLayout();
}
Loading