Solver.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073
  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\DependencyResolver\Operation;
  15. /**
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class Solver
  19. {
  20. const RULE_INTERNAL_ALLOW_UPDATE = 1;
  21. const RULE_JOB_INSTALL = 2;
  22. const RULE_JOB_REMOVE = 3;
  23. const RULE_JOB_LOCK = 4;
  24. const RULE_NOT_INSTALLABLE = 5;
  25. const RULE_NOTHING_PROVIDES_DEP = 6;
  26. const RULE_PACKAGE_CONFLICT = 7;
  27. const RULE_PACKAGE_NOT_EXIST = 8;
  28. const RULE_PACKAGE_REQUIRES = 9;
  29. const RULE_PACKAGE_OBSOLETES = 10;
  30. const RULE_INSTALLED_PACKAGE_OBSOLETES = 11;
  31. const RULE_PACKAGE_SAME_NAME = 12;
  32. const RULE_PACKAGE_IMPLICIT_OBSOLETES = 13;
  33. const RULE_LEARNED = 14;
  34. protected $policy;
  35. protected $pool;
  36. protected $installed;
  37. protected $rules;
  38. protected $updateAll;
  39. protected $ruleToJob = array();
  40. protected $addedMap = array();
  41. protected $fixMap = array();
  42. protected $updateMap = array();
  43. protected $noObsoletes = array();
  44. protected $watches = array();
  45. protected $removeWatches = array();
  46. protected $decisionMap;
  47. protected $installedMap;
  48. protected $packageToUpdateRule = array();
  49. protected $packageToFeatureRule = array();
  50. public function __construct(PolicyInterface $policy, Pool $pool, RepositoryInterface $installed)
  51. {
  52. $this->policy = $policy;
  53. $this->pool = $pool;
  54. $this->installed = $installed;
  55. $this->rules = new RuleSet;
  56. }
  57. /**
  58. * Creates a new rule for the requirements of a package
  59. *
  60. * This rule is of the form (-A|B|C), where B and C are the providers of
  61. * one requirement of the package A.
  62. *
  63. * @param PackageInterface $package The package with a requirement
  64. * @param array $providers The providers of the requirement
  65. * @param int $reason A RULE_* constant describing the
  66. * reason for generating this rule
  67. * @param mixed $reasonData Any data, e.g. the requirement name,
  68. * that goes with the reason
  69. * @return Rule The generated rule or null if tautological
  70. */
  71. public function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null)
  72. {
  73. $literals = array(new Literal($package, false));
  74. foreach ($providers as $provider) {
  75. // self fulfilling rule?
  76. if ($provider === $package) {
  77. return null;
  78. }
  79. $literals[] = new Literal($provider, true);
  80. }
  81. return new Rule($literals, $reason, $reasonData);
  82. }
  83. /**
  84. * Create a new rule for updating a package
  85. *
  86. * If package A1 can be updated to A2 or A3 the rule is (A1|A2|A3).
  87. *
  88. * @param PackageInterface $package The package to be updated
  89. * @param array $updates An array of update candidate packages
  90. * @param int $reason A RULE_* constant describing the
  91. * reason for generating this rule
  92. * @param mixed $reasonData Any data, e.g. the package name, that
  93. * goes with the reason
  94. * @return Rule The generated rule or null if tautology
  95. */
  96. protected function createUpdateRule(PackageInterface $package, array $updates, $reason, $reasonData = null)
  97. {
  98. $literals = array(new Literal($package, true));
  99. foreach ($updates as $update) {
  100. $literals[] = new Literal($update, true);
  101. }
  102. return new Rule($literals, $reason, $reasonData);
  103. }
  104. /**
  105. * Creates a new rule for installing a package
  106. *
  107. * The rule is simply (A) for a package A to be installed.
  108. *
  109. * @param PackageInterface $package The package to be installed
  110. * @param int $reason A RULE_* constant describing the
  111. * reason for generating this rule
  112. * @param mixed $reasonData Any data, e.g. the package name, that
  113. * goes with the reason
  114. * @return Rule The generated rule
  115. */
  116. public function createInstallRule(PackageInterface $package, $reason, $reasonData = null)
  117. {
  118. return new Rule(new Literal($package, true));
  119. }
  120. /**
  121. * Creates a rule to install at least one of a set of packages
  122. *
  123. * The rule is (A|B|C) with A, B and C different packages. If the given
  124. * set of packages is empty an impossible rule is generated.
  125. *
  126. * @param array $packages The set of packages to choose from
  127. * @param int $reason A RULE_* constant describing the reason for
  128. * generating this rule
  129. * @param mixed $reasonData Any data, e.g. the package name, that goes with
  130. * the reason
  131. * @return Rule The generated rule
  132. */
  133. public function createInstallOneOfRule(array $packages, $reason, $reasonData = null)
  134. {
  135. if (empty($packages)) {
  136. return $this->createImpossibleRule($reason, $reasonData);
  137. }
  138. $literals = array();
  139. foreach ($packages as $package) {
  140. $literals[] = new Literal($package, true);
  141. }
  142. return new Rule($literals, $reason, $reasonData);
  143. }
  144. /**
  145. * Creates a rule to remove a package
  146. *
  147. * The rule for a package A is (-A).
  148. *
  149. * @param PackageInterface $package The package to be removed
  150. * @param int $reason A RULE_* constant describing the
  151. * reason for generating this rule
  152. * @param mixed $reasonData Any data, e.g. the package name, that
  153. * goes with the reason
  154. * @return Rule The generated rule
  155. */
  156. public function createRemoveRule(PackageInterface $package, $reason, $reasonData = null)
  157. {
  158. return new Rule(array(new Literal($package, false)), $reason, $reasonData);
  159. }
  160. /**
  161. * Creates a rule for two conflicting packages
  162. *
  163. * The rule for conflicting packages A and B is (-A|-B). A is called the issuer
  164. * and B the provider.
  165. *
  166. * @param PackageInterface $issuer The package declaring the conflict
  167. * @param Package $provider The package causing the conflict
  168. * @param int $reason A RULE_* constant describing the
  169. * reason for generating this rule
  170. * @param mixed $reasonData Any data, e.g. the package name, that
  171. * goes with the reason
  172. * @return Rule The generated rule
  173. */
  174. public function createConflictRule(PackageInterface $issuer, PackageInterface $provider, $reason, $reasonData = null)
  175. {
  176. // ignore self conflict
  177. if ($issuer === $provider) {
  178. return null;
  179. }
  180. return new Rule(array(new Literal($issuer, false), new Literal($provider, false)), $reason, $reasonData);
  181. }
  182. /**
  183. * Intentionally creates a rule impossible to solve
  184. *
  185. * The rule is an empty one so it can never be satisfied.
  186. *
  187. * @param int $reason A RULE_* constant describing the reason for
  188. * generating this rule
  189. * @param mixed $reasonData Any data, e.g. the package name, that goes with
  190. * the reason
  191. * @return Rule An empty rule
  192. */
  193. public function createImpossibleRule($reason, $reasonData = null)
  194. {
  195. return new Rule(array(), $reason, $reasonData);
  196. }
  197. /**
  198. * Adds a rule unless it duplicates an existing one of any type
  199. *
  200. * To be able to directly pass in the result of one of the rule creation
  201. * methods the rule may also be null to indicate that no rule should be
  202. * added.
  203. *
  204. * @param int $type A TYPE_* constant defining the rule type
  205. * @param Rule $newRule The rule about to be added
  206. */
  207. private function addRule($type, Rule $newRule = null) {
  208. if ($newRule) {
  209. if ($this->rules->containsEqual($newRule)) {
  210. return;
  211. }
  212. $this->rules->add($newRule, $type);
  213. }
  214. }
  215. public function addRulesForPackage(PackageInterface $package)
  216. {
  217. $workQueue = new \SPLQueue;
  218. $workQueue->enqueue($package);
  219. while (!$workQueue->isEmpty()) {
  220. $package = $workQueue->dequeue();
  221. if (isset($this->addedMap[$package->getId()])) {
  222. continue;
  223. }
  224. $this->addedMap[$package->getId()] = true;
  225. $dontFix = 0;
  226. if (isset($this->installedMap[$package->getId()]) && !isset($this->fixMap[$package->getId()])) {
  227. $dontFix = 1;
  228. }
  229. if (!$dontFix && !$this->policy->installable($this, $this->pool, $this->installedMap, $package)) {
  230. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRemoveRule($package, self::RULE_NOT_INSTALLABLE, (string) $package));
  231. continue;
  232. }
  233. foreach ($package->getRequires() as $link) {
  234. $possibleRequires = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
  235. // the strategy here is to not insist on dependencies
  236. // that are already broken. so if we find one provider
  237. // that was already installed, we know that the
  238. // dependency was not broken before so we enforce it
  239. if ($dontFix) {
  240. $foundInstalled = false;
  241. foreach ($possibleRequires as $require) {
  242. if (isset($this->installedMap[$require->getId()])) {
  243. $foundInstalled = true;
  244. break;
  245. }
  246. }
  247. // no installed provider found: previously broken dependency => don't add rule
  248. if (!$foundInstalled) {
  249. continue;
  250. }
  251. }
  252. $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createRequireRule($package, $possibleRequires, self::RULE_PACKAGE_REQUIRES, (string) $link));
  253. foreach ($possibleRequires as $require) {
  254. $workQueue->enqueue($require);
  255. }
  256. }
  257. foreach ($package->getConflicts() as $link) {
  258. $possibleConflicts = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
  259. foreach ($possibleConflicts as $conflict) {
  260. if ($dontFix && isset($this->installedMap[$conflict->getId()])) {
  261. continue;
  262. }
  263. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createConflictRule($package, $conflict, self::RULE_PACKAGE_CONFLICT, (string) $link));
  264. }
  265. }
  266. // check obsoletes and implicit obsoletes of a package
  267. // if ignoreinstalledsobsoletes is not set, we're also checking
  268. // obsoletes of installed packages (like newer rpm versions)
  269. //
  270. /** @TODO: if ($this->noInstalledObsoletes) */
  271. if (true) {
  272. $noObsoletes = isset($this->noObsoletes[$package->getId()]);
  273. $isInstalled = (isset($this->installedMap[$package->getId()]));
  274. foreach ($package->getReplaces() as $link) {
  275. $obsoleteProviders = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
  276. foreach ($obsoleteProviders as $provider) {
  277. if ($provider === $package) {
  278. continue;
  279. }
  280. if ($isInstalled && $dontFix && isset($this->installedMap[$provider->getId()])) {
  281. continue; // don't repair installed/installed problems
  282. }
  283. $reason = ($isInstalled) ? self::RULE_INSTALLED_PACKAGE_OBSOLETES : self::RULE_PACKAGE_OBSOLETES;
  284. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createConflictRule($package, $provider, $reason, (string) $link));
  285. }
  286. }
  287. // check implicit obsoletes
  288. // for installed packages we only need to check installed/installed problems (and
  289. // only when dontFix is not set), as the others are picked up when looking at the
  290. // uninstalled package.
  291. if (!$isInstalled || !$dontFix) {
  292. $obsoleteProviders = $this->pool->whatProvides($package->getName(), null);
  293. foreach ($obsoleteProviders as $provider) {
  294. if ($provider === $package) {
  295. continue;
  296. }
  297. if ($isInstalled && !isset($this->installedMap[$provider->getId()])) {
  298. continue;
  299. }
  300. // obsolete same packages even when noObsoletes
  301. if ($noObsoletes && (!$package->equals($provider))) {
  302. continue;
  303. }
  304. $reason = ($package->getName() == $provider->getName()) ? self::RULE_PACKAGE_SAME_NAME : self::RULE_PACKAGE_IMPLICIT_OBSOLETES;
  305. $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createConflictRule($package, $provider, $reason, (string) $package));
  306. }
  307. }
  308. }
  309. foreach ($package->getRecommends() as $link) {
  310. foreach ($this->pool->whatProvides($link->getTarget(), $link->getConstraint()) as $recommend) {
  311. $workQueue->enqueue($recommend);
  312. }
  313. }
  314. foreach ($package->getSuggests() as $link) {
  315. foreach ($this->pool->whatProvides($link->getTarget(), $link->getConstraint()) as $suggest) {
  316. $workQueue->enqueue($suggest);
  317. }
  318. }
  319. }
  320. }
  321. /**
  322. * Adds all rules for all update packages of a given package
  323. *
  324. * @param PackageInterface $package Rules for this package's updates are to
  325. * be added
  326. * @param bool $allowAll Whether downgrades are allowed
  327. */
  328. private function addRulesForUpdatePackages(PackageInterface $package, $allowAll)
  329. {
  330. $updates = $this->policy->findUpdatePackages($this, $this->pool, $this->installedMap, $package, $allowAll);
  331. $this->addRulesForPackage($package);
  332. foreach ($updates as $update) {
  333. $this->addRulesForPackage($update);
  334. }
  335. }
  336. /**
  337. * Alters watch chains for a rule.
  338. *
  339. * Next1/2 always points to the next rule that is watching the same package.
  340. * The watches array contains rules to start from for each package
  341. *
  342. */
  343. private function addWatchesToRule(Rule $rule)
  344. {
  345. // skip simple assertions of the form (A) or (-A)
  346. if ($rule->isAssertion()) {
  347. return;
  348. }
  349. if (!isset($this->watches[$rule->watch1])) {
  350. $this->watches[$rule->watch1] = null;
  351. }
  352. $rule->next1 = $this->watches[$rule->watch1];
  353. $this->watches[$rule->watch1] = $rule;
  354. if (!isset($this->watches[$rule->watch2])) {
  355. $this->watches[$rule->watch2] = null;
  356. }
  357. $rule->next2 = $this->watches[$rule->watch2];
  358. $this->watches[$rule->watch2] = $rule;
  359. }
  360. /**
  361. * Put watch2 on rule's literal with highest level
  362. */
  363. private function watch2OnHighest(Rule $rule)
  364. {
  365. $literals = $rule->getLiterals();
  366. // if there are only 2 elements, both are being watched anyway
  367. if ($literals < 3) {
  368. return;
  369. }
  370. $watchLevel = 0;
  371. foreach ($literals as $literal) {
  372. $level = abs($this->decisionMap[$literal->getPackageId()]);
  373. if ($level > $watchLevel) {
  374. $rule->watch2 = $literal->getId();
  375. $watchLevel = $level;
  376. }
  377. }
  378. }
  379. private function findDecisionRule(PackageInterface $package)
  380. {
  381. foreach ($this->decisionQueue as $i => $literal) {
  382. if ($package === $literal->getPackage()) {
  383. return $this->decisionQueueWhy[$i];
  384. }
  385. }
  386. return null;
  387. }
  388. // aka solver_makeruledecisions
  389. private function makeAssertionRuleDecisions()
  390. {
  391. // do we need to decide a SYSTEMSOLVABLE at level 1?
  392. $decisionStart = count($this->decisionQueue);
  393. for ($ruleIndex = 0; $ruleIndex < count($this->rules); $ruleIndex++) {
  394. $rule = $this->rules->ruleById($ruleIndex);
  395. if ($rule->isWeak() || !$rule->isAssertion() || $rule->isDisabled()) {
  396. continue;
  397. }
  398. $literals = $rule->getLiterals();
  399. $literal = $literals[0];
  400. if (!$this->decided($literal->getPackage())) {
  401. $this->decisionQueue[] = $literal;
  402. $this->decisionQueueWhy[] = $rule;
  403. $this->addDecision($literal, 1);
  404. continue;
  405. }
  406. if ($this->decisionsSatisfy($literal)) {
  407. continue;
  408. }
  409. // found a conflict
  410. if (RuleSet::TYPE_LEARNED === $rule->getType()) {
  411. $rule->disable();
  412. continue;
  413. }
  414. $conflict = $this->findDecisionRule($literal->getPackage());
  415. /** TODO: handle conflict with systemsolvable? */
  416. $this->learnedPool[] = array($rule, $conflict);
  417. if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
  418. if ($rule->getType() == RuleSet::TYPE_JOB) {
  419. $why = $this->ruleToJob[$rule->getId()];
  420. } else {
  421. $why = $rule;
  422. }
  423. $this->problems[] = array($why);
  424. $this->disableProblem($why);
  425. continue;
  426. }
  427. // conflict with another job or update/feature rule
  428. $this->problems[] = array();
  429. // push all of our rules (can only be feature or job rules)
  430. // asserting this literal on the problem stack
  431. foreach ($this->rules->getIteratorFor(array(RuleSet::TYPE_JOB, RuleSet::TYPE_UPDATE, RuleSet::TYPE_FEATURE)) as $assertRule) {
  432. if ($assertRule->isDisabled() || !$assertRule->isAssertion() || $assertRule->isWeak()) {
  433. continue;
  434. }
  435. $assertRuleLiterals = $assertRule->getLiterals();
  436. $assertRuleLiteral = $assertRuleLiterals[0];
  437. if ($literal->getPackageId() !== $assertRuleLiteral->getPackageId()) {
  438. continue;
  439. }
  440. if ($assertRule->getType() === RuleSet::TYPE_JOB) {
  441. $why = $this->ruleToJob[$assertRule->getId()];
  442. } else {
  443. $why = $assertRule;
  444. }
  445. $this->problems[count($this->problems) - 1][] = $why;
  446. $this->disableProblem($why);
  447. }
  448. // start over
  449. while (count($this->decisionQueue) > $decisionStart) {
  450. $decisionLiteral = array_pop($this->decisionQueue);
  451. array_pop($this->decisionQueueWhy);
  452. unset($this->decisionQueueFree[count($this->decisionQueue)]);
  453. $this->decisionMap[$decisionLiteral->getPackageId()] = 0;
  454. }
  455. $ruleIndex = -1;
  456. }
  457. foreach ($this->rules as $rule) {
  458. if (!$rule->isWeak() || !$rule->isAssertion() || $rule->isDisabled()) {
  459. continue;
  460. }
  461. $literals = $rule->getLiterals();
  462. $literal = $literals[0];
  463. if ($this->decisionMap[$literal->getPackageId()] == 0) {
  464. $this->decisionQueue[] = $literal;
  465. $this->decisionQueueWhy[] = $rule;
  466. $this->addDecision($literal, 1);
  467. continue;
  468. }
  469. if ($this->decisionsSatisfy($literals[0])) {
  470. continue;
  471. }
  472. // conflict, but this is a weak rule => disable
  473. if ($rule->getType() == RuleSet::TYPE_JOB) {
  474. $why = $this->ruleToJob[$rule->getId()];
  475. } else {
  476. $why = $rule;
  477. }
  478. $this->disableProblem($why);
  479. /** TODO solver_reenablepolicyrules(solv, -(v + 1)); */
  480. }
  481. }
  482. public function addChoiceRules()
  483. {
  484. // void
  485. // solver_addchoicerules(Solver *solv)
  486. // {
  487. // Pool *pool = solv->pool;
  488. // Map m, mneg;
  489. // Rule *r;
  490. // Queue q, qi;
  491. // int i, j, rid, havechoice;
  492. // Id p, d, *pp;
  493. // Id p2, pp2;
  494. // Solvable *s, *s2;
  495. //
  496. // solv->choicerules = solv->nrules;
  497. // if (!pool->installed)
  498. // {
  499. // solv->choicerules_end = solv->nrules;
  500. // return;
  501. // }
  502. // solv->choicerules_ref = sat_calloc(solv->rpmrules_end, sizeof(Id));
  503. // queue_init(&q);
  504. // queue_init(&qi);
  505. // map_init(&m, pool->nsolvables);
  506. // map_init(&mneg, pool->nsolvables);
  507. // /* set up negative assertion map from infarch and dup rules */
  508. // for (rid = solv->infarchrules, r = solv->rules + rid; rid < solv->infarchrules_end; rid++, r++)
  509. // if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
  510. // MAPSET(&mneg, -r->p);
  511. // for (rid = solv->duprules, r = solv->rules + rid; rid < solv->duprules_end; rid++, r++)
  512. // if (r->p < 0 && !r->w2 && (r->d == 0 || r->d == -1))
  513. // MAPSET(&mneg, -r->p);
  514. // for (rid = 1; rid < solv->rpmrules_end ; rid++)
  515. // {
  516. // r = solv->rules + rid;
  517. // if (r->p >= 0 || ((r->d == 0 || r->d == -1) && r->w2 < 0))
  518. // continue; /* only look at requires rules */
  519. // // solver_printrule(solv, SAT_DEBUG_RESULT, r);
  520. // queue_empty(&q);
  521. // queue_empty(&qi);
  522. // havechoice = 0;
  523. // FOR_RULELITERALS(p, pp, r)
  524. // {
  525. // if (p < 0)
  526. // continue;
  527. // s = pool->solvables + p;
  528. // if (!s->repo)
  529. // continue;
  530. // if (s->repo == pool->installed)
  531. // {
  532. // queue_push(&q, p);
  533. // continue;
  534. // }
  535. // /* check if this package is "blocked" by a installed package */
  536. // s2 = 0;
  537. // FOR_PROVIDES(p2, pp2, s->name)
  538. // {
  539. // s2 = pool->solvables + p2;
  540. // if (s2->repo != pool->installed)
  541. // continue;
  542. // if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
  543. // continue;
  544. // if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
  545. // continue;
  546. // break;
  547. // }
  548. // if (p2)
  549. // {
  550. // /* found installed package p2 that we can update to p */
  551. // if (MAPTST(&mneg, p))
  552. // continue;
  553. // if (policy_is_illegal(solv, s2, s, 0))
  554. // continue;
  555. // queue_push(&qi, p2);
  556. // queue_push(&q, p);
  557. // continue;
  558. // }
  559. // if (s->obsoletes)
  560. // {
  561. // Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
  562. // s2 = 0;
  563. // while ((obs = *obsp++) != 0)
  564. // {
  565. // FOR_PROVIDES(p2, pp2, obs)
  566. // {
  567. // s2 = pool->solvables + p2;
  568. // if (s2->repo != pool->installed)
  569. // continue;
  570. // if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, pool->solvables + p2, obs))
  571. // continue;
  572. // if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
  573. // continue;
  574. // break;
  575. // }
  576. // if (p2)
  577. // break;
  578. // }
  579. // if (obs)
  580. // {
  581. // /* found installed package p2 that we can update to p */
  582. // if (MAPTST(&mneg, p))
  583. // continue;
  584. // if (policy_is_illegal(solv, s2, s, 0))
  585. // continue;
  586. // queue_push(&qi, p2);
  587. // queue_push(&q, p);
  588. // continue;
  589. // }
  590. // }
  591. // /* package p is independent of the installed ones */
  592. // havechoice = 1;
  593. // }
  594. // if (!havechoice || !q.count)
  595. // continue; /* no choice */
  596. //
  597. // /* now check the update rules of the installed package.
  598. // * if all packages of the update rules are contained in
  599. // * the dependency rules, there's no need to set up the choice rule */
  600. // map_empty(&m);
  601. // FOR_RULELITERALS(p, pp, r)
  602. // if (p > 0)
  603. // MAPSET(&m, p);
  604. // for (i = 0; i < qi.count; i++)
  605. // {
  606. // if (!qi.elements[i])
  607. // continue;
  608. // Rule *ur = solv->rules + solv->updaterules + (qi.elements[i] - pool->installed->start);
  609. // if (!ur->p)
  610. // ur = solv->rules + solv->featurerules + (qi.elements[i] - pool->installed->start);
  611. // if (!ur->p)
  612. // continue;
  613. // FOR_RULELITERALS(p, pp, ur)
  614. // if (!MAPTST(&m, p))
  615. // break;
  616. // if (p)
  617. // break;
  618. // for (j = i + 1; j < qi.count; j++)
  619. // if (qi.elements[i] == qi.elements[j])
  620. // qi.elements[j] = 0;
  621. // }
  622. // if (i == qi.count)
  623. // {
  624. // #if 0
  625. // printf("skipping choice ");
  626. // solver_printrule(solv, SAT_DEBUG_RESULT, solv->rules + rid);
  627. // #endif
  628. // continue;
  629. // }
  630. // d = q.count ? pool_queuetowhatprovides(pool, &q) : 0;
  631. // solver_addrule(solv, r->p, d);
  632. // queue_push(&solv->weakruleq, solv->nrules - 1);
  633. // solv->choicerules_ref[solv->nrules - 1 - solv->choicerules] = rid;
  634. // #if 0
  635. // printf("OLD ");
  636. // solver_printrule(solv, SAT_DEBUG_RESULT, solv->rules + rid);
  637. // printf("WEAK CHOICE ");
  638. // solver_printrule(solv, SAT_DEBUG_RESULT, solv->rules + solv->nrules - 1);
  639. // #endif
  640. // }
  641. // queue_free(&q);
  642. // queue_free(&qi);
  643. // map_free(&m);
  644. // map_free(&mneg);
  645. // solv->choicerules_end = solv->nrules;
  646. // }
  647. }
  648. /***********************************************************************
  649. ***
  650. *** Policy rule disabling/reenabling
  651. ***
  652. *** Disable all policy rules that conflict with our jobs. If a job
  653. *** gets disabled later on, reenable the involved policy rules again.
  654. ***
  655. *** /
  656. #define DISABLE_UPDATE 1
  657. #define DISABLE_INFARCH 2
  658. #define DISABLE_DUP 3
  659. */
  660. protected function jobToDisableQueue(array $job, array $disableQueue)
  661. {
  662. switch ($job['cmd']) {
  663. case 'install':
  664. foreach ($job['packages'] as $package) {
  665. if (isset($this->installedMap[$package->getId()])) {
  666. $disableQueue[] = array('type' => 'update', 'package' => $package);
  667. }
  668. /* all job packages obsolete * /
  669. qstart = q->count;
  670. pass = 0;
  671. memset(&omap, 0, sizeof(omap));
  672. FOR_JOB_SELECT(p, pp, select, what)
  673. {
  674. Id p2, pp2;
  675. if (pass == 1)
  676. map_grow(&omap, installed->end - installed->start);
  677. s = pool->solvables + p;
  678. if (s->obsoletes)
  679. {
  680. Id obs, *obsp;
  681. obsp = s->repo->idarraydata + s->obsoletes;
  682. while ((obs = *obsp++) != 0)
  683. FOR_PROVIDES(p2, pp2, obs)
  684. {
  685. Solvable *ps = pool->solvables + p2;
  686. if (ps->repo != installed)
  687. continue;
  688. if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
  689. continue;
  690. if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
  691. continue;
  692. if (pass)
  693. MAPSET(&omap, p2 - installed->start);
  694. else
  695. queue_push2(q, DISABLE_UPDATE, p2);
  696. }
  697. }
  698. FOR_PROVIDES(p2, pp2, s->name)
  699. {
  700. Solvable *ps = pool->solvables + p2;
  701. if (ps->repo != installed)
  702. continue;
  703. if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
  704. continue;
  705. if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
  706. continue;
  707. if (pass)
  708. MAPSET(&omap, p2 - installed->start);
  709. else
  710. queue_push2(q, DISABLE_UPDATE, p2);
  711. }
  712. if (pass)
  713. {
  714. for (i = j = qstart; i < q->count; i += 2)
  715. {
  716. if (MAPTST(&omap, q->elements[i + 1] - installed->start))
  717. {
  718. MAPCLR(&omap, q->elements[i + 1] - installed->start);
  719. q->elements[j + 1] = q->elements[i + 1];
  720. j += 2;
  721. }
  722. }
  723. queue_truncate(q, j);
  724. }
  725. if (q->count == qstart)
  726. break;
  727. pass++;
  728. }
  729. if (omap.size)
  730. map_free(&omap);
  731. if (qstart == q->count)
  732. return; /* nothing to prune * /
  733. if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
  734. return; /* all is set */
  735. /* now that we know which installed packages are obsoleted check each of them * /
  736. for (i = j = qstart; i < q->count; i += 2)
  737. {
  738. Solvable *is = pool->solvables + q->elements[i + 1];
  739. FOR_JOB_SELECT(p, pp, select, what)
  740. {
  741. int illegal = 0;
  742. s = pool->solvables + p;
  743. if ((set & SOLVER_SETEVR) != 0)
  744. illegal |= POLICY_ILLEGAL_DOWNGRADE; /* ignore * /
  745. if ((set & SOLVER_SETARCH) != 0)
  746. illegal |= POLICY_ILLEGAL_ARCHCHANGE; /* ignore * /
  747. if ((set & SOLVER_SETVENDOR) != 0)
  748. illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore * /
  749. illegal = policy_is_illegal(solv, is, s, illegal);
  750. if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
  751. {
  752. /* it's ok if the EV is different * /
  753. if (evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
  754. illegal = 0;
  755. }
  756. if (illegal)
  757. break;
  758. }
  759. if (!p)
  760. {
  761. /* no package conflicts with the update rule * /
  762. /* thus keep the DISABLE_UPDATE * /
  763. q->elements[j + 1] = q->elements[i + 1];
  764. j += 2;
  765. }
  766. }
  767. queue_truncate(q, j);
  768. return;*/
  769. }
  770. break;
  771. case 'remove':
  772. foreach ($job['packages'] as $package) {
  773. if (isset($this->installedMap[$package->getId()])) {
  774. $disableQueue[] = array('type' => 'update', 'package' => $package);
  775. }
  776. }
  777. break;
  778. }
  779. return $disableQueue;
  780. }
  781. protected function disableUpdateRule($package)
  782. {
  783. // find update & feature rule and disable
  784. if (isset($this->packageToUpdateRule[$package->getId()])) {
  785. $this->packageToUpdateRule[$package->getId()]->disable();
  786. }
  787. if (isset($this->packageToFeatureRule[$package->getId()])) {
  788. $this->packageToFeatureRule[$package->getId()]->disable();
  789. }
  790. }
  791. /**
  792. * Disables all policy rules that conflict with jobs
  793. */
  794. protected function disablePolicyRules()
  795. {
  796. $lastJob = null;
  797. $allQueue = array();
  798. $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
  799. foreach ($iterator as $rule) {
  800. if ($rule->isDisabled()) {
  801. continue;
  802. }
  803. $job = $this->ruleToJob[$rule->getId()];
  804. if ($job === $lastJob) {
  805. continue;
  806. }
  807. $lastJob = $job;
  808. $allQueue = $this->jobToDisableQueue($job, $allQueue);
  809. }
  810. foreach ($allQueue as $disable) {
  811. switch ($disable['type']) {
  812. case 'update':
  813. $this->disableUpdateRule($disable['package']);
  814. break;
  815. default:
  816. throw new \RuntimeException("Unsupported disable type: " . $disable['type']);
  817. }
  818. }
  819. }
  820. public function solve(Request $request)
  821. {
  822. $this->jobs = $request->getJobs();
  823. $installedPackages = $this->installed->getPackages();
  824. $this->installedMap = array();
  825. foreach ($installedPackages as $package) {
  826. $this->installedMap[$package->getId()] = $package;
  827. }
  828. $this->decisionMap = new \SplFixedArray($this->pool->getMaxId() + 1);
  829. foreach ($this->jobs as $job) {
  830. switch ($job['cmd']) {
  831. case 'update-all':
  832. foreach ($installedPackages as $package) {
  833. $this->updateMap[$package->getId()] = true;
  834. }
  835. break;
  836. case 'fix-all':
  837. foreach ($installedPackages as $package) {
  838. $this->fixMap[$package->getId()] = true;
  839. }
  840. break;
  841. }
  842. foreach ($job['packages'] as $package) {
  843. switch ($job['cmd']) {
  844. case 'fix':
  845. if (isset($this->installedMap[$package->getId()])) {
  846. $this->fixMap[$package->getId()] = true;
  847. }
  848. break;
  849. case 'update':
  850. if (isset($this->installedMap[$package->getId()])) {
  851. $this->updateMap[$package->getId()] = true;
  852. }
  853. break;
  854. }
  855. }
  856. }
  857. foreach ($installedPackages as $package) {
  858. $this->addRulesForPackage($package);
  859. }
  860. foreach ($installedPackages as $package) {
  861. $this->addRulesForUpdatePackages($package, true);
  862. }
  863. foreach ($this->jobs as $job) {
  864. foreach ($job['packages'] as $package) {
  865. switch ($job['cmd']) {
  866. case 'install':
  867. $this->installCandidateMap[$package->getId()] = true;
  868. $this->addRulesForPackage($package);
  869. break;
  870. }
  871. }
  872. }
  873. // solver_addrpmrulesforweak(solv, &addedmap);
  874. foreach ($installedPackages as $package) {
  875. // create a feature rule which allows downgrades
  876. $updates = $this->policy->findUpdatePackages($this, $this->pool, $this->installedMap, $package, true);
  877. $featureRule = $this->createUpdateRule($package, $updates, self::RULE_INTERNAL_ALLOW_UPDATE, (string) $package);
  878. // create an update rule which does not allow downgrades
  879. $updates = $this->policy->findUpdatePackages($this, $this->pool, $this->installedMap, $package, false);
  880. $rule = $this->createUpdateRule($package, $updates, self::RULE_INTERNAL_ALLOW_UPDATE, (string) $package);
  881. if ($rule->equals($featureRule)) {
  882. if ($this->policy->allowUninstall()) {
  883. $featureRule->setWeak(true);
  884. $this->addRule(RuleSet::TYPE_FEATURE, $featureRule);
  885. $this->packageToFeatureRule[$package->getId()] = $rule;
  886. } else {
  887. $this->addRule(RuleSet::TYPE_UPDATE, $rule);
  888. $this->packageToUpdateRule[$package->getId()] = $rule;
  889. }
  890. } else if ($this->policy->allowUninstall()) {
  891. $featureRule->setWeak(true);
  892. $rule->setWeak(true);
  893. $this->addRule(RuleSet::TYPE_FEATURE, $featureRule);
  894. $this->addRule(RuleSet::TYPE_UPDATE, $rule);
  895. $this->packageToFeatureRule[$package->getId()] = $rule;
  896. $this->packageToUpdateRule[$package->getId()] = $rule;
  897. }
  898. }
  899. foreach ($this->jobs as $job) {
  900. switch ($job['cmd']) {
  901. case 'install':
  902. $rule = $this->createInstallOneOfRule($job['packages'], self::RULE_JOB_INSTALL, $job['packageName']);
  903. $this->addRule(RuleSet::TYPE_JOB, $rule);
  904. $this->ruleToJob[$rule->getId()] = $job;
  905. break;
  906. case 'remove':
  907. // remove all packages with this name including uninstalled
  908. // ones to make sure none of them are picked as replacements
  909. // todo: cleandeps
  910. foreach ($job['packages'] as $package) {
  911. $rule = $this->createRemoveRule($package, self::RULE_JOB_REMOVE);
  912. $this->addRule(RuleSet::TYPE_JOB, $rule);
  913. $this->ruleToJob[$rule->getId()] = $job;
  914. }
  915. break;
  916. case 'lock':
  917. foreach ($job['packages'] as $package) {
  918. if (isset($this->installedMap[$package->getId()])) {
  919. $rule = $this->createInstallRule($package, self::RULE_JOB_LOCK);
  920. } else {
  921. $rule = $this->createRemoveRule($package, self::RULE_JOB_LOCK);
  922. }
  923. $this->addRule(RuleSet::TYPE_JOB, $rule);
  924. $this->ruleToJob[$rule->getId()] = $job;
  925. }
  926. break;
  927. }
  928. }
  929. $this->addChoiceRules();
  930. foreach ($this->rules as $rule) {
  931. $this->addWatchesToRule($rule);
  932. }
  933. /* disable update rules that conflict with our job */
  934. $this->disablePolicyRules();
  935. /* make decisions based on job/update assertions */
  936. $this->makeAssertionRuleDecisions();
  937. $installRecommended = 0;
  938. $this->runSat(true, $installRecommended);
  939. //$this->printDecisionMap();
  940. //findrecommendedsuggested(solv);
  941. //solver_prepare_solutions(solv);
  942. return $this->createTransaction();
  943. }
  944. protected function createTransaction()
  945. {
  946. $transaction = array();
  947. $installMeansUpdateMap = array();
  948. $ignoreRemoveMap = array();
  949. foreach ($this->decisionQueue as $i => $literal) {
  950. $package = $literal->getPackage();
  951. // !wanted & installed
  952. if (!$literal->isWanted() && isset($this->installedMap[$package->getId()])) {
  953. $literals = array();
  954. if (isset($this->packageToUpdateRule[$package->getId()])) {
  955. $literals = array_merge($literals, $this->packageToUpdateRule[$package->getId()]->getLiterals());
  956. }
  957. if (isset($this->packageToFeatureRule[$package->getId()])) {
  958. $literals = array_merge($literals, $this->packageToFeatureRule[$package->getId()]->getLiterals());
  959. }
  960. foreach ($literals as $updateLiteral) {
  961. if (!$updateLiteral->equals($literal)) {
  962. $installMeansUpdateMap[$updateLiteral->getPackageId()] = $package;
  963. }
  964. }
  965. }
  966. }
  967. foreach ($this->decisionQueue as $i => $literal) {
  968. $package = $literal->getPackage();
  969. // wanted & installed || !wanted & !installed
  970. if ($literal->isWanted() == (isset($this->installedMap[$package->getId()]))) {
  971. continue;
  972. }
  973. if ($literal->isWanted()) {
  974. if (isset($installMeansUpdateMap[$literal->getPackageId()])) {
  975. $source = $installMeansUpdateMap[$literal->getPackageId()];
  976. $transaction[] = new Operation\UpdateOperation(
  977. $source, $package, $this->decisionQueueWhy[$i]
  978. );
  979. // avoid updates to one package from multiple origins
  980. unset($installMeansUpdateMap[$literal->getPackageId()]);
  981. $ignoreRemove[$source->getId()] = true;
  982. } else {
  983. $transaction[] = new Operation\InstallOperation(
  984. $package, $this->decisionQueueWhy[$i]
  985. );
  986. }
  987. } else if (!isset($ignoreRemove[$package->getId()])) {
  988. $transaction[] = new Operation\UninstallOperation(
  989. $package, $this->decisionQueueWhy[$i]
  990. );
  991. }
  992. }
  993. return array_reverse($transaction);
  994. }
  995. protected $decisionQueue = array();
  996. protected $decisionQueueWhy = array();
  997. protected $decisionQueueFree = array();
  998. protected $propagateIndex;
  999. protected $branches = array();
  1000. protected $problems = array();
  1001. protected $learnedPool = array();
  1002. protected $recommendsIndex;
  1003. protected function literalFromId($id)
  1004. {
  1005. $package = $this->pool->packageById(abs($id));
  1006. return new Literal($package, $id > 0);
  1007. }
  1008. protected function addDecision(Literal $l, $level)
  1009. {
  1010. if ($l->isWanted()) {
  1011. $this->decisionMap[$l->getPackageId()] = $level;
  1012. } else {
  1013. $this->decisionMap[$l->getPackageId()] = -$level;
  1014. }
  1015. }
  1016. protected function addDecisionId($literalId, $level)
  1017. {
  1018. $packageId = abs($literalId);
  1019. if ($literalId > 0) {
  1020. $this->decisionMap[$packageId] = $level;
  1021. } else {
  1022. $this->decisionMap[$packageId] = -$level;
  1023. }
  1024. }
  1025. protected function decisionsContain(Literal $l)
  1026. {
  1027. return (
  1028. $this->decisionMap[$l->getPackageId()] > 0 && $l->isWanted() ||
  1029. $this->decisionMap[$l->getPackageId()] < 0 && !$l->isWanted()
  1030. );
  1031. }
  1032. protected function decisionsContainId($literalId)
  1033. {
  1034. $packageId = abs($literalId);
  1035. return (
  1036. $this->decisionMap[$packageId] > 0 && $literalId > 0 ||
  1037. $this->decisionMap[$packageId] < 0 && $literalId < 0
  1038. );
  1039. }
  1040. protected function decisionsSatisfy(Literal $l)
  1041. {
  1042. return ($l->isWanted() && $this->decisionMap[$l->getPackageId()] > 0) ||
  1043. (!$l->isWanted() && $this->decisionMap[$l->getPackageId()] <= 0);
  1044. }
  1045. protected function decisionsConflict(Literal $l)
  1046. {
  1047. return (
  1048. $this->decisionMap[$l->getPackageId()] > 0 && !$l->isWanted() ||
  1049. $this->decisionMap[$l->getPackageId()] < 0 && $l->isWanted()
  1050. );
  1051. }
  1052. protected function decisionsConflictId($literalId)
  1053. {
  1054. $packageId = abs($literalId);
  1055. return (
  1056. $this->decisionMap[$packageId] > 0 && !($literalId < 0) ||
  1057. $this->decisionMap[$packageId] < 0 && $literalId > 0
  1058. );
  1059. }
  1060. protected function decided(PackageInterface $p)
  1061. {
  1062. return $this->decisionMap[$p->getId()] != 0;
  1063. }
  1064. protected function undecided(PackageInterface $p)
  1065. {
  1066. return $this->decisionMap[$p->getId()] == 0;
  1067. }
  1068. protected function decidedInstall(PackageInterface $p) {
  1069. return $this->decisionMap[$p->getId()] > 0;
  1070. }
  1071. protected function decidedRemove(PackageInterface $p) {
  1072. return $this->decisionMap[$p->getId()] < 0;
  1073. }
  1074. /**
  1075. * Makes a decision and propagates it to all rules.
  1076. *
  1077. * Evaluates each term affected by the decision (linked through watches)
  1078. * If we find unit rules we make new decisions based on them
  1079. *
  1080. * @return Rule|null A rule on conflict, otherwise null.
  1081. */
  1082. protected function propagate($level)
  1083. {
  1084. while ($this->propagateIndex < count($this->decisionQueue)) {
  1085. // we invert the decided literal here, example:
  1086. // A was decided => (-A|B) now requires B to be true, so we look for
  1087. // rules which are fulfilled by -A, rather than A.
  1088. $literal = $this->decisionQueue[$this->propagateIndex]->inverted();
  1089. $this->propagateIndex++;
  1090. // /* foreach rule where 'pkg' is now FALSE */
  1091. //for (rp = watches + pkg; *rp; rp = next_rp)
  1092. if (!isset($this->watches[$literal->getId()])) {
  1093. continue;
  1094. }
  1095. for ($rule = $this->watches[$literal->getId()]; $rule !== null; $rule = $nextRule) {
  1096. $nextRule = $rule->getNext($literal);
  1097. if ($rule->isDisabled()) {
  1098. continue;
  1099. }
  1100. $otherWatch = $rule->getOtherWatch($literal);
  1101. if ($this->decisionsContainId($otherWatch)) {
  1102. continue;
  1103. }
  1104. $ruleLiterals = $rule->getLiterals();
  1105. if (sizeof($ruleLiterals) > 2) {
  1106. foreach ($ruleLiterals as $ruleLiteral) {
  1107. if ($otherWatch !== $ruleLiteral->getId() &&
  1108. !$this->decisionsConflict($ruleLiteral)) {
  1109. if ($literal->getId() === $rule->watch1) {
  1110. $rule->watch1 = $ruleLiteral->getId();
  1111. $rule->next1 = (isset($this->watches[$ruleLiteral->getId()])) ? $this->watches[$ruleLiteral->getId()] : null ;
  1112. } else {
  1113. $rule->watch2 = $ruleLiteral->getId();
  1114. $rule->next2 = (isset($this->watches[$ruleLiteral->getId()])) ? $this->watches[$ruleLiteral->getId()] : null ;
  1115. }
  1116. $this->watches[$ruleLiteral->getId()] = $rule;
  1117. continue 2;
  1118. }
  1119. }
  1120. }
  1121. // yay, we found a unit clause! try setting it to true
  1122. if ($this->decisionsConflictId($otherWatch)) {
  1123. return $rule;
  1124. }
  1125. $this->addDecisionId($otherWatch, $level);
  1126. $this->decisionQueue[] = $this->literalFromId($otherWatch);
  1127. $this->decisionQueueWhy[] = $rule;
  1128. }
  1129. }
  1130. return null;
  1131. }
  1132. /**
  1133. * Reverts a decision at the given level.
  1134. */
  1135. private function revert($level)
  1136. {
  1137. while (!empty($this->decisionQueue)) {
  1138. $literal = $this->decisionQueue[count($this->decisionQueue) - 1];
  1139. if (!$this->decisionMap[$literal->getPackageId()]) {
  1140. break;
  1141. }
  1142. $decisionLevel = abs($this->decisionMap[$literal->getPackageId()]);
  1143. if ($decisionLevel <= $level) {
  1144. break;
  1145. }
  1146. /** TODO: implement recommendations
  1147. *if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
  1148. * solv->recommendations.count--;
  1149. */
  1150. $this->decisionMap[$literal->getPackageId()] = 0;
  1151. array_pop($this->decisionQueue);
  1152. array_pop($this->decisionQueueWhy);
  1153. $this->propagateIndex = count($this->decisionQueue);
  1154. }
  1155. while (!empty($this->branches)) {
  1156. list($literals, $branchLevel) = $this->branches[count($this->branches) - 1];
  1157. if ($branchLevel >= $level) {
  1158. break;
  1159. }
  1160. array_pop($this->branches);
  1161. }
  1162. $this->recommendsIndex = -1;
  1163. }
  1164. /**-------------------------------------------------------------------
  1165. *
  1166. * setpropagatelearn
  1167. *
  1168. * add free decision (solvable to install) to decisionq
  1169. * increase level and propagate decision
  1170. * return if no conflict.
  1171. *
  1172. * in conflict case, analyze conflict rule, add resulting
  1173. * rule to learnt rule set, make decision from learnt
  1174. * rule (always unit) and re-propagate.
  1175. *
  1176. * returns the new solver level or 0 if unsolvable
  1177. *
  1178. */
  1179. private function setPropagateLearn($level, Literal $literal, $disableRules, Rule $rule)
  1180. {
  1181. assert($rule != null);
  1182. assert($literal != null);
  1183. $level++;
  1184. $this->addDecision($literal, $level);
  1185. $this->decisionQueue[] = $literal;
  1186. $this->decisionQueueWhy[] = $rule;
  1187. $this->decisionQueueFree[count($this->decisionQueueWhy) - 1] = true;
  1188. while (true) {
  1189. $rule = $this->propagate($level);
  1190. if (!$rule) {
  1191. break;
  1192. }
  1193. if ($level == 1) {
  1194. return $this->analyzeUnsolvable($rule, $disableRules);
  1195. }
  1196. // conflict
  1197. list($newLevel, $newRule, $why) = $this->analyze($level, $rule);
  1198. assert($newLevel > 0);
  1199. assert($newLevel < $level);
  1200. $level = $newLevel;
  1201. $this->revert($level);
  1202. assert($newRule != null);
  1203. $this->addRule(RuleSet::TYPE_LEARNED, $newRule);
  1204. $this->learnedWhy[$newRule->getId()] = $why;
  1205. $this->watch2OnHighest($newRule);
  1206. $this->addWatchesToRule($newRule);
  1207. $literals = $newRule->getLiterals();
  1208. $this->addDecision($literals[0], $level);
  1209. $this->decisionQueue[] = $literals[0];
  1210. $this->decisionQueueWhy[] = $newRule;
  1211. }
  1212. return $level;
  1213. }
  1214. private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule)
  1215. {
  1216. // choose best package to install from decisionQueue
  1217. $literals = $this->policy->selectPreferedPackages($this->pool, $this->installedMap, $decisionQueue);
  1218. $selectedLiteral = array_shift($literals);
  1219. // if there are multiple candidates, then branch
  1220. if (count($literals)) {
  1221. $this->branches[] = array($literals, -$level);
  1222. }
  1223. return $this->setPropagateLearn($level, $selectedLiteral, $disableRules, $rule);
  1224. }
  1225. protected function analyze($level, $rule)
  1226. {
  1227. $ruleLevel = 1;
  1228. $num = 0;
  1229. $l1num = 0;
  1230. $seen = array();
  1231. $learnedLiterals = array(null);
  1232. $decisionId = count($this->decisionQueue);
  1233. $this->learnedPool[] = array();
  1234. while(true) {
  1235. $this->learnedPool[count($this->learnedPool) - 1][] = $rule;
  1236. foreach ($rule->getLiterals() as $literal) {
  1237. // skip the one true literal
  1238. if ($this->decisionsSatisfy($literal)) {
  1239. continue;
  1240. }
  1241. if (isset($seen[$literal->getPackageId()])) {
  1242. continue;
  1243. }
  1244. $seen[$literal->getPackageId()] = true;
  1245. $l = abs($this->decisionMap[$literal->getPackageId()]);
  1246. if (1 === $l) {
  1247. $l1num++;
  1248. } else if ($level === $l) {
  1249. $num++;
  1250. } else {
  1251. // not level1 or conflict level, add to new rule
  1252. $learnedLiterals[] = $literal;
  1253. if ($l > $ruleLevel) {
  1254. $ruleLevel = $l;
  1255. }
  1256. }
  1257. }
  1258. $l1retry = true;
  1259. while ($l1retry) {
  1260. $l1retry = false;
  1261. if (!$num && !$l1num) {
  1262. // all level 1 literals done
  1263. break 2;
  1264. }
  1265. while (true) {
  1266. assert($decisionId > 0);
  1267. $decisionId--;
  1268. $literal = $this->decisionQueue[$decisionId];
  1269. if (isset($seen[$literal->getPackageId()])) {
  1270. break;
  1271. }
  1272. }
  1273. unset($seen[$literal->getPackageId()]);
  1274. if ($num && 0 === --$num) {
  1275. $learnedLiterals[0] = $this->literalFromId(-$literal->getPackageId());
  1276. if (!$l1num) {
  1277. break 2;
  1278. }
  1279. foreach ($learnedLiterals as $i => $learnedLiteral) {
  1280. if ($i !== 0) {
  1281. unset($seen[$literal->getPackageId()]);
  1282. }
  1283. }
  1284. // only level 1 marks left
  1285. $l1num++;
  1286. $l1retry = true;
  1287. }
  1288. $rule = $this->decisionQueueWhy[$decisionId];
  1289. }
  1290. }
  1291. $why = count($this->learnedPool) - 1;
  1292. $newRule = new Rule($learnedLiterals, self::RULE_LEARNED, $why);
  1293. return array($ruleLevel, $newRule, $why);
  1294. }
  1295. private function analyzeUnsolvableRule($conflictRule, &$lastWeakWhy)
  1296. {
  1297. $why = $conflictRule->getId();
  1298. if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) {
  1299. $learnedWhy = $this->learnedWhy[$why];
  1300. $problem = $this->learnedPool[$learnedWhy];
  1301. foreach ($problem as $problemRule) {
  1302. $this->analyzeUnsolvableRule($problemRule, $lastWeakWhy);
  1303. }
  1304. return;
  1305. }
  1306. if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) {
  1307. // package rules cannot be part of a problem
  1308. return;
  1309. }
  1310. if ($conflictRule->isWeak()) {
  1311. /** TODO why > or < lastWeakWhy? */
  1312. if (!$lastWeakWhy || $why > $lastWeakWhy->getId()) {
  1313. $lastWeakWhy = $conflictRule;
  1314. }
  1315. }
  1316. if ($conflictRule->getType() == RuleSet::TYPE_JOB) {
  1317. $why = $this->ruleToJob[$conflictRule->getId()];
  1318. }
  1319. // if this problem was already found skip it
  1320. if (in_array($why, $this->problems[count($this->problems) - 1], true)) {
  1321. return;
  1322. }
  1323. $this->problems[count($this->problems) - 1][] = $why;
  1324. }
  1325. private function analyzeUnsolvable($conflictRule, $disableRules)
  1326. {
  1327. $lastWeakWhy = null;
  1328. $this->problems[] = array();
  1329. $this->learnedPool[] = array($conflictRule);
  1330. $this->analyzeUnsolvableRule($conflictRule, $lastWeakWhy);
  1331. $seen = array();
  1332. $literals = $conflictRule->getLiterals();
  1333. /* unnecessary because unlike rule.d, watch2 == 2nd literal, unless watch2 changed
  1334. if (sizeof($literals) == 2) {
  1335. $literals[1] = $this->literalFromId($conflictRule->watch2);
  1336. }
  1337. */
  1338. foreach ($literals as $literal) {
  1339. // skip the one true literal
  1340. if ($this->decisionsSatisfy($literal)) {
  1341. continue;
  1342. }
  1343. $seen[$literal->getPackageId()] = true;
  1344. }
  1345. $decisionId = count($this->decisionQueue);
  1346. while ($decisionId > 0) {
  1347. $decisionId--;
  1348. $literal = $this->decisionQueue[$decisionId];
  1349. // skip literals that are not in this rule
  1350. if (!isset($seen[$literal->getPackageId()])) {
  1351. continue;
  1352. }
  1353. $why = $this->decisionQueueWhy[$decisionId];
  1354. $this->learnedPool[count($this->learnedPool) - 1][] = $why;
  1355. $this->analyzeUnsolvableRule($why, $lastWeakWhy);
  1356. $literals = $why->getLiterals();
  1357. /* unnecessary because unlike rule.d, watch2 == 2nd literal, unless watch2 changed
  1358. if (sizeof($literals) == 2) {
  1359. $literals[1] = $this->literalFromId($why->watch2);
  1360. }
  1361. */
  1362. foreach ($literals as $literal) {
  1363. // skip the one true literal
  1364. if ($this->decisionsSatisfy($literal)) {
  1365. continue;
  1366. }
  1367. $seen[$literal->getPackageId()] = true;
  1368. }
  1369. }
  1370. if ($lastWeakWhy) {
  1371. array_pop($this->problems);
  1372. array_pop($this->learnedPool);
  1373. if ($lastWeakWhy->getType() === RuleSet::TYPE_JOB) {
  1374. $why = $this->ruleToJob[$lastWeakWhy];
  1375. } else {
  1376. $why = $lastWeakWhy;
  1377. }
  1378. if ($lastWeakWhy->getType() == RuleSet::TYPE_CHOICE) {
  1379. $this->disableChoiceRules($lastWeakWhy);
  1380. }
  1381. $this->disableProblem($why);
  1382. /**
  1383. @TODO what does v < 0 mean here? ($why == v)
  1384. if (v < 0)
  1385. solver_reenablepolicyrules(solv, -(v + 1));
  1386. */
  1387. $this->resetSolver();
  1388. return true;
  1389. }
  1390. if ($disableRules) {
  1391. foreach ($this->problems[count($this->problems) - 1] as $why) {
  1392. $this->disableProblem($why);
  1393. }
  1394. $this->resetSolver();
  1395. return true;
  1396. }
  1397. return false;
  1398. }
  1399. private function disableProblem($why)
  1400. {
  1401. if ($why instanceof Rule) {
  1402. $why->disable();
  1403. } else if (is_array($why)) {
  1404. // disable all rules of this job
  1405. foreach ($this->ruleToJob as $ruleId => $job) {
  1406. if ($why === $job) {
  1407. $this->rules->ruleById($ruleId)->disable();
  1408. }
  1409. }
  1410. }
  1411. }
  1412. private function resetSolver()
  1413. {
  1414. while ($literal = array_pop($this->decisionQueue)) {
  1415. $this->decisionMap[$literal->getPackageId()] = 0;
  1416. }
  1417. $this->decisionQueueWhy = array();
  1418. $this->decisionQueueFree = array();
  1419. $this->recommendsIndex = -1;
  1420. $this->propagateIndex = 0;
  1421. $this->recommendations = array();
  1422. $this->branches = array();
  1423. $this->enableDisableLearnedRules();
  1424. $this->makeAssertionRuleDecisions();
  1425. }
  1426. /*-------------------------------------------------------------------
  1427. * enable/disable learnt rules
  1428. *
  1429. * we have enabled or disabled some of our rules. We now reenable all
  1430. * of our learnt rules except the ones that were learnt from rules that
  1431. * are now disabled.
  1432. */
  1433. private function enableDisableLearnedRules()
  1434. {
  1435. foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) {
  1436. $why = $this->learnedWhy[$rule->getId()];
  1437. $problem = $this->learnedPool[$why];
  1438. $foundDisabled = false;
  1439. foreach ($problem as $problemRule) {
  1440. if ($problemRule->disabled()) {
  1441. $foundDisabled = true;
  1442. break;
  1443. }
  1444. }
  1445. if ($foundDisabled && $rule->isEnabled()) {
  1446. $rule->disable();
  1447. } else if (!$foundDisabled && $rule->isDisabled()) {
  1448. $rule->enable();
  1449. }
  1450. }
  1451. }
  1452. private function runSat($disableRules = true, $installRecommended = false)
  1453. {
  1454. $this->propagateIndex = 0;
  1455. // /*
  1456. // * here's the main loop:
  1457. // * 1) propagate new decisions (only needed once)
  1458. // * 2) fulfill jobs
  1459. // * 3) try to keep installed packages
  1460. // * 4) fulfill all unresolved rules
  1461. // * 5) install recommended packages
  1462. // * 6) minimalize solution if we had choices
  1463. // * if we encounter a problem, we rewind to a safe level and restart
  1464. // * with step 1
  1465. // */
  1466. $decisionQueue = array();
  1467. $decisionSupplementQueue = array();
  1468. $disableRules = array();
  1469. $level = 1;
  1470. $systemLevel = $level + 1;
  1471. $minimizationsteps = 0;
  1472. $installedPos = 0;
  1473. $this->installedPackages = $this->installed->getPackages();
  1474. while (true) {
  1475. if (1 === $level) {
  1476. $conflictRule = $this->propagate($level);
  1477. if ($conflictRule !== null) {
  1478. if ($this->analyzeUnsolvable($conflictRule, $disableRules)) {
  1479. continue;
  1480. } else {
  1481. return;
  1482. }
  1483. }
  1484. }
  1485. // handle job rules
  1486. if ($level < $systemLevel) {
  1487. $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
  1488. foreach ($iterator as $rule) {
  1489. if ($rule->isEnabled()) {
  1490. $decisionQueue = array();
  1491. $noneSatisfied = true;
  1492. foreach ($rule->getLiterals() as $literal) {
  1493. if ($this->decisionsSatisfy($literal)) {
  1494. $noneSatisfied = false;
  1495. break;
  1496. }
  1497. $decisionQueue[] = $literal;
  1498. }
  1499. if ($noneSatisfied && count($decisionQueue)) {
  1500. // prune all update packages until installed version
  1501. // except for requested updates
  1502. if (count($this->installed) != count($this->updateMap)) {
  1503. $prunedQueue = array();
  1504. foreach ($decisionQueue as $literal) {
  1505. if (isset($this->installedMap[$literal->getPackageId()])) {
  1506. $prunedQueue[] = $literal;
  1507. if (isset($this->updateMap[$literal->getPackageId()])) {
  1508. $prunedQueue = $decisionQueue;
  1509. break;
  1510. }
  1511. }
  1512. }
  1513. $decisionQueue = $prunedQueue;
  1514. }
  1515. }
  1516. if ($noneSatisfied && count($decisionQueue)) {
  1517. $oLevel = $level;
  1518. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  1519. if (0 === $level) {
  1520. return;
  1521. }
  1522. if ($level <= $oLevel) {
  1523. break;
  1524. }
  1525. }
  1526. }
  1527. }
  1528. $systemLevel = $level + 1;
  1529. // jobs left
  1530. $iterator->next();
  1531. if ($iterator->valid()) {
  1532. continue;
  1533. }
  1534. }
  1535. // handle installed packages
  1536. if ($level < $systemLevel) {
  1537. // use two passes if any packages are being updated
  1538. // -> better user experience
  1539. for ($pass = (count($this->updateMap)) ? 0 : 1; $pass < 2; $pass++) {
  1540. $passLevel = $level;
  1541. for ($i = $installedPos, $n = 0; $n < count($this->installedPackages); $i++, $n++) {
  1542. $repeat = false;
  1543. if ($i == count($this->installedPackages)) {
  1544. $i = 0;
  1545. }
  1546. $literal = new Literal($this->installedPackages[$i], true);
  1547. if ($this->decisionsContain($literal)) {
  1548. continue;
  1549. }
  1550. // only process updates in first pass
  1551. /** TODO: && or || ? **/
  1552. if (0 === $pass && !isset($this->updateMap[$literal->getPackageId()])) {
  1553. continue;
  1554. }
  1555. $rule = null;
  1556. if (isset($this->packageToUpdateRule[$literal->getPackageId()])) {
  1557. $rule = $this->packageToUpdateRule[$literal->getPackageId()];
  1558. }
  1559. if ((!$rule || $rule->isDisabled()) && isset($this->packageToFeatureRule[$literal->getPackageId()])) {
  1560. $rule = $this->packageToFeatureRule[$literal->getPackageId()];
  1561. }
  1562. if (!$rule || $rule->isDisabled()) {
  1563. continue;
  1564. }
  1565. $updateRuleLiterals = $rule->getLiterals();
  1566. $decisionQueue = array();
  1567. if (!isset($this->noUpdate[$literal->getPackageId()]) && (
  1568. $this->decidedRemove($literal->getPackage()) ||
  1569. isset($this->updateMap[$literal->getPackageId()]) ||
  1570. !$literal->equals($updateRuleLiterals[0])
  1571. )) {
  1572. foreach ($updateRuleLiterals as $ruleLiteral) {
  1573. if ($this->decidedInstall($ruleLiteral->getPackage())) {
  1574. // already fulfilled
  1575. $decisionQueue = array();
  1576. break;
  1577. }
  1578. if ($this->undecided($ruleLiteral->getPackage())) {
  1579. $decisionQueue[] = $ruleLiteral;
  1580. }
  1581. }
  1582. }
  1583. if (sizeof($decisionQueue)) {
  1584. $oLevel = $level;
  1585. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  1586. if (0 === $level) {
  1587. return;
  1588. }
  1589. if ($level <= $oLevel) {
  1590. $repeat = true;
  1591. }
  1592. } else if (!$repeat && $this->undecided($literal->getPackage())) {
  1593. // still undecided? keep package.
  1594. $oLevel = $level;
  1595. if (isset($this->cleanDepsMap[$literal->getPackageId()])) {
  1596. // clean deps removes package
  1597. $level = $this->setPropagateLearn($level, $literal->invert(), $disableRules, null);
  1598. } else {
  1599. // ckeeping package
  1600. $level = $this->setPropagateLearn($level, $literal, $disableRules, $rule);
  1601. }
  1602. if (0 === $level) {
  1603. return;
  1604. }
  1605. if ($level <= $oLevel) {
  1606. $repeat = true;
  1607. }
  1608. }
  1609. if ($repeat) {
  1610. if (1 === $level || $level < $passLevel) {
  1611. // trouble
  1612. break;
  1613. }
  1614. if ($level < $oLevel) {
  1615. // redo all
  1616. $n = 0;
  1617. }
  1618. // repeat
  1619. $i--;
  1620. $n--;
  1621. continue;
  1622. }
  1623. }
  1624. if ($n < count($this->installedPackages)) {
  1625. $installedPos = $i; // retry this problem next time
  1626. break;
  1627. }
  1628. $installedPos = 0;
  1629. }
  1630. $systemlevel = $level + 1;
  1631. if ($pass < 2) {
  1632. // had trouble => retry
  1633. continue;
  1634. }
  1635. }
  1636. if ($level < $systemLevel) {
  1637. $systemLevel = $level;
  1638. }
  1639. for ($i = 0, $n = 0; $n < count($this->rules); $i++, $n++) {
  1640. if ($i == count($this->rules)) {
  1641. $i = 0;
  1642. }
  1643. $rule = $this->rules->ruleById($i);
  1644. $literals = $rule->getLiterals();
  1645. if ($rule->isDisabled()) {
  1646. continue;
  1647. }
  1648. $decisionQueue = array();
  1649. // make sure that
  1650. // * all negative literals are installed
  1651. // * no positive literal is installed
  1652. // i.e. the rule is not fulfilled and we
  1653. // just need to decide on the positive literals
  1654. //
  1655. foreach ($literals as $literal) {
  1656. if (!$literal->isWanted()) {
  1657. if (!$this->decidedInstall($literal->getPackage())) {
  1658. continue 2; // next rule
  1659. }
  1660. } else {
  1661. if ($this->decidedInstall($literal->getPackage())) {
  1662. continue 2; // next rule
  1663. }
  1664. if ($this->undecided($literal->getPackage())) {
  1665. $decisionQueue[] = $literal;
  1666. }
  1667. }
  1668. }
  1669. // need to have at least 2 item to pick from
  1670. if (count($decisionQueue) < 2) {
  1671. continue;
  1672. }
  1673. $oLevel = $level;
  1674. $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
  1675. if (0 === $level) {
  1676. return;
  1677. }
  1678. // open suse sat-solver uses this, but why is $level == 1 trouble?
  1679. // SYSTEMSOLVABLE related? we don't have that, so should work
  1680. //if ($level < $systemLevel || $level == 1) {
  1681. if ($level < $systemLevel) {
  1682. break; // trouble
  1683. }
  1684. // something changed, so look at all rules again
  1685. $n = -1;
  1686. }
  1687. // minimization step
  1688. if (count($this->branches)) {
  1689. $lastLiteral = null;
  1690. $lastLevel = null;
  1691. $lastBranchIndex = 0;
  1692. $lastBranchOffset = 0;
  1693. for ($i = count($this->branches) - 1; $i >= 0; $i--) {
  1694. list($literals, $level) = $this->branches[$i];
  1695. foreach ($literals as $offset => $literal) {
  1696. if ($literal && $literal->isWanted() && $this->decisionMap[$literal->getPackageId()] > $level + 1) {
  1697. $lastLiteral = $literal;
  1698. $lastBranchIndex = $i;
  1699. $lastBranchOffset = $offset;
  1700. $lastLevel = $level;
  1701. }
  1702. }
  1703. }
  1704. if ($lastLiteral) {
  1705. $this->branches[$lastBranchIndex][$lastBranchOffset] = null;
  1706. $minimizationSteps++;
  1707. $level = $lastLevel;
  1708. $this->revert($level);
  1709. $why = $this->decisionQueueWhy[count($this->decisionQueueWhy)];
  1710. $oLevel = $level;
  1711. $level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why);
  1712. if ($level == 0) {
  1713. return;
  1714. }
  1715. continue;
  1716. }
  1717. }
  1718. break;
  1719. }
  1720. }
  1721. public function printDecisionMap()
  1722. {
  1723. echo "\nDecisionMap: \n";
  1724. foreach ($this->decisionMap as $packageId => $level) {
  1725. if ($packageId === 0) {
  1726. continue;
  1727. }
  1728. if ($level > 0) {
  1729. echo ' +' . $this->pool->packageById($packageId) . "\n";
  1730. } else {
  1731. echo ' -' . $this->pool->packageById($packageId) . "\n";
  1732. }
  1733. }
  1734. echo "\n";
  1735. }
  1736. public function printDecisionQueue()
  1737. {
  1738. echo "DecisionQueue: \n";
  1739. foreach ($this->decisionQueue as $i => $literal) {
  1740. echo ' ' . $literal . ' ' . $this->decisionQueueWhy[$i] . "\n";
  1741. }
  1742. echo "\n";
  1743. }
  1744. }