1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * Copyright (C) 2000-2002 Ximian, Inc.
5 * Copyright (C) 2005 SUSE Linux Products GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <boost/static_assert.hpp>
23 #include "zypp/solver/detail/Resolver.h"
24 #include "zypp/solver/detail/Helper.h"
25 #include "zypp/solver/detail/Testcase.h"
26 #include "zypp/solver/detail/SATResolver.h"
28 #include "zypp/Capabilities.h"
29 #include "zypp/ZConfig.h"
30 #include "zypp/base/Logger.h"
31 #include "zypp/base/String.h"
32 #include "zypp/base/Gettext.h"
33 #include "zypp/base/Algorithm.h"
34 #include "zypp/ResPool.h"
35 #include "zypp/ResFilters.h"
36 #include "zypp/sat/Pool.h"
37 #include "zypp/sat/Solvable.h"
38 #include "zypp/sat/Transaction.h"
39 #include "zypp/ResolverProblem.h"
41 #define MAXSOLVERRUNS 5
43 /////////////////////////////////////////////////////////////////////////
45 { ///////////////////////////////////////////////////////////////////////
46 ///////////////////////////////////////////////////////////////////////
48 { /////////////////////////////////////////////////////////////////////
49 /////////////////////////////////////////////////////////////////////
51 { ///////////////////////////////////////////////////////////////////
55 IMPL_PTR_TYPE(Resolver);
58 //---------------------------------------------------------------------------
61 std::ostream & Resolver::dumpOn( std::ostream & os ) const
63 os << "<resolver>" << endl;
64 #define OUTS(t) os << " " << #t << ":\t" << t << endl;
65 OUTS( _forceResolve );
69 OUTS( _onlyRequires );
70 OUTS( _allowVendorChange );
71 OUTS( _solveSrcPackages );
72 OUTS( _cleandepsOnRemove );
73 OUTS( _ignoreAlreadyRecommended );
75 return os << "<resolver/>";
79 //---------------------------------------------------------------------------
81 Resolver::Resolver (const ResPool & pool)
84 , _poolchanged(_pool.serial() )
85 , _forceResolve (false)
86 , _upgradeMode (false)
89 , _onlyRequires ( ZConfig::instance().solver_onlyRequires() )
90 , _allowVendorChange ( ZConfig::instance().solver_allowVendorChange() )
91 , _solveSrcPackages ( false )
92 , _cleandepsOnRemove ( ZConfig::instance().solver_cleandepsOnRemove() )
93 , _ignoreAlreadyRecommended ( true )
96 sat::Pool satPool( sat::Pool::instance() );
97 _satResolver = new SATResolver(_pool, satPool.get());
101 Resolver::~Resolver()
106 //---------------------------------------------------------------------------
108 void Resolver::setAllowVendorChange( TriBool state_r )
110 _allowVendorChange = indeterminate(state_r) ? ZConfig::instance().solver_allowVendorChange() : bool(state_r);
113 void Resolver::setOnlyRequires( TriBool state_r )
115 _onlyRequires = indeterminate(state_r) ? ZConfig::instance().solver_onlyRequires() : bool(state_r);
118 void Resolver::setCleandepsOnRemove( TriBool state_r )
120 _cleandepsOnRemove = indeterminate(state_r) ? ZConfig::instance().solver_cleandepsOnRemove() : bool(state_r);
123 //---------------------------------------------------------------------------
125 ResPool Resolver::pool() const
128 void Resolver::reset( bool keepExtras )
133 _extra_requires.clear();
134 _extra_conflicts.clear();
137 _isInstalledBy.clear();
139 _satifiedByInstalled.clear();
140 _installedSatisfied.clear();
143 bool Resolver::doUpgrade()
145 // Setting Resolver to upgrade mode. SAT solver will do the update
147 return resolvePool();
150 void Resolver::doUpdate()
153 return _satResolver->doUpdate();
156 PoolItemList Resolver::problematicUpdateItems() const
157 { return _satResolver->problematicUpdateItems(); }
159 void Resolver::addExtraRequire( const Capability & capability )
160 { _extra_requires.insert (capability); }
162 void Resolver::removeExtraRequire( const Capability & capability )
163 { _extra_requires.erase (capability); }
165 void Resolver::addExtraConflict( const Capability & capability )
166 { _extra_conflicts.insert (capability); }
168 void Resolver::removeExtraConflict( const Capability & capability )
169 { _extra_conflicts.erase (capability); }
171 void Resolver::removeQueueItem( SolverQueueItem_Ptr item )
174 for (SolverQueueItemList::const_iterator iter = _added_queue_items.begin();
175 iter != _added_queue_items.end(); iter++) {
177 _added_queue_items.remove(*iter);
183 _removed_queue_items.push_back (item);
184 _removed_queue_items.unique ();
188 void Resolver::addQueueItem( SolverQueueItem_Ptr item )
191 for (SolverQueueItemList::const_iterator iter = _removed_queue_items.begin();
192 iter != _removed_queue_items.end(); iter++) {
194 _removed_queue_items.remove(*iter);
200 _added_queue_items.push_back (item);
201 _added_queue_items.unique ();
205 void Resolver::addWeak( const PoolItem & item )
206 { _addWeak.push_back( item ); }
208 //---------------------------------------------------------------------------
210 struct UndoTransact : public resfilter::PoolItemFilterFunctor
212 ResStatus::TransactByValue resStatus;
213 UndoTransact ( const ResStatus::TransactByValue &status)
217 bool operator()( PoolItem item ) // only transacts() items go here
219 item.status().resetTransact( resStatus );// clear any solver/establish transactions
225 struct DoTransact : public resfilter::PoolItemFilterFunctor
227 ResStatus::TransactByValue resStatus;
228 DoTransact ( const ResStatus::TransactByValue &status)
232 bool operator()( PoolItem item ) // only transacts() items go here
234 item.status().setTransact( true, resStatus );
240 bool Resolver::verifySystem()
242 UndoTransact resetting (ResStatus::APPL_HIGH);
244 _DEBUG ("Resolver::verifySystem() ");
248 invokeOnEach ( _pool.begin(), _pool.end(),
249 resfilter::ByTransact( ), // Resetting all transcations
250 functor::functorRef<bool,PoolItem>(resetting) );
252 return resolvePool();
256 //----------------------------------------------------------------------------
259 void Resolver::undo()
261 UndoTransact info(ResStatus::APPL_LOW);
262 MIL << "*** undo ***" << endl;
263 invokeOnEach ( _pool.begin(), _pool.end(),
264 resfilter::ByTransact( ), // collect transacts from Pool to resolver queue
265 functor::functorRef<bool,PoolItem>(info) );
266 // Regard dependencies of the item weak onl
269 // Additional QueueItems which has to be regarded by the solver
270 _removed_queue_items.clear();
271 _added_queue_items.clear();
276 void Resolver::solverInit()
278 // Solving with libsolv
279 static bool poolDumped = false;
280 MIL << "-------------- Calling SAT Solver -------------------" << endl;
281 if ( getenv("ZYPP_FULLLOG") ) {
282 Testcase testcase("/var/log/YaST2/autoTestcase");
284 testcase.createTestcase (*this, true, false); // dump pool
287 testcase.createTestcase (*this, false, false); // write control file only
291 _satResolver->setFixsystem ( isVerifyingMode() );
292 _satResolver->setIgnorealreadyrecommended ( ignoreAlreadyRecommended() );
293 _satResolver->setOnlyRequires ( onlyRequires() );
294 _satResolver->setAllowdowngrade (false);
295 _satResolver->setAllowarchchange (false);
296 _satResolver->setAllowvendorchange ( allowVendorChange() );
297 _satResolver->setAllowuninstall ( forceResolve() );
298 _satResolver->setUpdatesystem (false);
299 _satResolver->setNoupdateprovide (false);
300 _satResolver->setDosplitprovides (true);
301 _satResolver->setSolveSrcPackages ( solveSrcPackages() );
302 _satResolver->setCleandepsOnRemove ( cleandepsOnRemove() );
304 _satResolver->setDistupgrade (_upgradeMode);
306 // may overwrite some settings
307 _satResolver->setDistupgrade_removeunsupported (false);
310 // Resetting additional solver information
311 _isInstalledBy.clear();
313 _satifiedByInstalled.clear();
314 _installedSatisfied.clear();
317 bool Resolver::resolvePool()
320 return _satResolver->resolvePool(_extra_requires, _extra_conflicts, _addWeak, _upgradeRepos );
323 bool Resolver::resolveQueue( solver::detail::SolverQueueItemList & queue )
327 // add/remove additional SolverQueueItems
328 for (SolverQueueItemList::const_iterator iter = _removed_queue_items.begin();
329 iter != _removed_queue_items.end(); iter++) {
330 for (SolverQueueItemList::const_iterator iterQueue = queue.begin(); iterQueue != queue.end(); iterQueue++) {
331 if ( (*iterQueue)->cmp(*iter) == 0) {
332 MIL << "remove from queue" << *iter;
333 queue.remove(*iterQueue);
339 for (SolverQueueItemList::const_iterator iter = _added_queue_items.begin();
340 iter != _added_queue_items.end(); iter++) {
342 for (SolverQueueItemList::const_iterator iterQueue = queue.begin(); iterQueue != queue.end(); iterQueue++) {
343 if ( (*iterQueue)->cmp(*iter) == 0) {
349 MIL << "add to queue" << *iter;
350 queue.push_back(*iter);
354 // The application has to take care to write these solutions back to e.g. selectables in order
355 // give the user a chance for changing these decisions again.
356 _removed_queue_items.clear();
357 _added_queue_items.clear();
359 return _satResolver->resolveQueue(queue, _addWeak);
362 sat::Transaction Resolver::getTransaction()
364 // FIXME: That's an ugly way of pushing autoInstalled into the transaction.
365 sat::Transaction ret( sat::Transaction::loadFromPool );
366 ret.autoInstalled( _satResolver->autoInstalled() );
371 //----------------------------------------------------------------------------
372 // Getting more information about the solve results
374 ResolverProblemList Resolver::problems() const
376 MIL << "Resolver::problems()" << endl;
377 return _satResolver->problems();
380 void Resolver::applySolutions( const ProblemSolutionList & solutions )
382 for_( iter, solutions.begin(), solutions.end() )
384 ProblemSolution_Ptr solution = *iter;
385 if ( !solution->apply( *this ) )
390 void Resolver::collectResolverInfo()
393 && _isInstalledBy.empty()
394 && _installs.empty()) {
397 PoolItemList itemsToInstall = _satResolver->resultItemsToInstall();
399 for (PoolItemList::const_iterator instIter = itemsToInstall.begin();
400 instIter != itemsToInstall.end(); instIter++) {
402 for (Capabilities::const_iterator capIt = (*instIter)->dep (Dep::REQUIRES).begin(); capIt != (*instIter)->dep (Dep::REQUIRES).end(); ++capIt)
404 sat::WhatProvides possibleProviders(*capIt);
405 for_( iter, possibleProviders.begin(), possibleProviders.end() ) {
406 PoolItem provider = ResPool::instance().find( *iter );
408 // searching if this provider will already be installed
410 bool alreadySetForInstallation = false;
411 ItemCapKindMap::const_iterator pos = _isInstalledBy.find(provider);
412 while (pos != _isInstalledBy.end()
413 && pos->first == provider
415 alreadySetForInstallation = true;
416 ItemCapKind capKind = pos->second;
417 if (capKind.item == *instIter) found = true;
422 && provider.status().isToBeInstalled()) {
423 if (provider.status().isBySolver()) {
424 ItemCapKind capKindisInstalledBy( *instIter, *capIt, Dep::REQUIRES, !alreadySetForInstallation );
425 _isInstalledBy.insert (make_pair( provider, capKindisInstalledBy));
427 // no initial installation cause it has been set be e.g. user
428 ItemCapKind capKindisInstalledBy( *instIter, *capIt, Dep::REQUIRES, false );
429 _isInstalledBy.insert (make_pair( provider, capKindisInstalledBy));
431 ItemCapKind capKindisInstalledBy( provider, *capIt, Dep::REQUIRES, !alreadySetForInstallation );
432 _installs.insert (make_pair( *instIter, capKindisInstalledBy));
435 if (provider.status().staysInstalled()) { // Is already satisfied by an item which is installed
436 ItemCapKind capKindisInstalledBy( provider, *capIt, Dep::REQUIRES, false );
437 _satifiedByInstalled.insert (make_pair( *instIter, capKindisInstalledBy));
439 ItemCapKind installedSatisfied( *instIter, *capIt, Dep::REQUIRES, false );
440 _installedSatisfied.insert (make_pair( provider, installedSatisfied));
445 if (!(_satResolver->onlyRequires())) {
447 for (Capabilities::const_iterator capIt = (*instIter)->dep (Dep::RECOMMENDS).begin(); capIt != (*instIter)->dep (Dep::RECOMMENDS).end(); ++capIt)
449 sat::WhatProvides possibleProviders(*capIt);
450 for_( iter, possibleProviders.begin(), possibleProviders.end() ) {
451 PoolItem provider = ResPool::instance().find( *iter );
453 // searching if this provider will already be installed
455 bool alreadySetForInstallation = false;
456 ItemCapKindMap::const_iterator pos = _isInstalledBy.find(provider);
457 while (pos != _isInstalledBy.end()
458 && pos->first == provider
460 alreadySetForInstallation = true;
461 ItemCapKind capKind = pos->second;
462 if (capKind.item == *instIter) found = true;
467 && provider.status().isToBeInstalled()) {
468 if (provider.status().isBySolver()) {
469 ItemCapKind capKindisInstalledBy( *instIter, *capIt, Dep::RECOMMENDS, !alreadySetForInstallation );
470 _isInstalledBy.insert (make_pair( provider, capKindisInstalledBy));
472 // no initial installation cause it has been set be e.g. user
473 ItemCapKind capKindisInstalledBy( *instIter, *capIt, Dep::RECOMMENDS, false );
474 _isInstalledBy.insert (make_pair( provider, capKindisInstalledBy));
476 ItemCapKind capKindisInstalledBy( provider, *capIt, Dep::RECOMMENDS, !alreadySetForInstallation );
477 _installs.insert (make_pair( *instIter, capKindisInstalledBy));
480 if (provider.status().staysInstalled()) { // Is already satisfied by an item which is installed
481 ItemCapKind capKindisInstalledBy( provider, *capIt, Dep::RECOMMENDS, false );
482 _satifiedByInstalled.insert (make_pair( *instIter, capKindisInstalledBy));
484 ItemCapKind installedSatisfied( *instIter, *capIt, Dep::RECOMMENDS, false );
485 _installedSatisfied.insert (make_pair( provider, installedSatisfied));
491 for (Capabilities::const_iterator capIt = (*instIter)->dep (Dep::SUPPLEMENTS).begin(); capIt != (*instIter)->dep (Dep::SUPPLEMENTS).end(); ++capIt)
493 sat::WhatProvides possibleProviders(*capIt);
494 for_( iter, possibleProviders.begin(), possibleProviders.end() ) {
495 PoolItem provider = ResPool::instance().find( *iter );
496 // searching if this item will already be installed
498 bool alreadySetForInstallation = false;
499 ItemCapKindMap::const_iterator pos = _isInstalledBy.find(*instIter);
500 while (pos != _isInstalledBy.end()
501 && pos->first == *instIter
503 alreadySetForInstallation = true;
504 ItemCapKind capKind = pos->second;
505 if (capKind.item == provider) found = true;
510 && instIter->status().isToBeInstalled()) {
511 if (instIter->status().isBySolver()) {
512 ItemCapKind capKindisInstalledBy( provider, *capIt, Dep::SUPPLEMENTS, !alreadySetForInstallation );
513 _isInstalledBy.insert (make_pair( *instIter, capKindisInstalledBy));
515 // no initial installation cause it has been set be e.g. user
516 ItemCapKind capKindisInstalledBy( provider, *capIt, Dep::SUPPLEMENTS, false );
517 _isInstalledBy.insert (make_pair( *instIter, capKindisInstalledBy));
519 ItemCapKind capKindisInstalledBy( *instIter, *capIt, Dep::SUPPLEMENTS, !alreadySetForInstallation );
520 _installs.insert (make_pair( provider, capKindisInstalledBy));
523 if (instIter->status().staysInstalled()) { // Is already satisfied by an item which is installed
524 ItemCapKind capKindisInstalledBy( *instIter, *capIt, Dep::SUPPLEMENTS, !alreadySetForInstallation );
525 _satifiedByInstalled.insert (make_pair( provider, capKindisInstalledBy));
527 ItemCapKind installedSatisfied( provider, *capIt, Dep::SUPPLEMENTS, false );
528 _installedSatisfied.insert (make_pair( *instIter, installedSatisfied));
538 ItemCapKindList Resolver::isInstalledBy( const PoolItem & item )
541 collectResolverInfo();
543 for (ItemCapKindMap::const_iterator iter = _isInstalledBy.find(item); iter != _isInstalledBy.end();) {
544 ItemCapKind info = iter->second;
545 PoolItem iterItem = iter->first;
546 if (iterItem == item) {
551 iter = _isInstalledBy.end();
557 ItemCapKindList Resolver::installs( const PoolItem & item )
560 collectResolverInfo();
562 for (ItemCapKindMap::const_iterator iter = _installs.find(item); iter != _installs.end();) {
563 ItemCapKind info = iter->second;
564 PoolItem iterItem = iter->first;
565 if (iterItem == item) {
570 iter = _installs.end();
576 ItemCapKindList Resolver::satifiedByInstalled( const PoolItem & item )
579 collectResolverInfo();
581 for (ItemCapKindMap::const_iterator iter = _satifiedByInstalled.find(item); iter != _satifiedByInstalled.end();) {
582 ItemCapKind info = iter->second;
583 PoolItem iterItem = iter->first;
584 if (iterItem == item) {
589 iter = _satifiedByInstalled.end();
595 ItemCapKindList Resolver::installedSatisfied( const PoolItem & item )
598 collectResolverInfo();
600 for (ItemCapKindMap::const_iterator iter = _installedSatisfied.find(item); iter != _installedSatisfied.end();) {
601 ItemCapKind info = iter->second;
602 PoolItem iterItem = iter->first;
603 if (iterItem == item) {
608 iter = _installedSatisfied.end();
615 ///////////////////////////////////////////////////////////////////
616 };// namespace detail
617 /////////////////////////////////////////////////////////////////////
618 /////////////////////////////////////////////////////////////////////
619 };// namespace solver
620 ///////////////////////////////////////////////////////////////////////
621 ///////////////////////////////////////////////////////////////////////
623 /////////////////////////////////////////////////////////////////////////