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