Solver.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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. private function printDecisionMap()
  846. {
  847. echo "\nDecisionMap: \n";
  848. foreach ($this->decisionMap as $packageId => $level) {
  849. if ($packageId === 0) {
  850. continue;
  851. }
  852. if ($level > 0) {
  853. echo ' +' . $this->pool->packageById($packageId)."\n";
  854. } elseif ($level < 0) {
  855. echo ' -' . $this->pool->packageById($packageId)."\n";
  856. } else {
  857. echo ' ?' . $this->pool->packageById($packageId)."\n";
  858. }
  859. }
  860. echo "\n";
  861. }
  862. private function printDecisionQueue()
  863. {
  864. echo "DecisionQueue: \n";
  865. foreach ($this->decisionQueue as $i => $literal) {
  866. echo ' ' . $literal . ' ' . $this->decisionQueueWhy[$i]." level ".$this->decisionMap[$literal->getPackageId()]."\n";
  867. }
  868. echo "\n";
  869. }
  870. private function printWatches()
  871. {
  872. echo "\nWatches:\n";
  873. foreach ($this->watches as $literalId => $watch) {
  874. echo ' '.$this->literalFromId($literalId)."\n";
  875. $queue = array(array(' ', $watch));
  876. while (!empty($queue)) {
  877. list($indent, $watch) = array_pop($queue);
  878. echo $indent.$watch;
  879. if ($watch) {
  880. echo ' [id='.$watch->getId().',watch1='.$this->literalFromId($watch->watch1).',watch2='.$this->literalFromId($watch->watch2)."]";
  881. }
  882. echo "\n";
  883. if ($watch && ($watch->next1 == $watch || $watch->next2 == $watch)) {
  884. if ($watch->next1 == $watch) {
  885. echo $indent." 1 *RECURSION*";
  886. }
  887. if ($watch->next2 == $watch) {
  888. echo $indent." 2 *RECURSION*";
  889. }
  890. } elseif ($watch && ($watch->next1 || $watch->next2)) {
  891. $indent = str_replace(array('1', '2'), ' ', $indent);
  892. array_push($queue, array($indent.' 2 ', $watch->next2));
  893. array_push($queue, array($indent.' 1 ', $watch->next1));
  894. }
  895. }
  896. echo "\n";
  897. }
  898. }
  899. }