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