1
0
Fork 0

Collect solver problems in Problem objects with human readable output.

pull/472/head
Nils Adermann 2012-03-18 20:41:10 +01:00
parent 2249dd0548
commit 2c87fe5a22
6 changed files with 230 additions and 86 deletions

View File

@ -0,0 +1,149 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\DependencyResolver;
/**
* Represents a problem detected while solving dependencies
*
* @author Nils Adermann <naderman@naderman.de>
*/
class Problem
{
/**
* A set of reasons for the problem, each is a rule or a job and a rule
* @var array
*/
protected $reasons;
/**
* Add a job as a reason
*
* @param array $job A job descriptor which is a reason for this problem
* @param Rule $rule An optional rule associated with the job
*/
public function addJobRule($job, Rule $rule = null)
{
$this->addReason(serialize($job), array(
'rule' => $rule,
'job' => $job,
));
}
/**
* Add a rule as a reason
*
* @param Rule $rule A rule which is a reason for this problem
*/
public function addRule(Rule $rule)
{
$this->addReason($rule->getId(), array(
'rule' => $rule,
'job' => null,
));
}
/**
* Retrieve all reasons for this problem
*
* @return array The problem's reasons
*/
public function getReasons()
{
return $this->reasons;
}
/**
* A human readable textual representation of the problem's reasons
*/
public function __toString()
{
if (count($this->reasons) === 1) {
reset($this->reasons);
$reason = current($this->reasons);
$rule = $reason['rule'];
$job = $reason['job'];
if ($job && $job['cmd'] === 'install' && empty($job['packages'])) {
return 'The requested package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'could not be found.';
}
}
$messages = array("Problem caused by:");
foreach ($this->reasons as $reason) {
$rule = $reason['rule'];
$job = $reason['job'];
if ($job) {
$messages[] = $this->jobToText($job);
} elseif ($rule) {
if ($rule instanceof Rule) {
$message = '';
if ($rule->getType() == RuleSet::TYPE_LEARNED) {
$message .= 'learned:';
}
$messages[] = $message.$rule;
}
}
}
return implode("\n\t\t\t- ", $messages);
}
/**
* Store a reason descriptor but ignore duplicates
*
* @param string $id A canonical identifier for the reason
* @param string $reason The reason descriptor
*/
protected function addReason($id, $reason)
{
if (!isset($this->reasons[$id])) {
$this->reasons[$id] = $reason;
}
}
/**
* Turns a job into a human readable description
*
* @param array $job
* @return string
*/
protected function jobToText($job)
{
switch ($job['cmd']) {
case 'install':
return 'Installation of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested. Satisfiable by packages ['.implode(', ', $job['packages']).'].';
case 'update':
return 'Update of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested.';
case 'remove':
return 'Removal of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested.';
}
return 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.implode(', ', $job['packages']).'])';
}
/**
* Turns a constraint into text usable in a sentence describing a job
*
* @param LinkConstraint $constraint
* @return string
*/
protected function constraintToText($constraint)
{
return ($constraint) ? 'with constraint '.$constraint.' ' : '';
}
}

View File

@ -52,6 +52,7 @@ class Request
'packages' => $packages,
'cmd' => $cmd,
'packageName' => $packageName,
'constraint' => $constraint,
);
}

View File

@ -466,24 +466,29 @@ class Solver
$conflict = $this->findDecisionRule($literal->getPackage());
/** TODO: handle conflict with systemsolvable? */
$this->learnedPool[] = array($rule, $conflict);
if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
if ($rule->getType() == RuleSet::TYPE_JOB) {
$why = $this->ruleToJob[$rule->getId()];
} else {
$why = $rule;
}
$this->problems[] = array($why);
$problem = new Problem;
$this->disableProblem($why);
if ($rule->getType() == RuleSet::TYPE_JOB) {
$job = $this->ruleToJob[$rule->getId()];
$problem->addJobRule($job, $rule);
$problem->addRule($conflict);
$this->disableProblem($job);
} else {
$problem->addRule($rule);
$problem->addRule($conflict);
$this->disableProblem($rule);
}
$this->problems[] = $problem;
continue;
}
// conflict with another job or update/feature rule
$this->problems[] = array();
$problem = new Problem;
$problem->addRule($rule);
$problem->addRule($conflict);
// push all of our rules (can only be feature or job rules)
// asserting this literal on the problem stack
@ -500,14 +505,16 @@ class Solver
}
if ($assertRule->getType() === RuleSet::TYPE_JOB) {
$why = $this->ruleToJob[$assertRule->getId()];
} else {
$why = $assertRule;
}
$this->problems[count($this->problems) - 1][] = $why;
$job = $this->ruleToJob[$assertRule->getId()];
$this->disableProblem($why);
$problem->addJobRule($job, $assertRule);
$this->disableProblem($job);
} else {
$problem->addRule($assertRule);
$this->disableProblem($assertRule);
}
}
$this->problems[] = $problem;
// start over
while (count($this->decisionQueue) > $decisionStart) {
@ -977,7 +984,9 @@ class Solver
switch ($job['cmd']) {
case 'install':
if (empty($job['packages'])) {
$this->problems[] = array($job);
$problem = new Problem();
$problem->addJobRule($job);
$this->problems[] = $problem;
} else {
$rule = $this->createInstallOneOfRule($job['packages'], self::RULE_JOB_INSTALL, $job['packageName']);
$this->addRule(RuleSet::TYPE_JOB, $rule);
@ -1028,7 +1037,7 @@ class Solver
//solver_prepare_solutions(solv);
if ($this->problems) {
throw new SolverProblemsException($this->problems, $this->learnedPool);
throw new SolverProblemsException($this->problems);
}
return $this->createTransaction();
@ -1492,17 +1501,16 @@ class Solver
return array($ruleLevel, $newRule, $why);
}
private function analyzeUnsolvableRule($conflictRule, &$lastWeakWhy)
private function analyzeUnsolvableRule($problem, $conflictRule, &$lastWeakWhy)
{
$why = $conflictRule->getId();
if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) {
$learnedWhy = $this->learnedWhy[$why];
$problem = $this->learnedPool[$learnedWhy];
$problemRules = $this->learnedPool[$learnedWhy];
foreach ($problem as $problemRule) {
$this->analyzeUnsolvableRule($problemRule, $lastWeakWhy);
foreach ($problemRules as $problemRule) {
$this->analyzeUnsolvableRule($problem, $problemRule, $lastWeakWhy);
}
return;
}
@ -1520,24 +1528,22 @@ class Solver
}
if ($conflictRule->getType() == RuleSet::TYPE_JOB) {
$why = $this->ruleToJob[$conflictRule->getId()];
$job = $this->ruleToJob[$conflictRule->getId()];
$problem->addJobRule($job, $conflictRule);
} else {
$problem->addRule($conflictRule);
}
// if this problem was already found skip it
if (in_array($why, $this->problems[count($this->problems) - 1], true)) {
return;
}
$this->problems[count($this->problems) - 1][] = $why;
}
private function analyzeUnsolvable($conflictRule, $disableRules)
{
$lastWeakWhy = null;
$this->problems[] = array();
$this->learnedPool[] = array($conflictRule);
$problem = new Problem;
$problem->addRule($conflictRule);
$this->analyzeUnsolvableRule($conflictRule, $lastWeakWhy);
$this->analyzeUnsolvableRule($problem, $conflictRule, $lastWeakWhy);
$this->problems[] = $problem;
$seen = array();
$literals = $conflictRule->getLiterals();
@ -1569,9 +1575,9 @@ class Solver
}
$why = $this->decisionQueueWhy[$decisionId];
$this->learnedPool[count($this->learnedPool) - 1][] = $why;
$problem->addRule($why);
$this->analyzeUnsolvableRule($why, $lastWeakWhy);
$this->analyzeUnsolvableRule($problem, $why, $lastWeakWhy);
$literals = $why->getLiterals();
/* unnecessary because unlike rule.d, watch2 == 2nd literal, unless watch2 changed
@ -1591,7 +1597,6 @@ class Solver
if ($lastWeakWhy) {
array_pop($this->problems);
array_pop($this->learnedPool);
if ($lastWeakWhy->getType() === RuleSet::TYPE_JOB) {
$why = $this->ruleToJob[$lastWeakWhy];
@ -1616,8 +1621,12 @@ class Solver
}
if ($disableRules) {
foreach ($this->problems[count($this->problems) - 1] as $why) {
$this->disableProblem($why);
foreach ($this->problems[count($this->problems) - 1] as $reason) {
if ($reason['job']) {
$this->disableProblem($reason['job']);
} else {
$this->disableProblem($reason['rule']);
}
}
$this->resetSolver();
@ -1670,10 +1679,10 @@ class Solver
{
foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) {
$why = $this->learnedWhy[$rule->getId()];
$problem = $this->learnedPool[$why];
$problemRules = $this->learnedPool[$why];
$foundDisabled = false;
foreach ($problem as $problemRule) {
foreach ($problemRules as $problemRule) {
if ($problemRule->disabled()) {
$foundDisabled = true;
break;

View File

@ -19,47 +19,26 @@ class SolverProblemsException extends \RuntimeException
{
protected $problems;
public function __construct(array $problems, array $learnedPool)
public function __construct(array $problems)
{
$message = '';
foreach ($problems as $i => $problem) {
$message .= '[';
foreach ($problem as $why) {
$this->problems = $problems;
if (is_int($why) && isset($learnedPool[$why])) {
$rules = $learnedPool[$why];
} else {
$rules = $why;
}
parent::__construct($this->createMessage());
}
if (isset($rules['packages'])) {
$message .= $this->jobToText($rules);
} else {
$message .= '(';
foreach ($rules as $rule) {
if ($rule instanceof Rule) {
if ($rule->getType() == RuleSet::TYPE_LEARNED) {
$message .= 'learned: ';
}
$message .= $rule . ', ';
} else {
$message .= 'String(' . $rule . '), ';
}
}
$message .= ')';
}
$message .= ', ';
}
$message .= "]\n";
protected function createMessage()
{
$messages = array();
foreach ($this->problems as $problem) {
$messages[] = (string) $problem;
}
parent::__construct($message);
return "\n\tProblems:\n\t\t- ".implode("\n\t\t- ", $messages);
}
public function jobToText($job)
public function getProblems()
{
//$output = serialize($job);
$output = 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.implode(', ', $job['packages']).'])';
return $output;
return $this->problems;
}
}

View File

@ -40,9 +40,9 @@ class RequestTest extends TestCase
$this->assertEquals(
array(
array('packages' => array($foo), 'cmd' => 'install', 'packageName' => 'foo'),
array('packages' => array($bar), 'cmd' => 'install', 'packageName' => 'bar'),
array('packages' => array($foobar), 'cmd' => 'remove', 'packageName' => 'foobar'),
array('packages' => array($foo), 'cmd' => 'install', 'packageName' => 'foo', 'constraint' => null),
array('packages' => array($bar), 'cmd' => 'install', 'packageName' => 'bar', 'constraint' => null),
array('packages' => array($foobar), 'cmd' => 'remove', 'packageName' => 'foobar', 'constraint' => null),
),
$request->getJobs());
}
@ -63,11 +63,11 @@ class RequestTest extends TestCase
$pool->addRepository($repo2);
$request = new Request($pool);
$request->install('foo', $this->getVersionConstraint('=', '1'));
$request->install('foo', $constraint = $this->getVersionConstraint('=', '1'));
$this->assertEquals(
array(
array('packages' => array($foo1, $foo2), 'cmd' => 'install', 'packageName' => 'foo'),
array('packages' => array($foo1, $foo2), 'cmd' => 'install', 'packageName' => 'foo', 'constraint' => $constraint),
),
$request->getJobs()
);

View File

@ -59,13 +59,15 @@ class SolverTest extends TestCase
$this->repo->addPackage($this->getPackage('A', '1.0'));
$this->reposComplete();
$this->request->install('B');
$this->request->install('B', $this->getVersionConstraint('=', '1'));
try {
$transaction = $this->solver->solve($this->request);
$this->fail('Unsolvable conflict did not resolve in exception.');
$this->fail('Unsolvable conflict did not result in exception.');
} catch (SolverProblemsException $e) {
// TODO assert problem properties
$problems = $e->getProblems();
$this->assertEquals(1, count($problems));
$this->assertEquals('The requested package "b" with constraint == 1.0.0.0 could not be found.', (string) $problems[0]);
}
}
@ -589,8 +591,10 @@ class SolverTest extends TestCase
try {
$transaction = $this->solver->solve($this->request);
$this->fail('Unsolvable conflict did not resolve in exception.');
$this->fail('Unsolvable conflict did not result in exception.');
} catch (SolverProblemsException $e) {
$problems = $e->getProblems();
$this->assertEquals(1, count($problems));
// TODO assert problem properties
}
}
@ -610,8 +614,10 @@ class SolverTest extends TestCase
try {
$transaction = $this->solver->solve($this->request);
$this->fail('Unsolvable conflict did not resolve in exception.');
$this->fail('Unsolvable conflict did not result in exception.');
} catch (SolverProblemsException $e) {
$problems = $e->getProblems();
$this->assertEquals(1, count($problems));
// TODO assert problem properties
}
}