Skip to content

Commit

Permalink
improve graph
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Oct 19, 2023
1 parent 448a4af commit 19dac93
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 17 deletions.
18 changes: 17 additions & 1 deletion src/Interfaces/GraphInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,40 @@
use Chevere\DataStructure\Interfaces\VectorInterface;

/**
* Describes the component in charge of defining jobs dependencies order.
* Describes the component in charge of defining job execution order, where each node contains async jobs.
*
* @extends MappedInterface<VectorInterface<string>>
*/
interface GraphInterface extends MappedInterface, ToArrayInterface
{
/**
* Determines if the graph has the given `$job`.
*/
public function has(string $job): bool;

/**
* Retrieve dependencies for the given `$job`.
*
* @return VectorInterface<string>
*/
public function get(string $job): VectorInterface;

/**
* Determines if the given `$job` has the given `$dependencies`.
*/
public function hasDependencies(string $job, string ...$dependencies): bool;

/**
* Return an instance with the specified `$name` and `$job` put.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified `$name` and `$job` put.
*/
public function withPut(string $name, JobInterface $job): self;

/**
* Returns the graph as an array of arrays, where each array is a node with async jobs.
*
* @return array<int, array<int, string>>
*/
public function toArray(): array;
Expand Down
7 changes: 1 addition & 6 deletions src/Interfaces/JobsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
interface JobsInterface extends MappedInterface
{
public function __construct(JobInterface ...$jobs);

public function has(string $job): bool;

public function get(string $job): JobInterface;
Expand All @@ -46,10 +44,7 @@ public function references(): MapInterface;
*/
public function keys(): array;

/**
* @return array<int, string[]>
*/
public function graph(): array;
public function graph(): GraphInterface;

public function withAdded(JobInterface ...$jobs): self;

Expand Down
4 changes: 2 additions & 2 deletions src/Jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public function __construct(JobInterface ...$jobs)
$this->putAdded(...$jobs);
}

public function graph(): array
public function graph(): GraphInterface
{
return $this->graph->toArray();
return $this->graph;
}

public function variables(): MapInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function withRun(): RunnerInterface
{
$new = clone $this;
$jobs = $new->run->workflow()->jobs();
foreach ($jobs->graph() as $node) {
foreach ($jobs->graph()->toArray() as $node) {
$promises = $new->getPromises($node);
/** @var RunnerInterface[] $responses */
$responses = wait(all($promises));
Expand Down
14 changes: 7 additions & 7 deletions tests/JobsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testAsync(): void
[
['j1', 'j2'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
}

Expand All @@ -97,7 +97,7 @@ public function testSync(): void
['j1'],
['j2'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
}

Expand All @@ -112,7 +112,7 @@ public function testWithDependsOnJob(): void
['j1'],
['j2'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public function testWithDependsOnPreviousMultiple(): void
['j1', 'j2'],
['j3'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
}

Expand All @@ -159,7 +159,7 @@ public function testWithDependsOnPreviousSingle(): void
['j2'],
['j3'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
}

Expand All @@ -182,7 +182,7 @@ public function testWithDependsMix(): void
['j3', 'j5'],
['j6'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
}

Expand Down Expand Up @@ -331,7 +331,7 @@ public function testWithRunIfReference(): void
['j1'],
['j2', 'j3'],
],
$jobs->graph()
$jobs->graph()->toArray()
);
$this->assertTrue(
$jobs->references()->has($true->__toString(), $false->__toString())
Expand Down

0 comments on commit 19dac93

Please sign in to comment.