rename packages works for update now
[platform/upstream/libzypp.git] / zypp / solver / detail / Resolver.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* Resolver.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 to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19  * 02111-1307, USA.
20  */
21 #include <boost/static_assert.hpp>
22
23 #include "zypp/solver/detail/Resolver.h"
24 #include "zypp/solver/detail/Helper.h"
25 #include "zypp/solver/detail/Testcase.h"
26
27 #include "zypp/Capabilities.h"
28 #include "zypp/base/Logger.h"
29 #include "zypp/base/String.h"
30 #include "zypp/base/Gettext.h"
31 #include "zypp/base/Algorithm.h"
32 #include "zypp/ResPool.h"
33 #include "zypp/ResFilters.h"
34 #include "zypp/CapFilters.h"
35 #include "zypp/sat/Pool.h"
36 #include "zypp/sat/Solvable.h"
37 #include "zypp/sat/SATResolver.h"
38
39 /////////////////////////////////////////////////////////////////////////
40 namespace zypp
41 { ///////////////////////////////////////////////////////////////////////
42   ///////////////////////////////////////////////////////////////////////
43   namespace solver
44   { /////////////////////////////////////////////////////////////////////
45     /////////////////////////////////////////////////////////////////////
46     namespace detail
47     { ///////////////////////////////////////////////////////////////////
48
49 using namespace std;
50
51 IMPL_PTR_TYPE(Resolver);
52
53
54 //---------------------------------------------------------------------------
55
56
57 std::ostream &
58 Resolver::dumpOn( std::ostream & os ) const
59 {
60     return os << "<resolver/>";
61 }
62
63
64 //---------------------------------------------------------------------------
65
66 Resolver::Resolver (const ResPool & pool)
67     : _pool(pool)
68     , _satResolver(NULL)
69     , _poolchanged(_pool.serial() )
70     , _forceResolve(false)
71     , _upgradeMode(false)
72     , _verifying(false)
73
74 {
75     Testcase testcase("/var/log/YaST2/autoTestcase");    
76     sat::Pool satPool( sat::Pool::instance() );
77     _satResolver = new SATResolver(_pool, satPool.get());
78     testcase.createTestcase (*this, true, false); // create pool, do not solver
79 }
80
81
82 Resolver::~Resolver()
83 {
84 }
85
86 //---------------------------------------------------------------------------
87
88 ResPool
89 Resolver::pool (void) const
90 {
91     return _pool;
92 }
93
94 void
95 Resolver::reset (bool keepExtras )
96 {
97     _verifying = false;    
98
99     if (!keepExtras) {
100       _extra_requires.clear();
101       _extra_conflicts.clear();
102     }
103 }
104
105 void
106 Resolver::addExtraRequire (const Capability & capability)
107 {
108     _extra_requires.insert (capability);
109 }
110
111 void
112 Resolver::removeExtraRequire (const Capability & capability)
113 {
114     _extra_requires.erase (capability);
115 }
116
117 void
118 Resolver::addExtraConflict (const Capability & capability)
119 {
120     _extra_conflicts.insert (capability);
121 }
122
123 void
124 Resolver::removeExtraConflict (const Capability & capability)
125 {
126     _extra_conflicts.erase (capability);
127 }
128
129 void
130 Resolver::addIgnoreConflict (const PoolItem item,
131                    const Capability & capability)
132 {
133     _ignoreConflicts.insert(make_pair(item, capability));
134 }
135
136 void
137 Resolver::addIgnoreRequires (const PoolItem item,
138                              const Capability & capability)
139 {
140     _ignoreRequires.insert(make_pair(item, capability));
141 }
142
143 void
144 Resolver::addIgnoreObsoletes (const PoolItem item,
145                               const Capability & capability)
146 {
147     _ignoreObsoletes.insert(make_pair(item, capability));
148 }
149
150 void
151 Resolver::addIgnoreInstalledItem (const PoolItem item)
152 {
153     _ignoreInstalledItem.push_back (item);
154 }
155
156 void
157 Resolver::addIgnoreArchitectureItem (const PoolItem item)
158 {
159     _ignoreArchitectureItem.push_back (item);
160 }
161
162 void
163 Resolver::addIgnoreVendorItem (const PoolItem item)
164 {
165     _ignoreVendorItem.push_back (item);
166 }
167
168 //---------------------------------------------------------------------------
169
170 struct UndoTransact : public resfilter::PoolItemFilterFunctor
171 {
172     ResStatus::TransactByValue resStatus;
173     UndoTransact ( const ResStatus::TransactByValue &status)
174         :resStatus(status)
175     { }
176
177     bool operator()( PoolItem item )            // only transacts() items go here
178     {
179         item.status().resetTransact( resStatus );// clear any solver/establish transactions
180         return true;
181     }
182 };
183
184
185 struct DoTransact : public resfilter::PoolItemFilterFunctor
186 {
187     ResStatus::TransactByValue resStatus;
188     DoTransact ( const ResStatus::TransactByValue &status)
189         :resStatus(status)
190     { }
191
192     bool operator()( PoolItem item )            // only transacts() items go here
193     {
194         item.status().setTransact( true, resStatus );
195         return true;
196     }
197 };
198
199
200 bool
201 Resolver::verifySystem ()
202 {
203     UndoTransact resetting (ResStatus::APPL_HIGH);
204
205     _DEBUG ("Resolver::verifySystem() ");
206     
207     _verifying = true;    
208
209     invokeOnEach ( _pool.begin(), _pool.end(),
210                    resfilter::ByTransact( ),                    // Resetting all transcations
211                    functor::functorRef<bool,PoolItem>(resetting) );
212
213     return resolvePool();
214 }
215
216
217 //----------------------------------------------------------------------------
218 // undo
219
220 void
221 Resolver::undo(void)
222 {
223     UndoTransact info(ResStatus::APPL_LOW);
224     MIL << "*** undo ***" << endl;
225     invokeOnEach ( _pool.begin(), _pool.end(),
226                    resfilter::ByTransact( ),                    // collect transacts from Pool to resolver queue
227                    functor::functorRef<bool,PoolItem>(info) );
228     // These conflict should be ignored of the concering item
229     _ignoreConflicts.clear();
230     // These requires should be ignored of the concering item
231     _ignoreRequires.clear();
232     // These obsoletes should be ignored of the concering item
233     _ignoreObsoletes.clear();
234     // Ignore architecture of the item
235     _ignoreArchitecture.clear();
236     // Ignore the status "installed" of the item
237     _ignoreInstalledItem.clear();
238     // Ignore the architecture of the item
239     _ignoreArchitectureItem.clear();
240     // Ignore the vendor of the item
241     _ignoreVendorItem.clear();
242
243
244     return;
245 }
246
247
248 bool
249 Resolver::resolvePool()
250 {
251
252     // Solving with the satsolver
253         MIL << "-------------- Calling SAT Solver -------------------" << endl;
254         Testcase testcase("/var/log/YaST2/autoTestcase");
255         testcase.createTestcase (*this, false, false); // write control file only
256
257 #if 0
258         MIL << "------SAT-Pool------" << endl;
259         for (sat::Pool::SolvableIterator i = satPool.solvablesBegin();
260              i != satPool.solvablesEnd(); i++ ) {
261             MIL << *i << " ID: " << i->id() << endl;
262         }
263         MIL << "------SAT-Pool end------" << endl;
264 #endif
265         _satResolver->setFixsystem(false);
266         _satResolver->setAllowdowngrade(false);
267         _satResolver->setAllowarchchange(false);
268         _satResolver->setAllowvendorchange(false);
269         _satResolver->setAllowuninstall(false);
270         _satResolver->setUpdatesystem(false);
271         _satResolver->setAllowvirtualconflicts(false);
272         _satResolver->setNoupdateprovide(true);
273         _satResolver->setDosplitprovides(false);
274         
275         if (_upgradeMode) {
276             _satResolver->setAllowdowngrade(true);
277             _satResolver->setAllowarchchange(true);
278             _satResolver->setUpdatesystem(true);
279             _satResolver->setDosplitprovides(true);   
280         }
281
282         if (_forceResolve)
283             _satResolver->setAllowuninstall(true);
284
285         if (_verifying)
286             _satResolver->setFixsystem(true);
287         
288         return _satResolver->resolvePool(_extra_requires, _extra_conflicts);
289 }
290
291
292 ///////////////////////////////////////////////////////////////////
293     };// namespace detail
294     /////////////////////////////////////////////////////////////////////
295     /////////////////////////////////////////////////////////////////////
296   };// namespace solver
297   ///////////////////////////////////////////////////////////////////////
298   ///////////////////////////////////////////////////////////////////////
299 };// namespace zypp
300 /////////////////////////////////////////////////////////////////////////
301