- lazy solution creation, may speed up UI a lot
[platform/upstream/libsolv.git] / src / solver.h
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solver.h
10  *
11  */
12
13 #ifndef SATSOLVER_SOLVER_H
14 #define SATSOLVER_SOLVER_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include "pooltypes.h"
21 #include "pool.h"
22 #include "repo.h"
23 #include "queue.h"
24 #include "bitmap.h"
25 /*
26  * Callback definitions in order to "overwrite" the policies by an external application.
27  */
28  
29 typedef void  (*BestSolvableCb) (Pool *pool, Queue *canditates);
30 typedef int  (*ArchCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
31 typedef int  (*VendorCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
32 typedef void (*UpdateCandidateCb) (Pool *pool, Solvable *solvable, Queue *canditates);
33
34
35 /* ----------------------------------------------
36  * Rule
37  *
38  *   providerN(B) == Package Id of package providing tag B
39  *   N = 1, 2, 3, in case of multiple providers
40  *
41  * A requires B : !A | provider1(B) | provider2(B)
42  *
43  * A conflicts B : (!A | !provider1(B)) & (!A | !provider2(B)) ...
44  *
45  * 'not' is encoded as a negative Id
46  * 
47  * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
48  */
49
50 typedef struct rule {
51   Id p;                 /* first literal in rule */
52   Id d;                 /* Id offset into 'list of providers terminated by 0' as used by whatprovides; pool->whatprovides + d */
53                         /* in case of binary rules, d == 0, w1 == p, w2 == other literal */
54                         /* in case of disabled rules: ~d, aka -d - 1 */
55   Id w1, w2;            /* watches, literals not-yet-decided */
56                                        /* if !w2, assertion, not rule */
57   Id n1, n2;            /* next rules in linked list, corresponding to w1,w2 */
58 } Rule;
59
60 struct solver;
61
62 typedef struct solver {
63   Pool *pool;
64   Queue job;                            /* copy of the job we're solving */
65
66   Repo *installed;                      /* copy of pool->installed */
67   
68   /* list of rules, ordered
69    * rpm rules first, then features, updates, jobs, learnt
70    * see start/end offsets below
71    */
72   Rule *rules;                          /* all rules */
73   Id nrules;                            /* [Offset] index of the last rule */
74
75   Queue ruleassertions;                 /* Queue of all assertion rules */
76
77   /* start/end offset for rule 'areas' */
78     
79   Id rpmrules_end;                      /* [Offset] rpm rules end */
80     
81   Id featurerules;                      /* feature rules start/end */
82   Id featurerules_end;
83     
84   Id updaterules;                       /* policy rules, e.g. keep packages installed or update. All literals > 0 */
85   Id updaterules_end;
86     
87   Id jobrules;                          /* user rules */
88   Id jobrules_end;
89
90   Id infarchrules;                      /* inferior arch rules */
91   Id infarchrules_end;
92
93   Id duprules;                          /* dist upgrade rules */
94   Id duprules_end;
95     
96   Id learntrules;                       /* learnt rules, (end == nrules) */
97
98   Map noupdate;                         /* don't try to update these
99                                            installed solvables */
100   Map noobsoletes;                      /* ignore obsoletes for these
101                                            (multiinstall) */
102
103   Map updatemap;                        /* bring those packages to the newest version */
104   Queue weakruleq;                      /* index into 'rules' for weak ones */
105   Map weakrulemap;                      /* map rule# to '1' for weak rules, 1..learntrules */
106
107   Id *watches;                          /* Array of rule offsets
108                                          * watches has nsolvables*2 entries and is addressed from the middle
109                                          * middle-solvable : decision to conflict, offset point to linked-list of rules
110                                          * middle+solvable : decision to install: offset point to linked-list of rules
111                                          */
112
113   Queue ruletojob;                      /* index into job queue: jobs for which a rule exits */
114
115   /* our decisions: */
116   Queue decisionq;                      /* >0:install, <0:remove/conflict */
117   Queue decisionq_why;                  /* index of rule, Offset into rules */
118   int directdecisions;                  /* number of decisions with no rule */
119
120   Id *decisionmap;                      /* map for all available solvables,
121                                          * = 0: undecided
122                                          * > 0: level of decision when installed,
123                                          * < 0: level of decision when conflict */
124
125   /* learnt rule history */
126   Queue learnt_why;
127   Queue learnt_pool;
128
129   Queue branches;
130   int (*solution_callback)(struct solver *solv, void *data);
131   void *solution_callback_data;
132
133   int propagate_index;                  /* index into decisionq for non-propagated decisions */
134
135   Queue problems;                       /* list of lists of conflicting rules, < 0 for job rules */
136   Queue solutions;                      /* refined problem storage space */
137
138   Queue recommendations;                /* recommended packages */
139   Queue suggestions;                    /* suggested packages */
140   Queue orphaned;                       /* orphaned packages */
141
142   int stats_learned;                    /* statistic */
143   int stats_unsolvable;                 /* statistic */
144
145   Map recommendsmap;                    /* recommended packages from decisionmap */
146   Map suggestsmap;                      /* suggested packages from decisionmap */
147   int recommends_index;                 /* recommendsmap/suggestsmap is created up to this level */
148
149   Id *obsoletes;                        /* obsoletes for each installed solvable */
150   Id *obsoletes_data;                   /* data area for obsoletes */
151   Id *multiversionupdaters;             /* updaters for multiversion packages in updatesystem mode */
152
153   /*-------------------------------------------------------------------------------------------------------------
154    * Solver configuration
155    *-------------------------------------------------------------------------------------------------------------*/
156
157   int fixsystem;                        /* repair errors in rpm dependency graph */
158   int allowdowngrade;                   /* allow to downgrade installed solvable */
159   int allowarchchange;                  /* allow to change architecture of installed solvables */
160   int allowvendorchange;                /* allow to change vendor of installed solvables */
161   int allowuninstall;                   /* allow removal of installed solvables */
162   int updatesystem;                     /* update all packages to the newest version */
163   int allowvirtualconflicts;            /* false: conflicts on package name, true: conflicts on package provides */
164   int allowselfconflicts;               /* true: packages wich conflict with itself are installable */
165   int obsoleteusesprovides;             /* true: obsoletes are matched against provides, not names */
166   int implicitobsoleteusesprovides;     /* true: implicit obsoletes due to same name are matched against provides, not names */
167   int noupdateprovide;                  /* true: update packages needs not to provide old package */
168   int dosplitprovides;                  /* true: consider legacy split provides */
169   int dontinstallrecommended;           /* true: do not install recommended packages */
170   int ignorealreadyrecommended;         /* true: ignore recommended packages that were already recommended by the installed packages */
171   int dontshowinstalledrecommended;     /* true: do not show recommended packages that are already installed */
172   
173   /* distupgrade also needs updatesystem and dosplitprovides */
174   int distupgrade;
175   int distupgrade_removeunsupported;
176
177   int noinfarchcheck;                   /* true: do not forbid inferior architectures */
178
179   /* Callbacks for defining the bahaviour of the SAT solver */
180
181   /* Finding best candidate
182    *
183    * Callback definition:
184    * void  bestSolvable (Pool *pool, Queue *canditates)
185    *     candidates       : List of canditates which has to be sorted by the function call
186    *     return candidates: Sorted list of the candidates(first is the best).
187    */
188    BestSolvableCb bestSolvableCb;
189
190   /* Checking if two solvables has compatible architectures
191    *
192    * Callback definition:
193    *     int  archCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
194    *     
195    *     return 0 it the two solvables has compatible architectures
196    */
197    ArchCheckCb archCheckCb;
198
199   /* Checking if two solvables has compatible vendors
200    *
201    * Callback definition:
202    *     int  vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
203    *     
204    *     return 0 it the two solvables has compatible architectures
205    */
206    VendorCheckCb vendorCheckCb;
207     
208   /* Evaluate update candidate
209    *
210    * Callback definition:
211    * void pdateCandidateCb (Pool *pool, Solvable *solvable, Queue *canditates)
212    *     solvable   : for which updates should be search
213    *     candidates : List of candidates (This list depends on other
214    *                  restrictions like architecture and vendor policies too)
215    */
216    UpdateCandidateCb   updateCandidateCb;
217     
218
219   /* some strange queue that doesn't belong here */
220   Queue covenantq;                      /* Covenants honored by this solver (generic locks) */
221
222   
223   Map dupmap;                           /* packages from dup repos */
224   Map dupinvolvedmap;                   /* packages involved in dup process */
225
226   Queue *ruleinfoq;                     /* tmp space for solver_ruleinfo() */
227 } Solver;
228
229 /*
230  * queue commands
231  */
232
233 #define SOLVER_SOLVABLE                 0x01
234 #define SOLVER_SOLVABLE_NAME            0x02
235 #define SOLVER_SOLVABLE_PROVIDES        0x03
236 #define SOLVER_SOLVABLE_ONE_OF          0x04
237
238 #define SOLVER_SELECTMASK               0xff
239
240 #define SOLVER_NOOP                     0x0000
241 #define SOLVER_INSTALL                  0x0100
242 #define SOLVER_ERASE                    0x0200
243 #define SOLVER_UPDATE                   0x0300
244 #define SOLVER_WEAKENDEPS               0x0400
245 #define SOLVER_NOOBSOLETES              0x0500
246 #define SOLVER_LOCK                     0x0600
247 #define SOLVER_DISTUPGRADE              0x0700
248
249 #define SOLVER_JOBMASK                  0xff00
250
251 #define SOLVER_WEAK                     0x010000
252 #define SOLVER_ESSENTIAL                0x020000
253
254 /* old API compatibility, do not use in new code */
255 #if 1
256 #define SOLVER_INSTALL_SOLVABLE (SOLVER_INSTALL|SOLVER_SOLVABLE)
257 #define SOLVER_ERASE_SOLVABLE (SOLVER_ERASE|SOLVER_SOLVABLE)
258 #define SOLVER_INSTALL_SOLVABLE_NAME (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME)
259 #define SOLVER_ERASE_SOLVABLE_NAME (SOLVER_ERASE|SOLVER_SOLVABLE_NAME)
260 #define SOLVER_INSTALL_SOLVABLE_PROVIDES (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES)
261 #define SOLVER_ERASE_SOLVABLE_PROVIDES (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES)
262 #define SOLVER_INSTALL_SOLVABLE_UPDATE (SOLVER_UPDATE|SOLVER_SOLVABLE)
263 #define SOLVER_INSTALL_SOLVABLE_ONE_OF (SOLVER_INSTALL|SOLVER_SOLVABLE_ONE_OF)
264 #define SOLVER_WEAKEN_SOLVABLE_DEPS (SOLVER_WEAKENDEPS|SOLVER_SOLVABLE)
265 #define SOLVER_NOOBSOLETES_SOLVABLE (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE)
266 #define SOLVER_NOOBSOLETES_SOLVABLE_NAME (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME)
267 #define SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_PROVIDES)
268 #endif
269
270 typedef enum {
271   SOLVER_PROBLEM_UPDATE_RULE,
272   SOLVER_PROBLEM_JOB_RULE,
273   SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP,
274   SOLVER_PROBLEM_NOT_INSTALLABLE,
275   SOLVER_PROBLEM_NOTHING_PROVIDES_DEP,
276   SOLVER_PROBLEM_SAME_NAME,
277   SOLVER_PROBLEM_PACKAGE_CONFLICT,
278   SOLVER_PROBLEM_PACKAGE_OBSOLETES,
279   SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE,
280   SOLVER_PROBLEM_SELF_CONFLICT,
281   SOLVER_PROBLEM_RPM_RULE,
282   SOLVER_PROBLEM_DISTUPGRADE_RULE,
283   SOLVER_PROBLEM_INFARCH_RULE
284 } SolverProbleminfo;
285
286 typedef enum {
287   SOLVER_RULE_UNKNOWN = 0,
288   SOLVER_RULE_RPM = 0x100,
289   SOLVER_RULE_RPM_NOT_INSTALLABLE,
290   SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP,
291   SOLVER_RULE_RPM_PACKAGE_REQUIRES,
292   SOLVER_RULE_RPM_SELF_CONFLICT,
293   SOLVER_RULE_RPM_PACKAGE_CONFLICT,
294   SOLVER_RULE_RPM_SAME_NAME,
295   SOLVER_RULE_RPM_PACKAGE_OBSOLETES,
296   SOLVER_RULE_RPM_IMPLICIT_OBSOLETES,
297   SOLVER_RULE_UPDATE = 0x200,
298   SOLVER_RULE_FEATURE = 0x300,
299   SOLVER_RULE_JOB = 0x400,
300   SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP,
301   SOLVER_RULE_DISTUPGRADE = 0x500,
302   SOLVER_RULE_INFARCH = 0x600,
303   SOLVER_RULE_LEARNT = 0x700
304 } SolverRuleinfo;
305
306 #define SOLVER_RULE_TYPEMASK    0xff00
307
308 /* backward compatibility */
309 #define SOLVER_PROBLEM_UPDATE_RULE              SOLVER_RULE_UPDATE
310 #define SOLVER_PROBLEM_JOB_RULE                 SOLVER_RULE_JOB
311 #define SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP
312 #define SOLVER_PROBLEM_NOT_INSTALLABLE          SOLVER_RULE_RPM_NOT_INSTALLABLE
313 #define SOLVER_PROBLEM_NOTHING_PROVIDES_DEP     SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP
314 #define SOLVER_PROBLEM_SAME_NAME                SOLVER_RULE_RPM_SAME_NAME
315 #define SOLVER_PROBLEM_PACKAGE_CONFLICT         SOLVER_RULE_RPM_PACKAGE_CONFLICT
316 #define SOLVER_PROBLEM_PACKAGE_OBSOLETES        SOLVER_RULE_RPM_PACKAGE_OBSOLETES
317 #define SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE SOLVER_RULE_RPM_PACKAGE_REQUIRES
318 #define SOLVER_PROBLEM_SELF_CONFLICT            SOLVER_RULE_RPM_SELF_CONFLICT
319 #define SOLVER_PROBLEM_RPM_RULE                 SOLVER_RULE_RPM
320 #define SOLVER_PROBLEM_DISTUPGRADE_RULE         SOLVER_RULE_DISTUPGRADE
321 #define SOLVER_PROBLEM_INFARCH_RULE             SOLVER_RULE_INFARCH
322
323
324 #define SOLVER_SOLUTION_JOB             (0)
325 #define SOLVER_SOLUTION_DISTUPGRADE     (-1)
326 #define SOLVER_SOLUTION_INFARCH         (-2)
327
328 extern Solver *solver_create(Pool *pool);
329 extern void solver_free(Solver *solv);
330 extern void solver_solve(Solver *solv, Queue *job);
331 extern int solver_dep_installed(Solver *solv, Id dep);
332 extern int solver_splitprovides(Solver *solv, Id dep);
333
334 extern Id solver_problem_count(Solver *solv);
335 extern Id solver_next_problem(Solver *solv, Id problem);
336 extern Id solver_solution_count(Solver *solv, Id problem);
337 extern Id solver_next_solution(Solver *solv, Id problem, Id solution);
338 extern Id solver_solutionelement_count(Solver *solv, Id problem, Id solution);
339 extern Id solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp);
340 extern Id solver_findproblemrule(Solver *solv, Id problem);
341 extern void solver_findallproblemrules(Solver *solv, Id problem, Queue *rules);
342
343 extern SolverRuleinfo solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp);
344 extern SolverRuleinfo solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp);
345 extern int solver_allruleinfos(Solver *solv, Id rid, Queue *rq);
346
347 /* XXX: why is this not static? */
348 Id *solver_create_decisions_obsoletesmap(Solver *solv);
349
350 static inline int
351 solver_dep_fulfilled(Solver *solv, Id dep)
352 {
353   Pool *pool = solv->pool;
354   Id p, pp;
355
356   if (ISRELDEP(dep))
357     {
358       Reldep *rd = GETRELDEP(pool, dep);
359       if (rd->flags == REL_AND)
360         {
361           if (!solver_dep_fulfilled(solv, rd->name))
362             return 0;
363           return solver_dep_fulfilled(solv, rd->evr);
364         }
365       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
366         return solver_splitprovides(solv, rd->evr);
367       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
368         return solver_dep_installed(solv, rd->evr);
369     }
370   FOR_PROVIDES(p, pp, dep)
371     {
372       if (solv->decisionmap[p] > 0)
373         return 1;
374     }
375   return 0;
376 }
377
378 static inline int
379 solver_is_supplementing(Solver *solv, Solvable *s)
380 {
381   Id sup, *supp;
382   if (!s->supplements)
383     return 0;
384   supp = s->repo->idarraydata + s->supplements;
385   while ((sup = *supp++) != 0)
386     if (solver_dep_fulfilled(solv, sup))
387       return 1;
388   return 0;
389 }
390
391 static inline int
392 solver_is_enhancing(Solver *solv, Solvable *s)
393 {
394   Id enh, *enhp;
395   if (!s->enhances)
396     return 0;
397   enhp = s->repo->idarraydata + s->enhances;
398   while ((enh = *enhp++) != 0)
399     if (solver_dep_fulfilled(solv, enh))
400       return 1;
401   return 0;
402 }
403
404 void solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps);
405 int solver_calc_installsizechange(Solver *solv);
406 void solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res);
407
408 void solver_find_involved(Solver *solv, Queue *installedq, Solvable *s, Queue *q);
409
410 static inline void
411 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
412 {
413   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
414 }
415
416 /* iterate over all literals of a rule */
417 /* WARNING: loop body must not relocate whatprovidesdata, e.g. by
418  * looking up the providers of a dependency */
419 #define FOR_RULELITERALS(l, dp, r)                              \
420     for (l = r->d < 0 ? -r->d - 1 : r->d,                       \
421          dp = !l ? &r->w2 : pool->whatprovidesdata + l,         \
422          l = r->p; l; l = (dp != &r->w2 + 1 ? *dp++ : 0))
423
424 /* iterate over all packages selected by a job */
425 #define FOR_JOB_SELECT(p, pp, select, what) \
426     for (pp = (select == SOLVER_SOLVABLE ? 0 :  \
427                select == SOLVER_SOLVABLE_ONE_OF ? what : \
428                pool_whatprovides(pool, what)),                          \
429          p = (select == SOLVER_SOLVABLE ? what : pool->whatprovidesdata[pp++]) ; p ; p = pool->whatprovidesdata[pp++]) \
430       if (select != SOLVER_SOLVABLE_NAME || pool_match_nevr(pool, pool->solvables + p, what))
431
432 #ifdef __cplusplus
433 }
434 #endif
435
436 #endif /* SATSOLVER_SOLVER_H */