fa1e57c6ec2e5f6f8d75c80688b4ce7161d48562
[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 /* ----------------------------------------------
23  * Rule
24  *
25  *   providerN(B) == Package Id of package providing tag B
26  *   N = 1, 2, 3, in case of multiple providers
27  *
28  * A requires B : !A | provider1(B) | provider2(B)
29  *
30  * A conflicts B : (!A | !provider1(B)) & (!A | !provider2(B)) ...
31  *
32  * 'not' is encoded as a negative Id
33  * 
34  * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
35  */
36
37 typedef struct rule {
38   Id p;                 /* first literal in rule */
39   Id d;                 /* Id offset into 'list of providers terminated by 0' as used by whatprovides; pool->whatprovides + d */
40                         /* in case of binary rules, d == 0, w1 == p, w2 == other literal */
41   Id w1, w2;            /* watches, literals not-yet-decided */
42                                        /* if !w1, disabled */
43                                        /* if !w2, assertion, not rule */
44   Id n1, n2;            /* next rules in linked list, corresponding to w1,w2 */
45 } Rule;
46
47 struct solver;
48
49 typedef struct solver {
50   Pool *pool;
51   Repo *installed;
52
53   int fixsystem;                        /* repair errors in rpm dependency graph */
54   int allowdowngrade;                   /* allow to downgrade installed solvable */
55   int allowarchchange;                  /* allow to change architecture of installed solvables */
56   int allowvendorchange;                /* allow to change vendor of installed solvables */
57   int allowuninstall;                   /* allow removal of installed solvables */
58   int updatesystem;                     /* distupgrade */
59   int allowvirtualconflicts;            /* false: conflicts on package name, true: conflicts on package provides */
60   int noupdateprovide;                  /* true: update packages needs not to provide old package */
61   int dosplitprovides;                  /* true: consider legacy split provides */
62   
63   Rule *rules;                          /* all rules */
64   Id nrules;                            /* index of the last rule */
65
66   Id jobrules;                          /* user rules */
67   Id systemrules;                       /* policy rules, e.g. keep packages installed or update. All literals > 0 */
68   Id weakrules;                         /* rules that can be autodisabled */
69   Id learntrules;                       /* learnt rules */
70
71   Id *weaksystemrules;                  /* please try to install (r->d) */
72   Map noupdate;                         /* don't try to update these
73                                            installed solvables */
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 suggestions;                    /* suggested packages */
102
103   Map recommendsmap;                    /* recommended packages from decisionmap */
104   Map suggestsmap;                      /* suggested packages from decisionmap */
105   int recommends_index;                 /* recommended level */
106
107   Id *obsoletes;                        /* obsoletes for each installed solvable */
108   Id *obsoletes_data;                   /* data area for obsoletes */
109
110   Queue covenantq;                      /* Covenants honored by this solver (generic locks) */
111   
112 } Solver;
113
114 /*
115  * queue commands
116  */
117
118 typedef enum {
119   SOLVCMD_NULL=0,
120   SOLVER_INSTALL_SOLVABLE,
121   SOLVER_ERASE_SOLVABLE,
122   SOLVER_INSTALL_SOLVABLE_NAME,
123   SOLVER_ERASE_SOLVABLE_NAME,
124   SOLVER_INSTALL_SOLVABLE_PROVIDES,
125   SOLVER_ERASE_SOLVABLE_PROVIDES,
126   SOLVER_INSTALL_SOLVABLE_UPDATE
127 } SolverCmd;
128
129 typedef enum {
130   SOLVER_PROBLEM_UPDATE_RULE,
131   SOLVER_PROBLEM_JOB_RULE,
132   SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP,
133   SOLVER_PROBLEM_NOT_INSTALLABLE,
134   SOLVER_PROBLEM_NOTHING_PROVIDES_DEP,
135   SOLVER_PROBLEM_SAME_NAME,
136   SOLVER_PROBLEM_PACKAGE_CONFLICT,
137   SOLVER_PROBLEM_PACKAGE_OBSOLETES,
138   SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE
139 } SolverProbleminfo;
140
141
142 extern Solver *solver_create(Pool *pool, Repo *installed);
143 extern void solver_free(Solver *solv);
144 extern void solver_solve(Solver *solv, Queue *job);
145 extern int solver_dep_installed(Solver *solv, Id dep);
146 extern int solver_splitprovides(Solver *solv, Id dep);
147
148 extern Id solver_next_problem(Solver *solv, Id problem);
149 extern Id solver_next_solution(Solver *solv, Id problem, Id solution);
150 extern Id solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp);
151 extern Id solver_findproblemrule(Solver *solv, Id problem);
152 extern SolverProbleminfo solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp);
153
154 Id *create_obsoletesmap(Solver *solv);
155
156 /* debug functions, do not use */
157 void printdecisions(Solver *solv);
158 void printsolutions(Solver *solv, Queue *job);
159
160
161 static inline int
162 solver_dep_fulfilled(Solver *solv, Id dep)
163 {
164   Pool *pool = solv->pool;
165   Id p, *pp;
166
167   if (ISRELDEP(dep))
168     {
169       Reldep *rd = GETRELDEP(pool, dep);
170       if (rd->flags == REL_AND)
171         {
172           if (!solver_dep_fulfilled(solv, rd->name))
173             return 0;
174           return solver_dep_fulfilled(solv, rd->evr);
175         }
176       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
177         return solver_splitprovides(solv, rd->evr);
178       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
179         return solver_dep_installed(solv, rd->evr);
180     }
181   FOR_PROVIDES(p, pp, dep)
182     {
183       if (solv->decisionmap[p] > 0)
184         return 1;
185     }
186   return 0;
187 }
188
189 static inline int
190 solver_is_supplementing(Solver *solv, Solvable *s)
191 {
192   Id sup, *supp;
193   if (!s->supplements && !s->freshens)
194     return 0;
195   if (s->supplements)
196     {
197       supp = s->repo->idarraydata + s->supplements;
198       while ((sup = *supp++) != 0)
199         if (solver_dep_fulfilled(solv, sup))
200           break;
201       if (!sup)
202         return 0;
203     }
204   if (s->freshens)
205     {
206       supp = s->repo->idarraydata + s->freshens;
207       while ((sup = *supp++) != 0)
208         if (solver_dep_fulfilled(solv, sup))
209           break;
210       if (!sup)
211         return 0;
212     }
213   return 1;
214 }
215
216 static inline int
217 solver_is_enhancing(Solver *solv, Solvable *s)
218 {
219   Id enh, *enhp;
220   if (!s->enhances)
221     return 0;
222   enhp = s->repo->idarraydata + s->enhances;
223   while ((enh = *enhp++) != 0)
224     if (solver_dep_fulfilled(solv, enh))
225       return 1;
226   return 0;
227 }
228
229 #endif /* SATSOLVER_SOLVER_H */