Imported Upstream version 0.6.24
[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   if (!solv->weakrulemap.size)
156     return;
157   for (i = 1, r = solv->rules + i; i < solv->learntrules; i++, r++)
158     {
159       if (r->d >= 0) /* already enabled? */
160         continue;
161       if (!MAPTST(&solv->weakrulemap, i))
162         continue;
163       solver_enablerule(solv, r);
164     }
165   /* make sure broken orphan rules stay disabled */
166   if (solv->brokenorphanrules)
167     for (i = 0; i < solv->brokenorphanrules->count; i++)
168       solver_disablerule(solv, solv->rules + solv->brokenorphanrules->elements[i]);
169 }
170
171
172 /*-------------------------------------------------------------------
173  *
174  * refine_suggestion
175  *
176  * at this point, all rules that led to conflicts are disabled.
177  * we re-enable all rules of a problem set but rule "sug", then
178  * continue to disable more rules until there as again a solution.
179  */
180
181 /* FIXME: think about conflicting assertions */
182
183 static void
184 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined, int essentialok)
185 {
186   Pool *pool = solv->pool;
187   int i, j;
188   Id v;
189   Queue disabled;
190   int disabledcnt;
191
192   IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
193     {
194       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "refine_suggestion start\n");
195       for (i = 0; problem[i]; i++)
196         {
197           if (problem[i] == sug)
198             POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "=> ");
199           solver_printproblem(solv, problem[i]);
200         }
201     }
202   queue_empty(refined);
203   if (!essentialok && sug < 0 && (solv->job.elements[-sug - 1] & SOLVER_ESSENTIAL) != 0)
204     return;
205   queue_init(&disabled);
206   queue_push(refined, sug);
207
208   /* re-enable all problem rules with the exception of "sug"(gestion) */
209   solver_reset(solv);
210
211   for (i = 0; problem[i]; i++)
212     if (problem[i] != sug)
213       solver_enableproblem(solv, problem[i]);
214
215   if (sug < 0)
216     solver_reenablepolicyrules(solv, -sug);
217   else if (sug >= solv->updaterules && sug < solv->updaterules_end)
218     {
219       /* enable feature rule */
220       Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
221       if (r->p)
222         solver_enablerule(solv, r);
223     }
224
225   enableweakrules(solv);
226
227   for (;;)
228     {
229       int njob, nfeature, nupdate, pass;
230       queue_empty(&solv->problems);
231       solver_reset(solv);
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, branches_save, decisionq_reason_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
598   now = solv_timems(0);
599   queue_init(&redoq);
600   /* save decisionq, decisionq_why, decisionmap, and decisioncnt */
601   for (i = 0; i < solv->decisionq.count; i++)
602     {
603       Id p = solv->decisionq.elements[i];
604       queue_push(&redoq, p);
605       queue_push(&redoq, solv->decisionq_why.elements[i]);
606       queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
607     }
608
609   /* save problems queue */
610   problems_save = solv->problems;
611   memset(&solv->problems, 0, sizeof(solv->problems));
612
613   /* save branches queue */
614   branches_save = solv->problems;
615   memset(&solv->branches, 0, sizeof(solv->branches));
616
617   /* save decisionq_reason */
618   decisionq_reason_save = solv->decisionq_reason;
619   memset(&solv->decisionq_reason, 0, sizeof(solv->decisionq_reason));
620
621   /* extract problem from queue */
622   queue_init(&problem);
623   for (i = solidx + 1; i < solv->solutions.count; i++)
624     {
625       Id v = solv->solutions.elements[i];
626       if (!v)
627         break;
628       queue_push(&problem, v);
629       if (v < 0)
630         extraflags &= solv->job.elements[-v - 1];
631     }
632   if (extraflags == -1)
633     extraflags = 0;
634   if (problem.count > 1)
635     solv_sort(problem.elements, problem.count, sizeof(Id), problems_sortcmp, &solv->job);
636   queue_push(&problem, 0);      /* mark end for refine_suggestion */
637   problem.count--;
638 #if 0
639   for (i = 0; i < problem.count; i++)
640     printf("PP %d %d\n", i, problem.elements[i]);
641 #endif
642
643   /* refine each solution element */
644   nsol = 0;
645   essentialok = 0;
646   queue_init(&solution);
647   for (i = 0; i < problem.count; i++)
648     {
649       int solstart = solv->solutions.count;
650       refine_suggestion(solv, problem.elements, problem.elements[i], &solution, essentialok);
651       queue_push(&solv->solutions, 0);  /* reserve room for number of elements */
652       for (j = 0; j < solution.count; j++)
653         convertsolution(solv, solution.elements[j], &solv->solutions);
654       if (solv->solutions.count == solstart + 1)
655         {
656           solv->solutions.count--;      /* this one did not work out */
657           if (nsol || i + 1 < problem.count)
658             continue;                   /* got one or still hope */
659           if (!essentialok)
660             {
661               /* nothing found, start over */
662               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "nothing found, re-run with essentialok = 1\n");
663               essentialok = 1;
664               i = -1;
665               continue;
666             }
667           /* this is bad, we found no solution */
668           /* for now just offer a rule */
669           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "nothing found, already did essentialok, fake it\n");
670           queue_push(&solv->solutions, 0);
671           for (j = 0; j < problem.count; j++)
672             {
673               convertsolution(solv, problem.elements[j], &solv->solutions);
674               if (solv->solutions.count > solstart + 1)
675                 break;
676             }
677           if (solv->solutions.count == solstart + 1)
678             {
679               solv->solutions.count--;
680               continue;         /* sorry */
681             }
682         }
683       /* patch in number of solution elements */
684       solv->solutions.elements[solstart] = (solv->solutions.count - (solstart + 1)) / 2;
685       queue_push(&solv->solutions, 0);  /* add end marker */
686       queue_push(&solv->solutions, 0);  /* add end marker */
687       queue_push(&solv->solutions, problem.elements[i]);        /* just for bookkeeping */
688       queue_push(&solv->solutions, extraflags & SOLVER_CLEANDEPS);      /* our extraflags */
689       solv->solutions.elements[solidx + 1 + nsol++] = solstart;
690     }
691   solv->solutions.elements[solidx + 1 + nsol] = 0;      /* end marker */
692   solv->solutions.elements[solidx] = nsol;
693   queue_free(&problem);
694   queue_free(&solution);
695
696   /* restore decisions */
697   memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
698   queue_empty(&solv->decisionq);
699   queue_empty(&solv->decisionq_why);
700   for (i = 0; i < redoq.count; i += 3)
701     {
702       Id p = redoq.elements[i];
703       queue_push(&solv->decisionq, p);
704       queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
705       solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
706     }
707   queue_free(&redoq);
708
709   /* restore decision reasons */
710   queue_free(&solv->decisionq_reason);
711   solv->decisionq_reason = decisionq_reason_save;
712
713   /* restore problems */
714   queue_free(&solv->problems);
715   solv->problems = problems_save;
716
717   /* restore branches */
718   queue_free(&solv->branches);
719   solv->branches = branches_save;
720
721   if (solv->cleandeps_mistakes)
722     {
723       if (oldmistakes)
724         queue_truncate(solv->cleandeps_mistakes, oldmistakes);
725       else
726         {
727           queue_free(solv->cleandeps_mistakes);
728           solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
729         }
730     }
731
732   POOL_DEBUG(SOLV_DEBUG_STATS, "create_solutions for problem #%d took %d ms\n", probnr, solv_timems(now));
733 }
734
735
736 /**************************************************************************/
737
738 unsigned int
739 solver_problem_count(Solver *solv)
740 {
741   return solv->problems.count / 2;
742 }
743
744 Id
745 solver_next_problem(Solver *solv, Id problem)
746 {
747   if (!problem)
748     return solv->problems.count ? 1 : 0;
749   return (problem + 1) * 2 - 1 < solv->problems.count ? problem + 1 : 0;
750 }
751
752 unsigned int
753 solver_solution_count(Solver *solv, Id problem)
754 {
755   Id solidx = solv->problems.elements[problem * 2 - 1];
756   if (solv->solutions.elements[solidx] < 0)
757     create_solutions(solv, problem, solidx);
758   return solv->solutions.elements[solidx];
759 }
760
761 Id
762 solver_next_solution(Solver *solv, Id problem, Id solution)
763 {
764   Id solidx = solv->problems.elements[problem * 2 - 1];
765   if (solv->solutions.elements[solidx] < 0)
766     create_solutions(solv, problem, solidx);
767   return solv->solutions.elements[solidx + solution + 1] ? solution + 1 : 0;
768 }
769
770 unsigned int
771 solver_solutionelement_count(Solver *solv, Id problem, Id solution)
772 {
773   Id solidx = solv->problems.elements[problem * 2 - 1];
774   solidx = solv->solutions.elements[solidx + solution];
775   return solv->solutions.elements[solidx];
776 }
777
778 Id
779 solver_solutionelement_internalid(Solver *solv, Id problem, Id solution)
780 {
781   Id solidx = solv->problems.elements[problem * 2 - 1];
782   solidx = solv->solutions.elements[solidx + solution];
783   return solv->solutions.elements[solidx + 2 * solv->solutions.elements[solidx] + 3];
784 }
785
786 Id
787 solver_solutionelement_extrajobflags(Solver *solv, Id problem, Id solution)
788 {
789   Id solidx = solv->problems.elements[problem * 2 - 1];
790   solidx = solv->solutions.elements[solidx + solution];
791   return solv->solutions.elements[solidx + 2 * solv->solutions.elements[solidx] + 4];
792 }
793
794
795 /*
796  *  return the next item of the proposed solution
797  *  here are the possibilities for p / rp and what
798  *  the solver expects the application to do:
799  *    p                             rp
800  *  -------------------------------------------------------
801  *    SOLVER_SOLUTION_INFARCH       pkgid
802  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
803  *    SOLVER_SOLUTION_DISTUPGRADE   pkgid
804  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
805  *    SOLVER_SOLUTION_BEST          pkgid
806  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
807  *    SOLVER_SOLUTION_JOB           jobidx
808  *    -> remove job (jobidx - 1, jobidx) from job queue
809  *    SOLVER_SOLUTION_POOLJOB       jobidx
810  *    -> remove job (jobidx - 1, jobidx) from pool job queue
811  *    pkgid (> 0)                   0
812  *    -> add (SOLVER_ERASE|SOLVER_SOLVABLE, p) to the job
813  *    pkgid (> 0)                   pkgid (> 0)
814  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
815  *       (this will replace package p)
816  *
817  * Thus, the solver will either ask the application to remove
818  * a specific job from the job queue, or ask to add an install/erase
819  * job to it.
820  *
821  */
822
823 Id
824 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
825 {
826   Id solidx = solv->problems.elements[problem * 2 - 1];
827   solidx = solv->solutions.elements[solidx + solution];
828   if (!solidx)
829     return 0;
830   solidx += 1 + element * 2;
831   if (!solv->solutions.elements[solidx] && !solv->solutions.elements[solidx + 1])
832     return 0;
833   *p = solv->solutions.elements[solidx];
834   *rp = solv->solutions.elements[solidx + 1];
835   return element + 1;
836 }
837
838 void
839 solver_take_solutionelement(Solver *solv, Id p, Id rp, Id extrajobflags, Queue *job)
840 {
841   int i;
842
843   if (p == SOLVER_SOLUTION_POOLJOB)
844     {
845       solv->pool->pooljobs.elements[rp - 1] = SOLVER_NOOP;
846       solv->pool->pooljobs.elements[rp] = 0;
847       return;
848     }
849   if (p == SOLVER_SOLUTION_JOB)
850     {
851       job->elements[rp - 1] = SOLVER_NOOP;
852       job->elements[rp] = 0;
853       return;
854     }
855   if (rp <= 0 && p <= 0)
856     return;     /* just in case */
857   if (rp > 0)
858     p = SOLVER_INSTALL|SOLVER_SOLVABLE|SOLVER_NOTBYUSER|extrajobflags;
859   else
860     {
861       rp = p;
862       p = SOLVER_ERASE|SOLVER_SOLVABLE|extrajobflags;
863     }
864   for (i = 0; i < job->count; i += 2)
865     if (job->elements[i] == p && job->elements[i + 1] == rp)
866       return;
867   queue_push2(job, p, rp);
868 }
869
870 void
871 solver_take_solution(Solver *solv, Id problem, Id solution, Queue *job)
872 {
873   Id p, rp, element = 0;
874   Id extrajobflags = solver_solutionelement_extrajobflags(solv, problem, solution);
875   while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
876     solver_take_solutionelement(solv, p, rp, extrajobflags, job);
877 }
878
879
880 /*-------------------------------------------------------------------
881  *
882  * find problem rule
883  */
884
885 static void
886 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp, Map *rseen)
887 {
888   Id rid, d;
889   Id lreqr, lconr, lsysr, ljobr;
890   Rule *r;
891   Id jobassert = 0;
892   int i, reqset = 0;    /* 0: unset, 1: installed, 2: jobassert, 3: assert */
893   int conset = 0;       /* 0: unset, 1: installed */
894
895   /* find us a jobassert rule */
896   for (i = idx; (rid = solv->learnt_pool.elements[i]) != 0; i++)
897     {
898       if (rid < solv->jobrules || rid >= solv->jobrules_end)
899         continue;
900       r = solv->rules + rid;
901       d = r->d < 0 ? -r->d - 1 : r->d;
902       if (!d && r->w2 == 0 && r->p > 0)
903         {
904           jobassert = r->p;
905           break;
906         }
907     }
908
909   /* the problem rules are somewhat ordered from "near to the problem" to
910    * "near to the job" */
911   lreqr = lconr = lsysr = ljobr = 0;
912   while ((rid = solv->learnt_pool.elements[idx++]) != 0)
913     {
914       assert(rid > 0);
915       if (rid >= solv->learntrules)
916         {
917           if (MAPTST(rseen, rid - solv->learntrules))
918             continue;
919           MAPSET(rseen, rid - solv->learntrules);
920           findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr, rseen);
921         }
922       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) || (rid >= solv->yumobsrules && rid <= solv->yumobsrules_end))
923         {
924           if (!*jobrp)
925             *jobrp = rid;
926         }
927       else if (rid >= solv->updaterules && rid < solv->updaterules_end)
928         {
929           if (!*sysrp)
930             *sysrp = rid;
931         }
932       else
933         {
934           assert(rid < solv->pkgrules_end);
935           r = solv->rules + rid;
936           d = r->d < 0 ? -r->d - 1 : r->d;
937           if (!d && r->w2 < 0)
938             {
939               /* prefer conflicts of installed packages */
940               if (solv->installed && !conset)
941                 {
942                   if (r->p < 0 && (solv->pool->solvables[-r->p].repo == solv->installed ||
943                                   solv->pool->solvables[-r->w2].repo == solv->installed))
944                     {
945                       *conrp = rid;
946                       conset = 1;
947                     }
948                 }
949               if (!*conrp)
950                 *conrp = rid;
951             }
952           else
953             {
954               if (!d && r->w2 == 0 && reqset < 3)
955                 {
956                   if (*reqrp > 0 && r->p < -1)
957                     {
958                       Pool *pool = solv->pool;
959                       Id op = -solv->rules[*reqrp].p;
960                       if (op > 1 && pool->solvables[op].arch != pool->solvables[-r->p].arch &&
961                           pool->solvables[op].arch != pool->noarchid &&
962                           pool->solvables[-r->p].arch != pool->noarchid)
963                         continue;       /* different arch, skip */
964                     }
965                   /* prefer assertions */
966                   *reqrp = rid;
967                   reqset = 3;
968                 }
969               else if (jobassert && r->p == -jobassert)
970                 {
971                   /* prefer rules of job assertions */
972                   *reqrp = rid;
973                   reqset = 2;
974                 }
975               else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed && reqset <= 1)
976                 {
977                   /* prefer rules of job installed package so that the user doesn't get confused by strange packages */
978                   *reqrp = rid;
979                   reqset = 1;
980                 }
981               else if (!*reqrp)
982                 *reqrp = rid;
983             }
984         }
985     }
986   if (!*reqrp && lreqr)
987     *reqrp = lreqr;
988   if (!*conrp && lconr)
989     *conrp = lconr;
990   if (!*jobrp && ljobr)
991     *jobrp = ljobr;
992   if (!*sysrp && lsysr)
993     *sysrp = lsysr;
994 }
995
996 /*
997  * find problem rule
998  *
999  * search for a rule that describes the problem to the
1000  * user. Actually a pretty hopeless task that may leave the user
1001  * puzzled. To get all of the needed information use
1002  * solver_findallproblemrules() instead.
1003  */
1004
1005 Id
1006 solver_findproblemrule(Solver *solv, Id problem)
1007 {
1008   Id reqr, conr, sysr, jobr;
1009   Id idx = solv->problems.elements[2 * problem - 2];
1010   Map rseen;
1011   reqr = conr = sysr = jobr = 0;
1012   map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1013   findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr, &rseen);
1014   map_free(&rseen);
1015   /* check if the request is about a not-installed package requiring a installed
1016    * package conflicting with the non-installed package. In that case return the conflict */
1017   if (reqr && conr && solv->installed && solv->rules[reqr].p < 0 && solv->rules[conr].p < 0 && solv->rules[conr].w2 < 0)
1018     {
1019       Pool *pool = solv->pool;
1020       Solvable *s  = pool->solvables - solv->rules[reqr].p;
1021       Solvable *s1 = pool->solvables - solv->rules[conr].p;
1022       Solvable *s2 = pool->solvables - solv->rules[conr].w2;
1023       Id cp = 0;
1024       if (s == s1 && s2->repo == solv->installed)
1025         cp = -solv->rules[conr].w2;
1026       else if (s == s2 && s1->repo == solv->installed)
1027         cp = -solv->rules[conr].p;
1028       if (cp && s1->name != s2->name && s->repo != solv->installed)
1029         {
1030           Id p, pp;
1031           Rule *r = solv->rules + reqr;
1032           FOR_RULELITERALS(p, pp, r)
1033             if (p == cp)
1034               return conr;
1035         }
1036     }
1037   if (reqr)
1038     return reqr;        /* some requires */
1039   if (conr)
1040     return conr;        /* some conflict */
1041   if (sysr)
1042     return sysr;        /* an update rule */
1043   if (jobr)
1044     return jobr;        /* a user request */
1045   assert(0);
1046   return 0;
1047 }
1048
1049 /*-------------------------------------------------------------------*/
1050
1051 static void
1052 findallproblemrules_internal(Solver *solv, Id idx, Queue *rules, Map *rseen)
1053 {
1054   Id rid;
1055   while ((rid = solv->learnt_pool.elements[idx++]) != 0)
1056     {
1057       if (rid >= solv->learntrules)
1058         {
1059           if (MAPTST(rseen, rid - solv->learntrules))
1060             continue;
1061           MAPSET(rseen, rid - solv->learntrules);
1062           findallproblemrules_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], rules, rseen);
1063           continue;
1064         }
1065       queue_pushunique(rules, rid);
1066     }
1067 }
1068
1069 /*
1070  * find all problem rule
1071  *
1072  * return all rules that lead to the problem. This gives the user
1073  * all of the information to understand the problem, but the result
1074  * can be a large number of rules.
1075  */
1076
1077 void
1078 solver_findallproblemrules(Solver *solv, Id problem, Queue *rules)
1079 {
1080   Map rseen;
1081   queue_empty(rules);
1082   map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1083   findallproblemrules_internal(solv, solv->problems.elements[2 * problem - 2], rules, &rseen);
1084   map_free(&rseen);
1085 }
1086
1087 const char *
1088 solver_problemruleinfo2str(Solver *solv, SolverRuleinfo type, Id source, Id target, Id dep)
1089 {
1090   Pool *pool = solv->pool;
1091   char *s;
1092   switch (type)
1093     {
1094     case SOLVER_RULE_DISTUPGRADE:
1095       return pool_tmpjoin(pool, pool_solvid2str(pool, source), " does not belong to a distupgrade repository", 0);
1096     case SOLVER_RULE_INFARCH:
1097       return pool_tmpjoin(pool, pool_solvid2str(pool, source), " has inferior architecture", 0);
1098     case SOLVER_RULE_UPDATE:
1099       return pool_tmpjoin(pool, "problem with installed package ", pool_solvid2str(pool, source), 0);
1100     case SOLVER_RULE_JOB:
1101       return "conflicting requests";
1102     case SOLVER_RULE_JOB_UNSUPPORTED:
1103       return "unsupported request";
1104     case SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP:
1105       return pool_tmpjoin(pool, "nothing provides requested ", pool_dep2str(pool, dep), 0);
1106     case SOLVER_RULE_JOB_UNKNOWN_PACKAGE:
1107       return pool_tmpjoin(pool, "package ", pool_dep2str(pool, dep), " does not exist");
1108     case SOLVER_RULE_JOB_PROVIDED_BY_SYSTEM:
1109       return pool_tmpjoin(pool, pool_dep2str(pool, dep), " is provided by the system", 0);
1110     case SOLVER_RULE_PKG:
1111       return "some dependency problem";
1112     case SOLVER_RULE_BEST:
1113       if (source > 0)
1114         return pool_tmpjoin(pool, "cannot install the best update candidate for package ", pool_solvid2str(pool, source), 0);
1115      return "cannot install the best candidate for the job";
1116     case SOLVER_RULE_PKG_NOT_INSTALLABLE:
1117       return pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), " is not installable");
1118     case SOLVER_RULE_PKG_NOTHING_PROVIDES_DEP:
1119       s = pool_tmpjoin(pool, "nothing provides ", pool_dep2str(pool, dep), 0);
1120       return pool_tmpappend(pool, s, " needed by ", pool_solvid2str(pool, source));
1121     case SOLVER_RULE_PKG_SAME_NAME:
1122       s = pool_tmpjoin(pool, "cannot install both ", pool_solvid2str(pool, source), 0);
1123       return pool_tmpappend(pool, s, " and ", pool_solvid2str(pool, target));
1124     case SOLVER_RULE_PKG_CONFLICTS:
1125       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), 0);
1126       s = pool_tmpappend(pool, s, " conflicts with ", pool_dep2str(pool, dep));
1127       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1128     case SOLVER_RULE_PKG_OBSOLETES:
1129       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), 0);
1130       s = pool_tmpappend(pool, s, " obsoletes ", pool_dep2str(pool, dep));
1131       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1132     case SOLVER_RULE_PKG_INSTALLED_OBSOLETES:
1133       s = pool_tmpjoin(pool, "installed package ", pool_solvid2str(pool, source), 0);
1134       s = pool_tmpappend(pool, s, " obsoletes ", pool_dep2str(pool, dep));
1135       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1136     case SOLVER_RULE_PKG_IMPLICIT_OBSOLETES:
1137       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), 0);
1138       s = pool_tmpappend(pool, s, " implicitly obsoletes ", pool_dep2str(pool, dep));
1139       return pool_tmpappend(pool, s, " provided by ", pool_solvid2str(pool, target));
1140     case SOLVER_RULE_PKG_REQUIRES:
1141       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), " requires ");
1142       return pool_tmpappend(pool, s, pool_dep2str(pool, dep), ", but none of the providers can be installed");
1143     case SOLVER_RULE_PKG_SELF_CONFLICT:
1144       s = pool_tmpjoin(pool, "package ", pool_solvid2str(pool, source), " conflicts with ");
1145       return pool_tmpappend(pool, s, pool_dep2str(pool, dep), " provided by itself");
1146     case SOLVER_RULE_YUMOBS:
1147       s = pool_tmpjoin(pool, "both package ", pool_solvid2str(pool, source), " and ");
1148       s = pool_tmpjoin(pool, s, pool_solvid2str(pool, target), " obsolete ");
1149       return pool_tmpappend(pool, s, pool_dep2str(pool, dep), 0);
1150     default:
1151       return "bad problem rule type";
1152     }
1153 }
1154
1155 /* convenience function */
1156 const char *
1157 solver_problem2str(Solver *solv, Id problem)
1158 {
1159   Id type, source, target, dep;
1160   Id r = solver_findproblemrule(solv, problem);
1161   if (!r)
1162     return "no problem rule?";
1163   type = solver_ruleinfo(solv, r, &source, &target, &dep);
1164   return solver_problemruleinfo2str(solv, type, source, target, dep);
1165 }
1166
1167 const char *
1168 solver_solutionelement2str(Solver *solv, Id p, Id rp)
1169 {
1170   Pool *pool = solv->pool;
1171   if (p == SOLVER_SOLUTION_JOB || p == SOLVER_SOLUTION_POOLJOB)
1172     {
1173       Id how, what;
1174       if (p == SOLVER_SOLUTION_JOB)
1175         rp += solv->pooljobcnt;
1176       how = solv->job.elements[rp - 1];
1177       what = solv->job.elements[rp];
1178       return pool_tmpjoin(pool, "do not ask to ", pool_job2str(pool, how, what, 0), 0);
1179     }
1180   else if (p == SOLVER_SOLUTION_INFARCH)
1181     {
1182       Solvable *s = pool->solvables + rp;
1183       if (solv->installed && s->repo == solv->installed)
1184         return pool_tmpjoin(pool, "keep ", pool_solvable2str(pool, s), " despite the inferior architecture");
1185       else
1186         return pool_tmpjoin(pool, "install ", pool_solvable2str(pool, s), " despite the inferior architecture");
1187     }
1188   else if (p == SOLVER_SOLUTION_DISTUPGRADE)
1189     {
1190       Solvable *s = pool->solvables + rp;
1191       if (solv->installed && s->repo == solv->installed)
1192         return pool_tmpjoin(pool, "keep obsolete ", pool_solvable2str(pool, s), 0);
1193       else
1194         return pool_tmpjoin(pool, "install ", pool_solvable2str(pool, s), " from excluded repository");
1195     }
1196   else if (p == SOLVER_SOLUTION_BEST)
1197     {
1198       Solvable *s = pool->solvables + rp;
1199       if (solv->installed && s->repo == solv->installed)
1200         return pool_tmpjoin(pool, "keep old ", pool_solvable2str(pool, s), 0);
1201       else
1202         return pool_tmpjoin(pool, "install ", pool_solvable2str(pool, s), " despite the old version");
1203     }
1204   else if (p > 0 && rp == 0)
1205     return pool_tmpjoin(pool, "allow deinstallation of ", pool_solvid2str(pool, p), 0);
1206   else if (p > 0 && rp > 0)
1207     {
1208       const char *sp = pool_solvid2str(pool, p);
1209       const char *srp = pool_solvid2str(pool, rp);
1210       const char *str = pool_tmpjoin(pool, "allow replacement of ", sp, 0);
1211       return pool_tmpappend(pool, str, " with ", srp);
1212     }
1213   else
1214     return "bad solution element";
1215 }
1216