- support SOLVER_NOOBSOLETES* jobs, tell the solver that those packages
[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   Repo *installed;
65   
66   /* list of rules, ordered
67    * rpm rules first, then features, updates, jobs, learnt
68    * see start/end offsets below
69    */
70   Rule *rules;                          /* all rules */
71   Id nrules;                            /* [Offset] index of the last rule */
72
73   Queue ruleassertions;                 /* Queue of all assertion rules */
74
75   /* start/end offset for rule 'areas' */
76     
77   Id rpmrules_end;                      /* [Offset] rpm rules end */
78     
79   Id featurerules;                      /* feature rules start/end */
80   Id featurerules_end;
81     
82   Id updaterules;                       /* policy rules, e.g. keep packages installed or update. All literals > 0 */
83   Id updaterules_end;
84     
85   Id jobrules;                          /* user rules */
86   Id jobrules_end;
87     
88   Id learntrules;                       /* learnt rules, (end == nrules) */
89
90   Map noupdate;                         /* don't try to update these
91                                            installed solvables */
92   Map noobsoletes;                      /* ignore obsoletes for these
93                                            (multiinstall) */
94
95   Queue weakruleq;                      /* index into 'rules' for weak ones */
96   Map weakrulemap;                      /* map rule# to '1' for weak rules, 1..learntrules */
97
98   Id *watches;                          /* Array of rule offsets
99                                          * watches has nsolvables*2 entries and is addressed from the middle
100                                          * middle-solvable : decision to conflict, offset point to linked-list of rules
101                                          * middle+solvable : decision to install: offset point to linked-list of rules
102                                          */
103
104   Queue ruletojob;                      /* index into job queue: jobs for which a rule exits */
105
106   /* our decisions: */
107   Queue decisionq;                      /* >0:install, <0:remove/conflict */
108   Queue decisionq_why;                  /* index of rule, Offset into rules */
109   int directdecisions;                  /* number of decisions with no rule */
110
111   Id *decisionmap;                      /* map for all available solvables,
112                                            > 0: level of decision when installed,
113                                            < 0: level of decision when conflict */
114
115   /* learnt rule history */
116   Queue learnt_why;
117   Queue learnt_pool;
118
119   Queue branches;
120   int (*solution_callback)(struct solver *solv, void *data);
121   void *solution_callback_data;
122
123   int propagate_index;                  /* index into decisionq for non-propagated decisions */
124
125   Queue problems;                       /* index of conflicting rules, < 0 for job rules */
126   Queue recommendations;                /* recommended packages */
127   Queue suggestions;                    /* suggested packages */
128
129   int stats_learned;                    /* statistic */
130   int stats_unsolvable;                 /* statistic */
131
132   Map recommendsmap;                    /* recommended packages from decisionmap */
133   Map suggestsmap;                      /* suggested packages from decisionmap */
134   int recommends_index;                 /* recommendsmap/suggestsmap is created up to this level */
135
136   Id *obsoletes;                        /* obsoletes for each installed solvable */
137   Id *obsoletes_data;                   /* data area for obsoletes */
138
139   /*-------------------------------------------------------------------------------------------------------------
140    * Solver configuration
141    *-------------------------------------------------------------------------------------------------------------*/
142
143   int fixsystem;                        /* repair errors in rpm dependency graph */
144   int allowdowngrade;                   /* allow to downgrade installed solvable */
145   int allowarchchange;                  /* allow to change architecture of installed solvables */
146   int allowvendorchange;                /* allow to change vendor of installed solvables */
147   int allowuninstall;                   /* allow removal of installed solvables */
148   int updatesystem;                     /* distupgrade */
149   int allowvirtualconflicts;            /* false: conflicts on package name, true: conflicts on package provides */
150   int allowselfconflicts;               /* true: packages wich conflict with itself are installable */
151   int obsoleteusesprovides;             /* true: obsoletes are matched against provides, not names */
152   int implicitobsoleteusesprovides;     /* true: implicit obsoletes due to same name are matched against provides, not names */
153   int noupdateprovide;                  /* true: update packages needs not to provide old package */
154   int dosplitprovides;                  /* true: consider legacy split provides */
155   int dontinstallrecommended;           /* true: do not install recommended packages */
156   int dontshowinstalledrecommended;     /* true: do not show recommended packages that are already installed */
157   
158   /* Callbacks for defining the bahaviour of the SAT solver */
159
160   /* Finding best candidate
161    *
162    * Callback definition:
163    * void  bestSolvable (Pool *pool, Queue *canditates)
164    *     candidates       : List of canditates which has to be sorted by the function call
165    *     return candidates: Sorted list of the candidates(first is the best).
166    */
167    BestSolvableCb bestSolvableCb;
168
169   /* Checking if two solvables has compatible architectures
170    *
171    * Callback definition:
172    *     int  archCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
173    *     
174    *     return 0 it the two solvables has compatible architectures
175    */
176    ArchCheckCb archCheckCb;
177
178   /* Checking if two solvables has compatible vendors
179    *
180    * Callback definition:
181    *     int  vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
182    *     
183    *     return 0 it the two solvables has compatible architectures
184    */
185    VendorCheckCb vendorCheckCb;
186     
187   /* Evaluate update candidate
188    *
189    * Callback definition:
190    * void pdateCandidateCb (Pool *pool, Solvable *solvable, Queue *canditates)
191    *     solvable   : for which updates should be search
192    *     candidates : List of candidates (This list depends on other
193    *                  restrictions like architecture and vendor policies too)
194    */
195    UpdateCandidateCb   updateCandidateCb;
196     
197
198   /* some strange queue that doesn't belong here */
199
200   Queue covenantq;                      /* Covenants honored by this solver (generic locks) */
201
202   
203 } Solver;
204
205 /*
206  * queue commands
207  */
208
209 typedef enum {
210   SOLVCMD_NULL=0,
211   SOLVER_INSTALL_SOLVABLE,
212   SOLVER_ERASE_SOLVABLE,
213   SOLVER_INSTALL_SOLVABLE_NAME,
214   SOLVER_ERASE_SOLVABLE_NAME,
215   SOLVER_INSTALL_SOLVABLE_PROVIDES,
216   SOLVER_ERASE_SOLVABLE_PROVIDES,
217   SOLVER_INSTALL_SOLVABLE_UPDATE,
218   SOLVER_INSTALL_SOLVABLE_ONE_OF,
219   SOLVER_WEAKEN_SOLVABLE_DEPS,
220   SOLVER_NOOBSOLETES_SOLVABLE,
221   SOLVER_NOOBSOLETES_SOLVABLE_NAME,
222   SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES,
223
224   /* flags */
225   SOLVER_WEAK = 0x100,
226 } SolverCmd;
227
228 typedef enum {
229   SOLVER_PROBLEM_UPDATE_RULE,
230   SOLVER_PROBLEM_JOB_RULE,
231   SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP,
232   SOLVER_PROBLEM_NOT_INSTALLABLE,
233   SOLVER_PROBLEM_NOTHING_PROVIDES_DEP,
234   SOLVER_PROBLEM_SAME_NAME,
235   SOLVER_PROBLEM_PACKAGE_CONFLICT,
236   SOLVER_PROBLEM_PACKAGE_OBSOLETES,
237   SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE,
238   SOLVER_PROBLEM_SELF_CONFLICT
239 } SolverProbleminfo;
240
241
242 extern Solver *solver_create(Pool *pool, Repo *installed);
243 extern void solver_free(Solver *solv);
244 extern void solver_solve(Solver *solv, Queue *job);
245 extern int solver_dep_installed(Solver *solv, Id dep);
246 extern int solver_splitprovides(Solver *solv, Id dep);
247
248 extern Id solver_next_problem(Solver *solv, Id problem);
249 extern Id solver_next_solution(Solver *solv, Id problem, Id solution);
250 extern Id solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp);
251 extern Id solver_findproblemrule(Solver *solv, Id problem);
252 extern SolverProbleminfo solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp);
253
254 /* XXX: why is this not static? */
255 Id *create_decisions_obsoletesmap(Solver *solv);
256
257 static inline int
258 solver_dep_fulfilled(Solver *solv, Id dep)
259 {
260   Pool *pool = solv->pool;
261   Id p, *pp;
262
263   if (ISRELDEP(dep))
264     {
265       Reldep *rd = GETRELDEP(pool, dep);
266       if (rd->flags == REL_AND)
267         {
268           if (!solver_dep_fulfilled(solv, rd->name))
269             return 0;
270           return solver_dep_fulfilled(solv, rd->evr);
271         }
272       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
273         return solver_splitprovides(solv, rd->evr);
274       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
275         return solver_dep_installed(solv, rd->evr);
276     }
277   FOR_PROVIDES(p, pp, dep)
278     {
279       if (solv->decisionmap[p] > 0)
280         return 1;
281     }
282   return 0;
283 }
284
285 static inline int
286 solver_is_supplementing(Solver *solv, Solvable *s)
287 {
288   Id sup, *supp;
289   if (!s->supplements && !s->freshens)
290     return 0;
291   if (s->supplements)
292     {
293       supp = s->repo->idarraydata + s->supplements;
294       while ((sup = *supp++) != 0)
295         if (solver_dep_fulfilled(solv, sup))
296           break;
297       if (!sup)
298         return 0;
299     }
300   if (s->freshens)
301     {
302       supp = s->repo->idarraydata + s->freshens;
303       while ((sup = *supp++) != 0)
304         if (solver_dep_fulfilled(solv, sup))
305           break;
306       if (!sup)
307         return 0;
308     }
309   return 1;
310 }
311
312 static inline int
313 solver_is_enhancing(Solver *solv, Solvable *s)
314 {
315   Id enh, *enhp;
316   if (!s->enhances)
317     return 0;
318   enhp = s->repo->idarraydata + s->enhances;
319   while ((enh = *enhp++) != 0)
320     if (solver_dep_fulfilled(solv, enh))
321       return 1;
322   return 0;
323 }
324
325 void solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps);
326 int solver_calc_installsizechange(Solver *solv);
327
328 static inline void
329 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
330 {
331   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
332 }
333
334 #define FOR_RULELITERALS(l, dp, r)                              \
335     for (l = r->d < 0 ? -r->d - 1 : r->d,                       \
336          dp = !l ? &r->w2 : pool->whatprovidesdata + l,         \
337          l = r->p; l; l = (dp != &r->w2 + 1 ? *dp++ : 0))
338
339 #ifdef __cplusplus
340 }
341 #endif
342
343 #endif /* SATSOLVER_SOLVER_H */