implement SOLVER_FLAG_BREAK_ORPHANS
[platform/upstream/libsolv.git] / src / problems.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * problems.c
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <string.h>
17 #include <assert.h>
18
19 #include "solver.h"
20 #include "solver_private.h"
21 #include "bitmap.h"
22 #include "pool.h"
23 #include "util.h"
24 #include "evr.h"
25 #include "solverdebug.h"
26
27
28 /**********************************************************************************/
29
30 /* a problem is an item on the solver's problem list. It can either be >0, in that
31  * case it is a update/infarch/dup rule, or it can be <0, which makes it refer to a job
32  * consisting of multiple job rules.
33  */
34
35 void
36 solver_disableproblem(Solver *solv, Id v)
37 {
38   Rule *r;
39   int i;
40   Id *jp;
41
42   if (v > 0)
43     {
44       if (v >= solv->infarchrules && v < solv->infarchrules_end)
45         {
46           Pool *pool = solv->pool;
47           Id name = pool->solvables[-solv->rules[v].p].name;
48           while (v > solv->infarchrules && pool->solvables[-solv->rules[v - 1].p].name == name)
49             v--;
50           for (; v < solv->infarchrules_end && pool->solvables[-solv->rules[v].p].name == name; v++)
51             solver_disablerule(solv, solv->rules + v);
52           return;
53         }
54       if (v >= solv->duprules && v < solv->duprules_end)
55         {
56           Pool *pool = solv->pool;
57           Id name = pool->solvables[-solv->rules[v].p].name;
58           while (v > solv->duprules && pool->solvables[-solv->rules[v - 1].p].name == name)
59             v--;
60           for (; v < solv->duprules_end && pool->solvables[-solv->rules[v].p].name == name; v++)
61             solver_disablerule(solv, solv->rules + v);
62           return;
63         }
64       solver_disablerule(solv, solv->rules + v);
65 #if 0
66       /* XXX: doesn't work */
67       if (v >= solv->updaterules && v < solv->updaterules_end)
68         {
69           /* enable feature rule if we disabled the update rule */
70           r = solv->rules + (v - solv->updaterules + solv->featurerules);
71           if (r->p)
72             solver_enablerule(solv, r);
73         }
74 #endif
75       return;
76     }
77   v = -(v + 1);
78   jp = solv->ruletojob.elements;
79   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
80     if (*jp == v)
81       solver_disablerule(solv, r);
82 }
83
84 /*-------------------------------------------------------------------
85  * enableproblem
86  */
87
88 void
89 solver_enableproblem(Solver *solv, Id v)
90 {
91   Rule *r;
92   int i;
93   Id *jp;
94
95   if (v > 0)
96     {
97       if (v >= solv->infarchrules && v < solv->infarchrules_end)
98         {
99           Pool *pool = solv->pool;
100           Id name = pool->solvables[-solv->rules[v].p].name;
101           while (v > solv->infarchrules && pool->solvables[-solv->rules[v - 1].p].name == name)
102             v--;
103           for (; v < solv->infarchrules_end && pool->solvables[-solv->rules[v].p].name == name; v++)
104             solver_enablerule(solv, solv->rules + v);
105           return;
106         }
107       if (v >= solv->duprules && v < solv->duprules_end)
108         {
109           Pool *pool = solv->pool;
110           Id name = pool->solvables[-solv->rules[v].p].name;
111           while (v > solv->duprules && pool->solvables[-solv->rules[v - 1].p].name == name)
112             v--;
113           for (; v < solv->duprules_end && pool->solvables[-solv->rules[v].p].name == name; v++)
114             solver_enablerule(solv, solv->rules + v);
115           return;
116         }
117       if (v >= solv->featurerules && v < solv->featurerules_end)
118         {
119           /* do not enable feature rule if update rule is enabled */
120           r = solv->rules + (v - solv->featurerules + solv->updaterules);
121           if (r->d >= 0)
122             return;
123         }
124       solver_enablerule(solv, solv->rules + v);
125       if (v >= solv->updaterules && v < solv->updaterules_end)
126         {
127           /* disable feature rule when enabling update rule */
128           r = solv->rules + (v - solv->updaterules + solv->featurerules);
129           if (r->p)
130             solver_disablerule(solv, r);
131         }
132       return;
133     }
134   v = -(v + 1);
135   jp = solv->ruletojob.elements;
136   for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++, jp++)
137     if (*jp == v)
138       solver_enablerule(solv, r);
139 }
140
141
142 /*-------------------------------------------------------------------
143  * enable weak rules
144  *
145  * Reenable all disabled weak rules (marked in weakrulemap)
146  *
147  */
148
149 static void
150 enableweakrules(Solver *solv)
151 {
152   int i;
153   Rule *r;
154
155   for (i = 1, r = solv->rules + i; i < solv->learntrules; i++, r++)
156     {
157       if (r->d >= 0) /* already enabled? */
158         continue;
159       if (!MAPTST(&solv->weakrulemap, i))
160         continue;
161       solver_enablerule(solv, r);
162     }
163   /* make sure broken orphan rules stay disabled */
164   if (solv->brokenorphanrules)
165     for (i = 0; i < solv->brokenorphanrules->count; i++)
166       solver_disablerule(solv, solv->rules + solv->brokenorphanrules->elements[i]);
167 }
168
169
170 /*-------------------------------------------------------------------
171  *
172  * refine_suggestion
173  *
174  * at this point, all rules that led to conflicts are disabled.
175  * we re-enable all rules of a problem set but rule "sug", then
176  * continue to disable more rules until there as again a solution.
177  */
178
179 /* FIXME: think about conflicting assertions */
180
181 static void
182 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined, int essentialok)
183 {
184   Pool *pool = solv->pool;
185   int i, j;
186   Id v;
187   Queue disabled;
188   int disabledcnt;
189
190   IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
191     {
192       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "refine_suggestion start\n");
193       for (i = 0; problem[i]; i++)
194         {
195           if (problem[i] == sug)
196             POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "=> ");
197           solver_printproblem(solv, problem[i]);
198         }
199     }
200   queue_empty(refined);
201   if (!essentialok && sug < 0 && (solv->job.elements[-sug - 1] & SOLVER_ESSENTIAL) != 0)
202     return;
203   queue_init(&disabled);
204   queue_push(refined, sug);
205
206   /* re-enable all problem rules with the exception of "sug"(gestion) */
207   solver_reset(solv);
208
209   for (i = 0; problem[i]; i++)
210     if (problem[i] != sug)
211       solver_enableproblem(solv, problem[i]);
212
213   if (sug < 0)
214     solver_reenablepolicyrules(solv, -sug);
215   else if (sug >= solv->updaterules && sug < solv->updaterules_end)
216     {
217       /* enable feature rule */
218       Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
219       if (r->p)
220         solver_enablerule(solv, r);
221     }
222
223   enableweakrules(solv);
224
225   for (;;)
226     {
227       int njob, nfeature, nupdate, pass;
228       queue_empty(&solv->problems);
229       solver_reset(solv);
230
231       if (!solv->problems.count)
232         solver_run_sat(solv, 0, 0);
233
234       if (!solv->problems.count)
235         {
236           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "no more problems!\n");
237           break;                /* great, no more problems */
238         }
239       disabledcnt = disabled.count;
240       /* start with 1 to skip over proof index */
241       njob = nfeature = nupdate = 0;
242       for (pass = 0; pass < 2; pass++)
243         {
244           for (i = 1; i < solv->problems.count - 1; i++)
245             {
246               /* ignore solutions in refined */
247               v = solv->problems.elements[i];
248               if (v == 0)
249                 break;  /* end of problem reached */
250               if (sug != v)
251                 {
252                   /* check if v is in the given problems list
253                    * we allow disabling all problem rules *after* sug in
254                    * pass 2, to prevent getting the same solution twice */
255                   for (j = 0; problem[j]; j++)
256                     if (problem[j] == v || (pass && problem[j] == sug))
257                       break;
258                   if (problem[j] == v)
259                     continue;
260                 }
261               if (v >= solv->featurerules && v < solv->featurerules_end)
262                 nfeature++;
263               else if (v > 0)
264                 nupdate++;
265               else
266                 {
267                   if (!essentialok && (solv->job.elements[-v - 1] & SOLVER_ESSENTIAL) != 0)
268                     continue;   /* not that one! */
269                   njob++;
270                 }
271               queue_push(&disabled, v);
272             }
273           if (disabled.count != disabledcnt)
274             break;
275         }
276       if (disabled.count == disabledcnt)
277         {
278           /* no solution found, this was an invalid suggestion! */
279           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "no solution found!\n");
280           refined->count = 0;
281           break;
282         }
283       if (!njob && nupdate && nfeature)
284         {
285           /* got only update rules, filter out feature rules */
286           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "throwing away feature rules\n");
287           for (i = j = disabledcnt; i < disabled.count; i++)
288             {
289               v = disabled.elements[i];
290               if (v < solv->featurerules || v >= solv->featurerules_end)
291                 disabled.elements[j++] = v;
292             }
293           disabled.count = j;
294           nfeature = 0;
295         }
296       if (disabled.count == disabledcnt + 1)
297         {
298           /* just one suggestion, add it to refined list */
299           v = disabled.elements[disabledcnt];
300           if (!nfeature && v != sug)
301             queue_push(refined, v);     /* do not record feature rules */
302           solver_disableproblem(solv, v);
303           if (v >= solv->updaterules && v < solv->updaterules_end)
304             {
305               Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
306               if (r->p)
307                 solver_enablerule(solv, r);     /* enable corresponding feature rule */
308             }
309           if (v < 0)
310             solver_reenablepolicyrules(solv, -v);
311         }
312       else
313         {
314           /* more than one solution, disable all */
315           /* do not push anything on refine list, as we do not know which solution to choose */
316           /* thus, the user will get another problem if he selects this solution, where he
317            * can choose the right one */
318           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
319             {
320               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "more than one solution found:\n");
321               for (i = disabledcnt; i < disabled.count; i++)
322                 solver_printproblem(solv, disabled.elements[i]);
323             }
324           for (i = disabledcnt; i < disabled.count; i++)
325             {
326               v = disabled.elements[i];
327               solver_disableproblem(solv, v);
328               if (v >= solv->updaterules && v < solv->updaterules_end)
329                 {
330                   Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
331                   if (r->p)
332                     solver_enablerule(solv, r);
333                 }
334             }
335         }
336     }
337   /* all done, get us back into the same state as before */
338   /* enable refined rules again */
339   for (i = 0; i < disabled.count; i++)
340     solver_enableproblem(solv, disabled.elements[i]);
341   queue_free(&disabled);
342   /* reset policy rules */
343   for (i = 0; problem[i]; i++)
344     solver_enableproblem(solv, problem[i]);
345   solver_disablepolicyrules(solv);
346   /* disable problem rules again */
347   for (i = 0; problem[i]; i++)
348     solver_disableproblem(solv, problem[i]);
349   POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "refine_suggestion end\n");
350 }
351
352
353 /*-------------------------------------------------------------------
354  * sorting helper for problems
355  *
356  * bring update rules before job rules
357  * make essential job rules last
358  */
359
360 static int
361 problems_sortcmp(const void *ap, const void *bp, void *dp)
362 {
363   Queue *job = dp;
364   Id a = *(Id *)ap, b = *(Id *)bp;
365   if (a < 0 && b > 0)
366     return 1;
367   if (a > 0 && b < 0)
368     return -1;
369   if (a < 0 && b < 0)
370     {
371       int af = job->elements[-a - 1] & SOLVER_ESSENTIAL;
372       int bf = job->elements[-b - 1] & SOLVER_ESSENTIAL;
373       int x = af - bf;
374       if (x)
375         return x;
376     }
377   return a - b;
378 }
379
380 /*
381  * convert a solution rule into a job modifier
382  */
383 static void
384 convertsolution(Solver *solv, Id why, Queue *solutionq)
385 {
386   Pool *pool = solv->pool;
387   if (why < 0)
388     {
389       why = -why;
390       if (why < solv->pooljobcnt)
391         {
392           queue_push(solutionq, SOLVER_SOLUTION_POOLJOB);
393           queue_push(solutionq, why);
394         }
395       else
396         {
397           queue_push(solutionq, SOLVER_SOLUTION_JOB);
398           queue_push(solutionq, why - solv->pooljobcnt);
399         }
400       return;
401     }
402   if (why >= solv->infarchrules && why < solv->infarchrules_end)
403     {
404       Id p, name;
405       /* infarch rule, find replacement */
406       assert(solv->rules[why].p < 0);
407       name = pool->solvables[-solv->rules[why].p].name;
408       while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
409         why--;
410       p = 0;
411       for (; why < solv->infarchrules_end && pool->solvables[-solv->rules[why].p].name == name; why++)
412         if (solv->decisionmap[-solv->rules[why].p] > 0)
413           {
414             p = -solv->rules[why].p;
415             break;
416           }
417       if (!p)
418         return;         /* false alarm */
419       queue_push(solutionq, SOLVER_SOLUTION_INFARCH);
420       queue_push(solutionq, p);
421       return;
422     }
423   if (why >= solv->duprules && why < solv->duprules_end)
424     {
425       Id p, name;
426       /* dist upgrade rule, find replacement */
427       assert(solv->rules[why].p < 0);
428       name = pool->solvables[-solv->rules[why].p].name;
429       while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
430         why--;
431       p = 0;
432       for (; why < solv->duprules_end && pool->solvables[-solv->rules[why].p].name == name; why++)
433         if (solv->decisionmap[-solv->rules[why].p] > 0)
434           {
435             p = -solv->rules[why].p;
436             break;
437           }
438       if (!p)
439         return;         /* false alarm */
440       queue_push(solutionq, SOLVER_SOLUTION_DISTUPGRADE);
441       queue_push(solutionq, p);
442       return;
443     }
444   if (why >= solv->updaterules && why < solv->updaterules_end)
445     {
446       /* update rule, find replacement package */
447       Id p, pp, rp = 0;
448       Rule *rr;
449
450       /* check if this is a false positive, i.e. the update rule is fulfilled */
451       rr = solv->rules + why;
452       FOR_RULELITERALS(p, pp, rr)
453         if (p > 0 && solv->decisionmap[p] > 0)
454           return;       /* false alarm */
455
456       p = solv->installed->start + (why - solv->updaterules);
457       if (solv->dupmap_all && solv->rules[why].p != p && solv->decisionmap[p] > 0)
458         {
459           /* distupgrade case, allow to keep old package */
460           queue_push(solutionq, SOLVER_SOLUTION_DISTUPGRADE);
461           queue_push(solutionq, p);
462           return;
463         }
464       if (solv->decisionmap[p] > 0)
465         return;         /* false alarm, turned out we can keep the package */
466       rr = solv->rules + solv->featurerules + (why - solv->updaterules);
467       if (!rr->p)
468         rr = solv->rules + why;
469       if (rr->w2)
470         {
471           int mvrp = 0;         /* multi-version replacement */
472           FOR_RULELITERALS(rp, pp, rr)
473             {
474               if (rp > 0 && solv->decisionmap[rp] > 0 && pool->solvables[rp].repo != solv->installed)
475                 {
476                   mvrp = rp;
477                   if (!(solv->multiversion.size && MAPTST(&solv->multiversion, rp)))
478                     break;
479                 }
480             }
481           if (!rp && mvrp)
482             {
483               /* found only multi-version replacements */
484               /* have to split solution into two parts */
485               queue_push(solutionq, p);
486               queue_push(solutionq, mvrp);
487             }
488         }
489       queue_push(solutionq, p);
490       queue_push(solutionq, rp);
491       return;
492     }
493   if (why >= solv->bestrules && why < solv->bestrules_end)
494     {
495       int mvrp;
496       Id p, pp, rp = 0;
497       Rule *rr;
498       /* check false positive */
499       rr = solv->rules + why;
500       FOR_RULELITERALS(p, pp, rr)
501         if (p > 0 && solv->decisionmap[p] > 0)
502           return;       /* false alarm */
503       /* check update/feature rule */
504       p = solv->bestrules_pkg[why - solv->bestrules];
505       if (p < 0)
506         {
507           /* install job */
508           queue_push(solutionq, 0);
509           queue_push(solutionq, solv->ruletojob.elements[-p - solv->jobrules] + 1);
510           return;
511         }
512       if (solv->decisionmap[p] > 0)
513         {
514           /* disable best rule by keeping the old package */
515           queue_push(solutionq, SOLVER_SOLUTION_BEST);
516           queue_push(solutionq, p);
517           return;
518         }
519       rr = solv->rules + solv->featurerules + (p - solv->installed->start);
520       if (!rr->p)
521         rr = solv->rules + solv->updaterules + (p - solv->installed->start);
522       mvrp = 0;         /* multi-version replacement */
523       FOR_RULELITERALS(rp, pp, rr)
524         if (rp > 0 && solv->decisionmap[rp] > 0 && pool->solvables[rp].repo != solv->installed)
525           {
526             mvrp = rp;
527             if (!(solv->multiversion.size && MAPTST(&solv->multiversion, rp)))
528               break;
529           }
530       if (!rp && mvrp)
531         {
532           queue_push(solutionq, SOLVER_SOLUTION_BEST);  /* split, see above */
533           queue_push(solutionq, mvrp);
534           queue_push(solutionq, p);
535           queue_push(solutionq, 0);
536           return;
537         }
538       if (rp)
539         {
540           queue_push(solutionq, SOLVER_SOLUTION_BEST);
541           queue_push(solutionq, rp);
542         }
543       return;
544     }
545 }
546
547 /*
548  * convert problem data into a form usable for refining.
549  * Returns the number of problems.
550  */
551 int
552 solver_prepare_solutions(Solver *solv)
553 {
554   int i, j = 1, idx;
555
556   if (!solv->problems.count)
557     return 0;
558   queue_empty(&solv->solutions);
559   queue_push(&solv->solutions, 0);      /* dummy so idx is always nonzero */
560   idx = solv->solutions.count;
561   queue_push(&solv->solutions, -1);     /* unrefined */
562   /* proofidx stays in position, thus we start with 1 */
563   for (i = 1; i < solv->problems.count; i++)
564     {
565       Id p = solv->problems.elements[i];
566       queue_push(&solv->solutions, p);
567       if (p)
568         continue;
569       /* end of problem reached */
570       solv->problems.elements[j++] = idx;
571       if (i + 1 >= solv->problems.count)
572         break;
573       /* start another problem */
574       solv->problems.elements[j++] = solv->problems.elements[++i];  /* copy proofidx */
575       idx = solv->solutions.count;
576       queue_push(&solv->solutions, -1); /* unrefined */
577     }
578   solv->problems.count = j;
579   return j / 2;
580 }
581
582 /*
583  * refine the simple solution rule list provided by
584  * the solver into multiple lists of job modifiers.
585  */
586 static void
587 create_solutions(Solver *solv, int probnr, int solidx)
588 {
589   Pool *pool = solv->pool;
590   Queue redoq;
591   Queue problem, solution, problems_save;
592   int i, j, nsol;
593   int essentialok;
594   unsigned int now;
595   int oldmistakes = solv->cleandeps_mistakes ? solv->cleandeps_mistakes->count : 0;
596   Id extraflags = -1;
597   int decisioncnt_update;
598   int decisioncnt_keep;
599   int decisioncnt_resolve;
600   int decisioncnt_weak;
601   int decisioncnt_orphan;
602
603   now = solv_timems(0);
604   queue_init(&redoq);
605   /* save decisionq, decisionq_why, decisionmap, and decisioncnt */
606   for (i = 0; i < solv->decisionq.count; i++)
607     {
608       Id p = solv->decisionq.elements[i];
609       queue_push(&redoq, p);
610       queue_push(&redoq, solv->decisionq_why.elements[i]);
611       queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
612     }
613   decisioncnt_update = solv->decisioncnt_update;
614   decisioncnt_keep = solv->decisioncnt_keep;
615   decisioncnt_resolve = solv->decisioncnt_resolve;
616   decisioncnt_weak = solv->decisioncnt_weak;
617   decisioncnt_orphan = solv->decisioncnt_orphan;
618
619   /* save problems queue */
620   problems_save = solv->problems;
621   memset(&solv->problems, 0, sizeof(solv->problems));
622
623   /* extract problem from queue */
624   queue_init(&problem);
625   for (i = solidx + 1; i < solv->solutions.count; i++)
626     {
627       Id v = solv->solutions.elements[i];
628       if (!v)
629         break;
630       queue_push(&problem, v);
631       if (v < 0)
632         extraflags &= solv->job.elements[-v - 1];
633     }
634   if (extraflags == -1)
635     extraflags = 0;
636   if (problem.count > 1)
637     solv_sort(problem.elements, problem.count, sizeof(Id), problems_sortcmp, &solv->job);
638   queue_push(&problem, 0);      /* mark end for refine_suggestion */
639   problem.count--;
640 #if 0
641   for (i = 0; i < problem.count; i++)
642     printf("PP %d %d\n", i, problem.elements[i]);
643 #endif
644
645   /* refine each solution element */
646   nsol = 0;
647   essentialok = 0;
648   queue_init(&solution);
649   for (i = 0; i < problem.count; i++)
650     {
651       int solstart = solv->solutions.count;
652       refine_suggestion(solv, problem.elements, problem.elements[i], &solution, essentialok);
653       queue_push(&solv->solutions, 0);  /* reserve room for number of elements */
654       for (j = 0; j < solution.count; j++)
655         convertsolution(solv, solution.elements[j], &solv->solutions);
656       if (solv->solutions.count == solstart + 1)
657         {
658           solv->solutions.count--;      /* this one did not work out */
659           if (nsol || i + 1 < problem.count)
660             continue;                   /* got one or still hope */
661           if (!essentialok)
662             {
663               /* nothing found, start over */
664               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "nothing found, re-run with essentialok = 1\n");
665               essentialok = 1;
666               i = -1;
667               continue;
668             }
669           /* this is bad, we found no solution */
670           /* for now just offer a rule */
671           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "nothing found, already did essentialok, fake it\n");
672           queue_push(&solv->solutions, 0);
673           for (j = 0; j < problem.count; j++)
674             {
675               convertsolution(solv, problem.elements[j], &solv->solutions);
676               if (solv->solutions.count > solstart + 1)
677                 break;
678             }
679           if (solv->solutions.count == solstart + 1)
680             {
681               solv->solutions.count--;
682               continue;         /* sorry */
683             }
684         }
685       /* patch in number of solution elements */
686       solv->solutions.elements[solstart] = (solv->solutions.count - (solstart + 1)) / 2;
687       queue_push(&solv->solutions, 0);  /* add end marker */
688       queue_push(&solv->solutions, 0);  /* add end marker */
689       queue_push(&solv->solutions, problem.elements[i]);        /* just for bookkeeping */
690       queue_push(&solv->solutions, extraflags & SOLVER_CLEANDEPS);      /* our extraflags */
691       solv->solutions.elements[solidx + 1 + nsol++] = solstart;
692     }
693   solv->solutions.elements[solidx + 1 + nsol] = 0;      /* end marker */
694   solv->solutions.elements[solidx] = nsol;
695   queue_free(&problem);
696   queue_free(&solution);
697
698   /* restore decisions */
699   memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
700   queue_empty(&solv->decisionq);
701   queue_empty(&solv->decisionq_why);
702   for (i = 0; i < redoq.count; i += 3)
703     {
704       Id p = redoq.elements[i];
705       queue_push(&solv->decisionq, p);
706       queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
707       solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
708     }
709   queue_free(&redoq);
710   solv->decisioncnt_update = decisioncnt_update;
711   solv->decisioncnt_keep = decisioncnt_keep;
712   solv->decisioncnt_resolve = decisioncnt_resolve;
713   solv->decisioncnt_weak = decisioncnt_weak;
714   solv->decisioncnt_orphan = decisioncnt_orphan;
715
716   /* restore problems */
717   queue_free(&solv->problems);
718   solv->problems = problems_save;
719
720   if (solv->cleandeps_mistakes)
721     {
722       if (oldmistakes)
723         queue_truncate(solv->cleandeps_mistakes, oldmistakes);
724       else
725         {
726           queue_free(solv->cleandeps_mistakes);
727           solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
728         }
729     }
730
731   POOL_DEBUG(SOLV_DEBUG_STATS, "create_solutions for problem #%d took %d ms\n", probnr, solv_timems(now));
732 }
733
734
735 /**************************************************************************/
736
737 unsigned int
738 solver_problem_count(Solver *solv)
739 {
740   return solv->problems.count / 2;
741 }
742
743 Id
744 solver_next_problem(Solver *solv, Id problem)
745 {
746   if (!problem)
747     return solv->problems.count ? 1 : 0;
748   return (problem + 1) * 2 - 1 < solv->problems.count ? problem + 1 : 0;
749 }
750
751 unsigned int
752 solver_solution_count(Solver *solv, Id problem)
753 {
754   Id solidx = solv->problems.elements[problem * 2 - 1];
755   if (solv->solutions.elements[solidx] < 0)
756     create_solutions(solv, problem, solidx);
757   return solv->solutions.elements[solidx];
758 }
759
760 Id
761 solver_next_solution(Solver *solv, Id problem, Id solution)
762 {
763   Id solidx = solv->problems.elements[problem * 2 - 1];
764   if (solv->solutions.elements[solidx] < 0)
765     create_solutions(solv, problem, solidx);
766   return solv->solutions.elements[solidx + solution + 1] ? solution + 1 : 0;
767 }
768
769 unsigned int
770 solver_solutionelement_count(Solver *solv, Id problem, Id solution)
771 {
772   Id solidx = solv->problems.elements[problem * 2 - 1];
773   solidx = solv->solutions.elements[solidx + solution];
774   return solv->solutions.elements[solidx];
775 }
776
777 Id
778 solver_solutionelement_internalid(Solver *solv, Id problem, Id solution)
779 {
780   Id solidx = solv->problems.elements[problem * 2 - 1];
781   solidx = solv->solutions.elements[solidx + solution];
782   return solv->solutions.elements[solidx + 2 * solv->solutions.elements[solidx] + 3];
783 }
784
785 Id
786 solver_solutionelement_extrajobflags(Solver *solv, Id problem, Id solution)
787 {
788   Id solidx = solv->problems.elements[problem * 2 - 1];
789   solidx = solv->solutions.elements[solidx + solution];
790   return solv->solutions.elements[solidx + 2 * solv->solutions.elements[solidx] + 4];
791 }
792
793
794 /*
795  *  return the next item of the proposed solution
796  *  here are the possibilities for p / rp and what
797  *  the solver expects the application to do:
798  *    p                             rp
799  *  -------------------------------------------------------
800  *    SOLVER_SOLUTION_INFARCH       pkgid
801  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
802  *    SOLVER_SOLUTION_DISTUPGRADE   pkgid
803  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
804  *    SOLVER_SOLUTION_BEST          pkgid
805  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
806  *    SOLVER_SOLUTION_JOB           jobidx
807  *    -> remove job (jobidx - 1, jobidx) from job queue
808  *    SOLVER_SOLUTION_POOLJOB       jobidx
809  *    -> remove job (jobidx - 1, jobidx) from pool job queue
810  *    pkgid (> 0)                   0
811  *    -> add (SOLVER_ERASE|SOLVER_SOLVABLE, p) to the job
812  *    pkgid (> 0)                   pkgid (> 0)
813  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
814  *       (this will replace package p)
815  *
816  * Thus, the solver will either ask the application to remove
817  * a specific job from the job queue, or ask to add an install/erase
818  * job to it.
819  *
820  */
821
822 Id
823 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
824 {
825   Id solidx = solv->problems.elements[problem * 2 - 1];
826   solidx = solv->solutions.elements[solidx + solution];
827   if (!solidx)
828     return 0;
829   solidx += 1 + element * 2;
830   if (!solv->solutions.elements[solidx] && !solv->solutions.elements[solidx + 1])
831     return 0;
832   *p = solv->solutions.elements[solidx];
833   *rp = solv->solutions.elements[solidx + 1];
834   return element + 1;
835 }
836
837 void
838 solver_take_solutionelement(Solver *solv, Id p, Id rp, Id extrajobflags, Queue *job)
839 {
840   int i;
841
842   if (p == SOLVER_SOLUTION_POOLJOB)
843     {
844       solv->pool->pooljobs.elements[rp - 1] = SOLVER_NOOP;
845       solv->pool->pooljobs.elements[rp] = 0;
846       return;
847     }
848   if (p == SOLVER_SOLUTION_JOB)
849     {
850       job->elements[rp - 1] = SOLVER_NOOP;
851       job->elements[rp] = 0;
852       return;
853     }
854   if (rp <= 0 && p <= 0)
855     return;     /* just in case */
856   if (rp > 0)
857     p = SOLVER_INSTALL|SOLVER_SOLVABLE|SOLVER_NOTBYUSER|extrajobflags;
858   else
859     {
860       rp = p;
861       p = SOLVER_ERASE|SOLVER_SOLVABLE|extrajobflags;
862     }
863   for (i = 0; i < job->count; i += 2)
864     if (job->elements[i] == p && job->elements[i + 1] == rp)
865       return;
866   queue_push2(job, p, rp);
867 }
868
869 void
870 solver_take_solution(Solver *solv, Id problem, Id solution, Queue *job)
871 {
872   Id p, rp, element = 0;
873   Id extrajobflags = solver_solutionelement_extrajobflags(solv, problem, solution);
874   while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
875     solver_take_solutionelement(solv, p, rp, extrajobflags, job);
876 }
877
878
879 /*-------------------------------------------------------------------
880  *
881  * find problem rule
882  */
883
884 static void
885 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp, Map *rseen)
886 {
887   Id rid, d;
888   Id lreqr, lconr, lsysr, ljobr;
889   Rule *r;
890   Id jobassert = 0;
891   int i, reqset = 0;    /* 0: unset, 1: installed, 2: jobassert, 3: assert */
892   int conset = 0;       /* 0: unset, 1: installed */
893
894   /* find us a jobassert rule */
895   for (i = idx; (rid = solv->learnt_pool.elements[i]) != 0; i++)
896     {
897       if (rid < solv->jobrules || rid >= solv->jobrules_end)
898         continue;
899       r = solv->rules + rid;
900       d = r->d < 0 ? -r->d - 1 : r->d;
901       if (!d && r->w2 == 0 && r->p > 0)
902         {
903           jobassert = r->p;
904           break;
905         }
906     }
907
908   /* the problem rules are somewhat ordered from "near to the problem" to
909    * "near to the job" */
910   lreqr = lconr = lsysr = ljobr = 0;
911   while ((rid = solv->learnt_pool.elements[idx++]) != 0)
912     {
913       assert(rid > 0);
914       if (rid >= solv->learntrules)
915         {
916           if (MAPTST(rseen, rid - solv->learntrules))
917             continue;
918           MAPSET(rseen, rid - solv->learntrules);
919           findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr, rseen);
920         }
921       else if ((rid >= solv->jobrules && rid < solv->jobrules_end) || (rid >= solv->infarchrules && rid < solv->infarchrules_end) || (rid >= solv->duprules && rid < solv->duprules_end) || (rid >= solv->bestrules && rid < solv->bestrules_end))
922         {
923           if (!*jobrp)
924             *jobrp = rid;
925         }
926       else if (rid >= solv->updaterules && rid < solv->updaterules_end)
927         {
928           if (!*sysrp)
929             *sysrp = rid;
930         }
931       else
932         {
933           assert(rid < solv->rpmrules_end);
934           r = solv->rules + rid;
935           d = r->d < 0 ? -r->d - 1 : r->d;
936           if (!d && r->w2 < 0)
937             {
938               /* prefer conflicts of installed packages */
939               if (solv->installed && !conset)
940                 {
941                   if (r->p < 0 && (solv->pool->solvables[-r->p].repo == solv->installed ||
942                                   solv->pool->solvables[-r->w2].repo == solv->installed))
943                     {
944                       *conrp = rid;
945                       conset = 1;
946                     }
947                 }
948               if (!*conrp)
949                 *conrp = rid;
950             }
951           else
952             {
953               if (!d && r->w2 == 0 && reqset < 3)
954                 {
955                   if (*reqrp > 0 && r->p < -1)
956                     {
957                       Id op = -solv->rules[*reqrp].p;
958                       if (op > 1 && solv->pool->solvables[op].arch != solv->pool->solvables[-r->p].arch)
959                         continue;       /* different arch, skip */
960                     }
961                   /* prefer assertions */
962                   *reqrp = rid;
963                   reqset = 3;
964                 }
965               else if (jobassert && r->p == -jobassert)
966                 {
967                   /* prefer rules of job assertions */
968                   *reqrp = rid;
969                   reqset = 2;
970                 }
971               else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed && reqset <= 1)
972                 {
973                   /* prefer rules of job installed package so that the user doesn't get confused by strange packages */
974                   *reqrp = rid;
975                   reqset = 1;
976                 }
977               else if (!*reqrp)
978                 *reqrp = rid;
979             }
980         }
981     }
982   if (!*reqrp && lreqr)
983     *reqrp = lreqr;
984   if (!*conrp && lconr)
985     *conrp = lconr;
986   if (!*jobrp && ljobr)
987     *jobrp = ljobr;
988   if (!*sysrp && lsysr)
989     *sysrp = lsysr;
990 }
991
992 /*
993  * find problem rule
994  *
995  * search for a rule that describes the problem to the
996  * user. Actually a pretty hopeless task that may leave the user
997  * puzzled. To get all of the needed information use
998  * solver_findallproblemrules() instead.
999  */
1000
1001 Id
1002 solver_findproblemrule(Solver *solv, Id problem)
1003 {
1004   Id reqr, conr, sysr, jobr;
1005   Id idx = solv->problems.elements[2 * problem - 2];
1006   Map rseen;
1007   reqr = conr = sysr = jobr = 0;
1008   map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1009   findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr, &rseen);
1010   map_free(&rseen);
1011   /* check if the request is about a not-installed package requiring a installed
1012    * package conflicting with the non-installed package. In that case return the conflict */
1013   if (reqr && conr && solv->installed && solv->rules[reqr].p < 0 && solv->rules[conr].p < 0 && solv->rules[conr].w2 < 0)
1014     {
1015       Pool *pool = solv->pool;
1016       Solvable *s  = pool->solvables - solv->rules[reqr].p;
1017       Solvable *s1 = pool->solvables - solv->rules[conr].p;
1018       Solvable *s2 = pool->solvables - solv->rules[conr].w2;
1019       Id cp = 0;
1020       if (s == s1 && s2->repo == solv->installed)
1021         cp = -solv->rules[conr].w2;
1022       else if (s == s2 && s1->repo == solv->installed)
1023         cp = -solv->rules[conr].p;
1024       if (cp && s1->name != s2->name && s->repo != solv->installed)
1025         {
1026           Id p, pp;
1027           Rule *r = solv->rules + reqr;
1028           FOR_RULELITERALS(p, pp, r)
1029             if (p == cp)
1030               return conr;
1031         }
1032     }
1033   if (reqr)
1034     return reqr;        /* some requires */
1035   if (conr)
1036     return conr;        /* some conflict */
1037   if (sysr)
1038     return sysr;        /* an update rule */
1039   if (jobr)
1040     return jobr;        /* a user request */
1041   assert(0);
1042   return 0;
1043 }
1044
1045 /*-------------------------------------------------------------------*/
1046
1047 static void
1048 findallproblemrules_internal(Solver *solv, Id idx, Queue *rules, Map *rseen)
1049 {
1050   Id rid;
1051   while ((rid = solv->learnt_pool.elements[idx++]) != 0)
1052     {
1053       if (rid >= solv->learntrules)
1054         {
1055           if (MAPTST(rseen, rid - solv->learntrules))
1056             continue;
1057           MAPSET(rseen, rid - solv->learntrules);
1058           findallproblemrules_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], rules, rseen);
1059           continue;
1060         }
1061       queue_pushunique(rules, rid);
1062     }
1063 }
1064
1065 /*
1066  * find all problem rule
1067  *
1068  * return all rules that lead to the problem. This gives the user
1069  * all of the information to understand the problem, but the result
1070  * can be a large number of rules.
1071  */
1072
1073 void
1074 solver_findallproblemrules(Solver *solv, Id problem, Queue *rules)
1075 {
1076   Map rseen;
1077   queue_empty(rules);
1078   map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1079   findallproblemrules_internal(solv, solv->problems.elements[2 * problem - 2], rules, &rseen);
1080   map_free(&rseen);
1081 }
1082
1083 const char *
1084 solver_problemruleinfo2str(Solver *solv, SolverRuleinfo type, Id source, Id target, Id dep)
1085 {
1086   Pool *pool = solv->pool;
1087   char *s;
1088   switch (type)
1089     {
1090     case SOLVER_RULE_DISTUPGRADE:
1091       return pool_tmpjoin(pool, pool_solvid2str(pool, source), " does not belong to a distupgrade repository", 0);
1092     case SOLVER_RULE_INFARCH:
1093       return pool_tmpjoin(pool, pool_solvid2str(pool, source), " has inferior architecture", 0);
1094     case SOLVER_RULE_UPDATE:
1095       return pool_tmpjoin(pool, "problem with installed package ", pool_solvid2str(pool, source), 0);
1096     case SOLVER_RULE_JOB:
1097       return "conflicting requests";
1098     case SOLVER_RULE_JOB_UNSUPPORTED:
1099       return "unsupported request";
1100     case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
1101       return pool_tmpjoin(pool, "nothing provides requested ", pool_dep2str(pool, dep), 0);
1102     case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
1103       return pool_tmpjoin(pool, "package ", pool_dep2str(pool, dep), " does not exist");
1104     case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
1105       return pool_tmpjoin(pool, pool_dep2str(pool, dep), " is provided by the system", 0);
1106     case SOLVER_RULE_RPM:
1107       return "some dependency problem";
1108     case SOLVER_RULE_RPM_NOT_INSTALLABLE:
1109       return pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), " is not installable");
1110     case SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP:
1111       s = pool_tmpjoin(pool, "nothing provides ", pool_dep2str(pool, dep), 0);
1112       return pool_tmpappend(pool, s, " needed by ", pool_solvid2str(pool, source));
1113     case SOLVER_RULE_RPM_SAME_NAME:
1114       s = pool_tmpjoin(pool, "cannot install both ", pool_solvid2str(pool, source), 0);
1115       return pool_tmpappend(pool, s, " and ", pool_solvid2str(pool, target));
1116     case SOLVER_RULE_RPM_PACKAGE_CONFLICT:
1117       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), 0);
1118       s = pool_tmpappend(pool, s, " conflicts with ", pool_dep2str(pool, dep));
1119       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1120     case SOLVER_RULE_RPM_PACKAGE_OBSOLETES:
1121       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), 0);
1122       s = pool_tmpappend(pool, s, " obsoletes ", pool_dep2str(pool, dep));
1123       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1124     case SOLVER_RULE_RPM_INSTALLEDPKG_OBSOLETES:
1125       s = pool_tmpjoin(pool, "installed package ", pool_solvid2str(pool, source), 0);
1126       s = pool_tmpappend(pool, s, " obsoletes ", pool_dep2str(pool, dep));
1127       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1128     case SOLVER_RULE_RPM_IMPLICIT_OBSOLETES:
1129       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), 0);
1130       s = pool_tmpappend(pool, s, " implicitly obsoletes ", pool_dep2str(pool, dep));
1131       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1132     case SOLVER_RULE_RPM_PACKAGE_REQUIRES:
1133       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), " requires ");
1134       return pool_tmpappend(pool, s, pool_dep2str(pool, dep), ", but none of the providers can be installed");
1135     case SOLVER_RULE_RPM_SELF_CONFLICT:
1136       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), " conflicts with ");
1137       return pool_tmpappend(pool, s, pool_dep2str(pool, dep), " provided by itself");
1138     default:
1139       return "bad problem rule type";
1140     }
1141 }
1142
1143 /* convenience function */
1144 const char *
1145 solver_problem2str(Solver *solv, Id problem)
1146 {
1147   Id type, source, target, dep;
1148   Id r = solver_findproblemrule(solv, problem);
1149   if (!r)
1150     return "no problem rule?";
1151   type = solver_ruleinfo(solv, r, &source, &target, &dep);
1152   return solver_problemruleinfo2str(solv, type, source, target, dep);
1153 }
1154
1155 const char *
1156 solver_solutionelement2str(Solver *solv, Id p, Id rp)
1157 {
1158   Pool *pool = solv->pool;
1159   if (p == SOLVER_SOLUTION_JOB || p == SOLVER_SOLUTION_POOLJOB)
1160     {
1161       Id how, what;
1162       if (p == SOLVER_SOLUTION_JOB)
1163         rp += solv->pooljobcnt;
1164       how = solv->job.elements[rp - 1];
1165       what = solv->job.elements[rp];
1166       return pool_tmpjoin(pool, "do not ask to ", pool_job2str(pool, how, what, 0), 0);
1167     }
1168   else if (p == SOLVER_SOLUTION_INFARCH)
1169     {
1170       Solvable *s = pool->solvables + rp;
1171       if (solv->installed && s->repo == solv->installed)
1172         return pool_tmpjoin(pool, "keep ", pool_solvable2str(pool, s), " despite the inferior architecture");
1173       else
1174         return pool_tmpjoin(pool, "install ", pool_solvable2str(pool, s), " despite the inferior architecture");
1175     }
1176   else if (p == SOLVER_SOLUTION_DISTUPGRADE)
1177     {
1178       Solvable *s = pool->solvables + rp;
1179       if (solv->installed && s->repo == solv->installed)
1180         return pool_tmpjoin(pool, "keep obsolete ", pool_solvable2str(pool, s), 0);
1181       else
1182         return pool_tmpjoin(pool, "install ", pool_solvable2str(pool, s), " from excluded repository");
1183     }
1184   else if (p == SOLVER_SOLUTION_BEST)
1185     {
1186       Solvable *s = pool->solvables + rp;
1187       if (solv->installed && s->repo == solv->installed)
1188         return pool_tmpjoin(pool, "keep old ", pool_solvable2str(pool, s), 0);
1189       else
1190         return pool_tmpjoin(pool, "install ", pool_solvable2str(pool, s), " despite the old version");
1191     }
1192   else if (p > 0 && rp == 0)
1193     return pool_tmpjoin(pool, "allow deinstallation of ", pool_solvid2str(pool, p), 0);
1194   else if (p > 0 && rp > 0)
1195     {
1196       const char *sp = pool_solvid2str(pool, p);
1197       const char *srp = pool_solvid2str(pool, rp);
1198       const char *str = pool_tmpjoin(pool, "allow replacement of ", sp, 0);
1199       return pool_tmpappend(pool, str, " with ", srp);
1200     }
1201   else
1202     return "bad solution element";
1203 }
1204