API for retrieving additional dependencies
[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
141
142     ResolverQueueList _pending_queues;
143     ResolverQueueList _pruned_queues;
144     ResolverQueueList _complete_queues;
145     ResolverQueueList _deferred_queues;
146     ResolverQueueList _invalid_queues;
147
148     int _valid_solution_count;
149
150     ResolverContext_Ptr _best_context;
151     // Context of the last establishing call ( without any transaction )
152     ResolverContext_Ptr _establish_context;
153     bool _timed_out;
154
155     std::set<Repository> _subscribed;
156
157     Arch _architecture;
158
159     bool _forceResolve; // remove items which are conflicts with others or
160                         // have unfulfilled requirements.
161                         // This behaviour is favourited by ZMD
162     bool _upgradeMode;  // Resolver has been called with doUpgrade
163     bool _preferHighestVersion; // Prefer the result with the newest version
164                                 //if there are more solver results.
165
166     // helpers
167     bool doesObsoleteCapability (PoolItem_Ref candidate, const Capability & cap);
168     bool doesObsoleteItem (PoolItem_Ref candidate, PoolItem_Ref installed);
169
170     void collectResolverInfo (void);
171
172
173   public:
174
175     Resolver (const ResPool & pool);
176     virtual ~Resolver();
177
178     // ---------------------------------- I/O
179
180     virtual std::ostream & dumpOn( std::ostream & str ) const;
181     friend std::ostream& operator<<(std::ostream& str, const Resolver & obj)
182     { return obj.dumpOn (str); }
183     void dumpTaskList(const PoolItemList &install, const PoolItemList &remove );
184
185     // ---------------------------------- accessors
186
187     QueueItemList initialItems () const { return _initial_items; }
188
189     ResolverQueueList pendingQueues () const { return _pending_queues; }
190     ResolverQueueList prunedQueues () const { return _pruned_queues; }
191     ResolverQueueList completeQueues () const { return _complete_queues; }
192     ResolverQueueList deferredQueues () const { return _deferred_queues; }
193     ResolverQueueList invalidQueues () const { return _invalid_queues; }
194
195     ResolverContext_Ptr bestContext (void) const { return _best_context; }
196
197     /** depending on the last solver result, either return bestContext()
198         of the first invalid context */
199     ResolverContext_Ptr context (void) const;
200
201     // ---------------------------------- methods
202
203     void setTimeout (int seconds) { _timeout_seconds = seconds; }
204     void setMaxSolverPasses (int count) { _maxSolverPasses = count; }
205     int timeout () { return _timeout_seconds; }
206     int maxSolverPasses () { return _maxSolverPasses; }
207
208     ResPool pool (void) const;
209     void setPool (const ResPool & pool) { _pool = pool; }
210
211     void addSubscribedSource (Repository source);
212
213     void addPoolItemToInstall (PoolItem_Ref item);
214     void addPoolItemsToInstallFromList (PoolItemList & rl);
215
216     void addPoolItemToLockUninstalled (PoolItem_Ref item);
217
218     void addPoolItemToRemove (PoolItem_Ref item);
219     void addPoolItemsToRemoveFromList (PoolItemList & rl);
220
221     void addPoolItemToEstablish (PoolItem_Ref item);
222     void addPoolItemsToEstablishFromList (PoolItemList & rl);
223
224     void addPoolItemToVerify (PoolItem_Ref item);
225
226     void addExtraCapability (const Capability & capability);
227     void addExtraConflict (const Capability & capability);
228
229     const CapSet extraCapability () { return _extra_caps; }
230     const CapSet extraConflicts () { return _extra_conflicts; }
231
232     void addIgnoreConflict (const PoolItem_Ref item,
233                             const Capability & capability);
234     void addIgnoreRequires (const PoolItem_Ref item,
235                             const Capability & capability);
236     void addIgnoreObsoletes (const PoolItem_Ref item,
237                              const Capability & capability);
238     void addIgnoreInstalledItem (const PoolItem_Ref item);
239     void addIgnoreArchitectureItem (const PoolItem_Ref item);
240
241     void setForceResolve (const bool force) { _forceResolve = force; }
242     const bool forceResolve() { return _forceResolve; }
243     void setPreferHighestVersion (const bool highestVersion) { _preferHighestVersion = highestVersion; }
244     const bool preferHighestVersion() { return _preferHighestVersion; }
245
246     void setTryAllPossibilities (const bool tryAllPossibilities) { _tryAllPossibilities = tryAllPossibilities; }
247     const bool tryAllPossibilities () { return _tryAllPossibilities; };
248
249     bool verifySystem (bool considerNewHardware = false);
250     void establishState (ResolverContext_Ptr context = NULL);
251     bool establishPool (void);
252     void freshenState( ResolverContext_Ptr context = NULL, bool resetAfterSolve = true );
253     bool freshenPool( bool resetAfterSolve = true );
254     bool resolveDependencies (const ResolverContext_Ptr context = NULL);
255     bool resolvePool( bool tryAllPossibilities = false);
256
257     bool transactResObject( ResObject::constPtr robj,
258                             bool install = true,
259                             bool recursive = false);
260     bool transactResKind( Resolvable::Kind kind );
261     void transactReset( ResStatus::TransactByValue causer );
262
263     void doUpgrade( zypp::UpgradeStatistics & opt_stats_r );
264     PoolItemList problematicUpdateItems( void ) const { return _update_items; }
265
266
267     ResolverProblemList problems (const bool ignoreValidSolution = false) const;
268     void applySolutions (const ProblemSolutionList &solutions);
269     // returns a string list of ResolverInfo of the LAST not valid solution
270     std::list<std::string> problemDescription( void ) const;
271
272     // reset all SOLVER transaction in pool
273     void undo(void);
274
275     // Get more information about the solverrun
276     // Which item will be installed by another item or triggers an item for
277     // installation
278     const ItemCapKindList isInstalledBy (const PoolItem_Ref item);
279     const ItemCapKindList installs (const PoolItem_Ref item);
280
281     void reset (bool resetValidResults = false, bool keepExtras = false );
282
283     Arch architecture() const { return _architecture; }
284     void setArchitecture( const Arch & arch) { _architecture = arch; }
285
286     bool testing(void) const { return _testing; }
287     void setTesting( bool testing ) { _testing = testing; }
288 };
289
290 ///////////////////////////////////////////////////////////////////
291     };// namespace detail
292     /////////////////////////////////////////////////////////////////////
293     /////////////////////////////////////////////////////////////////////
294   };// namespace solver
295   ///////////////////////////////////////////////////////////////////////
296   ///////////////////////////////////////////////////////////////////////
297 };// namespace zypp
298 /////////////////////////////////////////////////////////////////////////
299
300 #endif // ZYPP_SOLVER_DETAIL_RESOLVER_H