Solver.php 71 KB

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