Skip to content

Commit

Permalink
Merge pull request #111 from tienvx/clean-up-graph-components
Browse files Browse the repository at this point in the history
Clean up graph components
  • Loading branch information
tienvx committed Dec 13, 2018
2 parents a5d4731 + 6cc8bd4 commit d7c559f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/Graph/Dumper/PlantUmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function dump(string $initialPlaces, Graph $graph, array $options = array
$code = $this->initialize($options);
/** @var Vertex $vertex */
foreach ($graph->getVertices() as $vertex) {
$placeEscaped = $this->escape($vertex->getId());
$placeEscaped = $this->escape(implode(',', json_decode($vertex->getId(), true)));
$code[] = "state $placeEscaped" . ($initialPlaces === $vertex->getId() ? ' '.self::INITIAL : '');
}
/** @var Directed $edge */
foreach ($graph->getEdges() as $edge) {
$fromEscaped = $this->escape($edge->getVertexStart()->getId());
$toEscaped = $this->escape($edge->getVertexEnd()->getId());
$fromEscaped = $this->escape(implode(',', json_decode($edge->getVertexStart()->getId())));
$toEscaped = $this->escape(implode(',', json_decode($edge->getVertexEnd()->getId())));
$transitionEscaped = $this->escape($edge->getAttribute('name'));
$code[] = "$fromEscaped --> $toEscaped: $transitionEscaped";
}
Expand Down
9 changes: 7 additions & 2 deletions src/Service/GraphBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Fhaculty\Graph\Graph;
use Fhaculty\Graph\Vertex;
use Graphp\Algorithms\ConnectedComponents;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;
use Symfony\Component\Workflow\StateMachine;
Expand Down Expand Up @@ -38,6 +39,9 @@ public function build(Workflow $workflow): Graph
$graph = $this->buildForStateMachine($workflow);
} else {
$graph = $this->buildForWorkflow($workflow);
$initVertex = json_encode([$workflow->getDefinition()->getInitialPlace()]);
$components = new ConnectedComponents($graph);
return $components->createGraphComponentVertex($graph->getVertex($initVertex));
}

$this->cache->set('mbt.graph.' . $workflow->getName(), $graph);
Expand Down Expand Up @@ -96,11 +100,12 @@ private function buildForWorkflow(Workflow $workflow)
{
$vertices = $graph->getVertices()->getVerticesMatch(function (Vertex $vertex) use ($froms) {
$places = $vertex->getAttribute('places');
return array_diff($places, $froms) && array_intersect($places, $froms) && !array_diff(array_intersect($places, $froms), $froms);
$intersect = array_intersect($places, $froms);
return array_diff($places, $froms) && count($intersect) === count($froms) && !array_diff($intersect, $froms);
});
foreach ($vertices as $vertex) {
$places = $vertex->getAttribute('places');
$newPlaces = array_replace($places, $froms, $tos);
$newPlaces = array_unique(array_merge(array_diff($places, $froms), $tos));
sort($places);
sort($newPlaces);
$from = json_encode($places);
Expand Down
62 changes: 56 additions & 6 deletions tests/Command/GraphDumpCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,60 @@ class GraphDumpCommandTest extends CommandTestCase
{
public function modelData()
{
$articleDot =
'digraph workflow {
ratio="compress" rankdir="LR" label="Article"
node [fontsize="9" fontname="Arial" color="#333333" fillcolor="lightblue" fixedsize="false" width="1"];
edge [fontsize="9" fontname="Arial" color="#333333" arrowhead="normal" arrowsize="0.5"];
place_5916096c35437dab3f6a120790f2a431656affb5 [label="[\"draft\"]", shape=circle, style="filled"];
place_9e77ae793650b6bd67da69003adb32a9b17eb712 [label="[\"wait_for_journalist\",\"wait_for_spellchecker\"]", shape=circle];
place_429a3f35b9bcae743353aa5f800f0bda20c6c5e7 [label="[\"approved_by_journalist\",\"wait_for_spellchecker\"]", shape=circle];
place_37e58158cff8e991ff736f76ef5256dc8b4ae850 [label="[\"approved_by_spellchecker\",\"wait_for_journalist\"]", shape=circle];
place_309776058fe7a6debc12bc716d2cac6a3167eb2a [label="[\"approved_by_journalist\",\"approved_by_spellchecker\"]", shape=circle];
place_0176512e721e0f9b27d66831090c2408c3c4a040 [label="[\"published\"]", shape=circle];
place_5916096c35437dab3f6a120790f2a431656affb5 -> place_9e77ae793650b6bd67da69003adb32a9b17eb712 [label="request_review" style="solid"];
place_9e77ae793650b6bd67da69003adb32a9b17eb712 -> place_429a3f35b9bcae743353aa5f800f0bda20c6c5e7 [label="journalist_approval" style="solid"];
place_9e77ae793650b6bd67da69003adb32a9b17eb712 -> place_37e58158cff8e991ff736f76ef5256dc8b4ae850 [label="spellchecker_approval" style="solid"];
place_429a3f35b9bcae743353aa5f800f0bda20c6c5e7 -> place_309776058fe7a6debc12bc716d2cac6a3167eb2a [label="spellchecker_approval" style="solid"];
place_309776058fe7a6debc12bc716d2cac6a3167eb2a -> place_0176512e721e0f9b27d66831090c2408c3c4a040 [label="publish" style="solid"];
place_37e58158cff8e991ff736f76ef5256dc8b4ae850 -> place_309776058fe7a6debc12bc716d2cac6a3167eb2a [label="journalist_approval" style="solid"];
}
';
$pullRequestPuml =
'@startuml
allow_mixing
title pull_request
skinparam titleBorderRoundCorner 15
skinparam titleBorderThickness 2
skinparam state {
BackgroundColor<<initial>> #87b741
BackgroundColor<<marked>> #3887C6
BorderColor #3887C6
BorderColor<<marked>> Black
FontColor<<marked>> White
}
state "start" <<initial>>
state "coding"
state "travis"
state "review"
state "merged"
state "closed"
"start" --> "travis": "submit"
"coding" --> "travis": "update"
"travis" --> "travis": "update"
"review" --> "travis": "update"
"travis" --> "review": "wait_for_review"
"review" --> "coding": "request_change"
"review" --> "merged": "accept"
"review" --> "closed": "reject"
"closed" --> "review": "reopen"
@enduml
';
return [
['article', 'Article', 'dot', 'digraph workflow'],
['pull_request', 'Pull Request', 'puml', '@startuml'],
['article', 'Article', 'dot', $articleDot],
['pull_request', 'Pull Request', 'puml', $pullRequestPuml],
];
}

Expand All @@ -19,9 +70,9 @@ public function modelData()
* @param string $model
* @param string $label
* @param string $format
* @param string $contains
* @param string $output
*/
public function testExecute(string $model, string $label, string $format, string $contains)
public function testExecute(string $model, string $label, string $format, string $output)
{
$command = $this->application->find('mbt:graph:dump');

Expand All @@ -33,7 +84,6 @@ public function testExecute(string $model, string $label, string $format, string
'--dump-format' => $format,
]);

$output = $commandTester->getDisplay();
$this->assertContains($contains, $output);
$this->assertEquals($output, $commandTester->getDisplay());
}
}

0 comments on commit d7c559f

Please sign in to comment.