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