5bef9ac19bae14e1601ac786d003d015f97cc336
[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 SOLVER_H
14 #define 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   
62   Rule *rules;                          /* all rules */
63   Id nrules;                            /* rpm rules */
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   Id *decisionmap;                      /* map for all available solvables, > 0: level of decision when installed, < 0 level of decision when conflict */
86
87   /* learnt rule history */
88   Queue learnt_why;
89   Queue learnt_pool;
90
91   Queue branches;
92   int (*solution_callback)(struct solver *solv, void *data);
93   void *solution_callback_data;
94
95   int propagate_index;
96
97   Queue problems;
98   Queue suggestions;                    /* suggested packages */
99
100   Map recommendsmap;                    /* recommended packages from decisionmap */
101   Map suggestsmap;                      /* suggested packages from decisionmap */
102   int recommends_index;                 /* recommended level */
103
104   Id *obsoletes;                        /* obsoletes for each installed solvable */
105   Id *obsoletes_data;                   /* data area for obsoletes */
106
107   int rc_output;                        /* output result compatible to redcarpet/zypp testsuite, set == 2 for pure rc (will suppress architecture) */
108 } Solver;
109
110 /*
111  * queue commands
112  */
113
114 enum solvcmds {
115   SOLVCMD_NULL=0,
116   SOLVER_INSTALL_SOLVABLE,
117   SOLVER_ERASE_SOLVABLE,
118   SOLVER_INSTALL_SOLVABLE_NAME,
119   SOLVER_ERASE_SOLVABLE_NAME,
120   SOLVER_INSTALL_SOLVABLE_PROVIDES,
121   SOLVER_ERASE_SOLVABLE_PROVIDES,
122   SOLVER_INSTALL_SOLVABLE_UPDATE
123 } SolverCmd;
124
125 extern Solver *solver_create(Pool *pool, Repo *installed);
126 extern void solver_free(Solver *solv);
127 extern void solve(Solver *solv, Queue *job);
128 extern int solver_dep_installed(Solver *solv, Id dep);
129
130 void printdecisions(Solver *solv);
131
132 void refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined);
133 int archchanges(Pool *pool, Solvable *s1, Solvable *s2);
134
135 static inline int
136 solver_dep_fulfilled(Solver *solv, Id dep)
137 {
138   Pool *pool = solv->pool;
139   Id p, *pp;
140
141   if (ISRELDEP(dep))
142     {
143       Reldep *rd = GETRELDEP(pool, dep);
144       if (rd->flags == REL_AND)
145         {
146           if (!solver_dep_fulfilled(solv, rd->name))
147             return 0;
148           return solver_dep_fulfilled(solv, rd->evr);
149         }
150       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
151         return solver_dep_installed(solv, rd->evr);
152     }
153   FOR_PROVIDES(p, pp, dep)
154     {
155       if (solv->decisionmap[p] > 0)
156         return 1;
157     }
158   return 0;
159 }
160
161 static inline int
162 solver_is_supplementing(Solver *solv, Solvable *s)
163 {
164   Id sup, *supp;
165   if (!s->supplements && !s->freshens)
166     return 0;
167   if (s->supplements)
168     {
169       supp = s->repo->idarraydata + s->supplements;
170       while ((sup = *supp++) != 0)
171         if (solver_dep_fulfilled(solv, sup))
172           break;
173       if (!sup)
174         return 0;
175     }
176   if (s->freshens)
177     {
178       supp = s->repo->idarraydata + s->freshens;
179       while ((sup = *supp++) != 0)
180         if (solver_dep_fulfilled(solv, sup))
181           break;
182       if (!sup)
183         return 0;
184     }
185   return 1;
186 }
187
188 static inline int
189 solver_is_enhancing(Solver *solv, Solvable *s)
190 {
191   Id enh, *enhp;
192   if (!s->enhances)
193     return 0;
194   enhp = s->repo->idarraydata + s->enhances;
195   while ((enh = *enhp++) != 0)
196     if (solver_dep_fulfilled(solv, enh))
197       return 1;
198   return 0;
199 }
200
201 #endif /* SOLVER_H */