- clean up update/feature rule handling in allowuninstall case. Automatically add...
[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 }
164
165
166 /*-------------------------------------------------------------------
167  * 
168  * refine_suggestion
169  * 
170  * at this point, all rules that led to conflicts are disabled.
171  * we re-enable all rules of a problem set but rule "sug", then
172  * continue to disable more rules until there as again a solution.
173  */
174
175 /* FIXME: think about conflicting assertions */
176
177 static void
178 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined, int essentialok)
179 {
180   Pool *pool = solv->pool;
181   int i, j;
182   Id v;
183   Queue disabled;
184   int disabledcnt;
185
186   IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
187     {
188       POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "refine_suggestion start\n");
189       for (i = 0; problem[i]; i++)
190         {
191           if (problem[i] == sug)
192             POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "=> ");
193           solver_printproblem(solv, problem[i]);
194         }
195     }
196   queue_empty(refined);
197   if (!essentialok && sug < 0 && (solv->job.elements[-sug - 1] & SOLVER_ESSENTIAL) != 0)
198     return;
199   queue_init(&disabled);
200   queue_push(refined, sug);
201
202   /* re-enable all problem rules with the exception of "sug"(gestion) */
203   solver_reset(solv);
204
205   for (i = 0; problem[i]; i++)
206     if (problem[i] != sug)
207       solver_enableproblem(solv, problem[i]);
208
209   if (sug < 0)
210     solver_reenablepolicyrules(solv, -sug);
211   else if (sug >= solv->updaterules && sug < solv->updaterules_end)
212     {
213       /* enable feature rule */
214       Rule *r = solv->rules + solv->featurerules + (sug - solv->updaterules);
215       if (r->p)
216         solver_enablerule(solv, r);
217     }
218
219   enableweakrules(solv);
220
221   for (;;)
222     {
223       int njob, nfeature, nupdate, pass;
224       queue_empty(&solv->problems);
225       solver_reset(solv);
226
227       if (!solv->problems.count)
228         solver_run_sat(solv, 0, 0);
229
230       if (!solv->problems.count)
231         {
232           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "no more problems!\n");
233           break;                /* great, no more problems */
234         }
235       disabledcnt = disabled.count;
236       /* start with 1 to skip over proof index */
237       njob = nfeature = nupdate = 0;
238       for (pass = 0; pass < 2; pass++)
239         {
240           for (i = 1; i < solv->problems.count - 1; i++)
241             {
242               /* ignore solutions in refined */
243               v = solv->problems.elements[i];
244               if (v == 0)
245                 break;  /* end of problem reached */
246               if (sug != v)
247                 {
248                   /* check if v is in the given problems list
249                    * we allow disabling all problem rules *after* sug in
250                    * pass 2, to prevent getting the same solution twice */
251                   for (j = 0; problem[j]; j++)
252                     if (problem[j] == v || (pass && problem[j] == sug))
253                       break;
254                   if (problem[j] == v)
255                     continue;
256                 }
257               if (v >= solv->featurerules && v < solv->featurerules_end)
258                 nfeature++;
259               else if (v > 0)
260                 nupdate++;
261               else
262                 {
263                   if (!essentialok && (solv->job.elements[-v -1] & SOLVER_ESSENTIAL) != 0)
264                     continue;   /* not that one! */
265                   njob++;
266                 }
267               queue_push(&disabled, v);
268             }
269           if (disabled.count != disabledcnt)
270             break;
271         }
272       if (disabled.count == disabledcnt)
273         {
274           /* no solution found, this was an invalid suggestion! */
275           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "no solution found!\n");
276           refined->count = 0;
277           break;
278         }
279       if (!njob && nupdate && nfeature)
280         {
281           /* got only update rules, filter out feature rules */
282           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "throwing away feature rules\n");
283           for (i = j = disabledcnt; i < disabled.count; i++)
284             {
285               v = disabled.elements[i];
286               if (v < solv->featurerules || v >= solv->featurerules_end)
287                 disabled.elements[j++] = v;
288             }
289           disabled.count = j;
290           nfeature = 0;
291         }
292       if (disabled.count == disabledcnt + 1)
293         {
294           /* just one suggestion, add it to refined list */
295           v = disabled.elements[disabledcnt];
296           if (!nfeature)
297             queue_push(refined, v);     /* do not record feature rules */
298           solver_disableproblem(solv, v);
299           if (v >= solv->updaterules && v < solv->updaterules_end)
300             {
301               Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
302               if (r->p)
303                 solver_enablerule(solv, r);     /* enable corresponding feature rule */
304             }
305           if (v < 0)
306             solver_reenablepolicyrules(solv, -v);
307         }
308       else
309         {
310           /* more than one solution, disable all */
311           /* do not push anything on refine list, as we do not know which solution to choose */
312           /* thus, the user will get another problem if he selects this solution, where he
313            * can choose the right one */
314           IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
315             {
316               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "more than one solution found:\n");
317               for (i = disabledcnt; i < disabled.count; i++)
318                 solver_printproblem(solv, disabled.elements[i]);
319             }
320           for (i = disabledcnt; i < disabled.count; i++)
321             {
322               v = disabled.elements[i];
323               solver_disableproblem(solv, v);
324               if (v >= solv->updaterules && v < solv->updaterules_end)
325                 {
326                   Rule *r = solv->rules + (v - solv->updaterules + solv->featurerules);
327                   if (r->p)
328                     solver_enablerule(solv, r);
329                 }
330             }
331         }
332     }
333   /* all done, get us back into the same state as before */
334   /* enable refined rules again */
335   for (i = 0; i < disabled.count; i++)
336     solver_enableproblem(solv, disabled.elements[i]);
337   queue_free(&disabled);
338   /* reset policy rules */
339   for (i = 0; problem[i]; i++)
340     solver_enableproblem(solv, problem[i]);
341   solver_disablepolicyrules(solv);
342   /* disable problem rules again */
343   for (i = 0; problem[i]; i++)
344     solver_disableproblem(solv, problem[i]);
345   POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "refine_suggestion end\n");
346 }
347
348
349 /*-------------------------------------------------------------------
350  * sorting helper for problems
351  *
352  * bring update rules before job rules
353  * make essential job rules last
354  */
355
356 static int
357 problems_sortcmp(const void *ap, const void *bp, void *dp)
358 {
359   Queue *job = dp;
360   Id a = *(Id *)ap, b = *(Id *)bp;
361   if (a < 0 && b > 0)
362     return 1;
363   if (a > 0 && b < 0)
364     return -1;
365   if (a < 0 && b < 0)
366     {
367       int af = job->elements[-a - 1] & SOLVER_ESSENTIAL;
368       int bf = job->elements[-b - 1] & SOLVER_ESSENTIAL;
369       int x = af - bf;
370       if (x)
371         return x;
372     }
373   return a - b;
374 }
375
376 /*
377  * convert a solution rule into a job modifier
378  */
379 static void
380 convertsolution(Solver *solv, Id why, Queue *solutionq)
381 {
382   Pool *pool = solv->pool;
383   if (why < 0)
384     {
385       queue_push(solutionq, 0);
386       queue_push(solutionq, -why);
387       return;
388     }
389   if (why >= solv->infarchrules && why < solv->infarchrules_end)
390     {
391       Id p, name;
392       /* infarch rule, find replacement */
393       assert(solv->rules[why].p < 0);
394       name = pool->solvables[-solv->rules[why].p].name;
395       while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
396         why--;
397       p = 0;
398       for (; why < solv->infarchrules_end && pool->solvables[-solv->rules[why].p].name == name; why++)
399         if (solv->decisionmap[-solv->rules[why].p] > 0)
400           {
401             p = -solv->rules[why].p;
402             break;
403           }
404       if (!p)
405         return;         /* false alarm */
406       queue_push(solutionq, SOLVER_SOLUTION_INFARCH);
407       queue_push(solutionq, p);
408       return;
409     }
410   if (why >= solv->duprules && why < solv->duprules_end)
411     {
412       Id p, name;
413       /* dist upgrade rule, find replacement */
414       assert(solv->rules[why].p < 0);
415       name = pool->solvables[-solv->rules[why].p].name;
416       while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
417         why--;
418       p = 0;
419       for (; why < solv->duprules_end && pool->solvables[-solv->rules[why].p].name == name; why++)
420         if (solv->decisionmap[-solv->rules[why].p] > 0)
421           {
422             p = -solv->rules[why].p;
423             break;
424           }
425       if (!p)
426         return;         /* false alarm */
427       queue_push(solutionq, SOLVER_SOLUTION_DISTUPGRADE);
428       queue_push(solutionq, p);
429       return;
430     }
431   if (why >= solv->updaterules && why < solv->updaterules_end)
432     {
433       /* update rule, find replacement package */
434       Id p, *dp, rp = 0;
435       Rule *rr;
436
437       assert(why >= solv->updaterules && why < solv->updaterules_end);
438       /* check if this is a false positive, i.e. the update rule is fulfilled */
439       rr = solv->rules + why;
440       FOR_RULELITERALS(p, dp, rr)
441         if (p > 0 && solv->decisionmap[p] > 0)
442           break;
443       if (p)
444         return;         /* false alarm */
445
446       p = solv->installed->start + (why - solv->updaterules);
447       rr = solv->rules + solv->featurerules + (why - solv->updaterules);
448       if (!rr->p)
449         rr = solv->rules + why;
450       if (solv->dupmap_all && solv->rules[why].p != p && solv->decisionmap[p] > 0)
451         {
452           /* distupgrade case, allow to keep old package */
453           queue_push(solutionq, SOLVER_SOLUTION_DISTUPGRADE);
454           queue_push(solutionq, p);
455           return;
456         }
457       if (solv->decisionmap[p] > 0)
458         return;         /* false alarm, turned out we can keep the package */
459       if (rr->w2)
460         {
461           int mvrp = 0;         /* multi-version replacement */
462           FOR_RULELITERALS(rp, dp, rr)
463             {
464               if (rp > 0 && solv->decisionmap[rp] > 0 && pool->solvables[rp].repo != solv->installed)
465                 {
466                   mvrp = rp;
467                   if (!(solv->noobsoletes.size && MAPTST(&solv->noobsoletes, rp)))
468                     break;
469                 }
470             }
471           if (!rp && mvrp)
472             {
473               /* found only multi-version replacements */
474               /* have to split solution into two parts */
475               queue_push(solutionq, p);
476               queue_push(solutionq, mvrp);
477             }
478         }
479       queue_push(solutionq, p);
480       queue_push(solutionq, rp);
481       return;
482     }
483 }
484
485 /*
486  * convert problem data into a form usable for refining.
487  * Returns the number of problems.
488  */
489 int
490 solver_prepare_solutions(Solver *solv)
491 {
492   int i, j = 1, idx;
493
494   if (!solv->problems.count)
495     return 0;
496   queue_empty(&solv->solutions);
497   queue_push(&solv->solutions, 0);      /* dummy so idx is always nonzero */
498   idx = solv->solutions.count;
499   queue_push(&solv->solutions, -1);     /* unrefined */
500   /* proofidx stays in position, thus we start with 1 */
501   for (i = 1; i < solv->problems.count; i++) 
502     {   
503       Id p = solv->problems.elements[i];
504       queue_push(&solv->solutions, p); 
505       if (p) 
506         continue;
507       /* end of problem reached */
508       solv->problems.elements[j++] = idx; 
509       if (i + 1 >= solv->problems.count)
510         break;
511       /* start another problem */
512       solv->problems.elements[j++] = solv->problems.elements[++i];  /* copy proofidx */
513       idx = solv->solutions.count;
514       queue_push(&solv->solutions, -1); /* unrefined */
515     }
516   solv->problems.count = j;  
517   return j / 2;
518 }
519
520 /*
521  * refine the simple solution rule list provided by
522  * the solver into multiple lists of job modifiers.
523  */
524 static void
525 create_solutions(Solver *solv, int probnr, int solidx)
526 {
527   Pool *pool = solv->pool;
528   Queue redoq;
529   Queue problem, solution, problems_save;
530   int i, j, nsol;
531   int essentialok;
532   unsigned int now;
533   int oldmistakes = solv->cleandeps_mistakes ? solv->cleandeps_mistakes->count : 0;
534   Id extraflags = -1;
535
536   now = solv_timems(0);
537   queue_init(&redoq);
538   /* save decisionq, decisionq_why, decisionmap */
539   for (i = 0; i < solv->decisionq.count; i++)
540     {
541       Id p = solv->decisionq.elements[i];
542       queue_push(&redoq, p);
543       queue_push(&redoq, solv->decisionq_why.elements[i]);
544       queue_push(&redoq, solv->decisionmap[p > 0 ? p : -p]);
545     }
546   /* save problems queue */
547   problems_save = solv->problems;
548   memset(&solv->problems, 0, sizeof(solv->problems));
549
550   /* extract problem from queue */
551   queue_init(&problem);
552   for (i = solidx + 1; i < solv->solutions.count; i++)
553     {
554       Id v = solv->solutions.elements[i];
555       if (!v)
556         break;
557       queue_push(&problem, v);
558       if (v < 0)
559         extraflags &= solv->job.elements[-v - 1];
560     }
561   if (extraflags == -1)
562     extraflags = 0;
563   if (problem.count > 1)
564     solv_sort(problem.elements, problem.count, sizeof(Id), problems_sortcmp, &solv->job);
565   queue_push(&problem, 0);      /* mark end for refine_suggestion */
566   problem.count--;
567 #if 0
568   for (i = 0; i < problem.count; i++)
569     printf("PP %d %d\n", i, problem.elements[i]);
570 #endif
571
572   /* refine each solution element */
573   nsol = 0;
574   essentialok = 0;
575   queue_init(&solution);
576   for (i = 0; i < problem.count; i++)
577     {
578       int solstart = solv->solutions.count;
579       refine_suggestion(solv, problem.elements, problem.elements[i], &solution, essentialok);
580       queue_push(&solv->solutions, 0);  /* reserve room for number of elements */
581       for (j = 0; j < solution.count; j++)
582         convertsolution(solv, solution.elements[j], &solv->solutions);
583       if (solv->solutions.count == solstart + 1)
584         {
585           solv->solutions.count--;      /* this one did not work out */
586           if (nsol || i + 1 < problem.count)
587             continue;                   /* got one or still hope */
588           if (!essentialok)
589             {
590               /* nothing found, start over */
591               POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "nothing found, re-run with essentialok = 1\n");
592               essentialok = 1;
593               i = -1;
594               continue;
595             }
596           /* this is bad, we found no solution */
597           /* for now just offer a rule */
598           POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "nothing found, already did essentialok, fake it\n");
599           queue_push(&solv->solutions, 0);
600           for (j = 0; j < problem.count; j++)
601             {
602               convertsolution(solv, problem.elements[j], &solv->solutions);
603               if (solv->solutions.count > solstart + 1)
604                 break;
605             }
606           if (solv->solutions.count == solstart + 1)
607             {
608               solv->solutions.count--;
609               continue;         /* sorry */
610             }
611         }
612       /* patch in number of solution elements */
613       solv->solutions.elements[solstart] = (solv->solutions.count - (solstart + 1)) / 2;
614       queue_push(&solv->solutions, 0);  /* add end marker */
615       queue_push(&solv->solutions, 0);  /* add end marker */
616       queue_push(&solv->solutions, problem.elements[i]);        /* just for bookkeeping */
617       queue_push(&solv->solutions, extraflags & SOLVER_CLEANDEPS);      /* our extraflags */
618       solv->solutions.elements[solidx + 1 + nsol++] = solstart;
619     }
620   solv->solutions.elements[solidx + 1 + nsol] = 0;      /* end marker */
621   solv->solutions.elements[solidx] = nsol;
622   queue_free(&problem);
623   queue_free(&solution);
624
625   /* restore decisions */
626   memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
627   queue_empty(&solv->decisionq);
628   queue_empty(&solv->decisionq_why);
629   for (i = 0; i < redoq.count; i += 3)
630     {
631       Id p = redoq.elements[i];
632       queue_push(&solv->decisionq, p);
633       queue_push(&solv->decisionq_why, redoq.elements[i + 1]);
634       solv->decisionmap[p > 0 ? p : -p] = redoq.elements[i + 2];
635     }
636   queue_free(&redoq);
637   /* restore problems */
638   queue_free(&solv->problems);
639   solv->problems = problems_save;
640
641   if (solv->cleandeps_mistakes)
642     {
643       if (oldmistakes)
644         queue_truncate(solv->cleandeps_mistakes, oldmistakes);
645       else
646         {
647           queue_free(solv->cleandeps_mistakes);
648           solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
649         }
650     }
651     
652   POOL_DEBUG(SOLV_DEBUG_STATS, "create_solutions for problem #%d took %d ms\n", probnr, solv_timems(now));
653 }
654
655
656 /**************************************************************************/
657
658 unsigned int
659 solver_problem_count(Solver *solv)
660 {
661   return solv->problems.count / 2;
662 }
663
664 Id
665 solver_next_problem(Solver *solv, Id problem)
666 {
667   if (!problem)
668     return solv->problems.count ? 1 : 0;
669   return (problem + 1) * 2 - 1 < solv->problems.count ? problem + 1 : 0;
670 }
671
672 unsigned int
673 solver_solution_count(Solver *solv, Id problem)
674 {
675   Id solidx = solv->problems.elements[problem * 2 - 1];
676   if (solv->solutions.elements[solidx] < 0)
677     create_solutions(solv, problem, solidx);
678   return solv->solutions.elements[solidx];
679 }
680
681 Id
682 solver_next_solution(Solver *solv, Id problem, Id solution)
683 {
684   Id solidx = solv->problems.elements[problem * 2 - 1];
685   if (solv->solutions.elements[solidx] < 0)
686     create_solutions(solv, problem, solidx);
687   return solv->solutions.elements[solidx + solution + 1] ? solution + 1 : 0;
688 }
689
690 unsigned int
691 solver_solutionelement_count(Solver *solv, Id problem, Id solution)
692 {
693   Id solidx = solv->problems.elements[problem * 2 - 1];
694   solidx = solv->solutions.elements[solidx + solution];
695   return solv->solutions.elements[solidx];
696 }
697
698 Id
699 solver_solutionelement_internalid(Solver *solv, Id problem, Id solution)
700 {
701   Id solidx = solv->problems.elements[problem * 2 - 1];
702   solidx = solv->solutions.elements[solidx + solution];
703   return solv->solutions.elements[solidx + 2 * solv->solutions.elements[solidx] + 3];
704 }
705
706 Id
707 solver_solutionelement_extrajobflags(Solver *solv, Id problem, Id solution)
708 {
709   Id solidx = solv->problems.elements[problem * 2 - 1];
710   solidx = solv->solutions.elements[solidx + solution];
711   return solv->solutions.elements[solidx + 2 * solv->solutions.elements[solidx] + 4];
712 }
713
714
715 /*
716  *  return the next item of the proposed solution
717  *  here are the possibilities for p / rp and what
718  *  the solver expects the application to do:
719  *    p                             rp
720  *  -------------------------------------------------------
721  *    SOLVER_SOLUTION_INFARCH       pkgid
722  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
723  *    SOLVER_SOLUTION_DISTUPGRADE   pkgid
724  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
725  *    SOLVER_SOLUTION_JOB           jobidx
726  *    -> remove job (jobidx - 1, jobidx) from job queue
727  *    pkgid (> 0)                   0
728  *    -> add (SOLVER_ERASE|SOLVER_SOLVABLE, p) to the job
729  *    pkgid (> 0)                   pkgid (> 0)
730  *    -> add (SOLVER_INSTALL|SOLVER_SOLVABLE, rp) to the job
731  *       (this will replace package p)
732  *         
733  * Thus, the solver will either ask the application to remove
734  * a specific job from the job queue, or ask to add an install/erase
735  * job to it.
736  *
737  */
738
739 Id
740 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
741 {
742   Id solidx = solv->problems.elements[problem * 2 - 1];
743   solidx = solv->solutions.elements[solidx + solution];
744   if (!solidx)
745     return 0;
746   solidx += 1 + element * 2;
747   if (!solv->solutions.elements[solidx] && !solv->solutions.elements[solidx + 1])
748     return 0;
749   *p = solv->solutions.elements[solidx];
750   *rp = solv->solutions.elements[solidx + 1];
751   return element + 1;
752 }
753
754 void
755 solver_take_solutionelement(Solver *solv, Id p, Id rp, Id extrajobflags, Queue *job)
756 {
757   int i;
758
759   if (p == SOLVER_SOLUTION_JOB)
760     {
761       job->elements[rp - 1] = SOLVER_NOOP;
762       job->elements[rp] = 0;
763       return;
764     }
765   if (rp <= 0 && p <= 0)
766     return;     /* just in case */
767   if (rp > 0)
768     p = SOLVER_INSTALL|SOLVER_SOLVABLE|extrajobflags;
769   else
770     {
771       rp = p;
772       p = SOLVER_ERASE|SOLVER_SOLVABLE|extrajobflags;
773     }
774   for (i = 0; i < job->count; i += 2)
775     if (job->elements[i] == p && job->elements[i + 1] == rp)
776       return;
777   queue_push2(job, p, rp);
778 }
779
780 void
781 solver_take_solution(Solver *solv, Id problem, Id solution, Queue *job)
782 {
783   Id p, rp, element = 0;
784   Id extrajobflags = solver_solutionelement_extrajobflags(solv, problem, solution);
785   while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
786     solver_take_solutionelement(solv, p, rp, extrajobflags, job);
787 }
788
789
790 /*-------------------------------------------------------------------
791  * 
792  * find problem rule
793  */
794
795 static void
796 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
797 {
798   Id rid, d;
799   Id lreqr, lconr, lsysr, ljobr;
800   Rule *r;
801   Id jobassert = 0;
802   int i, reqset = 0;    /* 0: unset, 1: installed, 2: jobassert, 3: assert */
803
804   /* find us a jobassert rule */
805   for (i = idx; (rid = solv->learnt_pool.elements[i]) != 0; i++)
806     {
807       if (rid < solv->jobrules || rid >= solv->jobrules_end)
808         continue;
809       r = solv->rules + rid;
810       d = r->d < 0 ? -r->d - 1 : r->d;
811       if (!d && r->w2 == 0 && r->p > 0)
812         {
813           jobassert = r->p;
814           break;
815         }
816     }
817
818   /* the problem rules are somewhat ordered from "near to the problem" to
819    * "near to the job" */
820   lreqr = lconr = lsysr = ljobr = 0;
821   while ((rid = solv->learnt_pool.elements[idx++]) != 0)
822     {
823       assert(rid > 0);
824       if (rid >= solv->learntrules)
825         findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
826       else if ((rid >= solv->jobrules && rid < solv->jobrules_end) || (rid >= solv->infarchrules && rid < solv->infarchrules_end) || (rid >= solv->duprules && rid < solv->duprules_end))
827         {
828           if (!*jobrp)
829             *jobrp = rid;
830         }
831       else if (rid >= solv->updaterules && rid < solv->updaterules_end)
832         {
833           if (!*sysrp)
834             *sysrp = rid;
835         }
836       else
837         {
838           assert(rid < solv->rpmrules_end);
839           r = solv->rules + rid;
840           d = r->d < 0 ? -r->d - 1 : r->d;
841           if (!d && r->w2 < 0)
842             {
843               if (!*conrp)
844                 *conrp = rid;
845             }
846           else
847             {
848               if (!d && r->w2 == 0 && reqset < 3)
849                 {
850                   if (*reqrp > 0 && r->p < -1)
851                     {
852                       Id op = -solv->rules[*reqrp].p;
853                       if (op > 1 && solv->pool->solvables[op].arch != solv->pool->solvables[-r->p].arch)
854                         continue;       /* different arch, skip */
855                     }
856                   /* prefer assertions */
857                   *reqrp = rid;
858                   reqset = 3;
859                 }
860               else if (jobassert && r->p == -jobassert)
861                 {
862                   /* prefer rules of job assertions */
863                   *reqrp = rid;
864                   reqset = 2;
865                 }
866               else if (solv->installed && r->p < 0 && solv->pool->solvables[-r->p].repo == solv->installed && reqset <= 1)
867                 {
868                   /* prefer rules of job installed package so that the user doesn't get confused by strange packages */
869                   *reqrp = rid;
870                   reqset = 1;
871                 }
872               else if (!*reqrp)
873                 *reqrp = rid;
874             }
875         }
876     }
877   if (!*reqrp && lreqr)
878     *reqrp = lreqr;
879   if (!*conrp && lconr)
880     *conrp = lconr;
881   if (!*jobrp && ljobr)
882     *jobrp = ljobr;
883   if (!*sysrp && lsysr)
884     *sysrp = lsysr;
885 }
886
887 /* 
888  * find problem rule
889  *
890  * search for a rule that describes the problem to the
891  * user. Actually a pretty hopeless task that may leave the user
892  * puzzled. To get all of the needed information use
893  * solver_findallproblemrules() instead.
894  */
895
896 Id
897 solver_findproblemrule(Solver *solv, Id problem)
898 {
899   Id reqr, conr, sysr, jobr;
900   Id idx = solv->problems.elements[2 * problem - 2];
901   reqr = conr = sysr = jobr = 0;
902   findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
903   if (reqr)
904     return reqr;        /* some requires */
905   if (conr)
906     return conr;        /* some conflict */
907   if (sysr)
908     return sysr;        /* an update rule */
909   if (jobr)
910     return jobr;        /* a user request */
911   assert(0);
912   return 0;
913 }
914
915 /*-------------------------------------------------------------------*/
916
917 static void
918 findallproblemrules_internal(Solver *solv, Id idx, Queue *rules)
919 {
920   Id rid;
921   while ((rid = solv->learnt_pool.elements[idx++]) != 0)
922     {
923       if (rid >= solv->learntrules)
924         {
925           findallproblemrules_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], rules);
926           continue;
927         }
928       queue_pushunique(rules, rid);
929     }
930 }
931
932 /*
933  * find all problem rule
934  *
935  * return all rules that lead to the problem. This gives the user
936  * all of the information to understand the problem, but the result
937  * can be a large number of rules.
938  */
939
940 void
941 solver_findallproblemrules(Solver *solv, Id problem, Queue *rules)
942 {
943   queue_empty(rules);
944   findallproblemrules_internal(solv, solv->problems.elements[2 * problem - 2], rules);
945 }
946
947 /* EOF */