diff --git a/src/Edge.php b/src/Edge.php index fe1452d9..968542b9 100644 --- a/src/Edge.php +++ b/src/Edge.php @@ -7,7 +7,6 @@ use Graphp\Graph\Exception\BadMethodCallException; use Graphp\Graph\Exception\InvalidArgumentException; use Graphp\Graph\Exception\LogicException; -use Graphp\Graph\Exception\RangeException; use Graphp\Graph\Set\Vertices; use Graphp\Graph\Set\VerticesAggregate; @@ -19,30 +18,6 @@ */ abstract class Edge implements VerticesAggregate, AttributeAware { - /** - * weight of this edge - * - * @var float|int|NULL - * @see self::getWeight() - */ - protected $weight = NULL; - - /** - * maximum capacity (maximum flow) - * - * @var float|int|NULL - * @see self::getCapacity() - */ - protected $capacity = NULL; - - /** - * flow (capacity currently in use) - * - * @var float|int|NULL - * @see self::getFlow() - */ - protected $flow = NULL; - protected $attributes = array(); /** @@ -106,119 +81,6 @@ abstract public function getVertexToFrom(Vertex $startVertex); */ abstract public function getVertexFromTo(Vertex $endVertex); - /** - * return weight of edge - * - * @return float|int|NULL numeric weight of edge or NULL=not set - */ - public function getWeight() - { - return $this->weight; - } - - /** - * set new weight for edge - * - * @param float|int|NULL $weight new numeric weight of edge or NULL=unset weight - * @return self $this (chainable) - * @throws InvalidArgumentException if given weight is not numeric - */ - public function setWeight($weight) - { - if ($weight !== NULL && !is_float($weight) && !is_int($weight)) { - throw new InvalidArgumentException('Invalid weight given - must be numeric or NULL'); - } - $this->weight = $weight; - - return $this; - } - - /** - * get total capacity of this edge - * - * @return float|int|NULL numeric capacity or NULL=not set - */ - public function getCapacity() - { - return $this->capacity; - } - - /** - * get the capacity remaining (total capacity - current flow) - * - * @return float|int|NULL numeric capacity remaining or NULL=no upper capacity set - */ - public function getCapacityRemaining() - { - if ($this->capacity === NULL) { - return NULL; - } - - return $this->capacity - $this->flow; - } - - /** - * set new total capacity of this edge - * - * @param float|int|NULL $capacity - * @return self $this (chainable) - * @throws InvalidArgumentException if $capacity is invalid (not numeric or negative) - * @throws RangeException if current flow exceeds new capacity - */ - public function setCapacity($capacity) - { - if ($capacity !== NULL) { - if (!is_float($capacity) && !is_int($capacity)) { - throw new InvalidArgumentException('Invalid capacity given - must be numeric'); - } - if ($capacity < 0) { - throw new InvalidArgumentException('Capacity must not be negative'); - } - if ($this->flow !== NULL && $this->flow > $capacity) { - throw new RangeException('Current flow of ' . $this->flow . ' exceeds new capacity'); - } - } - $this->capacity = $capacity; - - return $this; - } - - /** - * get current flow (capacity currently in use) - * - * @return float|int|NULL numeric flow or NULL=not set - */ - public function getFlow() - { - return $this->flow; - } - - /** - * set new total flow (capacity currently in use) - * - * @param float|int|NULL $flow - * @return self $this (chainable) - * @throws InvalidArgumentException if $flow is invalid (not numeric or negative) - * @throws RangeException if flow exceeds current maximum capacity - */ - public function setFlow($flow) - { - if ($flow !== NULL) { - if (!is_float($flow) && !is_int($flow)) { - throw new InvalidArgumentException('Invalid flow given - must be numeric'); - } - if ($flow < 0) { - throw new InvalidArgumentException('Flow must not be negative'); - } - if ($this->capacity !== NULL && $flow > $this->capacity) { - throw new RangeException('New flow exceeds maximum capacity'); - } - } - $this->flow = $flow; - - return $this; - } - /** * get set of all Vertices this edge connects * diff --git a/src/Graph.php b/src/Graph.php index a788486b..f7e1f470 100644 --- a/src/Graph.php +++ b/src/Graph.php @@ -87,11 +87,9 @@ public function createVertexClone(Vertex $originalVertex) if ($this->vertices->hasVertexId($id)) { throw new RuntimeException('Id of cloned vertex already exists'); } + $newVertex = new Vertex($this, $id); - // TODO: properly set attributes of vertex $newVertex->getAttributeBag()->setAttributes($originalVertex->getAttributeBag()->getAttributes()); - $newVertex->setBalance($originalVertex->getBalance()); - $newVertex->setGroup($originalVertex->getGroup()); return $newVertex; } @@ -106,11 +104,10 @@ public function createVertexClone(Vertex $originalVertex) public function createGraphCloneEdgeless() { $graph = new Graph(); - $graph->getAttributeBag()->setAttributes($this->getAttributeBag()->getAttributes()); - // TODO: set additional graph attributes + $graph->attributes = $this->attributes; + foreach ($this->getVertices() as $originalVertex) { $graph->createVertexClone($originalVertex); - // $graph->vertices[$vid] = $vertex; } return $graph; @@ -258,11 +255,8 @@ private function createEdgeCloneInternal(Edge $originalEdge, $ia, $ib) // create new edge between new a and b $newEdge = $this->createEdgeUndirected($a, $b); } - // TODO: copy edge attributes + $newEdge->getAttributeBag()->setAttributes($originalEdge->getAttributeBag()->getAttributes()); - $newEdge->setWeight($originalEdge->getWeight()); - $newEdge->setFlow($originalEdge->getFlow()); - $newEdge->setCapacity($originalEdge->getCapacity()); return $newEdge; } diff --git a/src/Vertex.php b/src/Vertex.php index d4b5ccb7..3298f99d 100644 --- a/src/Vertex.php +++ b/src/Vertex.php @@ -24,22 +24,6 @@ class Vertex implements EdgesAggregate, AttributeAware */ private $graph; - /** - * vertex balance - * - * @var int|float|NULL - * @see Vertex::setBalance() - */ - private $balance; - - /** - * group number - * - * @var int - * @see Vertex::setGroup() - */ - private $group = 0; - private $attributes = array(); /** @@ -71,48 +55,6 @@ public function getGraph() return $this->graph; } - public function getBalance() - { - return $this->balance; - } - - public function setBalance($balance) - { - if ($balance !== NULL && !is_float($balance) && !is_int($balance)) { - throw new InvalidArgumentException('Invalid balance given - must be numeric'); - } - $this->balance = $balance; - - return $this; - } - - /** - * set group number of this vertex - * - * @param int $group - * @return Vertex $this (chainable) - * @throws InvalidArgumentException if group is not numeric - */ - public function setGroup($group) - { - if (!is_int($group)) { - throw new InvalidArgumentException('Invalid group number'); - } - $this->group = $group; - - return $this; - } - - /** - * get group number - * - * @return int - */ - public function getGroup() - { - return $this->group; - } - /** * returns id of this Vertex * diff --git a/tests/EdgeAttributesTest.php b/tests/EdgeAttributesTest.php deleted file mode 100644 index 53d6ed7e..00000000 --- a/tests/EdgeAttributesTest.php +++ /dev/null @@ -1,98 +0,0 @@ -createVertex(1); - $graph->createVertex(2); - - // 1 -> 2 - $this->edge = $graph->createEdgeUndirected($graph->getVertex(1), $graph->getVertex(2)); - } - - public function testCanSetFlowAndCapacity() - { - $this->edge->setCapacity(100); - $this->edge->setFlow(10); - - $this->assertEquals(90, $this->edge->getCapacityRemaining()); - } - - public function testCanSetFlowBeforeCapacity() - { - $this->edge->setFlow(20); - - $this->assertEquals(null, $this->edge->getCapacityRemaining()); - } - - /** - * @expectedException RangeException - */ - public function testFlowMustNotExceedCapacity() - { - $this->edge->setCapacity(20); - $this->edge->setFlow(100); - } - - /** - * @expectedException RangeException - */ - public function testCapacityMustBeGreaterThanFlow() - { - $this->edge->setFlow(100); - $this->edge->setCapacity(20); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testWeightMustBeNumeric() - { - $this->edge->setWeight("10"); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testCapacityMustBeNumeric() - { - $this->edge->setCapacity("10"); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testCapacityMustBePositive() - { - $this->edge->setCapacity(-10); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testFlowMustBeNumeric() - { - $this->edge->setFlow("10"); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testFlowMustBePositive() - { - $this->edge->setFlow(-10); - } -} diff --git a/tests/GraphTest.php b/tests/GraphTest.php index 98eb46b8..f62da19d 100644 --- a/tests/GraphTest.php +++ b/tests/GraphTest.php @@ -17,7 +17,9 @@ public function setup() public function testVertexClone() { $graph = new Graph(); - $vertex = $graph->createVertex(123)->setBalance(10)->setGroup(4); + $vertex = $graph->createVertex(123); + $vertex->setAttribute('balance', 29); + $vertex->setAttribute('group', 4); $newgraph = new Graph(); $newvertex = $newgraph->createVertexClone($vertex); @@ -56,7 +58,11 @@ public function testGetVertexNonexistant() public function testGraphClone() { $graph = new Graph(); - $graph->createVertex(123)->setBalance(10)->setGroup(4); + $graph->setAttribute('title', 'graph'); + + $vertex = $graph->createVertex(123); + $vertex->setAttribute('balanace', 10); + $vertex->setAttribute('group', 4); $newgraph = $graph->createGraphClone(); diff --git a/tests/Set/BaseVerticesTest.php b/tests/Set/BaseVerticesTest.php index 4525eb00..e1efee94 100644 --- a/tests/Set/BaseVerticesTest.php +++ b/tests/Set/BaseVerticesTest.php @@ -220,30 +220,29 @@ public function returnTrue(Vertex $vertex) public function testOrderByGroup() { $graph = new Graph(); - $graph->createVertex()->setGroup(1); - $graph->createVertex()->setGroup(100); - $graph->createVertex()->setGroup(5); - $graph->createVertex()->setGroup(100); - $graph->createVertex()->setGroup(100); - $graph->createVertex()->setGroup(2); - $biggest = $graph->createVertex()->setGroup(200); + $graph->createVertex()->setAttribute('group', 1); + $graph->createVertex()->setAttribute('group', 100); + $graph->createVertex()->setAttribute('group', 5); + $graph->createVertex()->setAttribute('group', 100); + $graph->createVertex()->setAttribute('group', 100); + $graph->createVertex()->setAttribute('group', 2); + $biggest = $graph->createVertex()->setAttribute('group', 200); $vertices = $graph->getVertices(); - $verticesOrdered = $vertices->getVerticesOrder(function (Vertex $vertex) { - return $vertex->getGroup(); - }); + $verticesOrdered = $vertices->getVerticesOrder('group'); $this->assertInstanceOf('Graphp\Graph\Set\Vertices', $verticesOrdered); - $this->assertEquals(1, $verticesOrdered->getVertexFirst()->getGroup()); - $this->assertEquals(200, $verticesOrdered->getVertexLast()->getGroup()); + $this->assertEquals(1, $verticesOrdered->getVertexFirst()->getAttribute('group')); + $this->assertEquals(200, $verticesOrdered->getVertexLast()->getAttribute('group')); $this->assertSame($biggest, $verticesOrdered->getVertexLast()); $this->assertSame($biggest, $vertices->getVertexOrder(function (Vertex $vertex) { - return $vertex->getGroup(); + return $vertex->getAttribute('group'); }, true)); + $this->assertSame($biggest, $vertices->getVertexOrder('group', true)); $sumgroups = function(Vertex $vertex) { - return $vertex->getGroup(); + return $vertex->getAttribute('group'); }; $this->assertSame(508, $vertices->getSumCallback($sumgroups)); $this->assertSame(508, $verticesOrdered->getSumCallback($sumgroups)); diff --git a/tests/Set/EdgesTest.php b/tests/Set/EdgesTest.php index c4f03b5d..9ea3b40f 100644 --- a/tests/Set/EdgesTest.php +++ b/tests/Set/EdgesTest.php @@ -10,7 +10,6 @@ class EdgesTest extends TestCase { /** - * * @param array $edges * @return Edges */ @@ -44,7 +43,7 @@ public function testEmpty() $this->assertTrue($edges->isEmpty()); $this->assertTrue($edges->getEdges()->isEmpty()); $this->assertTrue($edges->getEdgesOrder(function (Edge $edge) { - return $edge->getWeight(); + return $edge->getAttribute('weight'); })->isEmpty()); $this->assertTrue($edges->getEdgesOrder('weight')->isEmpty()); $this->assertTrue($edges->getEdgesDistinct()->isEmpty()); @@ -251,30 +250,30 @@ public function testOrderByGroup() $graph = new Graph(); $v1 = $graph->createVertex(1); $v2 = $graph->createVertex(2); - $graph->createEdgeUndirected($v1, $v2)->setWeight(1); - $graph->createEdgeUndirected($v1, $v2)->setWeight(100); - $graph->createEdgeUndirected($v1, $v2)->setWeight(5); - $graph->createEdgeUndirected($v1, $v2)->setWeight(100); - $graph->createEdgeUndirected($v1, $v2)->setWeight(100); - $graph->createEdgeUndirected($v1, $v2)->setWeight(2); - $biggest = $graph->createEdgeUndirected($v1, $v2)->setWeight(200); + $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 1); + $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 100); + $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 5); + $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 100); + $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 100); + $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 2); + $biggest = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 200); $edges = $graph->getEdges(); $edgesOrdered = $edges->getEdgesOrder(function (Edge $edge) { - return $edge->getWeight(); + return $edge->getAttribute('weight'); }); $this->assertInstanceOf('Graphp\Graph\Set\Edges', $edgesOrdered); - $this->assertEquals(1, $edgesOrdered->getEdgeFirst()->getWeight()); - $this->assertEquals(200, $edgesOrdered->getEdgeLast()->getWeight()); + $this->assertEquals(1, $edgesOrdered->getEdgeFirst()->getAttribute('weight')); + $this->assertEquals(200, $edgesOrdered->getEdgeLast()->getAttribute('weight')); $this->assertSame($biggest, $edgesOrdered->getEdgeLast()); $this->assertSame($biggest, $edges->getEdgeOrder(function (Edge $edge) { - return $edge->getWeight(); + return $edge->getAttribute('weight'); }, true)); $sumweights = function(Edge $edge) { - return $edge->getWeight(); + return $edge->getAttribute('weight'); }; $this->assertSame(508, $edges->getSumCallback($sumweights)); $this->assertSame(508, $edgesOrdered->getSumCallback($sumweights)); @@ -295,6 +294,9 @@ public function testOrderByAttribute() $this->assertSame($e1, $edges->getEdgeLast()); $this->assertSame($e1, $edges->getEdgeOrder('weight', true)); + + $this->assertSame(30, $graph->getEdges()->getSumCallback('weight')); + $this->assertSame(30, $edges->getSumCallback('weight')); } public function testIntersection() diff --git a/tests/TestCase.php b/tests/TestCase.php index 15e947bf..c9b2b7d6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -14,6 +14,7 @@ protected function assertGraphEquals(Graph $expected, Graph $actual) { $f = function(Graph $graph){ $ret = get_class($graph); + $ret .= PHP_EOL . 'attributes: ' . json_encode($graph->getAttributeBag()->getAttributes()); $ret .= PHP_EOL . 'vertices: ' . count($graph->getVertices()); $ret .= PHP_EOL . 'edges: ' . count($graph->getEdges()); @@ -72,8 +73,6 @@ private function getVertexDump(Vertex $vertex) $ret .= PHP_EOL . 'id: ' . $vertex->getId(); $ret .= PHP_EOL . 'attributes: ' . json_encode($vertex->getAttributeBag()->getAttributes()); - $ret .= PHP_EOL . 'balance: ' . $vertex->getBalance(); - $ret .= PHP_EOL . 'group: ' . $vertex->getGroup(); return $ret; } @@ -87,9 +86,6 @@ private function getEdgeDump(Edge $edge) $vertices = $edge->getVertices()->getIds(); $ret .= $vertices[0] . ' -- ' . $vertices[1]; } - $ret .= PHP_EOL . 'flow: ' . $edge->getFlow(); - $ret .= PHP_EOL . 'capacity: ' . $edge->getCapacity(); - $ret .= PHP_EOL . 'weight: ' . $edge->getWeight(); $ret .= PHP_EOL . 'attributes: ' . json_encode($edge->getAttributeBag()->getAttributes()); return $ret; diff --git a/tests/VertexTest.php b/tests/VertexTest.php index 108265bb..bde5cea7 100644 --- a/tests/VertexTest.php +++ b/tests/VertexTest.php @@ -104,34 +104,6 @@ public function testDirectedLoopEdgeReturnsEdgeTwiceUndirectedAndOnceEachInAndOu $this->assertEquals(array($this->vertex), $this->vertex->getVerticesEdgeFrom()->getVector()); } - public function testBalance() - { - $this->vertex->setBalance(10); - $this->assertEquals(10, $this->vertex->getBalance()); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testBalanceInvalid() - { - $this->vertex->setBalance("10"); - } - - public function testGroup() - { - $this->vertex->setGroup(2); - $this->assertEquals(2, $this->vertex->getGroup()); - } - - /** - * @expectedException InvalidArgumentException - */ - public function testGroupInvalid() - { - $this->vertex->setGroup("3"); - } - /** * @expectedException InvalidArgumentException */ diff --git a/tests/WalkTest.php b/tests/WalkTest.php index 50a8436f..a58d955b 100644 --- a/tests/WalkTest.php +++ b/tests/WalkTest.php @@ -244,15 +244,15 @@ public function testFactoryFromVertices() $graph = new Graph(); $v1 = $graph->createVertex(1); $v2 = $graph->createVertex(2); - $e1 = $graph->createEdgeUndirected($v1, $v2)->setWeight(10)->setAttribute('weight', 10); - $e2 = $graph->createEdgeUndirected($v1, $v2)->setWeight(20)->setAttribute('weight', 20); + $e1 = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 10); + $e2 = $graph->createEdgeUndirected($v1, $v2)->setAttribute('weight', 20); // any edge in walk $walk = Walk::factoryFromVertices(array($v1, $v2)); // edge with weight 10 $walk = Walk::factoryFromVertices(array($v1, $v2), function (Edge $edge) { - return $edge->getWeight(); + return $edge->getAttribute('weight'); }); $this->assertSame($e1, $walk->getEdges()->getEdgeFirst()); @@ -262,7 +262,7 @@ public function testFactoryFromVertices() // edge with weight 20 $walk = Walk::factoryFromVertices(array($v1, $v2), function (Edge $edge) { - return $edge->getWeight(); + return $edge->getAttribute('weight'); }, true); $this->assertSame($e2, $walk->getEdges()->getEdgeFirst());