/usr/share/php/Composer/DependencyResolver/Solver.php is in composer 1.0.0~beta2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | <?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;
use Composer\IO\IOInterface;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\PlatformRepository;
/**
* @author Nils Adermann <naderman@naderman.de>
*/
class Solver
{
const BRANCH_LITERALS = 0;
const BRANCH_LEVEL = 1;
/** @var PolicyInterface */
protected $policy;
/** @var Pool */
protected $pool;
/** @var RepositoryInterface */
protected $installed;
/** @var Ruleset */
protected $rules;
/** @var RuleSetGenerator */
protected $ruleSetGenerator;
/** @var array */
protected $jobs;
/** @var int[] */
protected $updateMap = array();
/** @var RuleWatchGraph */
protected $watchGraph;
/** @var Decisions */
protected $decisions;
/** @var int[] */
protected $installedMap;
/** @var int */
protected $propagateIndex;
/** @var array[] */
protected $branches = array();
/** @var Problem[] */
protected $problems = array();
/** @var array */
protected $learnedPool = array();
/** @var array */
protected $learnedWhy = array();
/** @var IOInterface */
protected $io;
/**
* @param PolicyInterface $policy
* @param Pool $pool
* @param RepositoryInterface $installed
* @param IOInterface $io
*/
public function __construct(PolicyInterface $policy, Pool $pool, RepositoryInterface $installed, IOInterface $io)
{
$this->io = $io;
$this->policy = $policy;
$this->pool = $pool;
$this->installed = $installed;
$this->ruleSetGenerator = new RuleSetGenerator($policy, $pool);
}
/**
* @return int
*/
public function getRuleSetSize()
{
return count($this->rules);
}
// aka solver_makeruledecisions
private function makeAssertionRuleDecisions()
{
$decisionStart = count($this->decisions) - 1;
$rulesCount = count($this->rules);
for ($ruleIndex = 0; $ruleIndex < $rulesCount; $ruleIndex++) {
$rule = $this->rules->ruleById[$ruleIndex];
if (!$rule->isAssertion() || $rule->isDisabled()) {
continue;
}
$literals = $rule->literals;
$literal = $literals[0];
if (!$this->decisions->decided(abs($literal))) {
$this->decisions->decide($literal, 1, $rule);
continue;
}
if ($this->decisions->satisfy($literal)) {
continue;
}
// found a conflict
if (RuleSet::TYPE_LEARNED === $rule->getType()) {
$rule->disable();
continue;
}
$conflict = $this->decisions->decisionRule($literal);
if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
$problem = new Problem($this->pool);
$problem->addRule($rule);
$problem->addRule($conflict);
$this->disableProblem($rule);
$this->problems[] = $problem;
continue;
}
// conflict with another job
$problem = new Problem($this->pool);
$problem->addRule($rule);
$problem->addRule($conflict);
// push all of our rules (can only be job rules)
// asserting this literal on the problem stack
foreach ($this->rules->getIteratorFor(RuleSet::TYPE_JOB) as $assertRule) {
if ($assertRule->isDisabled() || !$assertRule->isAssertion()) {
continue;
}
$assertRuleLiterals = $assertRule->literals;
$assertRuleLiteral = $assertRuleLiterals[0];
if (abs($literal) !== abs($assertRuleLiteral)) {
continue;
}
$problem->addRule($assertRule);
$this->disableProblem($assertRule);
}
$this->problems[] = $problem;
$this->decisions->resetToOffset($decisionStart);
$ruleIndex = -1;
}
}
protected function setupInstalledMap()
{
$this->installedMap = array();
foreach ($this->installed->getPackages() as $package) {
$this->installedMap[$package->id] = $package;
}
}
/**
* @param bool $ignorePlatformReqs
*/
protected function checkForRootRequireProblems($ignorePlatformReqs)
{
foreach ($this->jobs as $job) {
switch ($job['cmd']) {
case 'update':
$packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
foreach ($packages as $package) {
if (isset($this->installedMap[$package->id])) {
$this->updateMap[$package->id] = true;
}
}
break;
case 'update-all':
foreach ($this->installedMap as $package) {
$this->updateMap[$package->id] = true;
}
break;
case 'install':
if ($ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $job['packageName'])) {
break;
}
if (!$this->pool->whatProvides($job['packageName'], $job['constraint'])) {
$problem = new Problem($this->pool);
$problem->addRule(new Rule(array(), null, null, $job));
$this->problems[] = $problem;
}
break;
}
}
}
/**
* @param Request $request
* @param bool $ignorePlatformReqs
* @return array
*/
public function solve(Request $request, $ignorePlatformReqs = false)
{
$this->jobs = $request->getJobs();
$this->setupInstalledMap();
$this->rules = $this->ruleSetGenerator->getRulesFor($this->jobs, $this->installedMap, $ignorePlatformReqs);
$this->checkForRootRequireProblems($ignorePlatformReqs);
$this->decisions = new Decisions($this->pool);
$this->watchGraph = new RuleWatchGraph;
foreach ($this->rules as $rule) {
$this->watchGraph->insert(new RuleWatchNode($rule));
}
/* make decisions based on job/update assertions */
$this->makeAssertionRuleDecisions();
$this->io->writeError('Resolving dependencies through SAT', true, IOInterface::DEBUG);
$before = microtime(true);
$this->runSat(true);
$this->io->writeError(sprintf('Dependency resolution completed in %.3f seconds', microtime(true) - $before), true, IOInterface::VERBOSE);
// decide to remove everything that's installed and undecided
foreach ($this->installedMap as $packageId => $void) {
if ($this->decisions->undecided($packageId)) {
$this->decisions->decide(-$packageId, 1, null);
}
}
if ($this->problems) {
throw new SolverProblemsException($this->problems, $this->installedMap);
}
$transaction = new Transaction($this->policy, $this->pool, $this->installedMap, $this->decisions);
return $transaction->getOperations();
}
/**
* Makes a decision and propagates it to all rules.
*
* Evaluates each term affected by the decision (linked through watches)
* If we find unit rules we make new decisions based on them
*
* @param int $level
* @return Rule|null A rule on conflict, otherwise null.
*/
protected function propagate($level)
{
while ($this->decisions->validOffset($this->propagateIndex)) {
$decision = $this->decisions->atOffset($this->propagateIndex);
$conflict = $this->watchGraph->propagateLiteral(
$decision[Decisions::DECISION_LITERAL],
$level,
$this->decisions
);
$this->propagateIndex++;
if ($conflict) {
return $conflict;
}
}
return null;
}
/**
* Reverts a decision at the given level.
*
* @param int $level
*/
private function revert($level)
{
while (!$this->decisions->isEmpty()) {
$literal = $this->decisions->lastLiteral();
if ($this->decisions->undecided($literal)) {
break;
}
$decisionLevel = $this->decisions->decisionLevel($literal);
if ($decisionLevel <= $level) {
break;
}
$this->decisions->revertLast();
$this->propagateIndex = count($this->decisions);
}
while (!empty($this->branches) && $this->branches[count($this->branches) - 1][self::BRANCH_LEVEL] >= $level) {
array_pop($this->branches);
}
}
/**
* setpropagatelearn
*
* add free decision (a positive literal) to decision queue
* increase level and propagate decision
* return if no conflict.
*
* in conflict case, analyze conflict rule, add resulting
* rule to learnt rule set, make decision from learnt
* rule (always unit) and re-propagate.
*
* returns the new solver level or 0 if unsolvable
*
* @param int $level
* @param string|int $literal
* @param bool $disableRules
* @param Rule $rule
* @return int
*/
private function setPropagateLearn($level, $literal, $disableRules, Rule $rule)
{
$level++;
$this->decisions->decide($literal, $level, $rule);
while (true) {
$rule = $this->propagate($level);
if (!$rule) {
break;
}
if ($level == 1) {
return $this->analyzeUnsolvable($rule, $disableRules);
}
// conflict
list($learnLiteral, $newLevel, $newRule, $why) = $this->analyze($level, $rule);
if ($newLevel <= 0 || $newLevel >= $level) {
throw new SolverBugException(
"Trying to revert to invalid level ".(int) $newLevel." from level ".(int) $level."."
);
} elseif (!$newRule) {
throw new SolverBugException(
"No rule was learned from analyzing $rule at level $level."
);
}
$level = $newLevel;
$this->revert($level);
$this->rules->add($newRule, RuleSet::TYPE_LEARNED);
$this->learnedWhy[spl_object_hash($newRule)] = $why;
$ruleNode = new RuleWatchNode($newRule);
$ruleNode->watch2OnHighest($this->decisions);
$this->watchGraph->insert($ruleNode);
$this->decisions->decide($learnLiteral, $level, $newRule);
}
return $level;
}
/**
* @param int $level
* @param array $decisionQueue
* @param bool $disableRules
* @param Rule $rule
* @return int
*/
private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule)
{
// choose best package to install from decisionQueue
$literals = $this->policy->selectPreferredPackages($this->pool, $this->installedMap, $decisionQueue, $rule->getRequiredPackage());
$selectedLiteral = array_shift($literals);
// if there are multiple candidates, then branch
if (count($literals)) {
$this->branches[] = array($literals, $level);
}
return $this->setPropagateLearn($level, $selectedLiteral, $disableRules, $rule);
}
/**
* @param int $level
* @param Rule $rule
* @return array
*/
protected function analyze($level, Rule $rule)
{
$analyzedRule = $rule;
$ruleLevel = 1;
$num = 0;
$l1num = 0;
$seen = array();
$learnedLiterals = array(null);
$decisionId = count($this->decisions);
$this->learnedPool[] = array();
while (true) {
$this->learnedPool[count($this->learnedPool) - 1][] = $rule;
foreach ($rule->literals as $literal) {
// skip the one true literal
if ($this->decisions->satisfy($literal)) {
continue;
}
if (isset($seen[abs($literal)])) {
continue;
}
$seen[abs($literal)] = true;
$l = $this->decisions->decisionLevel($literal);
if (1 === $l) {
$l1num++;
} elseif ($level === $l) {
$num++;
} else {
// not level1 or conflict level, add to new rule
$learnedLiterals[] = $literal;
if ($l > $ruleLevel) {
$ruleLevel = $l;
}
}
}
$l1retry = true;
while ($l1retry) {
$l1retry = false;
if (!$num && !--$l1num) {
// all level 1 literals done
break 2;
}
while (true) {
if ($decisionId <= 0) {
throw new SolverBugException(
"Reached invalid decision id $decisionId while looking through $rule for a literal present in the analyzed rule $analyzedRule."
);
}
$decisionId--;
$decision = $this->decisions->atOffset($decisionId);
$literal = $decision[Decisions::DECISION_LITERAL];
if (isset($seen[abs($literal)])) {
break;
}
}
unset($seen[abs($literal)]);
if ($num && 0 === --$num) {
$learnedLiterals[0] = -abs($literal);
if (!$l1num) {
break 2;
}
foreach ($learnedLiterals as $i => $learnedLiteral) {
if ($i !== 0) {
unset($seen[abs($learnedLiteral)]);
}
}
// only level 1 marks left
$l1num++;
$l1retry = true;
}
}
$decision = $this->decisions->atOffset($decisionId);
$rule = $decision[Decisions::DECISION_REASON];
}
$why = count($this->learnedPool) - 1;
if (!$learnedLiterals[0]) {
throw new SolverBugException(
"Did not find a learnable literal in analyzed rule $analyzedRule."
);
}
$newRule = new Rule($learnedLiterals, Rule::RULE_LEARNED, $why);
return array($learnedLiterals[0], $ruleLevel, $newRule, $why);
}
/**
* @param Problem $problem
* @param Rule $conflictRule
*/
private function analyzeUnsolvableRule(Problem $problem, Rule $conflictRule)
{
$why = spl_object_hash($conflictRule);
if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) {
$learnedWhy = $this->learnedWhy[$why];
$problemRules = $this->learnedPool[$learnedWhy];
foreach ($problemRules as $problemRule) {
$this->analyzeUnsolvableRule($problem, $problemRule);
}
return;
}
if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) {
// package rules cannot be part of a problem
return;
}
$problem->nextSection();
$problem->addRule($conflictRule);
}
/**
* @param Rule $conflictRule
* @param bool $disableRules
* @return int
*/
private function analyzeUnsolvable(Rule $conflictRule, $disableRules)
{
$problem = new Problem($this->pool);
$problem->addRule($conflictRule);
$this->analyzeUnsolvableRule($problem, $conflictRule);
$this->problems[] = $problem;
$seen = array();
$literals = $conflictRule->literals;
foreach ($literals as $literal) {
// skip the one true literal
if ($this->decisions->satisfy($literal)) {
continue;
}
$seen[abs($literal)] = true;
}
foreach ($this->decisions as $decision) {
$literal = $decision[Decisions::DECISION_LITERAL];
// skip literals that are not in this rule
if (!isset($seen[abs($literal)])) {
continue;
}
$why = $decision[Decisions::DECISION_REASON];
$problem->addRule($why);
$this->analyzeUnsolvableRule($problem, $why);
$literals = $why->literals;
foreach ($literals as $literal) {
// skip the one true literal
if ($this->decisions->satisfy($literal)) {
continue;
}
$seen[abs($literal)] = true;
}
}
if ($disableRules) {
foreach ($this->problems[count($this->problems) - 1] as $reason) {
$this->disableProblem($reason['rule']);
}
$this->resetSolver();
return 1;
}
return 0;
}
/**
* @param Rule $why
*/
private function disableProblem(Rule $why)
{
$job = $why->getJob();
if (!$job) {
$why->disable();
return;
}
// disable all rules of this job
foreach ($this->rules as $rule) {
/** @var Rule $rule */
if ($job === $rule->getJob()) {
$rule->disable();
}
}
}
private function resetSolver()
{
$this->decisions->reset();
$this->propagateIndex = 0;
$this->branches = array();
$this->enableDisableLearnedRules();
$this->makeAssertionRuleDecisions();
}
/**
* enable/disable learnt rules
*
* we have enabled or disabled some of our rules. We now re-enable all
* of our learnt rules except the ones that were learnt from rules that
* are now disabled.
*/
private function enableDisableLearnedRules()
{
foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) {
$why = $this->learnedWhy[spl_object_hash($rule)];
$problemRules = $this->learnedPool[$why];
$foundDisabled = false;
foreach ($problemRules as $problemRule) {
if ($problemRule->isDisabled()) {
$foundDisabled = true;
break;
}
}
if ($foundDisabled && $rule->isEnabled()) {
$rule->disable();
} elseif (!$foundDisabled && $rule->isDisabled()) {
$rule->enable();
}
}
}
/**
* @param bool $disableRules
*/
private function runSat($disableRules = true)
{
$this->propagateIndex = 0;
/*
* here's the main loop:
* 1) propagate new decisions (only needed once)
* 2) fulfill jobs
* 3) fulfill all unresolved rules
* 4) minimalize solution if we had choices
* if we encounter a problem, we rewind to a safe level and restart
* with step 1
*/
$decisionQueue = array();
$decisionSupplementQueue = array();
/**
* @todo this makes $disableRules always false; determine the rationale and possibly remove dead code?
*/
$disableRules = array();
$level = 1;
$systemLevel = $level + 1;
$installedPos = 0;
while (true) {
if (1 === $level) {
$conflictRule = $this->propagate($level);
if (null !== $conflictRule) {
if ($this->analyzeUnsolvable($conflictRule, $disableRules)) {
continue;
}
return;
}
}
// handle job rules
if ($level < $systemLevel) {
$iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
foreach ($iterator as $rule) {
if ($rule->isEnabled()) {
$decisionQueue = array();
$noneSatisfied = true;
foreach ($rule->literals as $literal) {
if ($this->decisions->satisfy($literal)) {
$noneSatisfied = false;
break;
}
if ($literal > 0 && $this->decisions->undecided($literal)) {
$decisionQueue[] = $literal;
}
}
if ($noneSatisfied && count($decisionQueue)) {
// prune all update packages until installed version
// except for requested updates
if (count($this->installed) != count($this->updateMap)) {
$prunedQueue = array();
foreach ($decisionQueue as $literal) {
if (isset($this->installedMap[abs($literal)])) {
$prunedQueue[] = $literal;
if (isset($this->updateMap[abs($literal)])) {
$prunedQueue = $decisionQueue;
break;
}
}
}
$decisionQueue = $prunedQueue;
}
}
if ($noneSatisfied && count($decisionQueue)) {
$oLevel = $level;
$level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
if (0 === $level) {
return;
}
if ($level <= $oLevel) {
break;
}
}
}
}
$systemLevel = $level + 1;
// jobs left
$iterator->next();
if ($iterator->valid()) {
continue;
}
}
if ($level < $systemLevel) {
$systemLevel = $level;
}
for ($i = 0, $n = 0; $n < count($this->rules); $i++, $n++) {
if ($i == count($this->rules)) {
$i = 0;
}
$rule = $this->rules->ruleById[$i];
$literals = $rule->literals;
if ($rule->isDisabled()) {
continue;
}
$decisionQueue = array();
// make sure that
// * all negative literals are installed
// * no positive literal is installed
// i.e. the rule is not fulfilled and we
// just need to decide on the positive literals
//
foreach ($literals as $literal) {
if ($literal <= 0) {
if (!$this->decisions->decidedInstall(abs($literal))) {
continue 2; // next rule
}
} else {
if ($this->decisions->decidedInstall(abs($literal))) {
continue 2; // next rule
}
if ($this->decisions->undecided(abs($literal))) {
$decisionQueue[] = $literal;
}
}
}
// need to have at least 2 item to pick from
if (count($decisionQueue) < 2) {
continue;
}
$level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
if (0 === $level) {
return;
}
// something changed, so look at all rules again
$n = -1;
}
if ($level < $systemLevel) {
continue;
}
// minimization step
if (count($this->branches)) {
$lastLiteral = null;
$lastLevel = null;
$lastBranchIndex = 0;
$lastBranchOffset = 0;
for ($i = count($this->branches) - 1; $i >= 0; $i--) {
list($literals, $l) = $this->branches[$i];
foreach ($literals as $offset => $literal) {
if ($literal && $literal > 0 && $this->decisions->decisionLevel($literal) > $l + 1) {
$lastLiteral = $literal;
$lastBranchIndex = $i;
$lastBranchOffset = $offset;
$lastLevel = $l;
}
}
}
if ($lastLiteral) {
unset($this->branches[$lastBranchIndex][self::BRANCH_LITERALS][$lastBranchOffset]);
$level = $lastLevel;
$this->revert($level);
$why = $this->decisions->lastReason();
$level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why);
if ($level == 0) {
return;
}
continue;
}
}
break;
}
}
}
|