regarding recommend/suggest settings from the SAT-solver
[platform/upstream/libzypp.git] / zypp / sat / SATResolver.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* SATResolver.cc
3  *
4  * Copyright (C) 2000-2002 Ximian, Inc.
5  * Copyright (C) 2005 SUSE Linux Products GmbH
6  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  * 02111-1307, USA.
20  */
21 #include <sstream>
22 #include "zypp/solver/detail/Helper.h"
23 #include "zypp/base/String.h"
24 #include "zypp/Capability.h"
25 #include "zypp/ResStatus.h"
26 #include "zypp/VendorAttr.h"
27 #include "zypp/base/Logger.h"
28 #include "zypp/base/String.h"
29 #include "zypp/base/Gettext.h"
30 #include "zypp/base/Algorithm.h"
31 #include "zypp/ResPool.h"
32 #include "zypp/ResFilters.h"
33 #include "zypp/sat/SATResolver.h"
34 #include "zypp/sat/Pool.h"
35 #include "zypp/sat/WhatProvides.h"
36 #include "zypp/solver/detail/ProblemSolutionCombi.h"
37
38 extern "C" {
39 #include "satsolver/repo_solv.h"
40 #include "satsolver/poolarch.h"
41 #include "satsolver/evr.h"
42 #include "satsolver/poolvendor.h"
43 #include "satsolver/policy.h"
44 }
45
46 /////////////////////////////////////////////////////////////////////////
47 namespace zypp
48 { ///////////////////////////////////////////////////////////////////////
49   ///////////////////////////////////////////////////////////////////////
50   namespace solver
51   { /////////////////////////////////////////////////////////////////////
52     /////////////////////////////////////////////////////////////////////
53     namespace detail
54     { ///////////////////////////////////////////////////////////////////
55
56 using namespace std;
57
58 IMPL_PTR_TYPE(SATResolver);
59
60 static PoolItemSet triggeredSolution;   // only the latest state of an item is interesting
61                                         // for the pool. Documents already inserted items.
62
63 //---------------------------------------------------------------------------
64 // Callbacks for SAT policies
65 //---------------------------------------------------------------------------
66
67 int vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2) {
68 //    DBG << "vendorCheck: " << id2str(pool, solvable1->vendor) << " <--> " << id2str(pool, solvable1->vendor) << endl;
69     return VendorAttr::instance().equivalent(id2str(pool, solvable1->vendor), id2str(pool, solvable2->vendor)) ? 0:1;
70 }
71
72
73 string
74 itemToString (PoolItem item, bool shortVersion)
75 {
76     ostringstream os;
77     if (!item) return "";
78
79     if (item->kind() != ResTraits<zypp::Package>::kind)
80         os << item->kind() << ':';
81     os  << item->name();
82     if (!shortVersion) {
83         os << '-' << item->edition();
84         if (item->arch() != "") {
85             os << '.' << item->arch();
86         }
87         Repository s = item->repository();
88         if (s) {
89             string alias = s.info().alias();
90             if (!alias.empty()
91                 && alias != "@system")
92             {
93                 os << '[' << s.info().alias() << ']';
94             }
95         }
96     }
97     return os.str();
98 }
99         
100
101 //---------------------------------------------------------------------------
102
103 std::ostream &
104 SATResolver::dumpOn( std::ostream & os ) const
105 {
106     return os << "<resolver/>";
107 }
108
109 //---------------------------------------------------------------------------
110
111 SATResolver::SATResolver (const ResPool & pool, Pool *SATPool)
112     : _pool (pool)
113     , _SATPool (SATPool)
114     , _solv(NULL)
115     , _fixsystem(false)
116     , _allowdowngrade(false)
117     , _allowarchchange(false)
118     , _allowvendorchange(false)
119     , _allowuninstall(false)
120     , _updatesystem(false)
121     , _allowvirtualconflicts(false)
122     , _noupdateprovide(false)
123     , _dosplitprovides(false)
124
125 {
126 }
127
128
129 SATResolver::~SATResolver()
130 {
131 }
132
133 //---------------------------------------------------------------------------
134
135
136 ResPool
137 SATResolver::pool (void) const
138 {
139     return _pool;
140 }
141
142
143 void
144 SATResolver::addPoolItemToInstall (PoolItem item)
145 {
146     bool found = false;
147     for (PoolItemList::const_iterator iter = _items_to_remove.begin();
148          iter != _items_to_remove.end(); iter++) {
149         if (*iter == item) {
150             _items_to_remove.remove(*iter);
151             found = true;
152             break;
153         }
154     }
155     if (!found) {
156         _items_to_install.push_back (item);
157         _items_to_install.unique ();
158     }
159 }
160
161
162 void
163 SATResolver::addPoolItemsToInstallFromList (PoolItemList & rl)
164 {
165     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
166         addPoolItemToInstall (*iter);
167     }
168 }
169
170
171 void
172 SATResolver::addPoolItemToRemove (PoolItem item)
173 {
174     bool found = false;
175     for (PoolItemList::const_iterator iter = _items_to_install.begin();
176          iter != _items_to_install.end(); iter++) {
177         if (*iter == item) {
178             _items_to_install.remove(*iter);
179             found = true;
180             break;
181         }
182     }
183     if (!found) {
184         _items_to_remove.push_back (item);
185         _items_to_remove.unique ();
186     }
187 }
188
189
190 void
191 SATResolver::addPoolItemsToRemoveFromList (PoolItemList & rl)
192 {
193     for (PoolItemList::const_iterator iter = rl.begin(); iter != rl.end(); iter++) {
194         addPoolItemToRemove (*iter);
195     }
196 }
197
198 void
199 SATResolver::addPoolItemToLock (PoolItem item)
200 {
201     _items_to_lock.push_back (item);
202     _items_to_lock.unique ();
203 }
204
205
206 //---------------------------------------------------------------------------
207
208 // copy marked item from solution back to pool
209 // if data != NULL, set as APPL_LOW (from establishPool())
210
211 static void
212 SATSolutionToPool (PoolItem item, const ResStatus & status, const ResStatus::TransactByValue causer)
213 {
214 #if 0
215     if (triggeredSolution.find(item) != triggeredSolution.end()) {
216         _XDEBUG("SATSolutionToPool(" << item << ") is already in the pool --> skip");
217         return;
218     }
219 #endif
220
221     triggeredSolution.insert(item);
222
223     // resetting transaction only
224     item.status().resetTransact (causer);
225
226     bool r;
227
228     if (status.isToBeInstalled()) {
229         r = item.status().setToBeInstalled (causer);
230         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") install !" << r);
231     }
232     else if (status.isToBeUninstalledDueToUpgrade()) {
233         r = item.status().setToBeUninstalledDueToUpgrade (causer);
234         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") upgrade !" << r);
235     }
236     else if (status.isToBeUninstalled()) {
237         r = item.status().setToBeUninstalled (causer);
238         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") remove !" << r);
239     }
240     else if (status.isIncomplete()
241              || status.isNeeded()) {
242         r = item.status().setIncomplete();
243         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") incomplete !" << r);
244     }
245     else if (status.isUnneeded()) {
246         r = item.status().setUnneeded();
247         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") unneeded !" << r);
248     }
249     else if (status.isSatisfied()) {
250         r = item.status().setSatisfied();
251         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") satisfied !" << r);
252     }
253     else if (status.isRecommended()) {
254         item.status().setRecommended(true);
255         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") recommended !" << r);
256     }
257     else if (status.isSuggested()) {
258         item.status().setSuggested(true);
259         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") suggested !" << r);        
260     } else {
261         _XDEBUG("SATSolutionToPool(" << item << ", " << status << ") unchanged !");
262     }
263     return;
264 }
265
266
267 //----------------------------------------------------------------------------
268 // helper functions for distupgrade 
269 //----------------------------------------------------------------------------
270
271 bool SATResolver::doesObsoleteItem (PoolItem candidate, PoolItem installed) {
272   Solvable *sCandidate = _SATPool->solvables + candidate.satSolvable().id();
273   ::_Repo *installedRepo = sat::Pool::instance().systemRepo().get();
274   
275   Id p, *pp, obsolete, *obsoleteIt;
276   
277   if ((!installedRepo || sCandidate->repo != installedRepo) && sCandidate->obsoletes) {
278       obsoleteIt = sCandidate->repo->idarraydata + sCandidate->obsoletes;
279       while ((obsolete = *obsoleteIt++) != 0)
280       {
281           for (pp = pool_whatprovides(_SATPool, obsolete) ; (p = *pp++) != 0; ) {
282               if (p > 0 &&  installed.satSolvable().id() == (sat::detail::SolvableIdType)p) {
283                   MIL << candidate << " obsoletes " << installed << endl;
284                   return true;
285               }
286           }
287       }
288   }
289   return false;
290 }
291
292 //----------------------------------------------------------------------------
293 //----------------------------------------------------------------------------
294 // resolvePool
295 //----------------------------------------------------------------------------
296 //----------------------------------------------------------------------------
297
298 //----------------------------------------------------------------------------
299 // Helper functions for the ZYPP-Pool
300 //----------------------------------------------------------------------------
301
302
303 //------------------------------------------------------------------------------------------------------------
304 //  This function loops over the pool and grabs all items
305 //  It clears all previous bySolver() states also
306 //
307 //  Every toBeInstalled is passed to zypp::solver:detail::Resolver.addPoolItemToInstall()
308 //  Every toBeUninstalled is passed to zypp::solver:detail::Resolver.addPoolItemToRemove()
309 //
310 //  Solver results must be written back to the pool.
311 //------------------------------------------------------------------------------------------------------------
312
313
314 struct SATCollectTransact : public resfilter::PoolItemFilterFunctor
315 {
316     SATResolver & resolver;
317
318     SATCollectTransact (SATResolver & r)
319         : resolver (r)
320     { }
321
322     bool operator()( PoolItem item )            // only transacts() items go here
323     {
324         ResStatus status = item.status();
325         bool by_solver = (status.isBySolver() || status.isByApplLow());
326
327         if (by_solver) {
328             _XDEBUG("Resetting " << item );
329             item.status().resetTransact( ResStatus::APPL_LOW );// clear any solver/establish transactions
330             return true;                                // back out here, dont re-queue former solver result
331         }
332
333         if (status.isToBeInstalled()) {
334             resolver.addPoolItemToInstall(item);        // -> install!
335         }
336         else if (status.isToBeUninstalled()) {
337             resolver.addPoolItemToRemove(item);         // -> remove !
338         }
339         else if (status.isLocked()
340                  || (status.isKept()
341                      && !by_solver)) {
342             resolver.addPoolItemToLock (item);
343         }
344
345         return true;
346     }
347 };
348
349
350 //----------------------------------------------------------------------------
351 //----------------------------------------------------------------------------
352 // solving.....
353 //----------------------------------------------------------------------------
354 //----------------------------------------------------------------------------
355
356
357 class CheckIfUpdate : public resfilter::PoolItemFilterFunctor
358 {
359   public:
360     bool is_updated;
361
362     CheckIfUpdate()
363         : is_updated( false )
364     {}
365
366     // check this item will be installed
367
368     bool operator()( PoolItem item )
369     {
370         if (item.status().isToBeInstalled())
371         {
372             is_updated = true;
373             return false;
374         }
375         return true;
376     }
377 };
378
379
380 bool
381 SATResolver::resolvePool(const CapabilitySet & requires_caps,
382                          const CapabilitySet & conflict_caps)
383 {
384     SATCollectTransact info (*this);
385     
386     MIL << "SATResolver::resolvePool()" << endl;
387
388     if (_solv) {
389         // remove old stuff
390         solver_free(_solv);
391         _solv = NULL;
392         queue_free( &(_jobQueue) );
393     }
394
395     queue_init( &_jobQueue );
396     _items_to_install.clear();
397     _items_to_remove.clear();
398     _items_to_lock.clear();
399
400     invokeOnEach ( _pool.begin(), _pool.end(),
401                    functor::functorRef<bool,PoolItem>(info) );
402
403     for (PoolItemList::const_iterator iter = _items_to_install.begin(); iter != _items_to_install.end(); iter++) {
404         PoolItem r = *iter;
405
406         Id id = (*iter)->satSolvable().id();
407         if (id == ID_NULL) {
408             ERR << "Install: " << *iter << " not found" << endl;
409         }
410         MIL << "Install " << *iter << " with the SAT-Pool ID: " << id << endl;
411         queue_push( &(_jobQueue), SOLVER_INSTALL_SOLVABLE );
412         queue_push( &(_jobQueue), id );
413     }
414
415     for (PoolItemList::const_iterator iter = _items_to_remove.begin(); iter != _items_to_remove.end(); iter++) {
416         sat::detail::IdType ident( (*iter)->satSolvable().ident().id() );
417         MIL << "Delete " << *iter << " with the string ID: " << ident << endl;
418         queue_push( &(_jobQueue), SOLVER_ERASE_SOLVABLE_NAME );
419         queue_push( &(_jobQueue), ident);
420     }
421
422     for (CapabilitySet::const_iterator iter = requires_caps.begin(); iter != requires_caps.end(); iter++) {
423         queue_push( &(_jobQueue), SOLVER_INSTALL_SOLVABLE_PROVIDES );
424         queue_push( &(_jobQueue), iter->id() );
425         MIL << "Requires " << *iter << endl;
426     }
427
428     for (CapabilitySet::const_iterator iter = conflict_caps.begin(); iter != conflict_caps.end(); iter++) {
429         queue_push( &(_jobQueue), SOLVER_ERASE_SOLVABLE_PROVIDES);
430         queue_push( &(_jobQueue), iter->id() );
431         MIL << "Conflicts " << *iter << endl;
432     }
433
434     for (PoolItemList::const_iterator iter = _items_to_lock.begin(); iter != _items_to_lock.end(); iter++) {
435         sat::detail::SolvableIdType ident( (*iter)->satSolvable().id() );
436         if (iter->status().isInstalled()) {
437             MIL << "Lock installed item " << *iter << " with the string ID: " << ident << endl;
438             queue_push( &(_jobQueue), SOLVER_INSTALL_SOLVABLE );
439             queue_push( &(_jobQueue), ident );
440         } else {
441             MIL << "Lock NOT installed item " << *iter << " with the string ID: " << ident << endl;
442             queue_push( &(_jobQueue), SOLVER_ERASE_SOLVABLE );
443             queue_push( &(_jobQueue), ident );
444         }
445     }
446
447     _solv = solver_create( _SATPool, sat::Pool::instance().systemRepo().get() );
448     _solv->vendorCheckCb = &vendorCheck;
449     _solv->fixsystem = _fixsystem;
450     _solv->updatesystem = _updatesystem;
451     _solv->allowdowngrade = _allowdowngrade;
452     _solv->allowuninstall = _allowuninstall;
453     _solv->allowarchchange = _allowarchchange;
454     _solv->dosplitprovides = _dosplitprovides;
455     _solv->noupdateprovide = _noupdateprovide;
456     
457     sat::Pool::instance().prepare();
458
459     // Solve !
460     MIL << "Starting solving...." << endl;
461     solver_solve( _solv, &(_jobQueue) );
462     MIL << "....Solver end" << endl;
463
464     // copying solution back to zypp pool
465     //-----------------------------------------
466
467     if (_solv->problems.count > 0 )
468     {
469         ERR << "Solverrun finished with an ERROR" << endl;
470         return false;
471     }
472
473     /*  solvables to be installed */
474     for (int i = 0; i < _solv->decisionq.count; i++)
475     {
476       Id p;
477       p = _solv->decisionq.elements[i];
478       if (p < 0 || !sat::Solvable(p))
479         continue;
480       if (sat::Solvable(p).repository().get() == _solv->installed)
481         continue;
482
483       PoolItem poolItem = _pool.find (sat::Solvable(p));
484       if (poolItem) {
485           SATSolutionToPool (poolItem, ResStatus::toBeInstalled, ResStatus::SOLVER);
486       } else {
487           ERR << "id " << p << " not found in ZYPP pool." << endl;
488       }
489     }
490
491     /* solvables to be erased */
492     for (int i = _solv->installed->start; i < _solv->installed->start + _solv->installed->nsolvables; i++)
493     {
494       if (_solv->decisionmap[i] > 0)
495         continue;
496
497       PoolItem poolItem = _pool.find (sat::Solvable(i));
498       if (poolItem) {
499           // Check if this is an update
500           CheckIfUpdate info;
501           invokeOnEach( _pool.byIdentBegin( poolItem ),
502                         _pool.byIdentEnd( poolItem ),
503                         resfilter::ByUninstalled(),                     // ByUninstalled
504                         functor::functorRef<bool,PoolItem> (info) );
505
506           if (info.is_updated) {
507               SATSolutionToPool (poolItem, ResStatus::toBeUninstalledDueToUpgrade , ResStatus::SOLVER);
508           } else {
509               SATSolutionToPool (poolItem, ResStatus::toBeUninstalled, ResStatus::SOLVER);
510           }
511       } else {
512           ERR << "id " << i << " not found in ZYPP pool." << endl;
513       }
514     }
515
516     /*  solvables which are recommended */
517     for (int i = 0; i < _solv->recommendations.count; i++)
518     {
519       Id p;
520       p = _solv->recommendations.elements[i];
521       if (p < 0 || !sat::Solvable(p))
522         continue;
523
524       PoolItem poolItem = _pool.find (sat::Solvable(p));
525       if (poolItem) {
526           SATSolutionToPool (poolItem, ResStatus::recommended, ResStatus::SOLVER);
527       } else {
528           ERR << "id " << p << " not found in ZYPP pool." << endl;
529       }
530     }
531
532     /*  solvables which are suggested */
533     for (int i = 0; i < _solv->suggestions.count; i++)
534     {
535       Id p;
536       p = _solv->suggestions.elements[i];
537       if (p < 0 || !sat::Solvable(p))
538         continue;
539
540       PoolItem poolItem = _pool.find (sat::Solvable(p));
541       if (poolItem) {
542           SATSolutionToPool (poolItem, ResStatus::suggested, ResStatus::SOLVER);
543       } else {
544           ERR << "id " << p << " not found in ZYPP pool." << endl;
545       }
546     }
547
548     // cleanup
549     solver_free(_solv);
550     _solv = NULL;
551     queue_free( &(_jobQueue) );    
552
553     MIL << "SATResolver::resolvePool() done" << endl;
554     return true;
555 }
556
557
558 //----------------------------------------------------------------------------
559 //----------------------------------------------------------------------------
560 // error handling
561 //----------------------------------------------------------------------------
562 //----------------------------------------------------------------------------
563
564 //----------------------------------------------------------------------------
565 // helper function
566 //----------------------------------------------------------------------------
567
568 struct FindPackage : public resfilter::ResObjectFilterFunctor
569 {
570     ProblemSolutionCombi *problemSolution;
571     TransactionKind action;
572     FindPackage (ProblemSolutionCombi *p, const TransactionKind act)
573        : problemSolution (p)
574          , action (act)
575     {
576     }
577
578     bool operator()( PoolItem p)
579     {
580         problemSolution->addSingleAction (p, action);
581         return true;
582     }
583 };
584
585
586 string SATResolver::SATprobleminfoString(Id problem, string &detail)
587 {
588   string ret;
589   Pool *pool = _solv->pool;
590   Id probr;
591   Id dep, source, target;
592   Solvable *s, *s2;
593
594   probr = solver_findproblemrule(_solv, problem);
595   switch (solver_problemruleinfo(_solv, &(_jobQueue), probr, &dep, &source, &target))
596   {
597       case SOLVER_PROBLEM_UPDATE_RULE:
598           s = pool_id2solvable(pool, source);
599           ret = str::form (_("problem with installed package %s"), solvable2str(pool, s));
600           break;
601       case SOLVER_PROBLEM_JOB_RULE:
602           ret = str::form (_("conflicting requests"));
603           break;
604       case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
605           ret = str::form (_("nothing provides requested %s"), dep2str(pool, dep));
606           break;
607       case SOLVER_PROBLEM_NOT_INSTALLABLE:
608           s = pool_id2solvable(pool, source);
609           ret = str::form (_("%s is not installable"), solvable2str(pool, s));
610           break;
611       case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
612           s = pool_id2solvable(pool, source);
613           ret = str::form (_("nothing provides %s needed by %s"), dep2str(pool, dep), solvable2str(pool, s));
614           break;
615       case SOLVER_PROBLEM_SAME_NAME:
616           s = pool_id2solvable(pool, source);
617           s2 = pool_id2solvable(pool, target);
618           ret = str::form (_("cannot install both %s and %s"), solvable2str(pool, s), solvable2str(pool, s2));
619           break;
620       case SOLVER_PROBLEM_PACKAGE_CONFLICT:
621           s = pool_id2solvable(pool, source);
622           s2 = pool_id2solvable(pool, target);
623           ret = str::form (_("%s conflicts with %s provided by %s"), solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
624           break;
625       case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
626           s = pool_id2solvable(pool, source);
627           s2 = pool_id2solvable(pool, target);
628           ret = str::form (_("%s obsoletes %s provided by %s"), solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
629           break;
630       case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
631           s = pool_id2solvable(pool, source);
632           Capability cap(dep);
633           sat::WhatProvides possibleProviders(cap);
634
635           // check, if a provider will be deleted
636           typedef list<PoolItem> ProviderList;
637           ProviderList providerlistInstalled, providerlistUninstalled;
638           for_( iter1, possibleProviders.begin(), possibleProviders.end() ) {
639               PoolItem provider1 = ResPool::instance().find( *iter1 );
640               // find pair of an installed/uninstalled item with the same NVR
641               bool found = false;
642               for_( iter2, possibleProviders.begin(), possibleProviders.end() ) {
643                   PoolItem provider2 = ResPool::instance().find( *iter2 );                
644                   if (compareByNVR (provider1.resolvable(),provider2.resolvable()) == 0
645                       && (provider1.status().isInstalled() && provider2.status().isUninstalled() 
646                           || provider2.status().isInstalled() && provider1.status().isUninstalled()))  {
647                       found = true;
648                       break;
649                   }
650               }
651               if (!found) {
652                   if (provider1.status().isInstalled())
653                       providerlistInstalled.push_back(provider1);
654                   else
655                       providerlistUninstalled.push_back(provider1);
656               }
657           }
658
659           ret = str::form (_("%s requires %s, but this requirement cannot be provided"), solvable2str(pool, s), dep2str(pool, dep));
660           if (providerlistInstalled.size() > 0) {
661               detail += _("deleted providers: ");
662               for (ProviderList::const_iterator iter = providerlistInstalled.begin(); iter != providerlistInstalled.end(); iter++) {
663                   if (iter == providerlistInstalled.begin())
664                       detail += itemToString (*iter, false);
665                   else
666                       detail += "\n                   " + itemToString (*iter, false);
667               }
668           }
669           if (providerlistUninstalled.size() > 0) {
670               if (detail.size() > 0)
671                   detail += _("\nuninstallable providers: ");
672               else
673                   detail = _("uninstallable providers: ");                
674               for (ProviderList::const_iterator iter = providerlistUninstalled.begin(); iter != providerlistUninstalled.end(); iter++) {
675                   if (iter == providerlistUninstalled.begin())
676                       detail += itemToString (*iter, false);                  
677                   else
678                       detail += "\n                   " + itemToString (*iter, false);                
679               }
680           }       
681           break;
682   }
683
684   return ret;
685 }
686
687 ResolverProblemList
688 SATResolver::problems ()
689 {
690     ResolverProblemList resolverProblems;
691     if (_solv && _solv->problems.count) {
692         Pool *pool = _solv->pool;
693         int pcnt;
694         Id p, rp, what;
695         Id problem, solution, element;
696         Solvable *s, *sd;
697
698         MIL << "Encountered problems! Here are the solutions:\n" << endl;
699         pcnt = 1;
700         problem = 0;
701         while ((problem = solver_next_problem(_solv, problem)) != 0) {
702             MIL << "Problem " <<  pcnt++ << ":" << endl;
703             MIL << "====================================" << endl;
704             string detail;
705             string whatString = SATprobleminfoString (problem,detail);
706             MIL << whatString << endl;
707             MIL << "------------------------------------" << endl;
708             ResolverProblem_Ptr resolverProblem = new ResolverProblem (whatString, detail);
709             solution = 0;
710             while ((solution = solver_next_solution(_solv, problem, solution)) != 0) {
711                 element = 0;
712                 ProblemSolutionCombi *problemSolution = new ProblemSolutionCombi(resolverProblem);
713                 while ((element = solver_next_solutionelement(_solv, problem, solution, element, &p, &rp)) != 0) {
714                     if (p == 0) {
715                         /* job, rp is index into job queue */
716                         what = _jobQueue.elements[rp];
717                         switch (_jobQueue.elements[rp-1])
718                         {
719                             case SOLVER_INSTALL_SOLVABLE: {
720                                 s = pool->solvables + what;
721                                 PoolItem poolItem = _pool.find (sat::Solvable(what));
722                                 if (poolItem) {
723                                     if (_solv->installed && s->repo == _solv->installed) {
724                                         problemSolution->addSingleAction (poolItem, REMOVE);
725                                         string description = str::form (_("do not keep %s installed"),  solvable2str(pool, s) );
726                                         MIL << description << endl;
727                                         problemSolution->addDescription (description);
728                                     } else {
729                                         problemSolution->addSingleAction (poolItem, REMOVE);
730                                         string description = str::form (_("do not install %s"), solvable2str(pool, s));
731                                         MIL << description << endl;
732                                         problemSolution->addDescription (description);
733                                     }
734                                 } else {
735                                     ERR << "SOLVER_INSTALL_SOLVABLE: No item found for " << id2str(pool, s->name) << "-"
736                                         <<  id2str(pool, s->evr) << "." <<  id2str(pool, s->arch) << endl;
737                                 }
738                             }
739                                 break;
740                             case SOLVER_ERASE_SOLVABLE: {
741                                 s = pool->solvables + what;
742                                 PoolItem poolItem = _pool.find (sat::Solvable(what));
743                                 if (poolItem) {
744                                     if (_solv->installed && s->repo == _solv->installed) {
745                                         problemSolution->addSingleAction (poolItem, KEEP);
746                                         string description = str::form (_("keep %s"), solvable2str(pool, s));
747                                         MIL << description << endl;
748                                         problemSolution->addDescription (description);
749                                     } else {
750                                         problemSolution->addSingleAction (poolItem, INSTALL);
751                                         string description = str::form (_("do not forbid installation of %s"), solvable2str(pool, s));
752                                         MIL << description << endl;
753                                         problemSolution->addDescription (description);
754                                     }
755                                 } else {
756                                     ERR << "SOLVER_ERASE_SOLVABLE: No item found for " << id2str(pool, s->name) << "-" <<  id2str(pool, s->evr) << "." <<
757                                         id2str(pool, s->arch) << endl;
758                                 }
759                             }
760                                 break;
761                             case SOLVER_INSTALL_SOLVABLE_NAME:
762                                 {
763                                 FindPackage info (problemSolution, KEEP);
764                                 IdString ident( what );
765                                 invokeOnEach( _pool.byIdentBegin( ident ),
766                                               _pool.byIdentEnd( ident ),
767                                               resfilter::ByUninstalled (),
768                                               functor::functorRef<bool,PoolItem> (info) );
769                                 string description = str::form (_("do not install %s"), ident.c_str() );
770                                 MIL << description << endl;
771                                 problemSolution->addDescription (description);
772                                 }
773                                 break;
774                             case SOLVER_ERASE_SOLVABLE_NAME:
775                                 {
776                                 FindPackage info (problemSolution, KEEP);
777                                 IdString ident( what );
778                                 invokeOnEach( _pool.byIdentBegin( ident ),
779                                               _pool.byIdentEnd( ident ),
780                                               functor::chain (resfilter::ByInstalled (),                        // ByInstalled
781                                                               resfilter::ByTransact ()),                        // will be deinstalled
782                                               functor::functorRef<bool,PoolItem> (info) );
783                                 string description = str::form (_("keep %s"), ident.c_str());
784                                 MIL << description << endl;
785                                 problemSolution->addDescription (description);
786                                 }
787                                 break;
788                             case SOLVER_INSTALL_SOLVABLE_PROVIDES:
789                                 {
790                                 Id p, *pp;
791                                 FOR_PROVIDES(p, pp, what);
792                                 {
793                                     PoolItem poolItem = _pool.find (sat::Solvable(p));
794                                     if (poolItem.status().isToBeInstalled()
795                                         || poolItem.status().staysUninstalled())
796                                         problemSolution->addSingleAction (poolItem, KEEP);
797                                 }
798                                 string description = str::form (_("do not ask to install a solvable providing %s"), dep2str(pool, what));
799                                 MIL << description << endl;
800                                 problemSolution->addDescription (description);
801                                 }
802                                 break;
803                             case SOLVER_ERASE_SOLVABLE_PROVIDES:
804                                 {
805                                 Id p, *pp;
806                                 FOR_PROVIDES(p, pp, what);
807                                 {
808                                     PoolItem poolItem = _pool.find (sat::Solvable(p));
809                                     if (poolItem.status().isToBeUninstalled()
810                                         || poolItem.status().staysInstalled())
811                                         problemSolution->addSingleAction (poolItem, KEEP);
812                                 }
813                                 string description = str::form (_("do not ask to delete all solvables providing %s"), dep2str(pool, what));
814                                 MIL << description << endl;
815                                 problemSolution->addDescription (description);
816                                 }
817                                 break;
818                             case SOLVER_INSTALL_SOLVABLE_UPDATE:
819                                 {
820                                 PoolItem poolItem = _pool.find (sat::Solvable(what));
821                                 s = pool->solvables + what;
822                                 if (poolItem) {
823                                     if (_solv->installed && s->repo == _solv->installed) {
824                                         problemSolution->addSingleAction (poolItem, KEEP);
825                                         string description = str::form (_("do not install most recent version of %s"), solvable2str(pool, s));
826                                         MIL << description << endl;
827                                         problemSolution->addDescription (description);
828                                     } else {
829                                         ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE " << poolItem << " is not selected for installation" << endl;
830                                     }
831                                 } else {
832                                     ERR << "SOLVER_INSTALL_SOLVABLE_UPDATE: No item found for " << id2str(pool, s->name) << "-" <<  id2str(pool, s->evr) << "." <<
833                                         id2str(pool, s->arch) << endl;
834                                 }
835                                 }
836                                 break;
837                             default:
838                                 MIL << "- do something different" << endl;
839                                 ERR << "No valid solution available" << endl;
840                                 break;
841                         }
842                     } else {
843                         /* policy, replace p with rp */
844                         s = pool->solvables + p;
845                         sd = rp ? pool->solvables + rp : 0;
846
847                         PoolItem itemFrom = _pool.find (sat::Solvable(p));
848                         if (rp)
849                         {
850                             int gotone = 0;
851
852                             PoolItem itemTo = _pool.find (sat::Solvable(rp));
853                             if (itemFrom && itemTo) {
854                                 problemSolution->addSingleAction (itemTo, INSTALL);
855
856                                 if (evrcmp(pool, s->evr, sd->evr, EVRCMP_COMPARE ) > 0)
857                                 {
858                                     string description = str::form (_("downgrade of %s to %s"), solvable2str(pool, s), solvable2str(pool, sd));
859                                     MIL << description << endl;
860                                     problemSolution->addDescription (description);
861                                     gotone = 1;
862                                 }
863                                 if (!_solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(_solv, s, sd))
864                                 {
865                                     string description = str::form (_("architecture change of %s to %s"), solvable2str(pool, s), solvable2str(pool, sd));
866                                     MIL << description << endl;
867                                     problemSolution->addDescription (description);
868                                     gotone = 1;
869                                 }
870                                 if (!_solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(_solv, s, sd))
871                                 {
872                                     string description = str::form (_("install %s (with vendor change)\n  %s\n-->\n  %s") ,
873                                                                     solvable2str(pool, sd) , id2str(pool, s->vendor),
874                                                                     string(sd->vendor ?  id2str(pool, sd->vendor) : " (no vendor) ").c_str() );
875                                     MIL << description << endl;
876                                     problemSolution->addDescription (description);
877                                     gotone = 1;
878                                 }
879                                 if (!gotone) {
880                                     string description = str::form (_("replacement of %s with %s"), solvable2str(pool, s), solvable2str(pool, sd));
881                                     MIL << description << endl;
882                                     problemSolution->addDescription (description);
883                                 }
884                             } else {
885                                 ERR << id2str(pool, s->name) << "-" <<  id2str(pool, s->evr) << "." <<  id2str(pool, s->arch)
886                                     << " or "  << id2str(pool, sd->name) << "-" <<  id2str(pool, sd->evr) << "." <<  id2str(pool, sd->arch) << " not found" << endl;
887                             }
888                         }
889                         else
890                         {
891                             if (itemFrom) {
892                                 string description = str::form (_("deinstallation of %s"), solvable2str(pool, s));
893                                 MIL << description << endl;
894                                 problemSolution->addDescription (description);
895                                 problemSolution->addSingleAction (itemFrom, REMOVE);
896                             }
897                         }
898                     }
899                 }
900                 resolverProblem->addSolution (problemSolution,
901                                               problemSolution->actionCount() > 1 ? true : false); // Solutions with more than 1 action will be shown first.
902                 MIL << "------------------------------------" << endl;
903             }
904             // save problem
905             resolverProblems.push_back (resolverProblem);
906         }
907     }
908     return resolverProblems;
909 }
910
911 void
912 SATResolver::applySolutions (const ProblemSolutionList & solutions)
913 {
914     for (ProblemSolutionList::const_iterator iter = solutions.begin();
915          iter != solutions.end(); ++iter) {
916         ProblemSolution_Ptr solution = *iter;
917         Resolver dummyResolver(_pool);
918         if (!solution->apply (dummyResolver))
919             break;
920     }
921 }
922
923
924
925 ///////////////////////////////////////////////////////////////////
926 };// namespace detail
927     /////////////////////////////////////////////////////////////////////
928     /////////////////////////////////////////////////////////////////////
929   };// namespace solver
930   ///////////////////////////////////////////////////////////////////////
931   ///////////////////////////////////////////////////////////////////////
932 };// namespace zypp
933 /////////////////////////////////////////////////////////////////////////
934