123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\DependencyResolver;
- use Composer\Repository\RepositoryInterface;
- use Composer\Package\PackageInterface;
- use Composer\DependencyResolver\Operation;
- /**
- * @author Nils Adermann <naderman@naderman.de>
- */
- class Solver
- {
- protected $policy;
- protected $pool;
- protected $installed;
- protected $rules;
- protected $updateAll;
- protected $ruleToJob = array();
- protected $addedMap = array();
- protected $updateMap = array();
- protected $watches = array();
- protected $removeWatches = array();
- protected $decisionMap;
- protected $installedMap;
- protected $installedPackages;
- protected $packageToFeatureRule = array();
- public function __construct(PolicyInterface $policy, Pool $pool, RepositoryInterface $installed)
- {
- $this->policy = $policy;
- $this->pool = $pool;
- $this->installed = $installed;
- $this->rules = new RuleSet;
- }
- /**
- * Creates a new rule for the requirements of a package
- *
- * This rule is of the form (-A|B|C), where B and C are the providers of
- * one requirement of the package A.
- *
- * @param PackageInterface $package The package with a requirement
- * @param array $providers The providers of the requirement
- * @param int $reason A RULE_* constant describing the
- * reason for generating this rule
- * @param mixed $reasonData Any data, e.g. the requirement name,
- * that goes with the reason
- * @return Rule The generated rule or null if tautological
- */
- protected function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null)
- {
- $literals = array(new Literal($package, false));
- foreach ($providers as $provider) {
- // self fulfilling rule?
- if ($provider === $package) {
- return null;
- }
- $literals[] = new Literal($provider, true);
- }
- return new Rule($literals, $reason, $reasonData);
- }
- /**
- * Create a new rule for updating a package
- *
- * If package A1 can be updated to A2 or A3 the rule is (A1|A2|A3).
- *
- * @param PackageInterface $package The package to be updated
- * @param array $updates An array of update candidate packages
- * @param int $reason A RULE_* constant describing the
- * reason for generating this rule
- * @param mixed $reasonData Any data, e.g. the package name, that
- * goes with the reason
- * @return Rule The generated rule or null if tautology
- */
- protected function createUpdateRule(PackageInterface $package, array $updates, $reason, $reasonData = null)
- {
- $literals = array(new Literal($package, true));
- foreach ($updates as $update) {
- $literals[] = new Literal($update, true);
- }
- return new Rule($literals, $reason, $reasonData);
- }
- /**
- * Creates a new rule for installing a package
- *
- * The rule is simply (A) for a package A to be installed.
- *
- * @param PackageInterface $package The package to be installed
- * @param int $reason A RULE_* constant describing the
- * reason for generating this rule
- * @param mixed $reasonData Any data, e.g. the package name, that
- * goes with the reason
- * @return Rule The generated rule
- */
- protected function createInstallRule(PackageInterface $package, $reason, $reasonData = null)
- {
- return new Rule(new Literal($package, true));
- }
- /**
- * Creates a rule to install at least one of a set of packages
- *
- * The rule is (A|B|C) with A, B and C different packages. If the given
- * set of packages is empty an impossible rule is generated.
- *
- * @param array $packages The set of packages to choose from
- * @param int $reason A RULE_* constant describing the reason for
- * generating this rule
- * @param mixed $reasonData Any data, e.g. the package name, that goes with
- * the reason
- * @return Rule The generated rule
- */
- protected function createInstallOneOfRule(array $packages, $reason, $reasonData = null)
- {
- $literals = array();
- foreach ($packages as $package) {
- $literals[] = new Literal($package, true);
- }
- return new Rule($literals, $reason, $reasonData);
- }
- /**
- * Creates a rule to remove a package
- *
- * The rule for a package A is (-A).
- *
- * @param PackageInterface $package The package to be removed
- * @param int $reason A RULE_* constant describing the
- * reason for generating this rule
- * @param mixed $reasonData Any data, e.g. the package name, that
- * goes with the reason
- * @return Rule The generated rule
- */
- protected function createRemoveRule(PackageInterface $package, $reason, $reasonData = null)
- {
- return new Rule(array(new Literal($package, false)), $reason, $reasonData);
- }
- /**
- * Creates a rule for two conflicting packages
- *
- * The rule for conflicting packages A and B is (-A|-B). A is called the issuer
- * and B the provider.
- *
- * @param PackageInterface $issuer The package declaring the conflict
- * @param Package $provider The package causing the conflict
- * @param int $reason A RULE_* constant describing the
- * reason for generating this rule
- * @param mixed $reasonData Any data, e.g. the package name, that
- * goes with the reason
- * @return Rule The generated rule
- */
- protected function createConflictRule(PackageInterface $issuer, PackageInterface $provider, $reason, $reasonData = null)
- {
- // ignore self conflict
- if ($issuer === $provider) {
- return null;
- }
- return new Rule(array(new Literal($issuer, false), new Literal($provider, false)), $reason, $reasonData);
- }
- /**
- * Adds a rule unless it duplicates an existing one of any type
- *
- * To be able to directly pass in the result of one of the rule creation
- * methods the rule may also be null to indicate that no rule should be
- * added.
- *
- * @param int $type A TYPE_* constant defining the rule type
- * @param Rule $newRule The rule about to be added
- */
- private function addRule($type, Rule $newRule = null) {
- if ($newRule) {
- if ($this->rules->containsEqual($newRule)) {
- return;
- }
- $this->rules->add($newRule, $type);
- }
- }
- protected function addRulesForPackage(PackageInterface $package)
- {
- $workQueue = new \SplQueue;
- $workQueue->enqueue($package);
- while (!$workQueue->isEmpty()) {
- $package = $workQueue->dequeue();
- if (isset($this->addedMap[$package->getId()])) {
- continue;
- }
- $this->addedMap[$package->getId()] = true;
- if (!$this->policy->installable($this, $this->pool, $this->installedMap, $package)) {
- $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRemoveRule($package, Rule::RULE_NOT_INSTALLABLE, (string) $package));
- continue;
- }
- foreach ($package->getRequires() as $link) {
- $possibleRequires = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
- $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createRequireRule($package, $possibleRequires, Rule::RULE_PACKAGE_REQUIRES, (string) $link));
- foreach ($possibleRequires as $require) {
- $workQueue->enqueue($require);
- }
- }
- foreach ($package->getConflicts() as $link) {
- $possibleConflicts = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
- foreach ($possibleConflicts as $conflict) {
- $this->addRule(RuleSet::TYPE_PACKAGE, $this->createConflictRule($package, $conflict, Rule::RULE_PACKAGE_CONFLICT, (string) $link));
- }
- }
- // check obsoletes and implicit obsoletes of a package
- $isInstalled = (isset($this->installedMap[$package->getId()]));
- foreach ($package->getReplaces() as $link) {
- $obsoleteProviders = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
- foreach ($obsoleteProviders as $provider) {
- if ($provider === $package) {
- continue;
- }
- $reason = ($isInstalled) ? Rule::RULE_INSTALLED_PACKAGE_OBSOLETES : Rule::RULE_PACKAGE_OBSOLETES;
- $this->addRule(RuleSet::TYPE_PACKAGE, $this->createConflictRule($package, $provider, $reason, (string) $link));
- }
- }
- // check implicit obsoletes
- // for installed packages we only need to check installed/installed problems,
- // as the others are picked up when looking at the uninstalled package.
- if (!$isInstalled) {
- $obsoleteProviders = $this->pool->whatProvides($package->getName(), null);
- foreach ($obsoleteProviders as $provider) {
- if ($provider === $package) {
- continue;
- }
- if ($isInstalled && !isset($this->installedMap[$provider->getId()])) {
- continue;
- }
- $reason = ($package->getName() == $provider->getName()) ? Rule::RULE_PACKAGE_SAME_NAME : Rule::RULE_PACKAGE_IMPLICIT_OBSOLETES;
- $this->addRule(RuleSet::TYPE_PACKAGE, $rule = $this->createConflictRule($package, $provider, $reason, (string) $package));
- }
- }
- }
- }
- /**
- * Adds all rules for all update packages of a given package
- *
- * @param PackageInterface $package Rules for this package's updates are to
- * be added
- * @param bool $allowAll Whether downgrades are allowed
- */
- private function addRulesForUpdatePackages(PackageInterface $package)
- {
- $updates = $this->policy->findUpdatePackages($this, $this->pool, $this->installedMap, $package);
- $this->addRulesForPackage($package);
- foreach ($updates as $update) {
- $this->addRulesForPackage($update);
- }
- }
- /**
- * Alters watch chains for a rule.
- *
- * Next1/2 always points to the next rule that is watching the same package.
- * The watches array contains rules to start from for each package
- *
- */
- private function addWatchesToRule(Rule $rule)
- {
- // skip simple assertions of the form (A) or (-A)
- if ($rule->isAssertion()) {
- return;
- }
- if (!isset($this->watches[$rule->watch1])) {
- $this->watches[$rule->watch1] = null;
- }
- $rule->next1 = $this->watches[$rule->watch1];
- $this->watches[$rule->watch1] = $rule;
- if (!isset($this->watches[$rule->watch2])) {
- $this->watches[$rule->watch2] = null;
- }
- $rule->next2 = $this->watches[$rule->watch2];
- $this->watches[$rule->watch2] = $rule;
- }
- /**
- * Put watch2 on rule's literal with highest level
- */
- private function watch2OnHighest(Rule $rule)
- {
- $literals = $rule->getLiterals();
- // if there are only 2 elements, both are being watched anyway
- if ($literals < 3) {
- return;
- }
- $watchLevel = 0;
- foreach ($literals as $literal) {
- $level = abs($this->decisionMap[$literal->getPackageId()]);
- if ($level > $watchLevel) {
- $rule->watch2 = $literal->getId();
- $watchLevel = $level;
- }
- }
- }
- private function findDecisionRule(PackageInterface $package)
- {
- foreach ($this->decisionQueue as $i => $literal) {
- if ($package === $literal->getPackage()) {
- return $this->decisionQueueWhy[$i];
- }
- }
- return null;
- }
- // aka solver_makeruledecisions
- private function makeAssertionRuleDecisions()
- {
- // do we need to decide a SYSTEMSOLVABLE at level 1?
- $decisionStart = count($this->decisionQueue);
- for ($ruleIndex = 0; $ruleIndex < count($this->rules); $ruleIndex++) {
- $rule = $this->rules->ruleById($ruleIndex);
- if ($rule->isWeak() || !$rule->isAssertion() || $rule->isDisabled()) {
- continue;
- }
- $literals = $rule->getLiterals();
- $literal = $literals[0];
- if (!$this->decided($literal->getPackage())) {
- $this->decisionQueue[] = $literal;
- $this->decisionQueueWhy[] = $rule;
- $this->addDecision($literal, 1);
- continue;
- }
- if ($this->decisionsSatisfy($literal)) {
- continue;
- }
- // found a conflict
- if (RuleSet::TYPE_LEARNED === $rule->getType()) {
- $rule->disable();
- continue;
- }
- $conflict = $this->findDecisionRule($literal->getPackage());
- /** TODO: handle conflict with systemsolvable? */
- if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
- $problem = new Problem;
- if ($rule->getType() == RuleSet::TYPE_JOB) {
- $job = $this->ruleToJob[$rule->getId()];
- $problem->addJobRule($job, $rule);
- $problem->addRule($conflict);
- $this->disableProblem($job);
- } else {
- $problem->addRule($rule);
- $problem->addRule($conflict);
- $this->disableProblem($rule);
- }
- $this->problems[] = $problem;
- continue;
- }
- // conflict with another job or update/feature rule
- $problem = new Problem;
- $problem->addRule($rule);
- $problem->addRule($conflict);
- // push all of our rules (can only be feature or job rules)
- // asserting this literal on the problem stack
- foreach ($this->rules->getIteratorFor(array(RuleSet::TYPE_JOB, RuleSet::TYPE_FEATURE)) as $assertRule) {
- if ($assertRule->isDisabled() || !$assertRule->isAssertion() || $assertRule->isWeak()) {
- continue;
- }
- $assertRuleLiterals = $assertRule->getLiterals();
- $assertRuleLiteral = $assertRuleLiterals[0];
- if ($literal->getPackageId() !== $assertRuleLiteral->getPackageId()) {
- continue;
- }
- if ($assertRule->getType() === RuleSet::TYPE_JOB) {
- $job = $this->ruleToJob[$assertRule->getId()];
- $problem->addJobRule($job, $assertRule);
- $this->disableProblem($job);
- } else {
- $problem->addRule($assertRule);
- $this->disableProblem($assertRule);
- }
- }
- $this->problems[] = $problem;
- // start over
- while (count($this->decisionQueue) > $decisionStart) {
- $decisionLiteral = array_pop($this->decisionQueue);
- array_pop($this->decisionQueueWhy);
- unset($this->decisionQueueFree[count($this->decisionQueue)]);
- $this->decisionMap[$decisionLiteral->getPackageId()] = 0;
- }
- $ruleIndex = -1;
- }
- foreach ($this->rules as $rule) {
- if (!$rule->isWeak() || !$rule->isAssertion() || $rule->isDisabled()) {
- continue;
- }
- $literals = $rule->getLiterals();
- $literal = $literals[0];
- if ($this->decisionMap[$literal->getPackageId()] == 0) {
- $this->decisionQueue[] = $literal;
- $this->decisionQueueWhy[] = $rule;
- $this->addDecision($literal, 1);
- continue;
- }
- if ($this->decisionsSatisfy($literals[0])) {
- continue;
- }
- // conflict, but this is a weak rule => disable
- if ($rule->getType() == RuleSet::TYPE_JOB) {
- $why = $this->ruleToJob[$rule->getId()];
- } else {
- $why = $rule;
- }
- $this->disableProblem($why);
- /** TODO solver_reenablepolicyrules(solv, -(v + 1)); */
- }
- }
- /***********************************************************************
- ***
- *** Policy rule disabling/reenabling
- ***
- *** Disable all policy rules that conflict with our jobs. If a job
- *** gets disabled later on, reenable the involved policy rules again.
- ***
- *** /
- #define DISABLE_UPDATE 1
- #define DISABLE_INFARCH 2
- #define DISABLE_DUP 3
- */
- protected function jobToDisableQueue(array $job, array $disableQueue)
- {
- switch ($job['cmd']) {
- case 'install':
- foreach ($job['packages'] as $package) {
- if (isset($this->installedMap[$package->getId()])) {
- $disableQueue[] = array('type' => 'update', 'package' => $package);
- }
- /* all job packages obsolete * /
- qstart = q->count;
- pass = 0;
- memset(&omap, 0, sizeof(omap));
- FOR_JOB_SELECT(p, pp, select, what)
- {
- Id p2, pp2;
- if (pass == 1)
- map_grow(&omap, installed->end - installed->start);
- s = pool->solvables + p;
- if (s->obsoletes)
- {
- Id obs, *obsp;
- obsp = s->repo->idarraydata + s->obsoletes;
- while ((obs = *obsp++) != 0)
- FOR_PROVIDES(p2, pp2, obs)
- {
- Solvable *ps = pool->solvables + p2;
- if (ps->repo != installed)
- continue;
- if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
- continue;
- if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
- continue;
- if (pass)
- MAPSET(&omap, p2 - installed->start);
- else
- queue_push2(q, DISABLE_UPDATE, p2);
- }
- }
- FOR_PROVIDES(p2, pp2, s->name)
- {
- Solvable *ps = pool->solvables + p2;
- if (ps->repo != installed)
- continue;
- if (!pool->implicitobsoleteusesprovides && ps->name != s->name)
- continue;
- if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
- continue;
- if (pass)
- MAPSET(&omap, p2 - installed->start);
- else
- queue_push2(q, DISABLE_UPDATE, p2);
- }
- if (pass)
- {
- for (i = j = qstart; i < q->count; i += 2)
- {
- if (MAPTST(&omap, q->elements[i + 1] - installed->start))
- {
- MAPCLR(&omap, q->elements[i + 1] - installed->start);
- q->elements[j + 1] = q->elements[i + 1];
- j += 2;
- }
- }
- queue_truncate(q, j);
- }
- if (q->count == qstart)
- break;
- pass++;
- }
- if (omap.size)
- map_free(&omap);
- if (qstart == q->count)
- return; /* nothing to prune * /
- if ((set & (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR)) == (SOLVER_SETEVR | SOLVER_SETARCH | SOLVER_SETVENDOR))
- return; /* all is set */
- /* now that we know which installed packages are obsoleted check each of them * /
- for (i = j = qstart; i < q->count; i += 2)
- {
- Solvable *is = pool->solvables + q->elements[i + 1];
- FOR_JOB_SELECT(p, pp, select, what)
- {
- int illegal = 0;
- s = pool->solvables + p;
- if ((set & SOLVER_SETEVR) != 0)
- illegal |= POLICY_ILLEGAL_DOWNGRADE; /* ignore * /
- if ((set & SOLVER_SETARCH) != 0)
- illegal |= POLICY_ILLEGAL_ARCHCHANGE; /* ignore * /
- if ((set & SOLVER_SETVENDOR) != 0)
- illegal |= POLICY_ILLEGAL_VENDORCHANGE; /* ignore * /
- illegal = policy_is_illegal(solv, is, s, illegal);
- if (illegal && illegal == POLICY_ILLEGAL_DOWNGRADE && (set & SOLVER_SETEV) != 0)
- {
- /* it's ok if the EV is different * /
- if (evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE_EVONLY) != 0)
- illegal = 0;
- }
- if (illegal)
- break;
- }
- if (!p)
- {
- /* no package conflicts with the update rule * /
- /* thus keep the DISABLE_UPDATE * /
- q->elements[j + 1] = q->elements[i + 1];
- j += 2;
- }
- }
- queue_truncate(q, j);
- return;*/
- }
- break;
- case 'remove':
- foreach ($job['packages'] as $package) {
- if (isset($this->installedMap[$package->getId()])) {
- $disableQueue[] = array('type' => 'update', 'package' => $package);
- }
- }
- break;
- }
- return $disableQueue;
- }
- protected function disableUpdateRule($package)
- {
- if (isset($this->packageToFeatureRule[$package->getId()])) {
- $this->packageToFeatureRule[$package->getId()]->disable();
- }
- }
- /**
- * Disables all policy rules that conflict with jobs
- */
- protected function disablePolicyRules()
- {
- $lastJob = null;
- $allQueue = array();
- $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
- foreach ($iterator as $rule) {
- if ($rule->isDisabled()) {
- continue;
- }
- $job = $this->ruleToJob[$rule->getId()];
- if ($job === $lastJob) {
- continue;
- }
- $lastJob = $job;
- $allQueue = $this->jobToDisableQueue($job, $allQueue);
- }
- foreach ($allQueue as $disable) {
- switch ($disable['type']) {
- case 'update':
- $this->disableUpdateRule($disable['package']);
- break;
- default:
- throw new \RuntimeException("Unsupported disable type: " . $disable['type']);
- }
- }
- }
- protected function setupInstalledMap()
- {
- $this->installedPackages = $this->installed->getPackages();
- $this->installedMap = array();
- foreach ($this->installedPackages as $package) {
- $this->installedMap[$package->getId()] = $package;
- }
- }
- public function solve(Request $request)
- {
- $this->jobs = $request->getJobs();
- $this->setupInstalledMap();
- if (version_compare(PHP_VERSION, '5.3.4', '>=')) {
- $this->decisionMap = new \SplFixedArray($this->pool->getMaxId() + 1);
- } else {
- $this->decisionMap = array_fill(0, $this->pool->getMaxId() + 1, 0);
- }
- foreach ($this->jobs as $job) {
- foreach ($job['packages'] as $package) {
- switch ($job['cmd']) {
- case 'update':
- if (isset($this->installedMap[$package->getId()])) {
- $this->updateMap[$package->getId()] = true;
- }
- break;
- }
- }
- switch ($job['cmd']) {
- case 'update-all':
- foreach ($this->installedMap as $package) {
- $this->updateMap[$package->getId()] = true;
- }
- break;
- }
- }
- foreach ($this->installedMap as $package) {
- $this->addRulesForPackage($package);
- }
- foreach ($this->installedMap as $package) {
- $this->addRulesForUpdatePackages($package);
- }
- foreach ($this->jobs as $job) {
- foreach ($job['packages'] as $package) {
- switch ($job['cmd']) {
- case 'install':
- $this->installCandidateMap[$package->getId()] = true;
- $this->addRulesForPackage($package);
- break;
- }
- }
- }
- // solver_addrpmrulesforweak(solv, &addedmap);
- foreach ($this->installedMap as $package) {
- $updates = $this->policy->findUpdatePackages($this, $this->pool, $this->installedMap, $package);
- $rule = $this->createUpdateRule($package, $updates, Rule::RULE_INTERNAL_ALLOW_UPDATE, (string) $package);
- $rule->setWeak(true);
- $this->addRule(RuleSet::TYPE_FEATURE, $rule);
- $this->packageToFeatureRule[$package->getId()] = $rule;
- }
- foreach ($this->jobs as $job) {
- switch ($job['cmd']) {
- case 'install':
- if (empty($job['packages'])) {
- $problem = new Problem();
- $problem->addJobRule($job);
- $this->problems[] = $problem;
- } else {
- $rule = $this->createInstallOneOfRule($job['packages'], Rule::RULE_JOB_INSTALL, $job['packageName']);
- $this->addRule(RuleSet::TYPE_JOB, $rule);
- $this->ruleToJob[$rule->getId()] = $job;
- }
- break;
- case 'remove':
- // remove all packages with this name including uninstalled
- // ones to make sure none of them are picked as replacements
- // todo: cleandeps
- foreach ($job['packages'] as $package) {
- $rule = $this->createRemoveRule($package, Rule::RULE_JOB_REMOVE);
- $this->addRule(RuleSet::TYPE_JOB, $rule);
- $this->ruleToJob[$rule->getId()] = $job;
- }
- break;
- case 'lock':
- foreach ($job['packages'] as $package) {
- if (isset($this->installedMap[$package->getId()])) {
- $rule = $this->createInstallRule($package, Rule::RULE_JOB_LOCK);
- } else {
- $rule = $this->createRemoveRule($package, Rule::RULE_JOB_LOCK);
- }
- $this->addRule(RuleSet::TYPE_JOB, $rule);
- $this->ruleToJob[$rule->getId()] = $job;
- }
- break;
- }
- }
- foreach ($this->rules as $rule) {
- $this->addWatchesToRule($rule);
- }
- /* disable update rules that conflict with our job */
- $this->disablePolicyRules();
- /* make decisions based on job/update assertions */
- $this->makeAssertionRuleDecisions();
- $installRecommended = 0;
- $this->runSat(true, $installRecommended);
- //$this->printDecisionMap();
- //findrecommendedsuggested(solv);
- //solver_prepare_solutions(solv);
- if ($this->problems) {
- throw new SolverProblemsException($this->problems);
- }
- return $this->createTransaction();
- }
- protected function createTransaction()
- {
- $transaction = array();
- $installMeansUpdateMap = array();
- foreach ($this->decisionQueue as $i => $literal) {
- $package = $literal->getPackage();
- // !wanted & installed
- if (!$literal->isWanted() && isset($this->installedMap[$package->getId()])) {
- $literals = array();
- if (isset($this->packageToFeatureRule[$package->getId()])) {
- $literals = array_merge($literals, $this->packageToFeatureRule[$package->getId()]->getLiterals());
- }
- foreach ($literals as $updateLiteral) {
- if (!$updateLiteral->equals($literal)) {
- $installMeansUpdateMap[$updateLiteral->getPackageId()] = $package;
- }
- }
- }
- }
- foreach ($this->decisionQueue as $i => $literal) {
- $package = $literal->getPackage();
- // wanted & installed || !wanted & !installed
- if ($literal->isWanted() == (isset($this->installedMap[$package->getId()]))) {
- continue;
- }
- if ($literal->isWanted()) {
- if (isset($installMeansUpdateMap[$literal->getPackageId()])) {
- $source = $installMeansUpdateMap[$literal->getPackageId()];
- $transaction[] = new Operation\UpdateOperation(
- $source, $package, $this->decisionQueueWhy[$i]
- );
- // avoid updates to one package from multiple origins
- unset($installMeansUpdateMap[$literal->getPackageId()]);
- $ignoreRemove[$source->getId()] = true;
- } else {
- $transaction[] = new Operation\InstallOperation(
- $package, $this->decisionQueueWhy[$i]
- );
- }
- } else if (!isset($ignoreRemove[$package->getId()])) {
- $transaction[] = new Operation\UninstallOperation(
- $package, $this->decisionQueueWhy[$i]
- );
- }
- }
- return array_reverse($transaction);
- }
- protected $decisionQueue = array();
- protected $decisionQueueWhy = array();
- protected $decisionQueueFree = array();
- protected $propagateIndex;
- protected $branches = array();
- protected $problems = array();
- protected $learnedPool = array();
- protected $recommendsIndex;
- protected function literalFromId($id)
- {
- $package = $this->pool->packageById(abs($id));
- return new Literal($package, $id > 0);
- }
- protected function addDecision(Literal $l, $level)
- {
- assert($this->decisionMap[$l->getPackageId()] == 0);
- if ($l->isWanted()) {
- $this->decisionMap[$l->getPackageId()] = $level;
- } else {
- $this->decisionMap[$l->getPackageId()] = -$level;
- }
- }
- protected function addDecisionId($literalId, $level)
- {
- $packageId = abs($literalId);
- assert($this->decisionMap[$packageId] == 0);
- if ($literalId > 0) {
- $this->decisionMap[$packageId] = $level;
- } else {
- $this->decisionMap[$packageId] = -$level;
- }
- }
- protected function decisionsContain(Literal $l)
- {
- return (
- $this->decisionMap[$l->getPackageId()] > 0 && $l->isWanted() ||
- $this->decisionMap[$l->getPackageId()] < 0 && !$l->isWanted()
- );
- }
- protected function decisionsContainId($literalId)
- {
- $packageId = abs($literalId);
- return (
- $this->decisionMap[$packageId] > 0 && $literalId > 0 ||
- $this->decisionMap[$packageId] < 0 && $literalId < 0
- );
- }
- protected function decisionsSatisfy(Literal $l)
- {
- return ($l->isWanted() && $this->decisionMap[$l->getPackageId()] > 0) ||
- (!$l->isWanted() && $this->decisionMap[$l->getPackageId()] <= 0);
- }
- protected function decisionsConflict(Literal $l)
- {
- return (
- $this->decisionMap[$l->getPackageId()] > 0 && !$l->isWanted() ||
- $this->decisionMap[$l->getPackageId()] < 0 && $l->isWanted()
- );
- }
- protected function decisionsConflictId($literalId)
- {
- $packageId = abs($literalId);
- return (
- ($this->decisionMap[$packageId] > 0 && $literalId < 0) ||
- ($this->decisionMap[$packageId] < 0 && $literalId > 0)
- );
- }
- protected function decided(PackageInterface $p)
- {
- return $this->decisionMap[$p->getId()] != 0;
- }
- protected function undecided(PackageInterface $p)
- {
- return $this->decisionMap[$p->getId()] == 0;
- }
- protected function decidedInstall(PackageInterface $p) {
- return $this->decisionMap[$p->getId()] > 0;
- }
- protected function decidedRemove(PackageInterface $p) {
- return $this->decisionMap[$p->getId()] < 0;
- }
- /**
- * Makes a decision and propagates it to all rules.
- *
- * Evaluates each term affected by the decision (linked through watches)
- * If we find unit rules we make new decisions based on them
- *
- * @return Rule|null A rule on conflict, otherwise null.
- */
- protected function propagate($level)
- {
- while ($this->propagateIndex < count($this->decisionQueue)) {
- // we invert the decided literal here, example:
- // A was decided => (-A|B) now requires B to be true, so we look for
- // rules which are fulfilled by -A, rather than A.
- $literal = $this->decisionQueue[$this->propagateIndex]->inverted();
- $this->propagateIndex++;
- // /* foreach rule where 'pkg' is now FALSE */
- //for (rp = watches + pkg; *rp; rp = next_rp)
- if (!isset($this->watches[$literal->getId()])) {
- continue;
- }
- $prevRule = null;
- for ($rule = $this->watches[$literal->getId()]; $rule !== null; $prevRule = $rule, $rule = $nextRule) {
- $nextRule = $rule->getNext($literal);
- if ($rule->isDisabled()) {
- continue;
- }
- $otherWatch = $rule->getOtherWatch($literal);
- if ($this->decisionsContainId($otherWatch)) {
- continue;
- }
- $ruleLiterals = $rule->getLiterals();
- if (sizeof($ruleLiterals) > 2) {
- foreach ($ruleLiterals as $ruleLiteral) {
- if ($otherWatch !== $ruleLiteral->getId() &&
- !$this->decisionsConflict($ruleLiteral)) {
- if ($literal->getId() === $rule->watch1) {
- $rule->watch1 = $ruleLiteral->getId();
- $rule->next1 = (isset($this->watches[$ruleLiteral->getId()])) ? $this->watches[$ruleLiteral->getId()] : null;
- } else {
- $rule->watch2 = $ruleLiteral->getId();
- $rule->next2 = (isset($this->watches[$ruleLiteral->getId()])) ? $this->watches[$ruleLiteral->getId()] : null;
- }
- if ($prevRule) {
- if ($prevRule->next1 == $rule) {
- $prevRule->next1 = $nextRule;
- } else {
- $prevRule->next2 = $nextRule;
- }
- } else {
- $this->watches[$literal->getId()] = $nextRule;
- }
- $this->watches[$ruleLiteral->getId()] = $rule;
- $rule = $prevRule;
- continue 2;
- }
- }
- }
- // yay, we found a unit clause! try setting it to true
- if ($this->decisionsConflictId($otherWatch)) {
- return $rule;
- }
- $this->addDecisionId($otherWatch, $level);
- $this->decisionQueue[] = $this->literalFromId($otherWatch);
- $this->decisionQueueWhy[] = $rule;
- }
- }
- return null;
- }
- /**
- * Reverts a decision at the given level.
- */
- private function revert($level)
- {
- while (!empty($this->decisionQueue)) {
- $literal = $this->decisionQueue[count($this->decisionQueue) - 1];
- if (!$this->decisionMap[$literal->getPackageId()]) {
- break;
- }
- $decisionLevel = abs($this->decisionMap[$literal->getPackageId()]);
- if ($decisionLevel <= $level) {
- break;
- }
- /** TODO: implement recommendations
- *if (v > 0 && solv->recommendations.count && v == solv->recommendations.elements[solv->recommendations.count - 1])
- * solv->recommendations.count--;
- */
- $this->decisionMap[$literal->getPackageId()] = 0;
- array_pop($this->decisionQueue);
- array_pop($this->decisionQueueWhy);
- $this->propagateIndex = count($this->decisionQueue);
- }
- while (!empty($this->branches)) {
- list($literals, $branchLevel) = $this->branches[count($this->branches) - 1];
- if ($branchLevel >= $level) {
- break;
- }
- array_pop($this->branches);
- }
- $this->recommendsIndex = -1;
- }
- /**-------------------------------------------------------------------
- *
- * setpropagatelearn
- *
- * add free decision (solvable to install) to decisionq
- * increase level and propagate decision
- * return if no conflict.
- *
- * in conflict case, analyze conflict rule, add resulting
- * rule to learnt rule set, make decision from learnt
- * rule (always unit) and re-propagate.
- *
- * returns the new solver level or 0 if unsolvable
- *
- */
- private function setPropagateLearn($level, Literal $literal, $disableRules, Rule $rule)
- {
- assert($rule != null);
- assert($literal != null);
- $level++;
- $this->addDecision($literal, $level);
- $this->decisionQueue[] = $literal;
- $this->decisionQueueWhy[] = $rule;
- $this->decisionQueueFree[count($this->decisionQueueWhy) - 1] = true;
- while (true) {
- $rule = $this->propagate($level);
- if (!$rule) {
- break;
- }
- if ($level == 1) {
- return $this->analyzeUnsolvable($rule, $disableRules);
- }
- // conflict
- list($learnLiteral, $newLevel, $newRule, $why) = $this->analyze($level, $rule);
- assert($newLevel > 0);
- assert($newLevel < $level);
- $level = $newLevel;
- $this->revert($level);
- assert($newRule != null);
- $this->addRule(RuleSet::TYPE_LEARNED, $newRule);
- $this->learnedWhy[$newRule->getId()] = $why;
- $this->watch2OnHighest($newRule);
- $this->addWatchesToRule($newRule);
- $this->addDecision($learnLiteral, $level);
- $this->decisionQueue[] = $learnLiteral;
- $this->decisionQueueWhy[] = $newRule;
- }
- return $level;
- }
- private function selectAndInstall($level, array $decisionQueue, $disableRules, Rule $rule)
- {
- // choose best package to install from decisionQueue
- $literals = $this->policy->selectPreferedPackages($this->pool, $this->installedMap, $decisionQueue);
- $selectedLiteral = array_shift($literals);
- // if there are multiple candidates, then branch
- if (count($literals)) {
- $this->branches[] = array($literals, $level);
- }
- return $this->setPropagateLearn($level, $selectedLiteral, $disableRules, $rule);
- }
- protected function analyze($level, $rule)
- {
- $ruleLevel = 1;
- $num = 0;
- $l1num = 0;
- $seen = array();
- $learnedLiterals = array(null);
- $decisionId = count($this->decisionQueue);
- $this->learnedPool[] = array();
- while(true) {
- $this->learnedPool[count($this->learnedPool) - 1][] = $rule;
- foreach ($rule->getLiterals() as $literal) {
- // skip the one true literal
- if ($this->decisionsSatisfy($literal)) {
- continue;
- }
- if (isset($seen[$literal->getPackageId()])) {
- continue;
- }
- $seen[$literal->getPackageId()] = true;
- $l = abs($this->decisionMap[$literal->getPackageId()]);
- if (1 === $l) {
- $l1num++;
- } else if ($level === $l) {
- $num++;
- } else {
- // not level1 or conflict level, add to new rule
- $learnedLiterals[] = $literal;
- if ($l > $ruleLevel) {
- $ruleLevel = $l;
- }
- }
- }
- $l1retry = true;
- while ($l1retry) {
- $l1retry = false;
- if (!$num && !--$l1num) {
- // all level 1 literals done
- break 2;
- }
- while (true) {
- assert($decisionId > 0);
- $decisionId--;
- $literal = $this->decisionQueue[$decisionId];
- if (isset($seen[$literal->getPackageId()])) {
- break;
- }
- }
- unset($seen[$literal->getPackageId()]);
- if ($num && 0 === --$num) {
- $learnedLiterals[0] = $this->literalFromId(-$literal->getPackageId());
- if (!$l1num) {
- break 2;
- }
- foreach ($learnedLiterals as $i => $learnedLiteral) {
- if ($i !== 0) {
- unset($seen[$literal->getPackageId()]);
- }
- }
- // only level 1 marks left
- $l1num++;
- $l1retry = true;
- }
- $rule = $this->decisionQueueWhy[$decisionId];
- }
- }
- $why = count($this->learnedPool) - 1;
- assert($learnedLiterals[0] !== null);
- $newRule = new Rule($learnedLiterals, Rule::RULE_LEARNED, $why);
- return array($learnedLiterals[0], $ruleLevel, $newRule, $why);
- }
- private function analyzeUnsolvableRule($problem, $conflictRule, &$lastWeakWhy)
- {
- $why = $conflictRule->getId();
- if ($conflictRule->getType() == RuleSet::TYPE_LEARNED) {
- $learnedWhy = $this->learnedWhy[$why];
- $problemRules = $this->learnedPool[$learnedWhy];
- foreach ($problemRules as $problemRule) {
- $this->analyzeUnsolvableRule($problem, $problemRule, $lastWeakWhy);
- }
- return;
- }
- if ($conflictRule->getType() == RuleSet::TYPE_PACKAGE) {
- // package rules cannot be part of a problem
- return;
- }
- if ($conflictRule->isWeak()) {
- /** TODO why > or < lastWeakWhy? */
- if (!$lastWeakWhy || $why > $lastWeakWhy->getId()) {
- $lastWeakWhy = $conflictRule;
- }
- }
- if ($conflictRule->getType() == RuleSet::TYPE_JOB) {
- $job = $this->ruleToJob[$conflictRule->getId()];
- $problem->addJobRule($job, $conflictRule);
- } else {
- $problem->addRule($conflictRule);
- }
- }
- private function analyzeUnsolvable($conflictRule, $disableRules)
- {
- $lastWeakWhy = null;
- $problem = new Problem;
- $problem->addRule($conflictRule);
- $this->analyzeUnsolvableRule($problem, $conflictRule, $lastWeakWhy);
- $this->problems[] = $problem;
- $seen = array();
- $literals = $conflictRule->getLiterals();
- /* unnecessary because unlike rule.d, watch2 == 2nd literal, unless watch2 changed
- if (sizeof($literals) == 2) {
- $literals[1] = $this->literalFromId($conflictRule->watch2);
- }
- */
- foreach ($literals as $literal) {
- // skip the one true literal
- if ($this->decisionsSatisfy($literal)) {
- continue;
- }
- $seen[$literal->getPackageId()] = true;
- }
- $decisionId = count($this->decisionQueue);
- while ($decisionId > 0) {
- $decisionId--;
- $literal = $this->decisionQueue[$decisionId];
- // skip literals that are not in this rule
- if (!isset($seen[$literal->getPackageId()])) {
- continue;
- }
- $why = $this->decisionQueueWhy[$decisionId];
- $problem->addRule($why);
- $this->analyzeUnsolvableRule($problem, $why, $lastWeakWhy);
- $literals = $why->getLiterals();
- /* unnecessary because unlike rule.d, watch2 == 2nd literal, unless watch2 changed
- if (sizeof($literals) == 2) {
- $literals[1] = $this->literalFromId($why->watch2);
- }
- */
- foreach ($literals as $literal) {
- // skip the one true literal
- if ($this->decisionsSatisfy($literal)) {
- continue;
- }
- $seen[$literal->getPackageId()] = true;
- }
- }
- if ($lastWeakWhy) {
- array_pop($this->problems);
- if ($lastWeakWhy->getType() === RuleSet::TYPE_JOB) {
- $why = $this->ruleToJob[$lastWeakWhy];
- } else {
- $why = $lastWeakWhy;
- }
- $this->disableProblem($why);
- /**
- @TODO what does v < 0 mean here? ($why == v)
- if (v < 0)
- solver_reenablepolicyrules(solv, -(v + 1));
- */
- $this->resetSolver();
- return true;
- }
- if ($disableRules) {
- foreach ($this->problems[count($this->problems) - 1] as $reason) {
- if ($reason['job']) {
- $this->disableProblem($reason['job']);
- } else {
- $this->disableProblem($reason['rule']);
- }
- }
- $this->resetSolver();
- return true;
- }
- return false;
- }
- private function disableProblem($why)
- {
- if ($why instanceof Rule) {
- $why->disable();
- } else if (is_array($why)) {
- // disable all rules of this job
- foreach ($this->ruleToJob as $ruleId => $job) {
- if ($why === $job) {
- $this->rules->ruleById($ruleId)->disable();
- }
- }
- }
- }
- private function resetSolver()
- {
- while ($literal = array_pop($this->decisionQueue)) {
- $this->decisionMap[$literal->getPackageId()] = 0;
- }
- $this->decisionQueueWhy = array();
- $this->decisionQueueFree = array();
- $this->recommendsIndex = -1;
- $this->propagateIndex = 0;
- $this->recommendations = array();
- $this->branches = array();
- $this->enableDisableLearnedRules();
- $this->makeAssertionRuleDecisions();
- }
- /*-------------------------------------------------------------------
- * enable/disable learnt rules
- *
- * we have enabled or disabled some of our rules. We now reenable all
- * of our learnt rules except the ones that were learnt from rules that
- * are now disabled.
- */
- private function enableDisableLearnedRules()
- {
- foreach ($this->rules->getIteratorFor(RuleSet::TYPE_LEARNED) as $rule) {
- $why = $this->learnedWhy[$rule->getId()];
- $problemRules = $this->learnedPool[$why];
- $foundDisabled = false;
- foreach ($problemRules as $problemRule) {
- if ($problemRule->isDisabled()) {
- $foundDisabled = true;
- break;
- }
- }
- if ($foundDisabled && $rule->isEnabled()) {
- $rule->disable();
- } else if (!$foundDisabled && $rule->isDisabled()) {
- $rule->enable();
- }
- }
- }
- private function runSat($disableRules = true, $installRecommended = false)
- {
- $this->propagateIndex = 0;
- // /*
- // * here's the main loop:
- // * 1) propagate new decisions (only needed once)
- // * 2) fulfill jobs
- // * 3) try to keep installed packages
- // * 4) fulfill all unresolved rules
- // * 5) install recommended packages
- // * 6) minimalize solution if we had choices
- // * if we encounter a problem, we rewind to a safe level and restart
- // * with step 1
- // */
- $decisionQueue = array();
- $decisionSupplementQueue = array();
- $disableRules = array();
- $level = 1;
- $systemLevel = $level + 1;
- $minimizationSteps = 0;
- $installedPos = 0;
- while (true) {
- if (1 === $level) {
- $conflictRule = $this->propagate($level);
- if ($conflictRule !== null) {
- if ($this->analyzeUnsolvable($conflictRule, $disableRules)) {
- continue;
- } else {
- return;
- }
- }
- }
- // handle job rules
- if ($level < $systemLevel) {
- $iterator = $this->rules->getIteratorFor(RuleSet::TYPE_JOB);
- foreach ($iterator as $rule) {
- if ($rule->isEnabled()) {
- $decisionQueue = array();
- $noneSatisfied = true;
- foreach ($rule->getLiterals() as $literal) {
- if ($this->decisionsSatisfy($literal)) {
- $noneSatisfied = false;
- break;
- }
- $decisionQueue[] = $literal;
- }
- if ($noneSatisfied && count($decisionQueue)) {
- // prune all update packages until installed version
- // except for requested updates
- if (count($this->installed) != count($this->updateMap)) {
- $prunedQueue = array();
- foreach ($decisionQueue as $literal) {
- if (isset($this->installedMap[$literal->getPackageId()])) {
- $prunedQueue[] = $literal;
- if (isset($this->updateMap[$literal->getPackageId()])) {
- $prunedQueue = $decisionQueue;
- break;
- }
- }
- }
- $decisionQueue = $prunedQueue;
- }
- }
- if ($noneSatisfied && count($decisionQueue)) {
- $oLevel = $level;
- $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
- if (0 === $level) {
- return;
- }
- if ($level <= $oLevel) {
- break;
- }
- }
- }
- }
- $systemLevel = $level + 1;
- // jobs left
- $iterator->next();
- if ($iterator->valid()) {
- continue;
- }
- }
- // handle installed packages
- if ($level < $systemLevel) {
- // use two passes if any packages are being updated
- // -> better user experience
- for ($pass = (count($this->updateMap)) ? 0 : 1; $pass < 2; $pass++) {
- $passLevel = $level;
- for ($i = $installedPos, $n = 0; $n < count($this->installedPackages); $i++, $n++) {
- $repeat = false;
- if ($i == count($this->installedPackages)) {
- $i = 0;
- }
- $literal = new Literal($this->installedPackages[$i], true);
- if ($this->decisionsContain($literal)) {
- continue;
- }
- // only process updates in first pass
- /** TODO: && or || ? **/
- if (0 === $pass && !isset($this->updateMap[$literal->getPackageId()])) {
- continue;
- }
- $rule = null;
- if (isset($this->packageToFeatureRule[$literal->getPackageId()])) {
- $rule = $this->packageToFeatureRule[$literal->getPackageId()];
- }
- if (!$rule || $rule->isDisabled()) {
- continue;
- }
- $updateRuleLiterals = $rule->getLiterals();
- $decisionQueue = array();
- if (!isset($this->noUpdate[$literal->getPackageId()]) && (
- $this->decidedRemove($literal->getPackage()) ||
- isset($this->updateMap[$literal->getPackageId()]) ||
- !$literal->equals($updateRuleLiterals[0])
- )) {
- foreach ($updateRuleLiterals as $ruleLiteral) {
- if ($this->decidedInstall($ruleLiteral->getPackage())) {
- // already fulfilled
- $decisionQueue = array();
- break;
- }
- if ($this->undecided($ruleLiteral->getPackage())) {
- $decisionQueue[] = $ruleLiteral;
- }
- }
- }
- if (sizeof($decisionQueue)) {
- $oLevel = $level;
- $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
- if (0 === $level) {
- return;
- }
- if ($level <= $oLevel) {
- $repeat = true;
- }
- } else if (!$repeat && $this->undecided($literal->getPackage())) {
- // still undecided? keep package.
- $oLevel = $level;
- if (isset($this->cleanDepsMap[$literal->getPackageId()])) {
- // clean deps removes package
- $level = $this->setPropagateLearn($level, $literal->invert(), $disableRules, null);
- } else {
- // ckeeping package
- $level = $this->setPropagateLearn($level, $literal, $disableRules, $rule);
- }
- if (0 === $level) {
- return;
- }
- if ($level <= $oLevel) {
- $repeat = true;
- }
- }
- if ($repeat) {
- if (1 === $level || $level < $passLevel) {
- // trouble
- break;
- }
- if ($level < $oLevel) {
- // redo all
- $n = 0;
- }
- // repeat
- $i--;
- $n--;
- continue;
- }
- }
- if ($n < count($this->installedPackages)) {
- $installedPos = $i; // retry this problem next time
- break;
- }
- $installedPos = 0;
- }
- $systemLevel = $level + 1;
- if ($pass < 2) {
- // had trouble => retry
- continue;
- }
- }
- if ($level < $systemLevel) {
- $systemLevel = $level;
- }
- for ($i = 0, $n = 0; $n < count($this->rules); $i++, $n++) {
- if ($i == count($this->rules)) {
- $i = 0;
- }
- $rule = $this->rules->ruleById($i);
- $literals = $rule->getLiterals();
- if ($rule->isDisabled()) {
- continue;
- }
- $decisionQueue = array();
- // make sure that
- // * all negative literals are installed
- // * no positive literal is installed
- // i.e. the rule is not fulfilled and we
- // just need to decide on the positive literals
- //
- foreach ($literals as $literal) {
- if (!$literal->isWanted()) {
- if (!$this->decidedInstall($literal->getPackage())) {
- continue 2; // next rule
- }
- } else {
- if ($this->decidedInstall($literal->getPackage())) {
- continue 2; // next rule
- }
- if ($this->undecided($literal->getPackage())) {
- $decisionQueue[] = $literal;
- }
- }
- }
- // need to have at least 2 item to pick from
- if (count($decisionQueue) < 2) {
- continue;
- }
- $oLevel = $level;
- $level = $this->selectAndInstall($level, $decisionQueue, $disableRules, $rule);
- if (0 === $level) {
- return;
- }
- // open suse sat-solver uses this, but why is $level == 1 trouble?
- // SYSTEMSOLVABLE related? we don't have that, so should work
- //if ($level < $systemLevel || $level == 1) {
- if ($level < $systemLevel) {
- break; // trouble
- }
- // something changed, so look at all rules again
- $n = -1;
- }
- // minimization step
- if (count($this->branches)) {
- $lastLiteral = null;
- $lastLevel = null;
- $lastBranchIndex = 0;
- $lastBranchOffset = 0;
- for ($i = count($this->branches) - 1; $i >= 0; $i--) {
- list($literals, $level) = $this->branches[$i];
- foreach ($literals as $offset => $literal) {
- if ($literal && $literal->isWanted() && $this->decisionMap[$literal->getPackageId()] > $level + 1) {
- $lastLiteral = $literal;
- $lastBranchIndex = $i;
- $lastBranchOffset = $offset;
- $lastLevel = $level;
- }
- }
- }
- if ($lastLiteral) {
- $this->branches[$lastBranchIndex][$lastBranchOffset] = null;
- $minimizationSteps++;
- $level = $lastLevel;
- $this->revert($level);
- $why = $this->decisionQueueWhy[count($this->decisionQueueWhy) - 1];
- $oLevel = $level;
- $level = $this->setPropagateLearn($level, $lastLiteral, $disableRules, $why);
- if ($level == 0) {
- return;
- }
- continue;
- }
- }
- break;
- }
- }
- private function printDecisionMap()
- {
- echo "\nDecisionMap: \n";
- foreach ($this->decisionMap as $packageId => $level) {
- if ($packageId === 0) {
- continue;
- }
- if ($level > 0) {
- echo ' +' . $this->pool->packageById($packageId)."\n";
- } elseif ($level < 0) {
- echo ' -' . $this->pool->packageById($packageId)."\n";
- } else {
- echo ' ?' . $this->pool->packageById($packageId)."\n";
- }
- }
- echo "\n";
- }
- private function printDecisionQueue()
- {
- echo "DecisionQueue: \n";
- foreach ($this->decisionQueue as $i => $literal) {
- echo ' ' . $literal . ' ' . $this->decisionQueueWhy[$i]." level ".$this->decisionMap[$literal->getPackageId()]."\n";
- }
- echo "\n";
- }
- private function printWatches()
- {
- echo "\nWatches:\n";
- foreach ($this->watches as $literalId => $watch) {
- echo ' '.$this->literalFromId($literalId)."\n";
- $queue = array(array(' ', $watch));
- while (!empty($queue)) {
- list($indent, $watch) = array_pop($queue);
- echo $indent.$watch;
- if ($watch) {
- echo ' [id='.$watch->getId().',watch1='.$this->literalFromId($watch->watch1).',watch2='.$this->literalFromId($watch->watch2)."]";
- }
- echo "\n";
- if ($watch && ($watch->next1 == $watch || $watch->next2 == $watch)) {
- if ($watch->next1 == $watch) {
- echo $indent." 1 *RECURSION*";
- }
- if ($watch->next2 == $watch) {
- echo $indent." 2 *RECURSION*";
- }
- } elseif ($watch && ($watch->next1 || $watch->next2)) {
- $indent = str_replace(array('1', '2'), ' ', $indent);
- array_push($queue, array($indent.' 2 ', $watch->next2));
- array_push($queue, array($indent.' 1 ', $watch->next1));
- }
- }
- echo "\n";
- }
- }
- }
|