classTerminationCost
final class TerminationCost
Answers the least a rule can cost to finish.
A walk that has to stop soon needs to know which alternative gets there first, and that depends on every rule below it. The answer is found by assuming nothing terminates and then relaxing: each pass takes the cheapest alternative of each rule under the current answers, and the passes repeat until none of them changes anything. A rule that never settles below the sentinel can only expand into itself and so can never be finished at all.
The same relaxation answers two different questions depending on what a step is counted as: tokens written, or rules expanded. What each costs is given to the constructor rather than written twice.
Methods§
public function __construct(
Grammar $grammar,
private Closure(string): bool $terminalSupported,
private int $perTerminal,
private int $perExpansion,
)Parameters
$grammar | Grammar | Grammar whose rules are being costed |
$terminalSupported | Closure(string): bool | Answers whether a terminal can be written at all |
$perTerminal | int | What one written terminal costs |
$perExpansion | int | What expanding one rule costs |
Test cases 8
TerminationCostTest::testOfCountsARuleThatCanOnlyExpandIntoItselfAsUnfinishablecoversTerminationCostTest::testOfCountsTheTokensOfTheCheapestAlternativecoversTerminationCostTest::testOfProductionAddsUpWhatEachSymbolCostscoversTerminationCostTest::testOfProductionCountsExpansionsWhenThatIsWhatAStepIscoversTerminationCostTest::testOfRefusesATokenNothingCanWritecoversTerminationCostTest::testOfTreatsASymbolWithNoRuleAsATokencoversTerminationCostTest::testSettledAnswersTheLeastEveryRuleCanCostcoversTerminationCostTest::testSumRefusesASequenceCarryingATokenNothingCanWritecovers
Calls 1
- method-call TerminationCost::settled() line 47
public function of(string $symbol): intAnswers the least one symbol can cost to finish.
A symbol the grammar declares no rule for is a token, and costs what reaching a token costs — unless nothing can write it, in which case it cannot be finished at all.
Parameters
$symbol | string | Symbol to cost |
Returns
int Least it can cost, or PHP_INT_MAX when it can never be finishedTest cases 4
TerminationCostTest::testOfCountsARuleThatCanOnlyExpandIntoItselfAsUnfinishablecovers and callsTerminationCostTest::testOfCountsTheTokensOfTheCheapestAlternativecovers and callsTerminationCostTest::testOfRefusesATokenNothingCanWritecovers and callsTerminationCostTest::testOfTreatsASymbolWithNoRuleAsATokencovers and calls
public function ofProduction(Production $production): intAnswers the least one production can cost to finish.
Parameters
$production | Production | Production to cost |
Returns
int Least it can cost, or PHP_INT_MAX when any part of it can never be finishedTest cases 2
TerminationCostTest::testOfProductionAddsUpWhatEachSymbolCostscovers and callsTerminationCostTest::testOfProductionCountsExpansionsWhenThatIsWhatAStepIscovers and calls
Called from 2
- method-call SqlFaker\Generation\Derivation\TerminationAnalyzer::estimateProductionLength() packages/sql-faker/src/Generation/Derivation/TerminationAnalyzer.php:62
- method-call SqlFaker\Generation\Derivation\TerminationAnalyzer::estimateProductionSteps() packages/sql-faker/src/Generation/Derivation/TerminationAnalyzer.php:74
Calls 1
- method-call TerminationCost::sum() line 75
public function settled(Grammar $grammar): array<string, int>Relaxes every rule's cost until no pass changes one.
Parameters
$grammar | Grammar | Grammar whose rules are being costed |
Returns
array<string, int> Rule name => least it can cost to finishTest cases 8
TerminationCostTest::testOfCountsARuleThatCanOnlyExpandIntoItselfAsUnfinishablecoversTerminationCostTest::testOfCountsTheTokensOfTheCheapestAlternativecoversTerminationCostTest::testOfProductionAddsUpWhatEachSymbolCostscoversTerminationCostTest::testOfProductionCountsExpansionsWhenThatIsWhatAStepIscoversTerminationCostTest::testOfRefusesATokenNothingCanWritecoversTerminationCostTest::testOfTreatsASymbolWithNoRuleAsATokencoversTerminationCostTest::testSettledAnswersTheLeastEveryRuleCanCostcovers and callsTerminationCostTest::testSumRefusesASequenceCarryingATokenNothingCanWritecovers
Called from 1
Calls 3
- function-call
array_fill_keysline 87 - function-call
array_keysline 87 - method-call TerminationCost::sum() line 95
public function sum(list<Symbol> $symbols, array<string, int> $minimum): intAdds up what a sequence of symbols costs under the answers found so far.
Parameters
$symbols | list<Symbol> | Symbols to cost |
$minimum | array<string, int> | Answers found so far |
Returns
int Least the sequence can cost, or PHP_INT_MAX when any part of it can never be finishedTest cases 8
TerminationCostTest::testOfCountsARuleThatCanOnlyExpandIntoItselfAsUnfinishablecoversTerminationCostTest::testOfCountsTheTokensOfTheCheapestAlternativecoversTerminationCostTest::testOfProductionAddsUpWhatEachSymbolCostscoversTerminationCostTest::testOfProductionCountsExpansionsWhenThatIsWhatAStepIscoversTerminationCostTest::testOfRefusesATokenNothingCanWritecoversTerminationCostTest::testOfTreatsASymbolWithNoRuleAsATokencoversTerminationCostTest::testSettledAnswersTheLeastEveryRuleCanCostcoversTerminationCostTest::testSumRefusesASequenceCarryingATokenNothingCanWritecovers and calls
Private surface 4§
Implementation details, listed for orientation only.
private array<string, int> $minimumprivate Closure(string): bool $terminalSupportedprivate int $perTerminalprivate int $perExpansionTest cases 25§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 8
TerminationCostTest::testOfCountsARuleThatCanOnlyExpandIntoItselfAsUnfinishablecovers and callsTerminationCostTest::testOfCountsTheTokensOfTheCheapestAlternativecovers and callsTerminationCostTest::testOfProductionAddsUpWhatEachSymbolCostscovers and callsTerminationCostTest::testOfProductionCountsExpansionsWhenThatIsWhatAStepIscovers and callsTerminationCostTest::testOfRefusesATokenNothingCanWritecovers and callsTerminationCostTest::testOfTreatsASymbolWithNoRuleAsATokencovers and callsTerminationCostTest::testSettledAnswersTheLeastEveryRuleCanCostcovers and callsTerminationCostTest::testSumRefusesASequenceCarryingATokenNothingCanWritecovers and calls
Other tests reaching this symbol 17
BytePlanCompilerTestcallsPlanBuilderTestcallsDerivationTestcallsDerivationTraceTestcallsTerminationAnalyzerTestcallsTokenGeneratorTestcallsLexemeSequenceTestcallsCandidateResolverTestcallsSqlGeneratorTestcallsTerminalSequenceTestcallsGenerationContextTestcallsMySqlProviderTestcallsGenerationContextTestcallsPostgreSqlProviderTestcallsSqlGeneratorFactoryTestcallsGenerationContextTestcallsSqliteProviderTestcalls
Relations§
Instantiated in 2
Method calls 3
- method-call SqlFaker\Generation\Derivation\TerminationAnalyzer::getMinLength() packages/sql-faker/src/Generation/Derivation/TerminationAnalyzer.php:50
- method-call SqlFaker\Generation\Derivation\TerminationAnalyzer::estimateProductionLength() packages/sql-faker/src/Generation/Derivation/TerminationAnalyzer.php:62
- method-call SqlFaker\Generation\Derivation\TerminationAnalyzer::estimateProductionSteps() packages/sql-faker/src/Generation/Derivation/TerminationAnalyzer.php:74