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