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