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