ProblemSolution* and SolutionAction compile now
[platform/upstream/libzypp.git] / zypp / solver / detail / InstallOrder.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* InstallOrder.cc
3  *
4  * Copyright (C) 2005 SUSE Linux Products GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20
21 // stolen from yast2-packagemanager
22 /*
23    File:       InstallOrder.h
24    Purpose:    Determine order for installing packages
25    Author:     Ludwig Nussel <lnussel@suse.de>
26    Maintainer: Ludwig Nussel <lnussel@suse.de>
27
28 /-*/
29
30 #include "zypp/solver/detail/InstallOrder.h"
31 #include "zypp/base/Logger.h"
32 #include "zypp/base/Iterator.h"
33 #include "zypp/base/Algorithm.h"
34
35 #include "zypp/ResFilters.h"
36 #include "zypp/ResStatus.h"
37
38 /////////////////////////////////////////////////////////////////////////
39 namespace zypp
40 { ///////////////////////////////////////////////////////////////////////
41   ///////////////////////////////////////////////////////////////////////
42   namespace solver
43   { /////////////////////////////////////////////////////////////////////
44     /////////////////////////////////////////////////////////////////////
45     namespace detail
46     { ///////////////////////////////////////////////////////////////////
47
48 using namespace std;
49 using namespace zypp;
50
51 //-----------------------------------------------------------------------------
52
53 InstallOrder::InstallOrder(const ResPool *pool, const PoolItemList & toinstall, const PoolItemList & installed)
54     : _pool (pool)
55     , _dirty (true)
56     , _numrun (0)
57 {
58     for (PoolItemList::const_iterator iter = toinstall.begin(); iter != toinstall.end(); ++iter)
59         _toinstall.insert (*iter);
60     for (PoolItemList::const_iterator iter = installed.begin(); iter != installed.end(); ++iter)
61         _installed.insert (*iter);
62 }
63
64
65 //-----------------------------------------------------------------------------
66
67 const void
68 InstallOrder::printAdj (std::ostream& os, bool reversed) const
69 {
70     const Graph& g = (reversed ? _rgraph : _graph);
71     os << "digraph pkgdeps {" << endl;
72     for (Graph::const_iterator gcit = g.begin(); gcit != g.end(); ++gcit)
73     {
74         Nodes::const_iterator niit = _nodes.find(gcit->first);
75         int order = niit->second.order;
76         string name = gcit->first.resolvable()->name();
77         os << "\"" << name << "\"" << "[label=\"" << name << "\\n" << order << "\"";
78         os << "] " << endl;
79         for (PoolItemSet::const_iterator scit = gcit->second.begin(); scit != gcit->second.end(); ++scit)
80         {
81             os << "\"" << name << "\" -> \"" << (*scit).resolvable()->name() << "\"" << endl;
82         }
83     }
84     os << "}" << endl;
85 }
86
87
88 //-----------------------------------------------------------------------------
89
90 // yea, that stuff is suboptimal. there should be a heap sorted by order
91 PoolItemList
92 InstallOrder::computeNextSet()
93 {
94     PoolItemList newlist;
95
96     if (_dirty) startrdfs();
97
98     for (Nodes::iterator it = _nodes.begin(); it != _nodes.end(); ++it)
99     {
100         if (it->second.order == 0)
101         {
102             _DBG("RC_SPEW") << "InstallOrder::computeNextSet found " << it->second.item << endl;
103
104             newlist.push_back(it->second.item);
105         }
106     }
107
108     return newlist;
109 }
110
111
112 // decrease order of every adjacent node
113 void
114 InstallOrder::setInstalled(const PoolItem  item )
115 {
116     _dirty = true;
117
118     PoolItemSet & adj = _rgraph[item];
119
120     _DBG("RC_SPEW") << "InstallOrder::setInstalled " << item << endl;
121
122     // order will be < 0
123     _nodes[item].order--;
124     _installed.insert (item);
125     _toinstall.erase (item);
126
127     for (PoolItemSet::iterator it = adj.begin(); it != adj.end(); ++it)
128     {
129         NodeInfo& info = _nodes[*it];
130         info.order--;
131         if (info.order < 0)
132         {
133             DBG << "order of node " << (*it) << " is < 0" << endl;
134         }
135     }
136 }
137
138
139 void
140 InstallOrder::setInstalled( const PoolItemList & rl )
141 {
142     for (PoolItemList::const_iterator it = rl.begin(); it != rl.end(); ++it)
143     {
144         setInstalled(*it);
145     }
146 }
147
148 //-----------------------------------------------------------------------------
149
150
151 void
152 InstallOrder::startrdfs()
153 {
154     _nodes.clear();
155     _rgraph.clear();
156     _graph.clear();
157
158     _rdfstime = 1;
159
160     _topsorted.clear();
161
162     _numrun++;
163     _DBG("RC_SPEW") << "run #" << _numrun << endl;
164
165     // initialize all nodes
166     for (PoolItemSet::iterator it = _toinstall.begin(); it != _toinstall.end(); ++it)
167     {
168         const PoolItem item = *it;
169         _nodes[item] = NodeInfo (item);
170         _rgraph[item] = PoolItemSet();
171         _graph[item] = PoolItemSet();
172     }
173
174     // visit all nodes
175     for (PoolItemSet::iterator it = _toinstall.begin(); it != _toinstall.end(); ++it)
176     {
177         const PoolItem item = *it;
178         if (_nodes[item].visited == false)
179         {
180             _DBG("RC_SPEW") << "start recursion on " << item << endl;
181             rdfsvisit (item);
182         }
183     }
184
185     _dirty = false;
186 }
187
188
189
190
191 struct CollectProviders : public resfilter::OnCapMatchCallbackFunctor
192 {
193     const PoolItem requestor;
194     PoolItemSet & tovisit;   
195     PoolItemSet & toinstall;
196     PoolItemSet & installed;
197
198     CollectProviders info (const PoolItem pi, PoolItemSet & tv, PoolItemSet & ti, PoolItemSet i)
199         : requestor (pi)
200         , tovisit (tv)
201         , toinstall (ti)
202         , installed (i)
203     { }
204
205
206     bool operator()( PoolItem provider, const Capability & match ) const
207     {
208       // Untill we can pass the functor by reference to algorithms.
209       return const_cast<RequireProcess&>(*this).fake( provider, match );
210     }
211     bool fake( PoolItem provider, const Capability & match )
212     {
213         // item provides cap which matches a requirement from info->requestor
214         //   this function gets _all_ providers and filter out those which are
215         //   either installed or in our toinstall input list
216         //
217
218         if ((provider != requestor)                                     // package could provide its own requirement
219 #warning fixme
220 //      && (!(item->status().isInstalled())                             // only visit if provider is not already installed
221             && (toinstall->find(provider) != toinstall->end()           // only look at packages
222                 || installed->find(provider) != installed->end())) {    //   we are currently considering anyways
223             tovisit->insert (provider);
224         }
225
226         return true;
227     }
228
229 };
230
231
232 void
233 InstallOrder::rdfsvisit (const PoolItem  item)
234 {
235     typedef list<Capability> CapList;
236     CapList requires;
237
238     _DBG ("RC_SPEW") << "InstallOrder::rdfsvisit, visiting " << item << endl;
239
240     NodeInfo& nodeinfo = _nodes[item];
241
242     nodeinfo.visited = true;
243     nodeinfo.begintime = _rdfstime;
244     _rdfstime++;
245
246     // put prerequires first and requires last on list to ensure
247     // that prerequires are processed first
248
249     for (CapSet::const_iterator it = item->dep (Dep::PREREQUIRES).begin(); it != item->dep (Dep::PREREQUIRES).end(); ++it)
250     {
251         const Capability cap = *it;
252         requires.push_back(cap);
253     }
254
255     for (CapSet::const_iterator it = item->dep (Dep::REQUIRES).begin(); it != item->dep (Dep::REQUIRES).end(); ++it)
256     {
257         const Capability cap = *it;
258         requires.push_back(cap);
259     }
260
261     for (CapList::const_iterator iter = requires.begin(); iter != requires.end(); ++iter)
262     {
263         const Capability requirement = *iter;
264         _DBG("RC_SPEW") << "check requirement " << requirement << " of " << item << endl;
265         PoolItemSet tovisit;
266
267         CollectProviders info ( item, tovisit, _toinstall, _installed );
268
269 #warning fixme
270 //      _world->foreachProvidingResItem (requirement, collect_providers, &info);
271
272         Dep dep (Dep::PROVIDES);
273         invokeOnEach( _pool->byCapabilityIndexBegin( requirement.index(), dep ),
274                       _pool->byCapabilityIndexEnd( requirement.index(), dep ),
275                       resfilter::callOnCapMatchIn( dep, requirement, info ) );
276
277         for (PoolItemSet::iterator it = tovisit.begin(); it != tovisit.end(); ++it)
278         {
279             const PoolItem must_visit = *it;
280             if (_nodes[must_visit].visited == false)
281             {
282                 nodeinfo.order++;
283                 _rgraph[must_visit].insert (item);
284                 _graph[item].insert (must_visit);
285                 rdfsvisit(must_visit);
286             }
287             else if (_nodes[must_visit].endtime == 0)
288             {
289                 if (must_visit != item)
290                 {
291                     DBG << "dependency loop: " << item << " -> " << must_visit << endl;
292                 }
293             }
294             else
295             {
296                 // filter multiple depends on same item (cosmetic)
297                 PoolItemSet & lrg = _rgraph[must_visit];
298                 if (lrg.find(item) == lrg.end())
299                 {
300                     nodeinfo.order++;
301                     lrg.insert(item);
302
303                     PoolItemSet & lg = _graph[item];
304                     if (lg.find (must_visit) == lg.end())
305                         lg.insert (must_visit);
306                 }
307             }
308         }
309     }
310     _topsorted.push_back(item);
311     _nodes[item].endtime = _rdfstime;
312     _rdfstime++;
313
314     _DBG("RC_SPEW") << item << " done" << endl;
315 }
316
317
318 //-----------------------------------------------------------------------------
319
320 const PoolItemList
321 InstallOrder::getTopSorted() const
322 {
323     return _topsorted;
324 }
325
326
327 ///////////////////////////////////////////////////////////////////
328     };// namespace detail
329     /////////////////////////////////////////////////////////////////////
330     /////////////////////////////////////////////////////////////////////
331   };// namespace solver
332   ///////////////////////////////////////////////////////////////////////
333   ///////////////////////////////////////////////////////////////////////
334 };// namespace zypp
335 /////////////////////////////////////////////////////////////////////////