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