Bugfixes concerning vendor handling:
[platform/upstream/libzypp.git] / zypp / solver / detail / Resolver.h
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* Resolver.h
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
22 #ifndef ZYPP_SOLVER_DETAIL_RESOLVER_H
23 #define ZYPP_SOLVER_DETAIL_RESOLVER_H
24
25 #include <iosfwd>
26 #include <list>
27 #include <map>
28 #include <string>
29
30 #include "zypp/base/ReferenceCounted.h"
31 #include "zypp/base/PtrTypes.h"
32
33 #include "zypp/ResPool.h"
34 #include "zypp/base/SerialNumber.h"
35
36 #include "zypp/solver/detail/Types.h"
37 #include "zypp/solver/detail/ResolverQueue.h"
38 #include "zypp/solver/detail/ResolverContext.h"
39 #include "zypp/solver/detail/ContextPool.h"
40
41 #include "zypp/ProblemTypes.h"
42 #include "zypp/ResolverProblem.h"
43 #include "zypp/ProblemSolution.h"
44 #include "zypp/UpgradeStatistics.h"
45
46 #include "zypp/CapSet.h"
47
48
49 /////////////////////////////////////////////////////////////////////////
50 namespace zypp
51 { ///////////////////////////////////////////////////////////////////////
52   ///////////////////////////////////////////////////////////////////////
53   namespace solver
54   { /////////////////////////////////////////////////////////////////////
55     /////////////////////////////////////////////////////////////////////
56     namespace detail
57     { ///////////////////////////////////////////////////////////////////
58
59     ///////////////////////////////////////////////////////////////////
60     //
61     //  CLASS NAME : ItemCapKind
62     //
63     /** */
64     struct ItemCapKind
65     {
66         public:
67         Capability cap; //Capability which has triggerd this selection
68         Dep capKind; //Kind of that capability
69         PoolItem_Ref item; //Item which has triggered this selection
70         bool initialInstallation; //This item has triggered the installation
71                                   //Not already fullfilled requierement only.
72
73             ItemCapKind( PoolItem i, Capability c, Dep k, bool initial)
74                 : cap( c )
75                 , capKind( k )
76                 , item( i )
77                 , initialInstallation( initial )
78             { }
79     };
80     typedef std::multimap<PoolItem_Ref,ItemCapKind> ItemCapKindMap;
81     typedef std::list<ItemCapKind> ItemCapKindList;
82
83 ///////////////////////////////////////////////////////////////////
84 //
85 //      CLASS NAME : Resolver
86
87 class Resolver : public base::ReferenceCounted, private base::NonCopyable {
88
89   private:
90     ResPool _pool;
91     SerialNumberWatcher _poolchanged;
92
93     unsigned _timeout_seconds;
94     unsigned _maxSolverPasses;
95     bool _verifying;
96     bool _testing;
97
98     // In order reducing solver time we are reducing the branches
99     // by skipping resolvables which have worse architecture,edition
100     // than a resolvable which provides the same cababilities.
101     // BUT if there is no valid solution we will regard the "other"
102     // resolvables in a second solver run too.
103     bool _tryAllPossibilities; // Try ALL alternatives
104
105     // list populated by calls to addPoolItemTo*()
106     QueueItemList _initial_items;
107     PoolItemList _items_to_install;
108     PoolItemList _items_to_establish;
109     PoolItemList _items_to_remove;
110     PoolItemList _items_to_verify;
111     PoolItemList _items_to_lockUninstalled;
112
113     // pool of valid contexts which are "recycled" in order to fasten the solver
114     ContextPool contextPool;
115
116     // list of problematic items after doUpgrade()
117     PoolItemList _update_items;
118
119     // Additional information about the solverrun
120     ItemCapKindMap _isInstalledBy;
121     ItemCapKindMap _installs;
122
123     CapSet _extra_caps;
124     CapSet _extra_conflicts;
125
126     //typedef std::multimap<PoolItem_Ref,Capability> IgnoreMap;
127
128     // These conflict should be ignored of the concering item
129     IgnoreMap _ignoreConflicts;
130     // These requires should be ignored of the concering item
131     IgnoreMap _ignoreRequires;
132     // These obsoletes should be ignored of the concering item
133     IgnoreMap _ignoreObsoletes;
134     // Ignore architecture of the item
135     PoolItemList _ignoreArchitecture;
136     // Ignore the status "installed" of the item
137     PoolItemList _ignoreInstalledItem;
138     // Ignore the architecture of the item
139     PoolItemList _ignoreArchitectureItem;
140     // Ignore the vendor of the item
141     PoolItemList _ignoreVendorItem;
142
143     ResolverQueueList _pending_queues;
144     ResolverQueueList _pruned_queues;
145     ResolverQueueList _complete_queues;
146     ResolverQueueList _deferred_queues;
147     ResolverQueueList _invalid_queues;
148
149     int _valid_solution_count;
150
151     ResolverContext_Ptr _best_context;
152     // Context of the last establishing call ( without any transaction )
153     ResolverContext_Ptr _establish_context;
154     bool _timed_out;
155
156     std::set<Repository> _subscribed;
157
158     Arch _architecture;
159
160     bool _forceResolve; // remove items which are conflicts with others or
161                         // have unfulfilled requirements.
162                         // This behaviour is favourited by ZMD
163     bool _upgradeMode;  // Resolver has been called with doUpgrade
164     bool _preferHighestVersion; // Prefer the result with the newest version
165                                 //if there are more solver results.
166
167     // helpers
168     bool doesObsoleteCapability (PoolItem_Ref candidate, const Capability & cap);
169     bool doesObsoleteItem (PoolItem_Ref candidate, PoolItem_Ref installed);
170
171     void collectResolverInfo (void);
172
173
174   public:
175
176     Resolver (const ResPool & pool);
177     virtual ~Resolver();
178
179     // ---------------------------------- I/O
180
181     virtual std::ostream & dumpOn( std::ostream & str ) const;
182     friend std::ostream& operator<<(std::ostream& str, const Resolver & obj)
183     { return obj.dumpOn (str); }
184     void dumpTaskList(const PoolItemList &install, const PoolItemList &remove );
185
186     // ---------------------------------- accessors
187
188     QueueItemList initialItems () const { return _initial_items; }
189
190     ResolverQueueList pendingQueues () const { return _pending_queues; }
191     ResolverQueueList prunedQueues () const { return _pruned_queues; }
192     ResolverQueueList completeQueues () const { return _complete_queues; }
193     ResolverQueueList deferredQueues () const { return _deferred_queues; }
194     ResolverQueueList invalidQueues () const { return _invalid_queues; }
195
196     ResolverContext_Ptr bestContext (void) const { return _best_context; }
197
198     /** depending on the last solver result, either return bestContext()
199         of the first invalid context */
200     ResolverContext_Ptr context (void) const;
201
202     // ---------------------------------- methods
203
204     void setTimeout (int seconds) { _timeout_seconds = seconds; }
205     void setMaxSolverPasses (int count) { _maxSolverPasses = count; }
206     int timeout () { return _timeout_seconds; }
207     int maxSolverPasses () { return _maxSolverPasses; }
208
209     ResPool pool (void) const;
210     void setPool (const ResPool & pool) { _pool = pool; }
211
212     void addSubscribedSource (Repository source);
213
214     void addPoolItemToInstall (PoolItem_Ref item);
215     void addPoolItemsToInstallFromList (PoolItemList & rl);
216
217     void addPoolItemToLockUninstalled (PoolItem_Ref item);
218
219     void addPoolItemToRemove (PoolItem_Ref item);
220     void addPoolItemsToRemoveFromList (PoolItemList & rl);
221
222     void addPoolItemToEstablish (PoolItem_Ref item);
223     void addPoolItemsToEstablishFromList (PoolItemList & rl);
224
225     void addPoolItemToVerify (PoolItem_Ref item);
226
227     void addExtraCapability (const Capability & capability);
228     void addExtraConflict (const Capability & capability);
229
230     const CapSet extraCapability () { return _extra_caps; }
231     const CapSet extraConflicts () { return _extra_conflicts; }
232
233     void addIgnoreConflict (const PoolItem_Ref item,
234                             const Capability & capability);
235     void addIgnoreRequires (const PoolItem_Ref item,
236                             const Capability & capability);
237     void addIgnoreObsoletes (const PoolItem_Ref item,
238                              const Capability & capability);
239     void addIgnoreInstalledItem (const PoolItem_Ref item);
240     void addIgnoreArchitectureItem (const PoolItem_Ref item);
241     void addIgnoreVendorItem (const PoolItem_Ref item);    
242
243     void setForceResolve (const bool force) { _forceResolve = force; }
244     const bool forceResolve() { return _forceResolve; }
245     void setPreferHighestVersion (const bool highestVersion) { _preferHighestVersion = highestVersion; }
246     const bool preferHighestVersion() { return _preferHighestVersion; }
247
248     void setTryAllPossibilities (const bool tryAllPossibilities) { _tryAllPossibilities = tryAllPossibilities; }
249     const bool tryAllPossibilities () { return _tryAllPossibilities; };
250
251     bool verifySystem (bool considerNewHardware = false);
252     void establishState (ResolverContext_Ptr context = NULL);
253     bool establishPool (void);
254     void freshenState( ResolverContext_Ptr context = NULL, bool resetAfterSolve = true );
255     bool freshenPool( bool resetAfterSolve = true );
256     bool resolveDependencies (const ResolverContext_Ptr context = NULL);
257     bool resolvePool( bool tryAllPossibilities = false);
258
259     bool transactResObject( ResObject::constPtr robj,
260                             bool install = true,
261                             bool recursive = false);
262     bool transactResKind( Resolvable::Kind kind );
263     void transactReset( ResStatus::TransactByValue causer );
264
265     void doUpgrade( zypp::UpgradeStatistics & opt_stats_r );
266     PoolItemList problematicUpdateItems( void ) const { return _update_items; }
267
268
269     ResolverProblemList problems (const bool ignoreValidSolution = false) const;
270     void applySolutions (const ProblemSolutionList &solutions);
271     // returns a string list of ResolverInfo of the LAST not valid solution
272     std::list<std::string> problemDescription( void ) const;
273
274     // reset all SOLVER transaction in pool
275     void undo(void);
276
277     // Get more information about the solverrun
278     // Which item will be installed by another item or triggers an item for
279     // installation
280     const ItemCapKindList isInstalledBy (const PoolItem_Ref item);
281     const ItemCapKindList installs (const PoolItem_Ref item);
282
283     void reset (bool resetValidResults = false, bool keepExtras = false );
284
285     Arch architecture() const { return _architecture; }
286     void setArchitecture( const Arch & arch) { _architecture = arch; }
287
288     bool testing(void) const { return _testing; }
289     void setTesting( bool testing ) { _testing = testing; }
290 };
291
292 ///////////////////////////////////////////////////////////////////
293     };// namespace detail
294     /////////////////////////////////////////////////////////////////////
295     /////////////////////////////////////////////////////////////////////
296   };// namespace solver
297   ///////////////////////////////////////////////////////////////////////
298   ///////////////////////////////////////////////////////////////////////
299 };// namespace zypp
300 /////////////////////////////////////////////////////////////////////////
301
302 #endif // ZYPP_SOLVER_DETAIL_RESOLVER_H