565177aefd1773d2f0d41d0afb5e26a1cc65d7f8
[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/TriBool.h"
35 #include "zypp/base/SerialNumber.h"
36
37 #include "zypp/solver/detail/Types.h"
38 #include "zypp/solver/detail/SolverQueueItem.h"
39
40 #include "zypp/ProblemTypes.h"
41 #include "zypp/ResolverProblem.h"
42 #include "zypp/ProblemSolution.h"
43 #include "zypp/Capabilities.h"
44 #include "zypp/Capability.h"
45
46
47 /////////////////////////////////////////////////////////////////////////
48 namespace zypp
49 { ///////////////////////////////////////////////////////////////////////
50
51   namespace sat
52   {
53     class Transaction;
54   }
55
56   ///////////////////////////////////////////////////////////////////////
57   namespace solver
58   { /////////////////////////////////////////////////////////////////////
59     /////////////////////////////////////////////////////////////////////
60     namespace detail
61     { ///////////////////////////////////////////////////////////////////
62
63     class SATResolver;
64
65     ///////////////////////////////////////////////////////////////////
66     //
67     //  CLASS NAME : ItemCapKind
68     //
69     /** */
70     struct ItemCapKind
71     {
72         public:
73         Capability cap; //Capability which has triggerd this selection
74         Dep capKind; //Kind of that capability
75         PoolItem item; //Item which has triggered this selection
76         bool initialInstallation; //This item has triggered the installation
77                                   //Not already fullfilled requierement only.
78
79     ItemCapKind() : capKind(Dep::PROVIDES) {}
80             ItemCapKind( PoolItem i, Capability c, Dep k, bool initial)
81                 : cap( c )
82                 , capKind( k )
83                 , item( i )
84                 , initialInstallation( initial )
85             { }
86     };
87     typedef std::multimap<PoolItem,ItemCapKind> ItemCapKindMap;
88     typedef std::list<ItemCapKind> ItemCapKindList;
89
90
91 ///////////////////////////////////////////////////////////////////
92 //
93 //      CLASS NAME : Resolver
94 /** A mid layer class we should remove
95  * \todo Merge this and class SATResolver. Logic and date are horribly
96  * distributed between this and SATResolver. Either SATResolver becomes
97  * a pure wrapper adapting the libsolv C interface to fit our needs, and
98  * all the solver logic and problem handling goes here; or completely merge
99  * both classes.
100  */
101 class Resolver : public base::ReferenceCounted, private base::NonCopyable {
102
103   private:
104     ResPool _pool;
105     SATResolver *_satResolver;
106     SerialNumberWatcher _poolchanged;
107
108     CapabilitySet _extra_requires;
109     CapabilitySet _extra_conflicts;
110     std::set<Repository> _upgradeRepos;
111
112     // Regard dependencies of the item weak onl
113     PoolItemList _addWeak;
114
115     /** \name Solver flags */
116     //@{
117     bool _forceResolve;           // remove items which are conflicts with others or
118                                   // have unfulfilled requirements.
119                                   // This behaviour is favourited by ZMD
120     bool _upgradeMode;            // Resolver has been called with doUpgrade
121     bool _updateMode;            // Resolver has been called with doUpdate
122     bool _verifying;              // The system will be checked
123     bool _onlyRequires;           // do install required resolvables only
124                                   // no recommended resolvables, language
125                                   // packages, hardware packages (modalias)
126     bool _allowVendorChange;    // whether the solver should allow or disallow vendor changes.
127     bool _solveSrcPackages;     // whether to generate solver jobs for selected source packges.
128     bool _cleandepsOnRemove;    // whether removing a package should also remove no longer needed requirements
129
130     bool _ignoreAlreadyRecommended;   //ignore recommended packages that have already been recommended by the installed packages
131     //@}
132
133     // Additional QueueItems which has to be regarded by the solver
134     // This will be used e.g. by solution actions
135     solver::detail::SolverQueueItemList _removed_queue_items;
136     solver::detail::SolverQueueItemList _added_queue_items;
137
138     // Additional information about the solverrun
139     ItemCapKindMap _isInstalledBy;
140     ItemCapKindMap _installs;
141     ItemCapKindMap _satifiedByInstalled;
142     ItemCapKindMap _installedSatisfied;
143
144     // helpers
145     void collectResolverInfo();
146
147     // Unmaintained packages which does not fit to the updated system
148     // (broken dependencies) will be deleted.
149     // returns true if solving was successful
150     bool checkUnmaintainedItems ();
151
152     void solverInit();
153
154   public:
155
156     Resolver( const ResPool & pool );
157     virtual ~Resolver();
158
159     // ---------------------------------- I/O
160
161     virtual std::ostream & dumpOn( std::ostream & str ) const;
162     friend std::ostream& operator<<( std::ostream& str, const Resolver & obj )
163     { return obj.dumpOn (str); }
164
165     // ---------------------------------- methods
166
167     ResPool pool() const;
168     void setPool( const ResPool & pool ) { _pool = pool; }
169
170     void addUpgradeRepo( Repository repo_r )            { if ( repo_r && ! repo_r.isSystemRepo() ) _upgradeRepos.insert( repo_r ); }
171     bool upgradingRepo( Repository repo_r ) const       { return( _upgradeRepos.find( repo_r ) != _upgradeRepos.end() ); }
172     void removeUpgradeRepo( Repository repo_r )         { _upgradeRepos.erase( repo_r ); }
173     void removeUpgradeRepos()                           { _upgradeRepos.clear(); }
174     const std::set<Repository> & upgradeRepos() const   { return _upgradeRepos; }
175
176     void addExtraRequire( const Capability & capability );
177     void removeExtraRequire( const Capability & capability );
178     void addExtraConflict( const Capability & capability );
179     void removeExtraConflict( const Capability & capability );
180
181     void removeQueueItem( SolverQueueItem_Ptr item );
182     void addQueueItem( SolverQueueItem_Ptr item );
183
184     CapabilitySet extraRequires() const         { return _extra_requires; }
185     CapabilitySet extraConflicts() const        { return _extra_conflicts; }
186
187     void addWeak( const PoolItem & item );
188
189     bool verifySystem();
190     bool resolvePool();
191     bool resolveQueue( SolverQueueItemList & queue );
192     void doUpdate();
193
194     bool doUpgrade();
195     PoolItemList problematicUpdateItems() const;
196
197     /** \name Solver flags */
198     //@{
199     bool ignoreAlreadyRecommended() const       { return _ignoreAlreadyRecommended; }
200     void setIgnoreAlreadyRecommended( bool yesno_r ) { _ignoreAlreadyRecommended = yesno_r; }
201
202     bool onlyRequires () const                  { return _onlyRequires; }
203     void setOnlyRequires( TriBool state_r );
204
205     bool forceResolve() const                   { return _forceResolve; }
206     void setForceResolve( TriBool state_r )     { _forceResolve = indeterminate(state_r) ? false : bool(state_r); }
207
208     bool isUpgradeMode() const                  { return _upgradeMode; }// Resolver has been called with doUpgrade
209     void setUpgradeMode( bool yesno_r )         { _upgradeMode = yesno_r; }
210
211     bool isUpdateMode() const                   { return _updateMode; } // Resolver has been called with doUpdate
212
213     bool isVerifyingMode() const                { return _verifying; }  // The system will be checked
214     void setVerifyingMode( TriBool state_r )    { _verifying = indeterminate(state_r) ? false : bool(state_r); }
215
216     bool allowVendorChange() const              { return _allowVendorChange; }
217     void setAllowVendorChange( TriBool state_r );
218
219     bool solveSrcPackages() const               { return _solveSrcPackages; }
220     void setSolveSrcPackages( TriBool state_r ) { _solveSrcPackages = indeterminate(state_r) ? false : bool(state_r); }
221
222     bool cleandepsOnRemove() const              { return _cleandepsOnRemove; }
223     void setCleandepsOnRemove( TriBool state_r );
224     //@}
225
226 #define ZOLV_FLAG_TRIBOOL( ZSETTER, ZGETTER )   \
227     void ZSETTER( TriBool state_r );            \
228     bool ZGETTER() const;                       \
229
230     ZOLV_FLAG_TRIBOOL( dupSetAllowDowngrade,    dupAllowDowngrade )
231     ZOLV_FLAG_TRIBOOL( dupSetAllowNameChange,   dupAllowNameChange )
232     ZOLV_FLAG_TRIBOOL( dupSetAllowArchChange,   dupAllowArchChange )
233     ZOLV_FLAG_TRIBOOL( dupSetAllowVendorChange, dupAllowVendorChange )
234
235 #undef ZOLV_FLAG_TRIBOOL
236
237     ResolverProblemList problems() const;
238     void applySolutions( const ProblemSolutionList & solutions );
239
240     // Return the Transaction computed by the last solver run.
241     sat::Transaction getTransaction();
242
243     // reset all SOLVER transaction in pool
244     void undo();
245
246     void reset( bool keepExtras = false );
247
248     // Get more information about the solverrun
249     // Which item will be installed by another item or triggers an item for
250     // installation
251     ItemCapKindList isInstalledBy( const PoolItem & item );
252     ItemCapKindList installs( const PoolItem & item );
253     ItemCapKindList satifiedByInstalled (const PoolItem & item );
254     ItemCapKindList installedSatisfied( const PoolItem & item );
255
256 };
257
258 ///////////////////////////////////////////////////////////////////
259     };// namespace detail
260     /////////////////////////////////////////////////////////////////////
261     /////////////////////////////////////////////////////////////////////
262   };// namespace solver
263   ///////////////////////////////////////////////////////////////////////
264   ///////////////////////////////////////////////////////////////////////
265 };// namespace zypp
266 /////////////////////////////////////////////////////////////////////////
267
268 #endif // ZYPP_SOLVER_DETAIL_RESOLVER_H