Solver.php 27 KB

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