1
0
Fork 0

Get rid of Literal object / literal id mix, use literals only to save memory

pull/715/head
Nils Adermann 2012-05-19 20:38:56 +02:00
parent 9ffe0d13f5
commit 451bab1c2c
14 changed files with 225 additions and 454 deletions

View File

@ -50,44 +50,44 @@ class DefaultPolicy implements PolicyInterface
public function selectPreferedPackages(Pool $pool, array $installedMap, array $literals) public function selectPreferedPackages(Pool $pool, array $installedMap, array $literals)
{ {
$packages = $this->groupLiteralsByNamePreferInstalled($installedMap, $literals); $packages = $this->groupLiteralsByNamePreferInstalled($pool,$installedMap, $literals);
foreach ($packages as &$literals) { foreach ($packages as &$literals) {
$policy = $this; $policy = $this;
usort($literals, function ($a, $b) use ($policy, $pool, $installedMap) { usort($literals, function ($a, $b) use ($policy, $pool, $installedMap) {
return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $a->getPackage(), $b->getPackage(), true); return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b), true);
}); });
} }
foreach ($packages as &$literals) { foreach ($packages as &$literals) {
$literals = $this->pruneToBestVersion($literals); $literals = $this->pruneToBestVersion($pool, $literals);
$literals = $this->pruneToHighestPriorityOrInstalled($pool, $installedMap, $literals); $literals = $this->pruneToHighestPriorityOrInstalled($pool, $installedMap, $literals);
$literals = $this->pruneRemoteAliases($literals); $literals = $this->pruneRemoteAliases($pool, $literals);
} }
$selected = call_user_func_array('array_merge', $packages); $selected = call_user_func_array('array_merge', $packages);
// now sort the result across all packages to respect replaces across packages // now sort the result across all packages to respect replaces across packages
usort($selected, function ($a, $b) use ($policy, $pool, $installedMap) { usort($selected, function ($a, $b) use ($policy, $pool, $installedMap) {
return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $a->getPackage(), $b->getPackage()); return $policy->compareByPriorityPreferInstalled($pool, $installedMap, $pool->literalToPackage($a), $pool->literalToPackage($b));
}); });
return $selected; return $selected;
} }
protected function groupLiteralsByNamePreferInstalled(array $installedMap, $literals) protected function groupLiteralsByNamePreferInstalled(Pool $pool, array $installedMap, $literals)
{ {
$packages = array(); $packages = array();
foreach ($literals as $literal) { foreach ($literals as $literal) {
$packageName = $literal->getPackage()->getName(); $packageName = $pool->literalToPackage($literal)->getName();
if (!isset($packages[$packageName])) { if (!isset($packages[$packageName])) {
$packages[$packageName] = array(); $packages[$packageName] = array();
} }
if (isset($installedMap[$literal->getPackageId()])) { if (isset($installedMap[abs($literal)])) {
array_unshift($packages[$packageName], $literal); array_unshift($packages[$packageName], $literal);
} else { } else {
$packages[$packageName][] = $literal; $packages[$packageName][] = $literal;
@ -165,19 +165,21 @@ class DefaultPolicy implements PolicyInterface
return false; return false;
} }
protected function pruneToBestVersion($literals) protected function pruneToBestVersion(Pool $pool, $literals)
{ {
$bestLiterals = array($literals[0]); $bestLiterals = array($literals[0]);
$bestPackage = $literals[0]->getPackage(); $bestPackage = $pool->literalToPackage($literals[0]);
foreach ($literals as $i => $literal) { foreach ($literals as $i => $literal) {
if (0 === $i) { if (0 === $i) {
continue; continue;
} }
if ($this->versionCompare($literal->getPackage(), $bestPackage, '>')) { $package = $pool->literalToPackage($literal);
$bestPackage = $literal->getPackage();
if ($this->versionCompare($package, $bestPackage, '>')) {
$bestPackage = $package;
$bestLiterals = array($literal); $bestLiterals = array($literal);
} else if ($this->versionCompare($literal->getPackage(), $bestPackage, '==')) { } else if ($this->versionCompare($package, $bestPackage, '==')) {
$bestLiterals[] = $literal; $bestLiterals[] = $literal;
} }
} }
@ -215,7 +217,7 @@ class DefaultPolicy implements PolicyInterface
$priority = null; $priority = null;
foreach ($literals as $literal) { foreach ($literals as $literal) {
$package = $literal->getPackage(); $package = $pool->literalToPackage($literal);
if (isset($installedMap[$package->getId()])) { if (isset($installedMap[$package->getId()])) {
$selected[] = $literal; $selected[] = $literal;
@ -241,12 +243,12 @@ class DefaultPolicy implements PolicyInterface
* *
* If no package is a local alias, nothing happens * If no package is a local alias, nothing happens
*/ */
protected function pruneRemoteAliases(array $literals) protected function pruneRemoteAliases(Pool $pool, array $literals)
{ {
$hasLocalAlias = false; $hasLocalAlias = false;
foreach ($literals as $literal) { foreach ($literals as $literal) {
$package = $literal->getPackage(); $package = $pool->literalToPackage($literal);
if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
$hasLocalAlias = true; $hasLocalAlias = true;
@ -260,7 +262,7 @@ class DefaultPolicy implements PolicyInterface
$selected = array(); $selected = array();
foreach ($literals as $literal) { foreach ($literals as $literal) {
$package = $literal->getPackage(); $package = $pool->literalToPackage($literal);
if ($package instanceof AliasPackage && $package->isRootPackageAlias()) { if ($package instanceof AliasPackage && $package->isRootPackageAlias()) {
$selected[] = $literal; $selected[] = $literal;

View File

@ -1,67 +0,0 @@
<?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\Package\PackageInterface;
/**
* @author Nils Adermann <naderman@naderman.de>
*/
class Literal
{
protected $package;
protected $wanted;
protected $id;
public function __construct(PackageInterface $package, $wanted)
{
$this->package = $package;
$this->wanted = $wanted;
$this->id = ($this->wanted ? '' : '-') . $this->package->getId();
}
public function isWanted()
{
return $this->wanted;
}
public function getPackage()
{
return $this->package;
}
public function getPackageId()
{
return $this->package->getId();
}
public function getId()
{
return $this->id;
}
public function __toString()
{
return ($this->wanted ? '+' : '-') . $this->getPackage();
}
public function inverted()
{
return new Literal($this->getPackage(), !$this->isWanted());
}
public function equals(Literal $b)
{
return $this->id === $b->id;
}
}

View File

@ -151,4 +151,15 @@ class Pool
return $result; return $result;
} }
public function literalToPackage($literal)
{
$packageId = abs($literal);
return $this->packageById($packageId);
}
public function literalToString($literal)
{
return ($literal > 0 ? '+' : '-') . $this->literalToPackage($literal);
}
} }

View File

@ -29,6 +29,8 @@ class Rule
const RULE_LEARNED = 12; const RULE_LEARNED = 12;
const RULE_PACKAGE_ALIAS = 13; const RULE_PACKAGE_ALIAS = 13;
protected $pool;
protected $disabled; protected $disabled;
protected $literals; protected $literals;
protected $type; protected $type;
@ -37,18 +39,14 @@ class Rule
protected $job; protected $job;
public $watch1;
public $watch2;
public $next1;
public $next2;
public $ruleHash; public $ruleHash;
public function __construct(array $literals, $reason, $reasonData, $job = null) public function __construct(Pool $pool, array $literals, $reason, $reasonData, $job = null)
{ {
$this->pool = $pool;
// sort all packages ascending by id // sort all packages ascending by id
usort($literals, array($this, 'compareLiteralsById')); sort($literals);
$this->literals = $literals; $this->literals = $literals;
$this->reason = $reason; $this->reason = $reason;
@ -59,14 +57,9 @@ class Rule
$this->job = $job; $this->job = $job;
$this->watch1 = (count($this->literals) > 0) ? $literals[0]->getId() : 0;
$this->watch2 = (count($this->literals) > 1) ? $literals[1]->getId() : 0;
$this->type = -1; $this->type = -1;
$this->ruleHash = substr(md5(implode(',', array_map(function ($l) { $this->ruleHash = substr(md5(implode(',', $this->literals)), 0, 5);
return $l->getId();
}, $this->literals))), 0, 5);
} }
public function getHash() public function getHash()
@ -108,7 +101,7 @@ class Rule
} }
for ($i = 0, $n = count($this->literals); $i < $n; $i++) { for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
if ($this->literals[$i]->getId() !== $rule->literals[$i]->getId()) { if ($this->literals[$i] !== $rule->literals[$i]) {
return false; return false;
} }
} }
@ -166,24 +159,6 @@ class Rule
return 1 === count($this->literals); return 1 === count($this->literals);
} }
public function getNext(Literal $literal)
{
if ($this->watch1 == $literal->getId()) {
return $this->next1;
} else {
return $this->next2;
}
}
public function getOtherWatch(Literal $literal)
{
if ($this->watch1 == $literal->getId()) {
return $this->watch2;
} else {
return $this->watch1;
}
}
public function toHumanReadableString() public function toHumanReadableString()
{ {
$ruleText = ''; $ruleText = '';
@ -191,7 +166,7 @@ class Rule
if ($i != 0) { if ($i != 0) {
$ruleText .= '|'; $ruleText .= '|';
} }
$ruleText .= $literal; $ruleText .= $this->pool->literalToString($literal);
} }
switch ($this->reason) { switch ($this->reason) {
@ -205,18 +180,18 @@ class Rule
return "Remove command rule ($ruleText)"; return "Remove command rule ($ruleText)";
case self::RULE_PACKAGE_CONFLICT: case self::RULE_PACKAGE_CONFLICT:
$package1 = $this->literals[0]->getPackage(); $package1 = $this->pool->literalToPackage($this->literals[0]);
$package2 = $this->literals[1]->getPackage(); $package2 = $this->pool->literalToPackage($this->literals[1]);
return 'Package "'.$package1.'" conflicts with "'.$package2.'"'; return 'Package "'.$package1.'" conflicts with "'.$package2.'"';
case self::RULE_PACKAGE_REQUIRES: case self::RULE_PACKAGE_REQUIRES:
$literals = $this->literals; $literals = $this->literals;
$sourceLiteral = array_shift($literals); $sourceLiteral = array_shift($literals);
$sourcePackage = $sourceLiteral->getPackage(); $sourcePackage = $this->pool->literalToPackage($sourceLiteral);
$requires = array(); $requires = array();
foreach ($literals as $literal) { foreach ($literals as $literal) {
$requires[] = $literal->getPackage(); $requires[] = $this->pool->literalToPackage($literal);
} }
$text = 'Package "'.$sourcePackage.'" contains the rule '.$this->reasonData.'. '; $text = 'Package "'.$sourcePackage.'" contains the rule '.$this->reasonData.'. ';
@ -255,26 +230,11 @@ class Rule
if ($i != 0) { if ($i != 0) {
$result .= '|'; $result .= '|';
} }
$result .= $literal; $result .= $this->pool->literalToString($literal);
} }
$result .= ')'; $result .= ')';
return $result; return $result;
} }
/**
* Comparison function for sorting literals by their id
*
* @param Literal $a
* @param Literal $b
* @return int 0 if the literals are equal, 1 if b is larger than a, -1 else
*/
private function compareLiteralsById(Literal $a, Literal $b)
{
if ($a->getId() === $b->getId()) {
return 0;
}
return $a->getId() < $b->getId() ? -1 : 1;
}
} }

View File

@ -50,34 +50,17 @@ class RuleSetGenerator
*/ */
protected function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null) protected function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null)
{ {
$literals = array(new Literal($package, false)); $literals = array(-$package->getId());
foreach ($providers as $provider) { foreach ($providers as $provider) {
// self fulfilling rule? // self fulfilling rule?
if ($provider === $package) { if ($provider === $package) {
return null; return null;
} }
$literals[] = new Literal($provider, true); $literals[] = $provider->getId();
} }
return new Rule($literals, $reason, $reasonData); return new Rule($this->pool, $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));
} }
/** /**
@ -96,10 +79,10 @@ class RuleSetGenerator
{ {
$literals = array(); $literals = array();
foreach ($packages as $package) { foreach ($packages as $package) {
$literals[] = new Literal($package, true); $literals[] = $package->getId();
} }
return new Rule($literals, $reason, $job['packageName'], $job); return new Rule($this->pool, $literals, $reason, $job['packageName'], $job);
} }
/** /**
@ -115,7 +98,7 @@ class RuleSetGenerator
*/ */
protected function createRemoveRule(PackageInterface $package, $reason, $job) protected function createRemoveRule(PackageInterface $package, $reason, $job)
{ {
return new Rule(array(new Literal($package, false)), $reason, $job['packageName'], $job); return new Rule($this->pool, array(-$package->getId()), $reason, $job['packageName'], $job);
} }
/** /**
@ -139,7 +122,7 @@ class RuleSetGenerator
return null; return null;
} }
return new Rule(array(new Literal($issuer, false), new Literal($provider, false)), $reason, $reasonData); return new Rule($this->pool, array(-$issuer->getId(), -$provider->getId()), $reason, $reasonData);
} }
/** /**

View File

@ -38,33 +38,31 @@ class RuleWatchGraph
} }
} }
public function contains($literalId) public function contains($literal)
{ {
return isset($this->watches[$literalId]); return isset($this->watches[$literal]);
} }
public function walkLiteral($literalId, $level, $skipCallback, $conflictCallback, $decideCallback) public function walkLiteral($literal, $level, $skipCallback, $conflictCallback, $decideCallback)
{ {
if (!isset($this->watches[$literalId])) { if (!isset($this->watches[$literal])) {
return null; return null;
} }
$this->watches[$literalId]->rewind(); $this->watches[$literal]->rewind();
while ($this->watches[$literalId]->valid()) { while ($this->watches[$literal]->valid()) {
$node = $this->watches[$literalId]->current(); $node = $this->watches[$literal]->current();
$otherWatch = $node->getOtherWatch($literalId); $otherWatch = $node->getOtherWatch($literal);
if (!$node->getRule()->isDisabled() && !call_user_func($skipCallback, $otherWatch)) { if (!$node->getRule()->isDisabled() && !call_user_func($skipCallback, $otherWatch)) {
$ruleLiterals = $node->getRule()->getLiterals(); $ruleLiterals = $node->getRule()->getLiterals();
foreach ($ruleLiterals as $ruleLiteral) { foreach ($ruleLiterals as $ruleLiteral) {
$ruleLiteralId = $ruleLiteral->getId(); if ($literal !== $ruleLiteral &&
$otherWatch !== $ruleLiteral &&
!call_user_func($conflictCallback, $ruleLiteral)) {
if ($literalId !== $ruleLiteralId && $this->moveWatch($literal, $ruleLiteral, $node);
$otherWatch !== $ruleLiteralId &&
!call_user_func($conflictCallback, $ruleLiteralId)) {
$this->moveWatch($literalId, $ruleLiteralId, $node);
continue 2; continue 2;
} }
@ -77,7 +75,7 @@ class RuleWatchGraph
call_user_func($decideCallback, $otherWatch, $level, $node->getRule()); call_user_func($decideCallback, $otherWatch, $level, $node->getRule());
} }
$this->watches[$literalId]->next(); $this->watches[$literal]->next();
} }
return null; return null;

View File

@ -28,8 +28,8 @@ class RuleWatchNode
$literals = $rule->getLiterals(); $literals = $rule->getLiterals();
$this->watch1 = (count($literals) > 0) ? $literals[0]->getId() : 0; $this->watch1 = (count($literals) > 0) ? $literals[0] : 0;
$this->watch2 = (count($literals) > 1) ? $literals[1]->getId() : 0; $this->watch2 = (count($literals) > 1) ? $literals[1] : 0;
} }
/** /**
@ -47,10 +47,10 @@ class RuleWatchNode
$watchLevel = 0; $watchLevel = 0;
foreach ($literals as $literal) { foreach ($literals as $literal) {
$level = abs($decisionMap[$literal->getPackageId()]); $level = abs($decisionMap[abs($literal)]);
if ($level > $watchLevel) { if ($level > $watchLevel) {
$this->rule->watch2 = $literal->getId(); $this->rule->watch2 = $literal;
$watchLevel = $level; $watchLevel = $level;
} }
} }
@ -61,9 +61,9 @@ class RuleWatchNode
return $this->rule; return $this->rule;
} }
public function getOtherWatch($literalId) public function getOtherWatch($literal)
{ {
if ($this->watch1 == $literalId) { if ($this->watch1 == $literal) {
return $this->watch2; return $this->watch2;
} else { } else {
return $this->watch1; return $this->watch1;

View File

@ -13,7 +13,6 @@
namespace Composer\DependencyResolver; namespace Composer\DependencyResolver;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
use Composer\DependencyResolver\Operation; use Composer\DependencyResolver\Operation;
@ -51,10 +50,10 @@ class Solver
$this->ruleSetGenerator = new RuleSetGenerator($policy, $pool); $this->ruleSetGenerator = new RuleSetGenerator($policy, $pool);
} }
private function findDecisionRule(PackageInterface $package) private function findDecisionRule($packageId)
{ {
foreach ($this->decisionQueue as $i => $literal) { foreach ($this->decisionQueue as $i => $literal) {
if ($package === $literal->getPackage()) { if ($packageId === abs($literal)) {
return $this->decisionQueueWhy[$i]; return $this->decisionQueueWhy[$i];
} }
} }
@ -77,10 +76,8 @@ class Solver
$literals = $rule->getLiterals(); $literals = $rule->getLiterals();
$literal = $literals[0]; $literal = $literals[0];
if (!$this->decided($literal->getPackage())) { if (!$this->decided(abs($literal))) {
$this->decisionQueue[] = $literal; $this->decide($literal, 1, $rule);
$this->decisionQueueWhy[] = $rule;
$this->addDecision($literal, 1);
continue; continue;
} }
@ -94,7 +91,7 @@ class Solver
continue; continue;
} }
$conflict = $this->findDecisionRule($literal->getPackage()); $conflict = $this->findDecisionRule(abs($literal));
if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) { if ($conflict && RuleSet::TYPE_PACKAGE === $conflict->getType()) {
@ -122,7 +119,7 @@ class Solver
$assertRuleLiterals = $assertRule->getLiterals(); $assertRuleLiterals = $assertRule->getLiterals();
$assertRuleLiteral = $assertRuleLiterals[0]; $assertRuleLiteral = $assertRuleLiterals[0];
if ($literal->getPackageId() !== $assertRuleLiteral->getPackageId()) { if (abs($literal) !== abs($assertRuleLiteral)) {
continue; continue;
} }
@ -136,7 +133,7 @@ class Solver
$decisionLiteral = array_pop($this->decisionQueue); $decisionLiteral = array_pop($this->decisionQueue);
array_pop($this->decisionQueueWhy); array_pop($this->decisionQueueWhy);
unset($this->decisionQueueFree[count($this->decisionQueue)]); unset($this->decisionQueueFree[count($this->decisionQueue)]);
$this->decisionMap[$decisionLiteral->getPackageId()] = 0; $this->decisionMap[abs($decisionLiteral)] = 0;
} }
$ruleIndex = -1; $ruleIndex = -1;
} }
@ -149,10 +146,8 @@ class Solver
$literals = $rule->getLiterals(); $literals = $rule->getLiterals();
$literal = $literals[0]; $literal = $literals[0];
if ($this->decisionMap[$literal->getPackageId()] == 0) { if ($this->decisionMap[abs($literal)] == 0) {
$this->decisionQueue[] = $literal; $this->decide($literal, 1, $rule);
$this->decisionQueueWhy[] = $rule;
$this->addDecision($literal, 1);
continue; continue;
} }
@ -191,7 +186,7 @@ class Solver
case 'install': case 'install':
if (!$job['packages']) { if (!$job['packages']) {
$problem = new Problem(); $problem = new Problem();
$problem->addRule(new Rule(array(), null, null, $job)); $problem->addRule(new Rule($this->pool, array(), null, null, $job));
$this->problems[] = $problem; $this->problems[] = $problem;
} }
break; break;
@ -237,24 +232,20 @@ class Solver
return new Literal($package, $id > 0); return new Literal($package, $id > 0);
} }
protected function addDecision(Literal $l, $level) protected function addDecision($literal, $level)
{ {
$this->addDecisionId($l->getId(), $level); $packageId = abs($literal);
}
protected function addDecisionId($literalId, $level)
{
$packageId = abs($literalId);
$previousDecision = $this->decisionMap[$packageId]; $previousDecision = $this->decisionMap[$packageId];
if ($previousDecision != 0) { if ($previousDecision != 0) {
$literal = $this->literalFromId($literalId); $literalString = $this->pool->literalToString($literal);
$package = $this->pool->literalToPackage($literal);
throw new SolverBugException( throw new SolverBugException(
"Trying to decide $literal on level $level, even though ".$literal->getPackage()." was previously decided as ".(int) $previousDecision."." "Trying to decide $literalString on level $level, even though $package was previously decided as ".(int) $previousDecision."."
); );
} }
if ($literalId > 0) { if ($literal > 0) {
$this->decisionMap[$packageId] = $level; $this->decisionMap[$packageId] = $level;
} else { } else {
$this->decisionMap[$packageId] = -$level; $this->decisionMap[$packageId] = -$level;
@ -263,67 +254,49 @@ class Solver
public function decide($literal, $level, $why) public function decide($literal, $level, $why)
{ {
$this->addDecisionId($literal, $level); $this->addDecision($literal, $level);
$this->decisionQueue[] = $this->literalFromId($literal); $this->decisionQueue[] = $literal;
$this->decisionQueueWhy[] = $why; $this->decisionQueueWhy[] = $why;
} }
protected function decisionsContain(Literal $l) public function decisionsContain($literal)
{ {
$packageId = abs($literal);
return ( return (
$this->decisionMap[$l->getPackageId()] > 0 && $l->isWanted() || $this->decisionMap[$packageId] > 0 && $literal > 0 ||
$this->decisionMap[$l->getPackageId()] < 0 && !$l->isWanted() $this->decisionMap[$packageId] < 0 && $literal < 0
); );
} }
public function decisionsContainId($literalId) protected function decisionsSatisfy($literal)
{ {
$packageId = abs($literalId); $packageId = abs($literal);
return ( return (
$this->decisionMap[$packageId] > 0 && $literalId > 0 || $literal > 0 && $this->decisionMap[$packageId] > 0 ||
$this->decisionMap[$packageId] < 0 && $literalId < 0 $literal < 0 && $this->decisionMap[$packageId] < 0
); );
} }
protected function decisionsSatisfy(Literal $l) public function decisionsConflict($literal)
{
return ($l->isWanted() && $this->decisionMap[$l->getPackageId()] > 0) ||
(!$l->isWanted() && $this->decisionMap[$l->getPackageId()] < 0);
}
protected function decisionsConflict(Literal $l)
{ {
$packageId = abs($literal);
return ( return (
$this->decisionMap[$l->getPackageId()] > 0 && !$l->isWanted() || ($this->decisionMap[$packageId] > 0 && $literal < 0) ||
$this->decisionMap[$l->getPackageId()] < 0 && $l->isWanted() ($this->decisionMap[$packageId] < 0 && $literal > 0)
); );
} }
protected function decided($packageId)
public function decisionsConflictId($literalId)
{ {
$packageId = abs($literalId); return $this->decisionMap[$packageId] != 0;
return (
($this->decisionMap[$packageId] > 0 && $literalId < 0) ||
($this->decisionMap[$packageId] < 0 && $literalId > 0)
);
} }
protected function decided(PackageInterface $p) protected function undecided($packageId)
{ {
return $this->decisionMap[$p->getId()] != 0; return $this->decisionMap[$packageId] == 0;
} }
protected function undecided(PackageInterface $p) protected function decidedInstall($packageId) {
{ return $this->decisionMap[$packageId] > 0;
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;
} }
/** /**
@ -341,15 +314,15 @@ class Solver
// A was decided => (-A|B) now requires B to be true, so we look for // A was decided => (-A|B) now requires B to be true, so we look for
// rules which are fulfilled by -A, rather than A. // rules which are fulfilled by -A, rather than A.
$literal = $this->decisionQueue[$this->propagateIndex]->inverted(); $literal = -$this->decisionQueue[$this->propagateIndex];
$this->propagateIndex++; $this->propagateIndex++;
$conflict = $this->watchGraph->walkLiteral( $conflict = $this->watchGraph->walkLiteral(
$literal->getId(), $literal,
$level, $level,
array($this, 'decisionsContainId'), array($this, 'decisionsContain'),
array($this, 'decisionsConflictId'), array($this, 'decisionsConflict'),
array($this, 'decide') array($this, 'decide')
); );
@ -369,17 +342,17 @@ class Solver
while (!empty($this->decisionQueue)) { while (!empty($this->decisionQueue)) {
$literal = $this->decisionQueue[count($this->decisionQueue) - 1]; $literal = $this->decisionQueue[count($this->decisionQueue) - 1];
if (!$this->decisionMap[$literal->getPackageId()]) { if (!$this->decisionMap[abs($literal)]) {
break; break;
} }
$decisionLevel = abs($this->decisionMap[$literal->getPackageId()]); $decisionLevel = abs($this->decisionMap[abs($literal)]);
if ($decisionLevel <= $level) { if ($decisionLevel <= $level) {
break; break;
} }
$this->decisionMap[$literal->getPackageId()] = 0; $this->decisionMap[abs($literal)] = 0;
array_pop($this->decisionQueue); array_pop($this->decisionQueue);
array_pop($this->decisionQueueWhy); array_pop($this->decisionQueueWhy);
@ -412,13 +385,11 @@ class Solver
* returns the new solver level or 0 if unsolvable * returns the new solver level or 0 if unsolvable
* *
*/ */
private function setPropagateLearn($level, Literal $literal, $disableRules, Rule $rule) private function setPropagateLearn($level, $literal, $disableRules, Rule $rule)
{ {
$level++; $level++;
$this->addDecision($literal, $level); $this->decide($literal, $level, $rule);
$this->decisionQueue[] = $literal;
$this->decisionQueueWhy[] = $rule;
$this->decisionQueueFree[count($this->decisionQueueWhy) - 1] = true; $this->decisionQueueFree[count($this->decisionQueueWhy) - 1] = true;
while (true) { while (true) {
@ -457,9 +428,7 @@ class Solver
$ruleNode->watch2OnHighest($this->decisionMap); $ruleNode->watch2OnHighest($this->decisionMap);
$this->watchGraph->insert($ruleNode); $this->watchGraph->insert($ruleNode);
$this->addDecision($learnLiteral, $level); $this->decide($learnLiteral, $level, $newRule);
$this->decisionQueue[] = $learnLiteral;
$this->decisionQueueWhy[] = $newRule;
} }
return $level; return $level;
@ -502,12 +471,12 @@ class Solver
continue; continue;
} }
if (isset($seen[$literal->getPackageId()])) { if (isset($seen[abs($literal)])) {
continue; continue;
} }
$seen[$literal->getPackageId()] = true; $seen[abs($literal)] = true;
$l = abs($this->decisionMap[$literal->getPackageId()]); $l = abs($this->decisionMap[abs($literal)]);
if (1 === $l) { if (1 === $l) {
$l1num++; $l1num++;
@ -543,15 +512,15 @@ class Solver
$literal = $this->decisionQueue[$decisionId]; $literal = $this->decisionQueue[$decisionId];
if (isset($seen[$literal->getPackageId()])) { if (isset($seen[abs($literal)])) {
break; break;
} }
} }
unset($seen[$literal->getPackageId()]); unset($seen[abs($literal)]);
if ($num && 0 === --$num) { if ($num && 0 === --$num) {
$learnedLiterals[0] = $this->literalFromId(-$literal->getPackageId()); $learnedLiterals[0] = -abs($literal);
if (!$l1num) { if (!$l1num) {
break 2; break 2;
@ -559,7 +528,7 @@ class Solver
foreach ($learnedLiterals as $i => $learnedLiteral) { foreach ($learnedLiterals as $i => $learnedLiteral) {
if ($i !== 0) { if ($i !== 0) {
unset($seen[$literal->getPackageId()]); unset($seen[abs($learnedLiteral)]);
} }
} }
// only level 1 marks left // only level 1 marks left
@ -579,7 +548,7 @@ class Solver
); );
} }
$newRule = new Rule($learnedLiterals, Rule::RULE_LEARNED, $why); $newRule = new Rule($this->pool, $learnedLiterals, Rule::RULE_LEARNED, $why);
return array($learnedLiterals[0], $ruleLevel, $newRule, $why); return array($learnedLiterals[0], $ruleLevel, $newRule, $why);
} }
@ -626,18 +595,12 @@ class Solver
$seen = array(); $seen = array();
$literals = $conflictRule->getLiterals(); $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) { foreach ($literals as $literal) {
// skip the one true literal // skip the one true literal
if ($this->decisionsSatisfy($literal)) { if ($this->decisionsSatisfy($literal)) {
continue; continue;
} }
$seen[$literal->getPackageId()] = true; $seen[abs($literal)] = true;
} }
$decisionId = count($this->decisionQueue); $decisionId = count($this->decisionQueue);
@ -648,7 +611,7 @@ class Solver
$literal = $this->decisionQueue[$decisionId]; $literal = $this->decisionQueue[$decisionId];
// skip literals that are not in this rule // skip literals that are not in this rule
if (!isset($seen[$literal->getPackageId()])) { if (!isset($seen[abs($literal)])) {
continue; continue;
} }
@ -658,18 +621,13 @@ class Solver
$this->analyzeUnsolvableRule($problem, $why, $lastWeakWhy); $this->analyzeUnsolvableRule($problem, $why, $lastWeakWhy);
$literals = $why->getLiterals(); $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) { foreach ($literals as $literal) {
// skip the one true literal // skip the one true literal
if ($this->decisionsSatisfy($literal)) { if ($this->decisionsSatisfy($literal)) {
continue; continue;
} }
$seen[$literal->getPackageId()] = true; $seen[abs($literal)] = true;
} }
} }
@ -713,7 +671,7 @@ class Solver
private function resetSolver() private function resetSolver()
{ {
while ($literal = array_pop($this->decisionQueue)) { while ($literal = array_pop($this->decisionQueue)) {
$this->decisionMap[$literal->getPackageId()] = 0; $this->decisionMap[abs($literal)] = 0;
} }
$this->decisionQueueWhy = array(); $this->decisionQueueWhy = array();
@ -812,9 +770,9 @@ class Solver
if (count($this->installed) != count($this->updateMap)) { if (count($this->installed) != count($this->updateMap)) {
$prunedQueue = array(); $prunedQueue = array();
foreach ($decisionQueue as $literal) { foreach ($decisionQueue as $literal) {
if (isset($this->installedMap[$literal->getPackageId()])) { if (isset($this->installedMap[abs($literal)])) {
$prunedQueue[] = $literal; $prunedQueue[] = $literal;
if (isset($this->updateMap[$literal->getPackageId()])) { if (isset($this->updateMap[abs($literal)])) {
$prunedQueue = $decisionQueue; $prunedQueue = $decisionQueue;
break; break;
} }
@ -873,15 +831,15 @@ class Solver
// just need to decide on the positive literals // just need to decide on the positive literals
// //
foreach ($literals as $literal) { foreach ($literals as $literal) {
if (!$literal->isWanted()) { if ($literal <= 0) {
if (!$this->decidedInstall($literal->getPackage())) { if (!$this->decidedInstall(abs($literal))) {
continue 2; // next rule continue 2; // next rule
} }
} else { } else {
if ($this->decidedInstall($literal->getPackage())) { if ($this->decidedInstall(abs($literal))) {
continue 2; // next rule continue 2; // next rule
} }
if ($this->undecided($literal->getPackage())) { if ($this->undecided(abs($literal))) {
$decisionQueue[] = $literal; $decisionQueue[] = $literal;
} }
} }
@ -919,7 +877,7 @@ class Solver
list($literals, $level) = $this->branches[$i]; list($literals, $level) = $this->branches[$i];
foreach ($literals as $offset => $literal) { foreach ($literals as $offset => $literal) {
if ($literal && $literal->isWanted() && $this->decisionMap[$literal->getPackageId()] > $level + 1) { if ($literal && $literal > 0 && $this->decisionMap[abs($literal)] > $level + 1) {
$lastLiteral = $literal; $lastLiteral = $literal;
$lastBranchIndex = $i; $lastBranchIndex = $i;
$lastBranchOffset = $offset; $lastBranchOffset = $offset;

View File

@ -43,35 +43,35 @@ class Transaction
$installMeansUpdateMap = array(); $installMeansUpdateMap = array();
foreach ($this->decisionQueue as $i => $literal) { foreach ($this->decisionQueue as $i => $literal) {
$package = $literal->getPackage(); $package = $this->pool->literalToPackage($literal);
// !wanted & installed // !wanted & installed
if (!$literal->isWanted() && isset($this->installedMap[$package->getId()])) { if ($literal <= 0 && isset($this->installedMap[$package->getId()])) {
$updates = $this->policy->findUpdatePackages($this->pool, $this->installedMap, $package); $updates = $this->policy->findUpdatePackages($this->pool, $this->installedMap, $package);
$literals = array(new Literal($package, true)); $literals = array($package->getId());
foreach ($updates as $update) { foreach ($updates as $update) {
$literals[] = new Literal($update, true); $literals[] = $update->getId();
} }
foreach ($literals as $updateLiteral) { foreach ($literals as $updateLiteral) {
if (!$updateLiteral->equals($literal)) { if ($updateLiteral !== $literal) {
$installMeansUpdateMap[$updateLiteral->getPackageId()] = $package; $installMeansUpdateMap[abs($updateLiteral)] = $package;
} }
} }
} }
} }
foreach ($this->decisionQueue as $i => $literal) { foreach ($this->decisionQueue as $i => $literal) {
$package = $literal->getPackage(); $package = $this->pool->literalToPackage($literal);
// wanted & installed || !wanted & !installed // wanted & installed || !wanted & !installed
if ($literal->isWanted() == (isset($this->installedMap[$package->getId()]))) { if (($literal > 0) == (isset($this->installedMap[$package->getId()]))) {
continue; continue;
} }
if ($literal->isWanted()) { if ($literal > 0) {
if ($package instanceof AliasPackage) { if ($package instanceof AliasPackage) {
$transaction[] = new Operation\MarkAliasInstalledOperation( $transaction[] = new Operation\MarkAliasInstalledOperation(
$package, $this->decisionQueueWhy[$i] $package, $this->decisionQueueWhy[$i]
@ -79,16 +79,16 @@ class Transaction
continue; continue;
} }
if (isset($installMeansUpdateMap[$literal->getPackageId()])) { if (isset($installMeansUpdateMap[abs($literal)])) {
$source = $installMeansUpdateMap[$literal->getPackageId()]; $source = $installMeansUpdateMap[abs($literal)];
$transaction[] = new Operation\UpdateOperation( $transaction[] = new Operation\UpdateOperation(
$source, $package, $this->decisionQueueWhy[$i] $source, $package, $this->decisionQueueWhy[$i]
); );
// avoid updates to one package from multiple origins // avoid updates to one package from multiple origins
unset($installMeansUpdateMap[$literal->getPackageId()]); unset($installMeansUpdateMap[abs($literal)]);
$ignoreRemove[$source->getId()] = true; $ignoreRemove[$source->getId()] = true;
} else { } else {
$transaction[] = new Operation\InstallOperation( $transaction[] = new Operation\InstallOperation(

View File

@ -16,7 +16,6 @@ use Composer\Repository\ArrayRepository;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\DependencyResolver\DefaultPolicy; use Composer\DependencyResolver\DefaultPolicy;
use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\Pool;
use Composer\DependencyResolver\Literal;
use Composer\Package\Link; use Composer\Package\Link;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Package\LinkConstraint\VersionConstraint;
@ -44,8 +43,8 @@ class DefaultPolicyTest extends TestCase
$this->repo->addPackage($packageA = $this->getPackage('A', '1.0')); $this->repo->addPackage($packageA = $this->getPackage('A', '1.0'));
$this->pool->addRepository($this->repo); $this->pool->addRepository($this->repo);
$literals = array(new Literal($packageA, true)); $literals = array($packageA->getId());
$expected = array(new Literal($packageA, true)); $expected = array($packageA->getId());
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
@ -58,8 +57,8 @@ class DefaultPolicyTest extends TestCase
$this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0')); $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0'));
$this->pool->addRepository($this->repo); $this->pool->addRepository($this->repo);
$literals = array(new Literal($packageA1, true), new Literal($packageA2, true)); $literals = array($packageA1->getId(), $packageA2->getId());
$expected = array(new Literal($packageA2, true)); $expected = array($packageA2->getId());
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
@ -73,8 +72,8 @@ class DefaultPolicyTest extends TestCase
$this->pool->addRepository($this->repoInstalled); $this->pool->addRepository($this->repoInstalled);
$this->pool->addRepository($this->repo); $this->pool->addRepository($this->repo);
$literals = array(new Literal($packageA, true), new Literal($packageAInstalled, true)); $literals = array($packageA->getId(), $packageAInstalled->getId());
$expected = array(new Literal($packageA, true)); $expected = array($packageA->getId());
$selected = $this->policy->selectPreferedPackages($this->pool, $this->mapFromRepo($this->repoInstalled), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, $this->mapFromRepo($this->repoInstalled), $literals);
@ -92,8 +91,8 @@ class DefaultPolicyTest extends TestCase
$this->pool->addRepository($this->repoImportant); $this->pool->addRepository($this->repoImportant);
$this->pool->addRepository($this->repo); $this->pool->addRepository($this->repo);
$literals = array(new Literal($packageA, true), new Literal($packageAImportant, true)); $literals = array($packageA->getId(), $packageAImportant->getId());
$expected = array(new Literal($packageAImportant, true)); $expected = array($packageAImportant->getId());
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
@ -119,10 +118,10 @@ class DefaultPolicyTest extends TestCase
$packages = $this->pool->whatProvides('a', new VersionConstraint('=', '2.1.9999999.9999999-dev')); $packages = $this->pool->whatProvides('a', new VersionConstraint('=', '2.1.9999999.9999999-dev'));
$literals = array(); $literals = array();
foreach ($packages as $package) { foreach ($packages as $package) {
$literals[] = new Literal($package, true); $literals[] = $package->getId();
} }
$expected = array(new Literal($packageAAliasImportant, true)); $expected = array($packageAAliasImportant->getId());
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
@ -139,7 +138,7 @@ class DefaultPolicyTest extends TestCase
$this->pool->addRepository($this->repo); $this->pool->addRepository($this->repo);
$literals = array(new Literal($packageA, true), new Literal($packageB, true)); $literals = array($packageA->getId(), $packageB->getId());
$expected = $literals; $expected = $literals;
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);
@ -157,8 +156,8 @@ class DefaultPolicyTest extends TestCase
$this->pool->addRepository($this->repo); $this->pool->addRepository($this->repo);
$literals = array(new Literal($packageA, true), new Literal($packageB, true)); $literals = array($packageA->getId(), $packageB->getId());
$expected = array(new Literal($packageA, true), new Literal($packageB, true)); $expected = $literals;
$selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals); $selected = $this->policy->selectPreferedPackages($this->pool, array(), $literals);

View File

@ -1,63 +0,0 @@
<?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\Test\DependencyResolver;
use Composer\DependencyResolver\Literal;
use Composer\Test\TestCase;
class LiteralTest extends TestCase
{
protected $package;
public function setUp()
{
$this->package = $this->getPackage('foo', '1');
$this->package->setId(12);
}
public function testLiteralWanted()
{
$literal = new Literal($this->package, true);
$this->assertEquals(12, $literal->getId());
$this->assertEquals('+'.(string) $this->package, (string) $literal);
}
public function testLiteralUnwanted()
{
$literal = new Literal($this->package, false);
$this->assertEquals(-12, $literal->getId());
$this->assertEquals('-'.(string) $this->package, (string) $literal);
}
public function testLiteralInverted()
{
$literal = new Literal($this->package, false);
$inverted = $literal->inverted();
$this->assertInstanceOf('\Composer\DependencyResolver\Literal', $inverted);
$this->assertTrue($inverted->isWanted());
$this->assertSame($this->package, $inverted->getPackage());
$this->assertFalse($literal->equals($inverted));
$doubleInverted = $inverted->inverted();
$this->assertInstanceOf('\Composer\DependencyResolver\Literal', $doubleInverted);
$this->assertFalse($doubleInverted->isWanted());
$this->assertSame($this->package, $doubleInverted->getPackage());
$this->assertTrue($literal->equals($doubleInverted));
}
}

View File

@ -15,6 +15,7 @@ namespace Composer\Test\DependencyResolver;
use Composer\DependencyResolver\Rule; use Composer\DependencyResolver\Rule;
use Composer\DependencyResolver\RuleSet; use Composer\DependencyResolver\RuleSet;
use Composer\DependencyResolver\RuleSetIterator; use Composer\DependencyResolver\RuleSetIterator;
use Composer\DependencyResolver\Pool;
class ResultSetIteratorTest extends \PHPUnit_Framework_TestCase class ResultSetIteratorTest extends \PHPUnit_Framework_TestCase
{ {
@ -22,13 +23,15 @@ class ResultSetIteratorTest extends \PHPUnit_Framework_TestCase
protected function setUp() protected function setUp()
{ {
$this->pool = new Pool;
$this->rules = array( $this->rules = array(
RuleSet::TYPE_JOB => array( RuleSet::TYPE_JOB => array(
new Rule(array(), 'job1', null), new Rule($this->pool, array(), 'job1', null),
new Rule(array(), 'job2', null), new Rule($this->pool, array(), 'job2', null),
), ),
RuleSet::TYPE_LEARNED => array( RuleSet::TYPE_LEARNED => array(
new Rule(array(), 'update1', null), new Rule($this->pool, array(), 'update1', null),
), ),
RuleSet::TYPE_PACKAGE => array(), RuleSet::TYPE_PACKAGE => array(),
); );

View File

@ -14,21 +14,29 @@ namespace Composer\Test\DependencyResolver;
use Composer\DependencyResolver\Rule; use Composer\DependencyResolver\Rule;
use Composer\DependencyResolver\RuleSet; use Composer\DependencyResolver\RuleSet;
use Composer\DependencyResolver\Literal; use Composer\DependencyResolver\Pool;
use Composer\Repository\ArrayRepository;
use Composer\Test\TestCase; use Composer\Test\TestCase;
class RuleSetTest extends TestCase class RuleSetTest extends TestCase
{ {
protected $pool;
public function setUp()
{
$this->pool = new Pool;
}
public function testAdd() public function testAdd()
{ {
$rules = array( $rules = array(
RuleSet::TYPE_PACKAGE => array(), RuleSet::TYPE_PACKAGE => array(),
RuleSet::TYPE_JOB => array( RuleSet::TYPE_JOB => array(
new Rule(array(), 'job1', null), new Rule($this->pool, array(), 'job1', null),
new Rule(array(), 'job2', null), new Rule($this->pool, array(), 'job2', null),
), ),
RuleSet::TYPE_LEARNED => array( RuleSet::TYPE_LEARNED => array(
new Rule(array(), 'update1', null), new Rule($this->pool, array(), 'update1', null),
), ),
); );
@ -48,15 +56,15 @@ class RuleSetTest extends TestCase
{ {
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$ruleSet->add(new Rule(array(), 'job1', null), 7); $ruleSet->add(new Rule($this->pool, array(), 'job1', null), 7);
} }
public function testCount() public function testCount()
{ {
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$ruleSet->add(new Rule(array(), 'job1', null), RuleSet::TYPE_JOB); $ruleSet->add(new Rule($this->pool, array(), 'job1', null), RuleSet::TYPE_JOB);
$ruleSet->add(new Rule(array(), 'job2', null), RuleSet::TYPE_JOB); $ruleSet->add(new Rule($this->pool, array(), 'job2', null), RuleSet::TYPE_JOB);
$this->assertEquals(2, $ruleSet->count()); $this->assertEquals(2, $ruleSet->count());
} }
@ -65,7 +73,7 @@ class RuleSetTest extends TestCase
{ {
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$ruleSet->add($rule, RuleSet::TYPE_JOB); $ruleSet->add($rule, RuleSet::TYPE_JOB);
$this->assertSame($rule, $ruleSet->ruleById(0)); $this->assertSame($rule, $ruleSet->ruleById(0));
@ -75,8 +83,8 @@ class RuleSetTest extends TestCase
{ {
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$rule1 = new Rule(array(), 'job1', null); $rule1 = new Rule($this->pool, array(), 'job1', null);
$rule2 = new Rule(array(), 'job1', null); $rule2 = new Rule($this->pool, array(), 'job1', null);
$ruleSet->add($rule1, RuleSet::TYPE_JOB); $ruleSet->add($rule1, RuleSet::TYPE_JOB);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED); $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
@ -90,8 +98,8 @@ class RuleSetTest extends TestCase
public function testGetIteratorFor() public function testGetIteratorFor()
{ {
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$rule1 = new Rule(array(), 'job1', null); $rule1 = new Rule($this->pool, array(), 'job1', null);
$rule2 = new Rule(array(), 'job1', null); $rule2 = new Rule($this->pool, array(), 'job1', null);
$ruleSet->add($rule1, RuleSet::TYPE_JOB); $ruleSet->add($rule1, RuleSet::TYPE_JOB);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED); $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
@ -104,8 +112,8 @@ class RuleSetTest extends TestCase
public function testGetIteratorWithout() public function testGetIteratorWithout()
{ {
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$rule1 = new Rule(array(), 'job1', null); $rule1 = new Rule($this->pool, array(), 'job1', null);
$rule2 = new Rule(array(), 'job1', null); $rule2 = new Rule($this->pool, array(), 'job1', null);
$ruleSet->add($rule1, RuleSet::TYPE_JOB); $ruleSet->add($rule1, RuleSet::TYPE_JOB);
$ruleSet->add($rule2, RuleSet::TYPE_LEARNED); $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
@ -149,9 +157,13 @@ class RuleSetTest extends TestCase
public function testToString() public function testToString()
{ {
$repo = new ArrayRepository;
$repo->addPackage($p = $this->getPackage('foo', '2.1'));
$this->pool->addRepository($repo);
$ruleSet = new RuleSet; $ruleSet = new RuleSet;
$literal = new Literal($this->getPackage('foo', '2.1'), true); $literal = $p->getId();
$rule = new Rule(array($literal), 'job1', null); $rule = new Rule($this->pool, array($literal), 'job1', null);
$ruleSet->add($rule, RuleSet::TYPE_JOB); $ruleSet->add($rule, RuleSet::TYPE_JOB);

View File

@ -13,14 +13,22 @@
namespace Composer\Test\DependencyResolver; namespace Composer\Test\DependencyResolver;
use Composer\DependencyResolver\Rule; use Composer\DependencyResolver\Rule;
use Composer\DependencyResolver\Literal; use Composer\DependencyResolver\Pool;
use Composer\Repository\ArrayRepository;
use Composer\Test\TestCase; use Composer\Test\TestCase;
class RuleTest extends TestCase class RuleTest extends TestCase
{ {
protected $pool;
public function setUp()
{
$this->pool = new Pool;
}
public function testGetHash() public function testGetHash()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->ruleHash = '123'; $rule->ruleHash = '123';
$this->assertEquals('123', $rule->getHash()); $this->assertEquals('123', $rule->getHash());
@ -28,7 +36,7 @@ class RuleTest extends TestCase
public function testSetAndGetId() public function testSetAndGetId()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->setId(666); $rule->setId(666);
$this->assertEquals(666, $rule->getId()); $this->assertEquals(666, $rule->getId());
@ -36,10 +44,10 @@ class RuleTest extends TestCase
public function testEqualsForRulesWithDifferentHashes() public function testEqualsForRulesWithDifferentHashes()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->ruleHash = '123'; $rule->ruleHash = '123';
$rule2 = new Rule(array(), 'job1', null); $rule2 = new Rule($this->pool, array(), 'job1', null);
$rule2->ruleHash = '321'; $rule2->ruleHash = '321';
$this->assertFalse($rule->equals($rule2)); $this->assertFalse($rule->equals($rule2));
@ -47,18 +55,10 @@ class RuleTest extends TestCase
public function testEqualsForRulesWithDifferentLiterals() public function testEqualsForRulesWithDifferentLiterals()
{ {
$literal = $this->getLiteralMock(); $rule = new Rule($this->pool, array(1), 'job1', null);
$literal->expects($this->any())
->method('getId')
->will($this->returnValue(1));
$rule = new Rule(array($literal), 'job1', null);
$rule->ruleHash = '123'; $rule->ruleHash = '123';
$literal = $this->getLiteralMock(); $rule2 = new Rule($this->pool, array(12), 'job1', null);
$literal->expects($this->any())
->method('getId')
->will($this->returnValue(12));
$rule2 = new Rule(array($literal), 'job1', null);
$rule2->ruleHash = '123'; $rule2->ruleHash = '123';
$this->assertFalse($rule->equals($rule2)); $this->assertFalse($rule->equals($rule2));
@ -66,18 +66,9 @@ class RuleTest extends TestCase
public function testEqualsForRulesWithDifferLiteralsQuantity() public function testEqualsForRulesWithDifferLiteralsQuantity()
{ {
$literal = $this->getLiteralMock(); $rule = new Rule($this->pool, array(1, 12), 'job1', null);
$literal->expects($this->any())
->method('getId')
->will($this->returnValue(1));
$literal2 = $this->getLiteralMock();
$literal2->expects($this->any())
->method('getId')
->will($this->returnValue(12));
$rule = new Rule(array($literal, $literal2), 'job1', null);
$rule->ruleHash = '123'; $rule->ruleHash = '123';
$rule2 = new Rule(array($literal), 'job1', null); $rule2 = new Rule($this->pool, array(1), 'job1', null);
$rule2->ruleHash = '123'; $rule2->ruleHash = '123';
$this->assertFalse($rule->equals($rule2)); $this->assertFalse($rule->equals($rule2));
@ -85,24 +76,15 @@ class RuleTest extends TestCase
public function testEqualsForRulesWithThisSameLiterals() public function testEqualsForRulesWithThisSameLiterals()
{ {
$literal = $this->getLiteralMock(); $rule = new Rule($this->pool, array(1, 12), 'job1', null);
$literal->expects($this->any()) $rule2 = new Rule($this->pool, array(1, 12), 'job1', null);
->method('getId')
->will($this->returnValue(1));
$literal2 = $this->getLiteralMock();
$literal2->expects($this->any())
->method('getId')
->will($this->returnValue(12));
$rule = new Rule(array($literal, $literal2), 'job1', null);
$rule2 = new Rule(array($literal, $literal2), 'job1', null);
$this->assertTrue($rule->equals($rule2)); $this->assertTrue($rule->equals($rule2));
} }
public function testSetAndGetType() public function testSetAndGetType()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->setType('someType'); $rule->setType('someType');
$this->assertEquals('someType', $rule->getType()); $this->assertEquals('someType', $rule->getType());
@ -110,7 +92,7 @@ class RuleTest extends TestCase
public function testEnable() public function testEnable()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->disable(); $rule->disable();
$rule->enable(); $rule->enable();
@ -120,7 +102,7 @@ class RuleTest extends TestCase
public function testDisable() public function testDisable()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->enable(); $rule->enable();
$rule->disable(); $rule->disable();
@ -130,10 +112,10 @@ class RuleTest extends TestCase
public function testSetWeak() public function testSetWeak()
{ {
$rule = new Rule(array(), 'job1', null); $rule = new Rule($this->pool, array(), 'job1', null);
$rule->setWeak(true); $rule->setWeak(true);
$rule2 = new Rule(array(), 'job1', null); $rule2 = new Rule($this->pool, array(), 'job1', null);
$rule2->setWeak(false); $rule2->setWeak(false);
$this->assertTrue($rule->isWeak()); $this->assertTrue($rule->isWeak());
@ -142,10 +124,8 @@ class RuleTest extends TestCase
public function testIsAssertions() public function testIsAssertions()
{ {
$literal = $this->getLiteralMock(); $rule = new Rule($this->pool, array(1, 12), 'job1', null);
$literal2 = $this->getLiteralMock(); $rule2 = new Rule($this->pool, array(1), 'job1', null);
$rule = new Rule(array($literal, $literal2), 'job1', null);
$rule2 = new Rule(array($literal), 'job1', null);
$this->assertFalse($rule->isAssertion()); $this->assertFalse($rule->isAssertion());
$this->assertTrue($rule2->isAssertion()); $this->assertTrue($rule2->isAssertion());
@ -153,18 +133,13 @@ class RuleTest extends TestCase
public function testToString() public function testToString()
{ {
$literal = new Literal($this->getPackage('foo', '2.1'), true); $repo = new ArrayRepository;
$literal2 = new Literal($this->getPackage('baz', '1.1'), false); $repo->addPackage($p1 = $this->getPackage('foo', '2.1'));
$repo->addPackage($p2 = $this->getPackage('baz', '1.1'));
$this->pool->addRepository($repo);
$rule = new Rule(array($literal, $literal2), 'job1', null); $rule = new Rule($this->pool, array($p1->getId(), -$p2->getId()), 'job1', null);
$this->assertEquals('(-baz-1.1.0.0|+foo-2.1.0.0)', $rule->__toString()); $this->assertEquals('(-baz-1.1.0.0|+foo-2.1.0.0)', $rule->__toString());
} }
private function getLiteralMock()
{
return $this->getMockBuilder('Composer\DependencyResolver\Literal')
->disableOriginalConstructor()
->getMock();
}
} }