Solver.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\DependencyResolver;
  12. use Composer\Repository\RepositoryInterface;
  13. use Composer\Repository\PlatformRepository;
  14. /**
  15. * @author Nils Adermann <naderman@naderman.de>
  16. */
  17. class Solver
  18. {
  19. const BRANCH_LITERALS = 0;
  20. const BRANCH_LEVEL = 1;
  21. protected $policy;
  22. protected $pool;
  23. protected $installed;
  24. protected $rules;
  25. protected $ruleSetGenerator;
  26. protected $updateAll;
  27. protected $addedMap = array();
  28. protected $updateMap = array();
  29. protected $watchGraph;
  30. protected $decisions;
  31. protected $installedMap;
  32. protected $propagateIndex;
  33. protected $branches = array();
  34. protected $problems = array();
  35. protected $learnedPool = array();
  36. public function __construct(PolicyInterface $policy, Pool $pool, RepositoryInterface $installed)
  37. {
  38. $this->policy = $policy;
  39. $this->pool = $pool;
  40. $this->installed = $installed;
  41. $this->ruleSetGenerator = new RuleSetGenerator($policy, $pool);
  42. }
  43. // aka solver_makeruledecisions
  44. private function makeAssertionRuleDecisions()
  45. {
  46. $decisionStart = count($this->decisions) - 1;
  47. $rulesCount = count($this->rules);
  48. for ($ruleIndex = 0; $ruleIndex < $rulesCount; $ruleIndex++) {
  49. $rule = $this->rules->ruleById($ruleIndex);
  50. if (!$rule->isAssertion() || $rule->isDisabled()) {
  51. continue;
  52. }
  53. $literals = $rule->literals;
  54. $literal = $literals[0];
  55. if (!$this->decisions->decided(abs($literal))) {
  56. $this->decisions->decide($literal, 1, $rule);
  57. continue;
  58. }
  59. if ($this->decisions->satisfy($literal)) {
  60. continue;
  61. }
  62. // found a conflict
  63. if (RuleSet::TYPE_LEARNED === $rule->getType()) {
  64. $rule->disable();
  65. continue;
  66. }
  67. $conflict = $this->decisions->decisionRule($literal);
  68. if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
  69. $problem = new Problem($this->pool);
  70. $problem->addRule($rule);
  71. $problem->addRule($conflict);
  72. $this->disableProblem($rule);
  73. $this->problems[] = $problem;
  74. continue;
  75. }
  76. // conflict with another job
  77. $problem = new Problem($this->pool);
  78. $problem->addRule($rule);
  79. $problem->addRule($conflict);
  80. // push all of our rules (can only be job rules)
  81. // asserting this literal on the problem stack
  82. foreach ($this->rules->getIteratorFor(RuleSet::TYPE_JOB) as $assertRule) {
  83. if ($assertRule->isDisabled() || !$assertRule->isAssertion()) {
  84. continue;
  85. }
  86. $assertRuleLiterals = $assertRule->literals;
  87. $assertRuleLiteral = $assertRuleLiterals[0];
  88. if (abs($literal) !== abs($assertRuleLiteral)) {
  89. continue;
  90. }
  91. $problem->addRule($assertRule);
  92. $this->disableProblem($assertRule);
  93. }
  94. $this->problems[] = $problem;
  95. $this->decisions->resetToOffset($decisionStart);
  96. $ruleIndex = -1;
  97. }
  98. }
  99. protected function setupInstalledMap()
  100. {
  101. $this->installedMap = array();
  102. foreach ($this->installed->getPackages() as $package) {
  103. $this->installedMap[$package->id] = $package;
  104. }
  105. }
  106. protected function checkForRootRequireProblems($ignorePlatformReqs)
  107. {
  108. foreach ($this->jobs as $job) {
  109. switch ($job['cmd']) {
  110. case 'update':
  111. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  112. foreach ($packages as $package) {
  113. if (isset($this->installedMap[$package->id])) {
  114. $this->updateMap[$package->id] = true;
  115. }
  116. }
  117. break;
  118. case 'update-all':
  119. foreach ($this->installedMap as $package) {
  120. $this->updateMap[$package->id] = true;
  121. }
  122. break;
  123. case 'install':
  124. if ($ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $job['packageName'])) {
  125. break;
  126. }
  127. if (!$this->pool->whatProvides($job['packageName'], $job['constraint'])) {
  128. $problem = new Problem($this->pool);
  129. $problem->addRule(new Rule($this->pool, array(), null, null, $job));
  130. $this->problems[] = $problem;
  131. }
  132. break;
  133. }
  134. }
  135. }
  136. public function solve(Request $request, $ignorePlatformReqs = false)
  137. {
  138. $this->jobs = $request->getJobs();
  139. $this->setupInstalledMap();
  140. $this->rules = $this->ruleSetGenerator->getRulesFor($this->jobs, $this->installedMap, $ignorePlatformReqs);
  141. $this->checkForRootRequireProblems($ignorePlatformReqs);
  142. $this->decisions = new Decisions($this->pool);
  143. $this->watchGraph = new RuleWatchGraph;
  144. foreach ($this->rules as $rule) {
  145. $this->watchGraph->insert(new RuleWatchNode($rule));
  146. }
  147. /* make decisions based on job/update assertions */
  148. $this->makeAssertionRuleDecisions();
  149. $this->runSat(true);
  150. // decide to remove everything that's installed and undecided
  151. foreach ($this->installedMap as $packageId => $void) {
  152. if ($this->decisions->undecided($packageId)) {
  153. $this->decisions->decide(-$packageId, 1, null);
  154. }
  155. }
  156. if ($this->problems) {
  157. throw new SolverProblemsException($this->problems, $this->installedMap);
  158. }
  159. $transaction = new Transaction($this->policy, $this->pool, $this->installedMap, $this->decisions);
  160. return $transaction->getOperations();
  161. }
  162. protected function literalFromId($id)
  163. {
  164. $package = $this->pool->packageById(abs($id));
  165. return new Literal($package, $id > 0);
  166. }
  167. /**
  168. * Makes a decision and propagates it to all rules.
  169. *
  170. * Evaluates each term affected by the decision (linked through watches)
  171. * If we find unit rules we make new decisions based on them
  172. *
  173. * @param integer $level
  174. * @return Rule|null A rule on conflict, otherwise null.
  175. */
  176. protected function propagate($level)
  177. {
  178. while ($this->decisions->validOffset($this->propagateIndex)) {
  179. $decision = $this->decisions->atOffset($this->propagateIndex);
  180. $conflict = $this->watchGraph->propagateLiteral(
  181. $decision[Decisions::DECISION_LITERAL],
  182. $level,
  183. $this->decisions
  184. );
  185. $this->propagateIndex++;
  186. if ($conflict) {
  187. return $conflict;
  188. }
  189. }
  190. return null;
  191. }
  192. /**
  193. * Reverts a decision at the given level.
  194. */
  195. private function revert($level)
  196. {
  197. while (!$this->decisions->isEmpty()) {
  198. $literal = $this->decisions->lastLiteral();
  199. if ($this->decisions->undecided($literal)) {
  200. break;
  201. }
  202. $decisionLevel = $this->decisions->decisionLevel($literal);
  203. if ($decisionLevel <= $level) {
  204. break;
  205. }
  206. $this->decisions->revertLast();
  207. $this->propagateIndex = count($this->decisions);
  208. }
  209. while (!empty($this->branches) && $this->branches[count($this->branches) - 1][self::BRANCH_LEVEL] >= $level) {
  210. array_pop($this->branches);
  211. }
  212. }
  213. /**-------------------------------------------------------------------
  214. *
  215. * setpropagatelearn
  216. *
  217. * add free decision (a positive literal) to decision queue
  218. * increase level and propagate decision
  219. * return if no conflict.
  220. *
  221. * in conflict case, analyze conflict rule, add resulting
  222. * rule to learnt rule set, make decision from learnt
  223. * rule (always unit) and re-propagate.
  224. *
  225. * returns the new solver level or 0 if unsolvable
  226. *
  227. */
  228. private function setPropagateLearn($level, $literal, $disableRules, Rule $rule)
  229. {
  230. $level++;
  231. $this->decisions->decide($literal, $level, $rule);
  232. while (true) {
  233. $rule = $this->propagate($level);
  234. if (!$rule) {
  235. break;
  236. }
  237. if ($level == 1) {
  238. return $this->analyzeUnsolvable($rule, $disableRules);
  239. }
  240. // conflict
  241. list($learnLiteral, $newLevel, $newRule, $why) = $this->analyze($level, $rule);
  242. if ($newLevel <= 0 || $newLevel >= $level) {
  243. throw new SolverBugException(
  244. "Trying to revert to invalid level ".(int) $newLevel." from level ".(int) $level."."
  245. );
  246. } elseif (!$newRule) {
  247. throw new SolverBugException(
  248. "No rule was learned from analyzing $rule at level $level."
  249. );
  250. }
  251. $level = $newLevel;
  252. $this->revert($level);
  253. $this->rules->add($newRule, RuleSet::TYPE_LEARNED);
  254. $this->learnedWhy[$newRule->getId()] = $why;
  255. $ruleNode = new RuleWatchNode($newRule);
  256. $ruleNode->watch2OnHighest($this->decisions);
  257. $this->watchGraph->insert($ruleNode);
  258. $this->decisions->decide($learnLiteral, $level, $newRule);
  259. }
  260. return $level;
  261. }
  262. private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule)
  263. {
  264. // choose best package to install from decisionQueue
  265. $literals = $this->policy->selectPreferedPackages($this->pool, $this->installedMap, $decisionQueue, $rule->getRequiredPackage());
  266. $selectedLiteral = array_shift($literals);
  267. // if there are multiple candidates, then branch
  268. if (count($literals)) {
  269. $this->branches[] = array($literals, $level);
  270. }
  271. return $this->setPropagateLearn($level, $selectedLiteral, $disableRules, $rule);
  272. }
  273. protected function analyze($level, $rule)
  274. {
  275. $analyzedRule = $rule;
  276. $ruleLevel = 1;
  277. $num = 0;
  278. $l1num = 0;
  279. $seen = array();
  280. $learnedLiterals = array(null);
  281. $decisionId = count($this->decisions);
  282. $this->learnedPool[] = array();
  283. while (true) {
  284. $this->learnedPool[count($this->learnedPool) - 1][] = $rule;
  285. foreach ($rule->literals as $literal) {
  286. // skip the one true literal
  287. if ($this->decisions->satisfy($literal)) {
  288. continue;
  289. }
  290. if (isset($seen[abs($literal)])) {
  291. continue;
  292. }
  293. $seen[abs($literal)] = true;
  294. $l = $this->decisions->decisionLevel($literal);
  295. if (1 === $l) {
  296. $l1num++;
  297. } elseif ($level === $l) {
  298. $num++;
  299. } else {
  300. // not level1 or conflict level, add to new rule
  301. $learnedLiterals[] = $literal;
  302. if ($l > $ruleLevel) {
  303. $ruleLevel = $l;
  304. }
  305. }
  306. }
  307. $l1retry = true;
  308. while ($l1retry) {
  309. $l1retry = false;
  310. if (!$num && !--$l1num) {
  311. // all level 1 literals done
  312. break 2;
  313. }
  314. while (true) {
  315. if ($decisionId <= 0) {
  316. throw new SolverBugException(
  317. "Reached invalid decision id $decisionId while looking through $rule for a literal present in the analyzed rule $analyzedRule."
  318. );
  319. }
  320. $decisionId--;
  321. $decision = $this->decisions->atOffset($decisionId);
  322. $literal = $decision[Decisions::DECISION_LITERAL];
  323. if (isset($seen[abs($literal)])) {
  324. break;
  325. }
  326. }
  327. unset($seen[abs($literal)]);
  328. if ($num && 0 === --$num) {
  329. $learnedLiterals[0] = -abs($literal);
  330. if (!$l1num) {
  331. break 2;
  332. }
  333. foreach ($learnedLiterals as $i => $learnedLiteral) {
  334. if ($i !== 0) {
  335. unset($seen[abs($learnedLiteral)]);
  336. }
  337. }
  338. // only level 1 marks left
  339. $l1num++;
  340. $l1retry = true;
  341. }
  342. }
  343. $decision = $this->decisions->atOffset($decisionId);
  344. $rule = $decision[Decisions::DECISION_REASON];
  345. }
  346. $why = count($this->learnedPool) - 1;
  347. if (!$learnedLiterals[0]) {
  348. throw new SolverBugException(
  349. "Did not find a learnable literal in analyzed rule $analyzedRule."
  350. );
  351. }
  352. $newRule = new Rule($this->pool, $learnedLiterals, Rule::RULE_LEARNED, $why);
  353. return array($learnedLiterals[0], $ruleLevel, $newRule, $why);
  354. }
  355. private function analyzeUnsolvableRule($problem, $conflictRule)
  356. {
  357. $why = $conflictRule->getId();
  358. if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) {
  359. $learnedWhy = $this->learnedWhy[$why];
  360. $problemRules = $this->learnedPool[$learnedWhy];
  361. foreach ($problemRules as $problemRule) {
  362. $this->analyzeUnsolvableRule($problem, $problemRule);
  363. }
  364. return;
  365. }
  366. if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) {
  367. // package rules cannot be part of a problem
  368. return;
  369. }
  370. $problem->nextSection();
  371. $problem->addRule($conflictRule);
  372. }
  373. private function analyzeUnsolvable($conflictRule, $disableRules)
  374. {
  375. $problem = new Problem($this->pool);
  376. $problem->addRule($conflictRule);
  377. $this->analyzeUnsolvableRule($problem, $conflictRule);
  378. $this->problems[] = $problem;
  379. $seen = array();
  380. $literals = $conflictRule->literals;
  381. foreach ($literals as $literal) {
  382. // skip the one true literal
  383. if ($this->decisions->satisfy($literal)) {
  384. continue;
  385. }
  386. $seen[abs($literal)] = true;
  387. }
  388. foreach ($this->decisions as $decision) {
  389. $literal = $decision[Decisions::DECISION_LITERAL];
  390. // skip literals that are not in this rule
  391. if (!isset($seen[abs($literal)])) {
  392. continue;
  393. }
  394. $why = $decision[Decisions::DECISION_REASON];
  395. $problem->addRule($why);
  396. $this->analyzeUnsolvableRule($problem, $why);
  397. $literals = $why->literals;
  398. foreach ($literals as $literal) {
  399. // skip the one true literal
  400. if ($this->decisions->satisfy($literal)) {
  401. continue;
  402. }
  403. $seen[abs($literal)] = true;
  404. }
  405. }
  406. if ($disableRules) {
  407. foreach ($this->problems[count($this->problems) - 1] as $reason) {
  408. $this->disableProblem($reason['rule']);
  409. }
  410. $this->resetSolver();
  411. return 1;
  412. }
  413. return 0;
  414. }
  415. private function disableProblem($why)
  416. {
  417. $job = $why->getJob();
  418. if (!$job) {
  419. $why->disable();
  420. return;
  421. }
  422. // disable all rules of this job
  423. foreach ($this->rules as $rule) {
  424. if ($job === $rule->getJob()) {
  425. $rule->disable();
  426. }
  427. }
  428. }
  429. private function resetSolver()
  430. {
  431. $this->decisions->reset();
  432. $this->propagateIndex = 0;
  433. $this->branches = array();
  434. $this->enableDisableLearnedRules();
  435. $this->makeAssertionRuleDecisions();
  436. }
  437. /*-------------------------------------------------------------------
  438. * enable/disable learnt rules
  439. *
  440. * we have enabled or disabled some of our rules. We now re-enable all
  441. * of our learnt rules except the ones that were learnt from rules that
  442. * are now disabled.
  443. */
  444. private function enableDisableLearnedRules()
  445. {
  446. foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) {
  447. $why = $this->learnedWhy[$rule->getId()];
  448. $problemRules = $this->learnedPool[$why];
  449. $foundDisabled = false;
  450. foreach ($problemRules as $problemRule) {
  451. if ($problemRule->isDisabled()) {
  452. $foundDisabled = true;
  453. break;
  454. }
  455. }
  456. if ($foundDisabled && $rule->isEnabled()) {
  457. $rule->disable();
  458. } elseif (!$foundDisabled && $rule->isDisabled()) {
  459. $rule->enable();
  460. }
  461. }
  462. }
  463. private function runSat($disableRules = true)
  464. {
  465. $this->propagateIndex = 0;
  466. // /*
  467. // * here's the main loop:
  468. // * 1) propagate new decisions (only needed once)
  469. // * 2) fulfill jobs
  470. // * 3) fulfill all unresolved rules
  471. // * 4) minimalize solution if we had choices
  472. // * if we encounter a problem, we rewind to a safe level and restart
  473. // * with step 1
  474. // */
  475. $decisionQueue = array();
  476. $decisionSupplementQueue = array();
  477. $disableRules = array();
  478. $level = 1;
  479. $systemLevel = $level + 1;
  480. $installedPos = 0;
  481. while (true) {
  482. if (1 === $level) {
  483. $conflictRule = $this->propagate($level);
  484. if (null !== $conflictRule) {
  485. if ($this->analyzeUnsolvable($conflictRule, $disableRules)) {
  486. continue;
  487. }
  488. return;
  489. }
  490. }
  491. // handle job rules
  492. if ($level < $systemLevel) {
  493. $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
  494. foreach ($iterator as $rule) {
  495. if ($rule->isEnabled()) {
  496. $decisionQueue = array();
  497. $noneSatisfied = true;
  498. foreach ($rule->literals as $literal) {
  499. if ($this->decisions->satisfy($literal)) {
  500. $noneSatisfied = false;
  501. break;
  502. }
  503. if ($literal > 0 && $this->decisions->undecided($literal)) {
  504. $decisionQueue[] = $literal;
  505. }
  506. }
  507. if ($noneSatisfied && count($decisionQueue)) {
  508. // prune all update packages until installed version
  509. // except for requested updates
  510. if (count($this->installed) != count($this->updateMap)) {
  511. $prunedQueue = array();
  512. foreach ($decisionQueue as $literal) {
  513. if (isset($this->installedMap[abs($literal)])) {
  514. $prunedQueue[] = $literal;
  515. if (isset($this->updateMap[abs($literal)])) {
  516. $prunedQueue = $decisionQueue;
  517. break;
  518. }
  519. }
  520. }
  521. $decisionQueue = $prunedQueue;
  522. }
  523. }
  524. if ($noneSatisfied && count($decisionQueue)) {
  525. $oLevel = $level;
  526. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  527. if (0 === $level) {
  528. return;
  529. }
  530. if ($level <= $oLevel) {
  531. break;
  532. }
  533. }
  534. }
  535. }
  536. $systemLevel = $level + 1;
  537. // jobs left
  538. $iterator->next();
  539. if ($iterator->valid()) {
  540. continue;
  541. }
  542. }
  543. if ($level < $systemLevel) {
  544. $systemLevel = $level;
  545. }
  546. for ($i = 0, $n = 0; $n < count($this->rules); $i++, $n++) {
  547. if ($i == count($this->rules)) {
  548. $i = 0;
  549. }
  550. $rule = $this->rules->ruleById($i);
  551. $literals = $rule->literals;
  552. if ($rule->isDisabled()) {
  553. continue;
  554. }
  555. $decisionQueue = array();
  556. // make sure that
  557. // * all negative literals are installed
  558. // * no positive literal is installed
  559. // i.e. the rule is not fulfilled and we
  560. // just need to decide on the positive literals
  561. //
  562. foreach ($literals as $literal) {
  563. if ($literal <= 0) {
  564. if (!$this->decisions->decidedInstall(abs($literal))) {
  565. continue 2; // next rule
  566. }
  567. } else {
  568. if ($this->decisions->decidedInstall(abs($literal))) {
  569. continue 2; // next rule
  570. }
  571. if ($this->decisions->undecided(abs($literal))) {
  572. $decisionQueue[] = $literal;
  573. }
  574. }
  575. }
  576. // need to have at least 2 item to pick from
  577. if (count($decisionQueue) < 2) {
  578. continue;
  579. }
  580. $oLevel = $level;
  581. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  582. if (0 === $level) {
  583. return;
  584. }
  585. // something changed, so look at all rules again
  586. $n = -1;
  587. }
  588. if ($level < $systemLevel) {
  589. continue;
  590. }
  591. // minimization step
  592. if (count($this->branches)) {
  593. $lastLiteral = null;
  594. $lastLevel = null;
  595. $lastBranchIndex = 0;
  596. $lastBranchOffset = 0;
  597. $l = 0;
  598. for ($i = count($this->branches) - 1; $i >= 0; $i--) {
  599. list($literals, $l) = $this->branches[$i];
  600. foreach ($literals as $offset => $literal) {
  601. if ($literal && $literal > 0 && $this->decisions->decisionLevel($literal) > $l + 1) {
  602. $lastLiteral = $literal;
  603. $lastBranchIndex = $i;
  604. $lastBranchOffset = $offset;
  605. $lastLevel = $l;
  606. }
  607. }
  608. }
  609. if ($lastLiteral) {
  610. unset($this->branches[$lastBranchIndex][self::BRANCH_LITERALS][$lastBranchOffset]);
  611. $level = $lastLevel;
  612. $this->revert($level);
  613. $why = $this->decisions->lastReason();
  614. $oLevel = $level;
  615. $level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why);
  616. if ($level == 0) {
  617. return;
  618. }
  619. continue;
  620. }
  621. }
  622. break;
  623. }
  624. }
  625. }