Solver.php 33 KB

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