- do not select src packages in yps
[platform/upstream/libsolv.git] / src / solver.c
1 /*
2  * solver.c
3  *
4  * SAT based dependency solver
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11
12 #include "solver.h"
13 #include "bitmap.h"
14 #include "pool.h"
15 #include "util.h"
16 #include "evr.h"
17
18 #define RULES_BLOCK 63
19
20 static Pool *prune_best_version_arch_sortcmp_data;
21
22 /*-----------------------------------------------------------------*/
23
24 /*
25  * prep for prune_best_version_arch
26  *   sort by name
27  */
28
29 static int
30 prune_best_version_arch_sortcmp(const void *ap, const void *bp)
31 {
32   Pool *pool = prune_best_version_arch_sortcmp_data;
33   Id a = *(Id *)ap;
34   Id b = *(Id *)bp;
35   return pool->solvables[a].name - pool->solvables[b].name;
36 }
37
38
39 #if 0
40 static Id
41 replaces_system(Solver *solv, Id id)
42 {
43   Pool *pool = solv->pool;
44   Source *system = solv->system;
45   Id *name = pool->solvables[id].name;
46
47   FOR_PROVIDES(p, pp, id)
48     {
49       s = pool->solvables + p;
50       if (s->name != name)
51         continue;
52       if (p >= system->start && p < system->start + system->nsolvables)
53         return p;
54     }
55 }
56 #endif
57
58 static int
59 dep_installed(Solver *solv, Id dep)
60 {
61   Pool *pool = solv->pool;
62   Id p, *pp;
63
64   if (ISRELDEP(dep))
65     {
66       Reldep *rd = GETRELDEP(pool, dep);
67       if (rd->flags == REL_AND)
68         {
69           if (!dep_installed(solv, rd->name))
70             return 0;
71           return dep_installed(solv, rd->evr);
72         }
73       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
74         return dep_installed(solv, rd->evr);
75     }
76   FOR_PROVIDES(p, pp, dep)
77     {
78       if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
79         return 1;
80     }
81   return 0;
82 }
83
84 static inline int
85 dep_fulfilled(Solver *solv, Id dep)
86 {
87   Pool *pool = solv->pool;
88   Id p, *pp;
89
90   if (ISRELDEP(dep))
91     {
92       Reldep *rd = GETRELDEP(pool, dep);
93       if (rd->flags == REL_AND)
94         {
95           if (!dep_fulfilled(solv, rd->name))
96             return 0;
97           return dep_fulfilled(solv, rd->evr);
98         }
99       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
100         return dep_installed(solv, rd->evr);
101     }
102   FOR_PROVIDES(p, pp, dep)
103     {
104       if (solv->decisionmap[p] > 0)
105         return 1;
106     }
107   return 0;
108 }
109
110 /*
111  * prune_to_recommended
112  *
113  */
114 static void
115 prune_to_recommended(Solver *solv, Queue *plist)
116 {
117   Pool *pool = solv->pool;
118   int i, j;
119   Solvable *s;
120   Id sup, *supp;
121
122   for (i = j = 0; i < plist->count; i++)
123     {
124       s = pool->solvables + plist->elements[i];
125       if (!s->supplements && !s->freshens)
126         continue;
127       if ((supp = s->supplements) != 0)
128         {
129           while ((sup = *supp++) != 0)
130             if (dep_fulfilled(solv, sup))
131               break;
132           if (!sup)
133             continue;
134         }
135       if ((supp = s->freshens) != 0)
136         {
137           while ((sup = *supp++) != 0)
138             if (dep_fulfilled(solv, sup))
139               break;
140           if (!sup)
141             continue;
142         }
143       plist->elements[j++] = s - pool->solvables;
144     }
145   if (j)
146     plist->count = j;
147 }
148
149 /*
150  * prune_best_version_arch
151  * 
152  * sort list of packages (given through plist) by name and evr
153  * return result through plist
154  * 
155  */
156
157 /* FIXME: must also look at update packages */
158
159 void
160 prune_best_version_arch(Pool *pool, Queue *plist)
161 {
162   Id best = ID_NULL;
163   int i, j;
164   Solvable *s;
165   Id a, bestscore;
166
167   if (plist->count < 2)         /* no need to prune for a single entry */
168     return;
169   if (pool->verbose) printf("prune_best_version_arch %d\n", plist->count);
170
171   /* prune to best architecture */
172   if (pool->id2arch)
173     {
174       bestscore = 0;
175       for (i = 0; i < plist->count; i++)
176         {
177           s = pool->solvables + plist->elements[i];
178           a = s->arch;
179           if (a > pool->lastarch)
180             continue;
181           a = pool->id2arch[a];
182           if (!bestscore || (a & 0xffff0000) < bestscore)
183             bestscore = a & 0xffff0000;
184         }
185       for (i = j = 0; i < plist->count; i++)
186         {
187           s = pool->solvables + plist->elements[i];
188           a = s->arch;
189           if (a > pool->lastarch)
190             continue;
191           a = pool->id2arch[a];
192           /* a == 1 -> noarch */
193           if (a != 1 && (a & 0xffff0000) != bestscore)
194             continue;
195           plist->elements[j++] = plist->elements[i];
196         }
197       plist->count = j;
198       if (j == 0)
199         return;
200     }
201
202   prune_best_version_arch_sortcmp_data = pool;
203   /* sort by name first */
204   qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
205
206   /* now find best 'per name' */
207   for (i = j = 0; i < plist->count; i++)
208     {
209       s = pool->solvables + plist->elements[i];
210       if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
211         continue;
212
213       if (pool->verbose) printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
214
215       if (!best)                       /* if no best yet, the current is best */
216         {
217           best = plist->elements[i];
218           continue;
219         }
220
221       /* name switch: re-init */
222       if (pool->solvables[best].name != s->name)   /* new name */
223         {
224           if (pool->verbose) printf("BEST: %s-%s.%s\n", id2str(pool, pool->solvables[best].name), id2str(pool, pool->solvables[best].evr), id2str(pool, pool->solvables[best].arch));
225           plist->elements[j++] = best; /* move old best to front */
226           best = plist->elements[i];   /* take current as new best */
227           continue;
228         }
229
230       if (pool->solvables[best].evr != s->evr)   /* compare evr */
231         {
232           if (evrcmp(pool, pool->solvables[best].evr, s->evr) < 0)
233             best = plist->elements[i];
234         }
235     }
236
237   if (best == ID_NULL)
238     best = plist->elements[0];
239
240   /* XXX also check obsoletes! */
241   if (pool->verbose) printf("BEST: %s-%s.%s\n", id2str(pool, pool->solvables[best].name), id2str(pool, pool->solvables[best].evr), id2str(pool, pool->solvables[best].arch));
242
243   plist->elements[j++] = best;
244   plist->count = j;
245
246 }
247
248 /*-----------------------------------------------------------------*/
249
250 /*
251  * print rules
252  */
253
254 static void
255 printruleelement(Solver *solv, Rule *r, Id v)
256 {
257   Pool *pool = solv->pool;
258   Solvable *s;
259   if (v < 0)
260     {
261       s = pool->solvables + -v;
262       printf("    !%s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), -v);
263     }
264   else
265     {
266       s = pool->solvables + v;
267       printf("    %s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), v);
268     }
269   if (r)
270     {
271       if (r->w1 == v)
272         printf(" (w1)");
273       if (r->w2 == v)
274         printf(" (w2)");
275     }
276   if (solv->decisionmap[s - pool->solvables] > 0)
277     printf(" I.%d", solv->decisionmap[s - pool->solvables]);
278   if (solv->decisionmap[s - pool->solvables] < 0)
279     printf(" C.%d", -solv->decisionmap[s - pool->solvables]);
280   printf("\n");
281 }
282
283
284 /*
285  * print rule
286  */
287
288 static void
289 printrule(Solver *solv, Rule *r)
290 {
291   int i;
292   Id v;
293
294   if (r >= solv->rules && r < solv->rules + solv->nrules)   /* r is a solver rule */
295     printf("Rule #%d:\n", (int)(r - solv->rules));
296   else
297     printf("Rule:\n");                 /* r is any rule */
298   for (i = 0; ; i++)
299     {
300       if (i == 0)
301         v = r->p;
302       else if (r->d == ID_NULL)
303         {
304           if (i == 2)
305             break;
306           v = r->w2;
307         }
308       else
309         v = solv->pool->whatprovidesdata[r->d + i - 1];
310       if (v == ID_NULL)
311         break;
312       printruleelement(solv, r, v);
313     }
314   printf("    next: %d %d\n", r->n1, r->n2);
315 }
316
317
318 /*-----------------------------------------------------------------*/
319
320 /*
321  * Rule handling
322  */
323
324 static Pool *unifyrules_sortcmp_data;
325
326 /*
327  * compare rules for unification sort
328  */
329
330 static int
331 unifyrules_sortcmp(const void *ap, const void *bp)
332 {
333   Pool *pool = unifyrules_sortcmp_data;
334   Rule *a = (Rule *)ap;
335   Rule *b = (Rule *)bp;
336   Id *ad, *bd;
337   int x;
338   
339   x = a->p - b->p;
340   if (x)
341     return x;                          /* p differs */
342
343   /* identical p */
344   if (a->d == 0 && b->d == 0)
345     return a->w2 - b->w2;              /* assertion: return w2 diff */
346
347   if (a->d == 0)                       /* a is assertion, b not */
348     {
349       x = a->w2 - pool->whatprovidesdata[b->d];
350       return x ? x : -1;
351     }
352
353   if (b->d == 0)                       /* b is assertion, a not */
354     {
355       x = pool->whatprovidesdata[a->d] - b->w2;
356       return x ? x : 1;
357     }
358
359   /* compare whatprovidesdata */
360   ad = pool->whatprovidesdata + a->d;
361   bd = pool->whatprovidesdata + b->d;
362   for (; *ad && *ad == *bd; ad++, bd++)
363     ;
364   return *ad - *bd;
365 }
366
367
368 /*
369  * unify rules
370  */
371
372 static void
373 unifyrules(Solver *solv)
374 {
375   int i, j;
376   Rule *ir, *jr;
377
378   if (solv->nrules <= 1)               /* nothing to unify */
379     return;
380
381   /* sort rules first */
382   unifyrules_sortcmp_data = solv->pool;
383   qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
384
385   /* prune rules
386    * i = unpruned
387    * j = pruned
388    */
389   jr = 0;
390   for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
391     {
392       if (jr && !unifyrules_sortcmp(ir, jr))
393         continue;                      /* prune! */
394       jr = solv->rules + j++;          /* keep! */
395       if (ir != jr)
396         *jr = *ir;
397     }
398
399   /* reduced count from nrules to j rules */
400   if (solv->pool->verbose) printf("pruned rules from %d to %d\n", solv->nrules, j);
401
402   /* adapt rule buffer */
403   solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
404   solv->nrules = j;
405 #if 1
406   {
407     int binr = 0;
408     int dc = 0;
409     Id *dp;
410     Rule *r;
411
412     for (i = 1; i < solv->nrules; i++)
413       {
414         r = solv->rules + i;
415         if (r->d == 0)                 /* assertion */
416           {
417             binr++;
418             continue;
419           }
420         dp = solv->pool->whatprovidesdata + r->d;
421         while (*dp++)
422           dc++;
423       }
424     if (solv->pool->verbose)
425       {
426         printf("  binary: %d\n", binr);
427         printf("  normal: %d\n", solv->nrules - 1 - binr);
428         printf("  normal lits: %d\n", dc);
429       }
430   }
431 #endif
432 }
433
434 #if 0
435
436 /*
437  * hash rule
438  */
439
440 static Hashval
441 hashrule(Solver *solv, Id p, Id d, int n)
442 {
443   unsigned int x = (unsigned int)p;
444   int *dp;
445
446   if (n <= 1)
447     return (x * 37) ^ (unsigned int)d; 
448   dp = solv->pool->whatprovidesdata + d;
449   while (*dp)
450     x = (x * 37) ^ (unsigned int)*dp++;
451   return x;
452 }
453 #endif
454
455
456 /*
457  * add rule
458  *  p = direct literal; > 0 for learnt, < 0 for installed pkg (rpm)
459  *  d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
460  *
461  *
462  * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
463  * 
464  * p < 0 : rule from rpm (installed pkg)
465  * d > 0 : Offset in whatprovidesdata (list of providers)
466  * 
467  * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
468  *  d < 0: Id of solvable (e.g. B1)
469  * 
470  * d == 0: unary rule, assertion => (A) or (-A)
471  * 
472  *   Install:    p > 0, d = 0   (A)             user requested install
473  *   Remove:     p < 0, d = 0   (-A)            user requested remove
474  *   Requires:   p < 0, d > 0   (-A|B1|B2|...)  d: <list of providers for requirement of p>
475  *   Updates:    p > 0, d > 0   (A|B1|B2|...)   d: <list of updates for solvable p>
476  *   Conflicts:  p < 0, d < 0   (-A|-B)         either p (conflict issuer) or d (conflict provider)
477  *   ?           p > 0, d < 0   (A|-B)
478  *   No-op ?:    p = 0, d = 0   (null)          (used as policy rule placeholder)
479  */
480
481 static Rule *
482 addrule(Solver *solv, Id p, Id d)
483 {
484   Rule *r = NULL;
485   Id *dp = NULL;
486
487   int n = 0;                           /* number of literals in rule - 1
488                                           0 = direct assertion (single literal)
489                                           1 = binary rule
490                                         */
491
492   /* it often happenes that requires lead to adding the same rpm rule
493    * multiple times, so we prune those duplicates right away to make
494    * the work for unifyrules a bit easier */
495
496   if (solv->nrules && !solv->jobrules)
497     {
498       r = solv->rules + solv->nrules - 1;   /* get the last added rule */
499       if (r->p == p && r->d == d && d != 0)   /* identical and not user requested */
500         return r;
501     }
502
503   if (d < 0)
504     {
505       if (p == d)
506         return NULL;                   /* ignore self conflict */
507       n = 1;
508     }
509   else if (d == 0)                     /* user requested */
510     n = 0;
511   else
512     {
513       for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, n++)
514         if (*dp == -p)
515           return NULL;  /* rule is self-fulfilling */
516       if (n == 1)
517         d = dp[-1];
518     }
519
520   if (n == 0)                          /* direct assertion */
521     {
522       if (!solv->jobrules)
523         {
524           /* this is a rpm rule assertion, we do not have to allocate it */
525           /* we can identify it by looking at the decision level, it will be 1 */
526           if (p > 0)                       /*  */
527             abort();
528           if (solv->decisionmap[-p] > 0)   /*  */
529             abort();
530           if (solv->decisionmap[-p])       /*  */
531             return NULL;
532           queuepush(&solv->decisionq, p);
533           queuepush(&solv->decisionq_why, 0);
534           solv->decisionmap[-p] = -1;
535
536           return NULL;
537         }
538     }
539   else if (n == 1 && p > d)
540     {
541       /* smallest literal first so we can find dups */
542       n = p;
543       p = d;
544       d = n;
545       n = 1;                           /* re-set n, was used as temp var */
546     }
547
548   if (r
549       && n == 1
550       && r->p == p
551       && r->w2 == d)
552   {
553     return r;
554   }
555
556   if (r
557       && r->d
558       && n > 1
559       && r->p == p)
560     {
561       Id *dp2 = solv->pool->whatprovidesdata + r->d;
562       for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, dp2++)
563       {
564         if (*dp != *dp2)
565           break;
566       }
567       if (*dp == *dp2)
568         return r;
569    }
570   
571   /*
572    * allocate new rule
573    */
574
575   /* check and extend rule buffer */
576   if ((solv->nrules & RULES_BLOCK) == 0)
577     {
578       solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
579     }
580
581   r = solv->rules + solv->nrules++;    /* point to rule space */
582
583   r->p = p;
584   if (n == 0)
585     {
586       /* direct assertion, no watch needed */
587       r->d = 0;
588       r->w1 = p;
589       r->w2 = 0;
590     }
591   else if (n == 1)
592     {
593       /* binary rule */
594       r->d = 0;
595       r->w1 = p;
596       r->w2 = d;
597     }
598   else
599     {
600       r->d = d;
601       r->w1 = p;
602       r->w2 = solv->pool->whatprovidesdata[d];
603     }
604   r->n1 = 0;
605   r->n2 = 0;
606
607   /* we don't add the decision for learnt rules, as the code does that
608    * right after calling addrule anyway */
609   if (n == 0
610       && p
611       && !solv->learntrules)
612     {
613       /* must be system or job rule, as there are only negative unary rpm rules */
614       Id vv = p > 0 ? p : -p;
615       if (solv->decisionmap[vv])
616         {
617           int i;
618           if (solv->decisionmap[vv] > 0 && p > 0)
619             return r;
620           if (solv->decisionmap[vv] < 0 && p < 0)
621             return r;
622           /* direct conflict! */
623           for (i = 0; i < solv->decisionq.count; i++)
624           {
625             if (solv->decisionq.elements[i] == -p)
626               break;
627           }
628           if (i == solv->decisionq.count)
629             abort();
630           if (solv->decisionq_why.elements[i] == 0)
631             {
632               /* conflict with rpm rule */
633               queuepush(&solv->problems, r - solv->rules);
634               queuepush(&solv->problems, 0);
635               r->w1 = 0;        /* disable */
636               return r;
637             }
638           /* conflict with other job or system rule */
639           queuepush(&solv->problems, solv->decisionq_why.elements[i]);
640           queuepush(&solv->problems, r - solv->rules);
641           queuepush(&solv->problems, 0);
642           r->w1 = 0;    /* disable */
643           /* also disable conflicting rule */
644           solv->rules[solv->decisionq_why.elements[i]].w1 = 0;
645           /* XXX: remove from decisionq! */
646 printf("XXX remove from decisionq\n");
647           return r;
648         }
649       queuepush(&solv->decisionq, p);
650       queuepush(&solv->decisionq_why, r - solv->rules);
651       solv->decisionmap[p > 0 ? p : -p] = p > 0 ? 1 : -1;
652     }
653   return r;
654 }
655
656
657 /*
658  * add (install) rules for solvable
659  * 
660  */
661
662 static void
663 addrulesforsolvable(Solver *solv, Solvable *s, Map *m)
664 {
665   Pool *pool = solv->pool;
666   Source *system = solv->system;
667   Queue q;
668   Id qbuf[64];
669   int i;
670   int dontfix;
671   Id req, *reqp;
672   Id con, *conp;
673   Id obs, *obsp;
674   Id rec, *recp;
675   Id p, *pp;
676   Id *dp;
677   Id n;
678
679   queueinit_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
680   queuepush(&q, s - pool->solvables);   /* push solvable Id */
681
682   while (q.count)
683     {
684       /*
685        * n: Id of solvable
686        * s: Pointer to solvable
687        */
688       
689       n = queueshift(&q);
690       if (MAPTST(m, n))                /* continue if already set in map */
691         continue;
692
693       MAPSET(m, n);
694       s = pool->solvables + n;         /* s = Solvable in question */
695
696       dontfix = 0;
697       if (system                       /* have rpm */
698           && !solv->fixsystem
699           && n >= system->start        /* its an rpm rule */
700           && n < system->start + system->nsolvables)
701       {
702         dontfix = 1;                   /* dont care about broken rpm deps */
703       }
704
705       /*-----------------------------------------
706        * check requires of s
707        */
708       
709       if ((reqp = s->requires) != ID_NULL)
710         {
711           while ((req = *reqp++) != ID_NULL)
712             {
713               if (req == SOLVABLE_PREREQMARKER)   /* skip the marker */
714                 continue;
715
716               dp = GET_PROVIDESP(req, p);      /* get providers of req */
717
718               if (!*dp                       /* dont care if noone provides rpmlib() */
719                   && !strncmp(id2str(pool, req), "rpmlib(", 7))
720                 {
721                   continue;
722                 }
723
724               if (dontfix)             /* rpm rule, dont care about breakage */
725                 {
726                   for (i = 0; dp[i]; i++)/* for all providers */
727                     {
728                       if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
729                         break;         /* provider is installed */
730                     }
731                   if (!dp[i])          /* no provider found */
732                     {
733                       if (pool->verbose) printf("ignoring broken requires %s of system package %s-%s.%s\n", dep2str(pool, req), id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
734                       continue;
735                     }
736                 }
737
738               if (!*dp)
739                 {
740                   /* nothing provides req! */
741   #if 1
742                   if (pool->verbose) printf("package %s-%s.%s is not installable (%s)\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), dep2str(pool, req));
743   #endif
744                   addrule(solv, -n, 0); /* mark requestor as uninstallable */
745                   if (solv->rc_output)
746                     printf(">!> !unflag %s-%s.%s[%s]\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), source_name(pool_source(pool, s)));
747                   continue;
748                 }
749   #if 0
750               printf("addrule %s-%s.%s %s %d %d\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), dep2str(pool, req), -n, dp - pool->whatprovidesdata);
751               for (i = 0; dp[i]; i++)
752                 printf("  %s-%s.%s\n", id2str(pool, pool->solvables[dp[i]].name), id2str(pool, pool->solvables[dp[i]].evr), id2str(pool, pool->solvables[dp[i]].arch));
753   #endif
754               /* add 'requires' dependency */
755               addrule(solv, -n, dp - pool->whatprovidesdata);   /* rule: (-requestor|provider1|provider2|...|providerN) */
756
757               /* descend the dependency tree */
758               for (; *dp != ID_NULL; dp++)   /* loop through all providers */
759                 {
760                   if (!MAPTST(m, *dp))
761                     queuepush(&q, *dp);
762                 }
763
764             } /* while, requirements of n */
765
766         } /* if, requirements */
767
768       
769       /*-----------------------------------------
770        * check conflicts of s
771        */
772       
773       if ((conp = s->conflicts) != ID_NULL)
774         {
775           while ((con = *conp++) != ID_NULL)
776             {
777               FOR_PROVIDES(p, pp, con)   /* loop through all providers of this conflict */
778                 {
779                                            /* dontfix: dont care about conflicts with already installed packs */
780                   if (dontfix && p >= system->start && p < system->start + system->nsolvables)
781                     continue;
782                   addrule(solv, -n, -p);   /* rule: -n|-p: either solvable _or_ provider of conflict */
783                 }
784             }
785         }
786
787       /*-----------------------------------------
788        * check obsoletes if not installed
789        */
790       if (!system || n < system->start || n >= (system->start + system->nsolvables))
791         {                              /* not installed */
792           if ((obsp = s->obsoletes) != ID_NULL)
793             {
794               while ((obs = *obsp++) != ID_NULL)
795                 {
796                   FOR_PROVIDES(p, pp, obs)
797                     addrule(solv, -n, -p);
798                 }
799             }
800           FOR_PROVIDES(p, pp, s->name)
801             {
802               if (s->name == pool->solvables[p].name)
803                 addrule(solv, -n, -p);
804             }
805         }
806
807       /*-----------------------------------------
808        * add recommends to the rule list
809        */
810       if ((recp = s->recommends) != ID_NULL)
811         while ((rec = *recp++) != ID_NULL)
812           {
813             FOR_PROVIDES(p, pp, rec)
814               if (!MAPTST(m, p))
815                 queuepush(&q, p);
816           }
817     }
818   queuefree(&q);
819 }
820
821 static void
822 addrulesforsupplements(Solver *solv, Map *m)
823 {
824   Pool *pool = solv->pool;
825   Solvable *s;
826   Id sup, *supp;
827   Id p, *pp;
828   int i, n;
829
830   if (pool->verbose) printf("addrulesforsupplements... (%d)\n", solv->nrules);
831   for (i = n = 1; n < pool->nsolvables; i++, n++)
832     {
833       if (i == pool->nsolvables)
834         i = 1;
835       if (MAPTST(m, i))
836         continue;
837       s = pool->solvables + i;
838       if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
839         continue;
840       if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
841         continue;
842       sup = 0;
843       if ((supp = s->supplements) != 0)
844         {
845           while ((sup = *supp++) != ID_NULL)
846             {
847               FOR_PROVIDES(p, pp, sup)
848                 if (MAPTST(m, p))
849                   break;
850               if (p)
851                 break;
852             }
853         }
854       if (!sup && (supp = s->freshens) != 0)
855         {
856           while ((sup = *supp++) != ID_NULL)
857             {
858               FOR_PROVIDES(p, pp, sup)
859                 if (MAPTST(m, p))
860                   break;
861               if (p)
862                 break;
863             }
864         }
865       if (!sup)
866         continue;
867       addrulesforsolvable(solv, s, m);
868       n = 0;
869     }
870   if (pool->verbose) printf("done. (%d)\n", solv->nrules);
871 }
872
873
874 static inline int
875 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
876 {
877   Id a1 = s1->arch, a2 = s2->arch;
878
879   /* we allow changes to/from noarch */
880   if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
881     return 0;
882   if (!pool->id2arch)
883     return 0;
884   a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
885   a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
886   if (((a1 ^ a2) & 0xffff0000) != 0)
887     return 1;
888   return 0;
889 }
890
891
892 static void
893 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
894 {
895   /* system packages get a special upgrade allowed rule */
896   Pool *pool = solv->pool;
897   Id p, *pp, n, p2, *pp2;
898   Id obs, *obsp;
899
900   QUEUEEMPTY(qs);
901   /*
902    * s = solvable ptr
903    * n = solvable Id
904    */
905   n = s - pool->solvables;
906   if (m && !MAPTST(m, n))       /* add rule for s if not already done */
907     addrulesforsolvable(solv, s, m);
908
909   /*
910    * look for updates for s
911    */
912   FOR_PROVIDES(p, pp, s->name)  /* every provider of s' name */
913     {
914       if (p == n)               /* skip itself */
915         continue;
916
917       if (s->name == pool->solvables[p].name)   /* name match */
918         {
919           if (!allowdowngrade                   /* consider downgrades ? */
920               && evrcmp(pool, s->evr, pool->solvables[p].evr) > 0)
921             continue;
922           /* XXX */
923           if (!allowarchchange && archchanges(pool, s, pool->solvables + p))
924             continue;
925         }
926       else if ((obsp = pool->solvables[p].obsoletes) != 0)   /* provides/obsoletes combination ? */
927         {
928           while ((obs = *obsp++) != 0)  /* for all obsoletes */
929             {
930               FOR_PROVIDES(p2, pp2, obs)   /* and all matching providers of the obsoletes */
931                 {
932                   if (p2 == n)          /* match ! */
933                     break;
934                 }
935               if (p2)                   /* match! */
936                 break;
937             }
938           if (!obs)                     /* continue if no match */
939             continue;
940           /* here we have 'p' with a matching provides/obsoletes combination
941            * thus flagging p as a valid update candidate for s
942            */
943         }
944       queuepush(qs, p);
945
946       if (m && !MAPTST(m, p))           /* mark p for install if not already done */
947         addrulesforsolvable(solv, pool->solvables + p, m);
948     }
949 }
950
951 /*
952  * add rule for update
953  *   (A|A1|A2|A3...)  An = update candidates for A
954  * 
955  * s = (installed) solvable
956  * m = 'addedmap', bit set if 'install' rule for solvable exists
957  */
958
959 static void
960 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
961 {
962   /* system packages get a special upgrade allowed rule */
963   Pool *pool = solv->pool;
964   Id p, d;
965   Rule *r;
966   Queue qs;
967   Id qsbuf[64];
968
969   queueinit_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
970   findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
971   p = s - pool->solvables;
972   if (dontaddrule)      /* we consider update candidates but dont force them */
973     {
974       queuefree(&qs);
975       return;
976     }
977
978   if (qs.count == 0)                   /* no updates found */
979     {
980 #if 0
981       printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
982 #endif
983       addrule(solv, p, 0);              /* request 'install' of s */
984       queuefree(&qs);
985       return;
986     }
987
988   d = pool_queuetowhatprovides(pool, &qs);   /* intern computed provider queue */
989   queuefree(&qs);
990   r = addrule(solv, p, d);             /* allow update of s */
991 #if 0
992   printf("new update rule ");
993   if (r)
994     printrule(solv, r);
995 #endif
996 }
997
998
999 /*-----------------------------------------------------------------*/
1000 /* watches */
1001
1002
1003 /*
1004  * makewatches
1005  * 
1006  * initial setup for all watches
1007  */
1008
1009 static void
1010 makewatches(Solver *solv)
1011 {
1012   Rule *r;
1013   int i;
1014   int nsolvables = solv->pool->nsolvables;
1015
1016   xfree(solv->watches);
1017                                        /* lower half for removals, upper half for installs */
1018   solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1019 #if 1
1020   /* do it reverse so rpm rules get triggered first */
1021   for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1022 #else
1023   for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1024 #endif
1025     {
1026       if (!r->w1                       /* rule is disabled */
1027           || !r->w2)                   /* rule is assertion */
1028         continue;
1029
1030       /* see addwatches(solv, r) */
1031       r->n1 = solv->watches[nsolvables + r->w1];
1032       solv->watches[nsolvables + r->w1] = r - solv->rules;
1033
1034       r->n2 = solv->watches[nsolvables + r->w2];
1035       solv->watches[nsolvables + r->w2] = r - solv->rules;
1036     }
1037 }
1038
1039
1040 /*
1041  * add watches (for rule)
1042  */
1043
1044 static void
1045 addwatches(Solver *solv, Rule *r)
1046 {
1047   int nsolvables = solv->pool->nsolvables;
1048
1049   r->n1 = solv->watches[nsolvables + r->w1];
1050   solv->watches[nsolvables + r->w1] = r - solv->rules;
1051
1052   r->n2 = solv->watches[nsolvables + r->w2];
1053   solv->watches[nsolvables + r->w2] = r - solv->rules;
1054 }
1055
1056
1057 /*-----------------------------------------------------------------*/
1058 /* rule propagation */
1059
1060 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1061
1062 /*
1063  * propagate
1064  * 
1065  * propagate decision to all rules
1066  */
1067
1068 static Rule *
1069 propagate(Solver *solv, int level)
1070 {
1071   Pool *pool = solv->pool;
1072   Id *rp, *nrp;
1073   Rule *r;
1074   Id p, pkg, ow;
1075   Id *dp;
1076   Id *decisionmap = solv->decisionmap;
1077   Id *watches = solv->watches + pool->nsolvables;
1078
1079   while (solv->propagate_index < solv->decisionq.count)
1080     {
1081       /* negative because our watches trigger if literal goes FALSE */
1082       pkg = -solv->decisionq.elements[solv->propagate_index++];
1083 #if 0
1084   printf("popagate for decision %d level %d\n", -pkg, level);
1085   printruleelement(solv, 0, -pkg);
1086 #endif
1087       for (rp = watches + pkg; *rp; rp = nrp)
1088         {
1089           r = solv->rules + *rp;
1090 #if 0
1091   printf("  watch triggered ");
1092   printrule(solv, r);
1093 #endif
1094           if (pkg == r->w1)
1095             {
1096               ow = r->w2;
1097               nrp = &r->n1;
1098             }
1099           else
1100             {
1101               ow = r->w1;
1102               nrp = &r->n2;
1103             }
1104           /* if clause is TRUE, nothing to do */
1105           if (DECISIONMAP_TRUE(ow))
1106             continue;
1107
1108           if (r->d)
1109             {
1110               /* not a binary clause, check if we need to move our watch */
1111               if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1112                 p = r->p;
1113               else
1114                 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1115                   if (p != ow && !DECISIONMAP_TRUE(-p))
1116                     break;
1117               if (p)
1118                 {
1119                   /* p is free to watch, move watch to p */
1120 #if 0
1121                   if (p > 0)
1122                     printf("    -> move w%d to %s-%s.%s\n", (pkg == r->w1 ? 1 : 2), id2str(pool, pool->solvables[p].name), id2str(pool, pool->solvables[p].evr), id2str(pool, pool->solvables[p].arch));
1123                   else
1124                     printf("    -> move w%d to !%s-%s.%s\n", (pkg == r->w1 ? 1 : 2), id2str(pool, pool->solvables[-p].name), id2str(pool, pool->solvables[-p].evr), id2str(pool, pool->solvables[-p].arch));
1125 #endif
1126                   *rp = *nrp;
1127                   nrp = rp;
1128                   if (pkg == r->w1)
1129                     {
1130                       r->w1 = p;
1131                       r->n1 = watches[p];
1132                     }
1133                   else
1134                     {
1135                       r->w2 = p;
1136                       r->n2 = watches[p];
1137                     }
1138                   watches[p] = r - solv->rules;
1139                   continue;
1140                 }
1141             }
1142           /* unit clause found, set other watch to TRUE */
1143           if (DECISIONMAP_TRUE(-ow))
1144             return r;           /* eek, a conflict! */
1145 #if 0
1146           printf("unit ");
1147           printrule(solv, r);
1148 #endif
1149           if (ow > 0)
1150             decisionmap[ow] = level;
1151           else
1152             decisionmap[-ow] = -level;
1153           queuepush(&solv->decisionq, ow);
1154           queuepush(&solv->decisionq_why, r - solv->rules);
1155 #if 0
1156             {
1157               Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1158               if (ow > 0)
1159                 printf("  -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1160               else
1161                 printf("  -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1162             }
1163 #endif
1164         }
1165     }
1166   return 0;     /* all is well */
1167 }
1168
1169
1170 /*-----------------------------------------------------------------*/
1171 /* Analysis */
1172
1173 /*
1174  * analyze
1175  *   and learn
1176  */
1177
1178 static int
1179 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1180 {
1181   Pool *pool = solv->pool;
1182   Queue r;
1183   int rlevel = 1;
1184   Map seen;             /* global? */
1185   Id v, vv, *dp;
1186   int l, i, idx;
1187   int num = 0;
1188   int learnt_why = solv->learnt_pool.count;
1189   Id *decisionmap = solv->decisionmap;
1190  
1191   queueinit(&r);
1192
1193   if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1194   mapinit(&seen, pool->nsolvables);
1195   idx = solv->decisionq.count;
1196   for (;;)
1197     {
1198       printrule(solv, c);
1199       queuepush(&solv->learnt_pool, c - solv->rules);
1200       dp = c->d ? pool->whatprovidesdata + c->d : 0;
1201       for (i = -1; ; i++)
1202         {
1203           if (i == -1)
1204             v = c->p;
1205           else if (c->d == 0)
1206             v = i ? 0 : c->w2;
1207           else
1208             v = *dp++;
1209           if (v == 0)
1210             break;
1211           if (DECISIONMAP_TRUE(v))      /* the one true literal */
1212               continue;
1213           vv = v > 0 ? v : -v;
1214           if (MAPTST(&seen, vv))
1215             continue;
1216           l = solv->decisionmap[vv];
1217           if (l < 0)
1218             l = -l;
1219           if (l == 1)
1220             {
1221 #if 0
1222               int j;
1223               for (j = 0; j < solv->decisionq.count; j++)
1224                 if (solv->decisionq.elements[j] == v)
1225                   break;
1226               if (j == solv->decisionq.count)
1227                 abort();
1228               queuepush(&rulq, -(j + 1));
1229 #endif
1230               continue;                 /* initial setting */
1231             }
1232           MAPSET(&seen, vv);
1233           if (l == level)
1234             num++;                      /* need to do this one as well */
1235           else
1236             {
1237               queuepush(&r, v);
1238 #if 0
1239   printf("PUSH %d ", v);
1240   printruleelement(solv, 0, v);
1241 #endif
1242               if (l > rlevel)
1243                 rlevel = l;
1244             }
1245         }
1246 #if 0
1247       printf("num = %d\n", num);
1248 #endif
1249       if (num <= 0)
1250         abort();
1251       for (;;)
1252         {
1253           v = solv->decisionq.elements[--idx];
1254           vv = v > 0 ? v : -v;
1255           if (MAPTST(&seen, vv))
1256             break;
1257         }
1258       c = solv->rules + solv->decisionq_why.elements[idx];
1259       MAPCLR(&seen, vv);
1260       if (--num == 0)
1261         break;
1262     }
1263   *pr = -v;
1264   if (r.count == 0)
1265     *dr = 0;
1266   else if (r.count == 1 && r.elements[0] < 0)
1267     *dr = r.elements[0];
1268   else
1269     *dr = pool_queuetowhatprovides(pool, &r);
1270   if (pool->verbose)
1271     {
1272       printf("learned rule for level %d (am %d)\n", rlevel, level);
1273       printruleelement(solv, 0, -v);
1274       for (i = 0; i < r.count; i++)
1275         {
1276           v = r.elements[i];
1277           printruleelement(solv, 0, v);
1278         }
1279     }
1280   mapfree(&seen);
1281   queuepush(&solv->learnt_pool, 0);
1282 #if 0
1283   for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1284     {
1285       printf("learnt_why ");
1286       printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1287     }
1288 #endif
1289   if (why)
1290     *why = learnt_why;
1291   return rlevel;
1292 }
1293
1294
1295 /*
1296  * reset_solver
1297  * reset the solver decisions to right after the rpm rules
1298  */
1299
1300 static void
1301 reset_solver(Solver *solv)
1302 {
1303   int i;
1304   Id v;
1305   Rule *r;
1306
1307   /* delete all learnt rules */
1308   solv->nrules = solv->learntrules;
1309   QUEUEEMPTY(&solv->learnt_why);
1310   QUEUEEMPTY(&solv->learnt_pool);
1311
1312   /* redo all direct decision without the disabled rules */
1313   for (i = 0; i < solv->decisionq.count; i++)
1314     {
1315       v = solv->decisionq.elements[i];
1316       solv->decisionmap[v > 0 ? v : -v] = 0;
1317     }
1318   for (i = 0; i < solv->decisionq_why.count; i++)
1319     if (solv->decisionq_why.elements[i])
1320       break;
1321     else
1322       {
1323         v = solv->decisionq.elements[i];
1324         solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1325       }
1326
1327   if (solv->pool->verbose)
1328     printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1329
1330   solv->decisionq_why.count = i;
1331   solv->decisionq.count = i;
1332   if (i < solv->propagate_index)
1333     solv->propagate_index = i;
1334   /* make direct decisions from enabled unary rules */
1335   for (i = solv->jobrules, r = solv->rules + solv->jobrules; i < solv->nrules; i++, r++)
1336     {
1337       if (!r->w1 || r->w2)
1338         continue;
1339 #if 0
1340       printrule(solv, r);
1341 #endif
1342       v = r->p;
1343       queuepush(&solv->decisionq, v);
1344       queuepush(&solv->decisionq_why, r - solv->rules);
1345       solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1346     }
1347   if (solv->pool->verbose)
1348     printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1349   /* recreate watches */
1350   makewatches(solv);
1351 }
1352
1353
1354 /*
1355  * analyze_unsolvable_rule
1356  */
1357
1358 static void
1359 analyze_unsolvable_rule(Solver *solv, Rule *c, int disablerules)
1360 {
1361   Id why;
1362   int i;
1363
1364   why = c - solv->rules;
1365 #if 0
1366   if (why >= solv->jobrules && why < solv->systemrules)
1367     printf("JOB ");
1368   if (why >= solv->systemrules && why < solv->learntrules)
1369     printf("SYSTEM %d ", why - solv->systemrules);
1370   if (solv->learntrules && why >= solv->learntrules)
1371     printf("LEARNED ");
1372   printrule(solv, c);
1373 #endif
1374   if (solv->learntrules && why >= solv->learntrules)
1375     {
1376       for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1377         analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], disablerules);
1378       return;
1379     }
1380   if (why >= solv->jobrules && why < solv->learntrules)
1381     {
1382       if (disablerules)
1383         {
1384           /* turn off rule for further analysis */
1385           c->w1 = 0;
1386         }
1387       /* unify problem */
1388       if (solv->problems.count)
1389         {
1390           for (i = solv->problems.count - 1; i >= 0; i--)
1391             if (solv->problems.elements[i] == 0)
1392               break;
1393             else if (solv->problems.elements[i] == why)
1394               return;
1395         }
1396       queuepush(&solv->problems, why);
1397     }
1398 }
1399
1400
1401 /*
1402  * analyze_unsolvable
1403  */
1404
1405 static void
1406 analyze_unsolvable(Solver *solv, Rule *c, int disablerules)
1407 {
1408   Pool *pool = solv->pool;
1409   Map seen;             /* global? */
1410   Id v, vv, *dp, why;
1411   int l, i, idx;
1412   Id *decisionmap = solv->decisionmap;
1413
1414 #if 0
1415   printf("ANALYZE UNSOLVABLE ----------------------\n");
1416 #endif
1417   mapinit(&seen, pool->nsolvables);
1418   analyze_unsolvable_rule(solv, c, disablerules);
1419   dp = c->d ? pool->whatprovidesdata + c->d : 0;
1420   for (i = -1; ; i++)
1421     {
1422       if (i == -1)
1423         v = c->p;
1424       else if (c->d == 0)
1425         v = i ? 0 : c->w2;
1426       else
1427         v = *dp++;
1428       if (v == 0)
1429         break;
1430       if (DECISIONMAP_TRUE(v))  /* the one true literal */
1431           continue;
1432       vv = v > 0 ? v : -v;
1433       l = solv->decisionmap[vv];
1434       if (l < 0)
1435         l = -l;
1436       MAPSET(&seen, vv);
1437     }
1438   idx = solv->decisionq.count;
1439   while (idx > 0)
1440     {
1441       v = solv->decisionq.elements[--idx];
1442       vv = v > 0 ? v : -v;
1443       if (!MAPTST(&seen, vv))
1444         continue;
1445       why = solv->decisionq_why.elements[idx];
1446       if (!why)
1447         {
1448 #if 0
1449           printf("RPM ");
1450           printruleelement(solv, 0, v);
1451 #endif
1452           continue;
1453         }
1454       c = solv->rules + why;
1455       analyze_unsolvable_rule(solv, c, disablerules);
1456       dp = c->d ? pool->whatprovidesdata + c->d : 0;
1457       for (i = -1; ; i++)
1458         {
1459           if (i == -1)
1460             v = c->p;
1461           else if (c->d == 0)
1462             v = i ? 0 : c->w2;
1463           else
1464             v = *dp++;
1465           if (v == 0)
1466             break;
1467           if (DECISIONMAP_TRUE(v))      /* the one true literal */
1468               continue;
1469           vv = v > 0 ? v : -v;
1470           l = solv->decisionmap[vv];
1471           if (l < 0)
1472             l = -l;
1473           MAPSET(&seen, vv);
1474         }
1475     }
1476   mapfree(&seen);
1477   queuepush(&solv->problems, 0);        /* mark end of this problem */
1478   if (disablerules)
1479     reset_solver(solv);
1480 #if 0
1481   printf("analyze_unsolvables done\n");
1482 #endif
1483 }
1484
1485
1486 /*-----------------------------------------------------------------*/
1487 /* Decision revert */
1488
1489 /*
1490  * revert
1491  * revert decision at level
1492  */
1493
1494 static void
1495 revert(Solver *solv, int level)
1496 {
1497   Id v, vv;
1498   while (solv->decisionq.count)
1499     {
1500       v = solv->decisionq.elements[solv->decisionq.count - 1];
1501       vv = v > 0 ? v : -v;
1502       if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1503         break;
1504 #if 0
1505       printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1506 #endif
1507       solv->decisionmap[vv] = 0;
1508       solv->decisionq.count--;
1509       solv->decisionq_why.count--;
1510       solv->propagate_index = solv->decisionq.count;
1511     }
1512 }
1513
1514
1515 /*
1516  * watch2onhighest
1517  */
1518
1519 static void
1520 watch2onhighest(Solver *solv, Rule *r)
1521 {
1522   int l, wl = 0;
1523   Id v, *dp;
1524
1525   if (!r->d)
1526     return;     /* binary rule, both watches are set */
1527   dp = solv->pool->whatprovidesdata + r->d;
1528   while ((v = *dp++) != 0)
1529     {
1530       l = solv->decisionmap[v < 0 ? -v : v];
1531       if (l < 0)
1532         l = -l;
1533       if (l > wl)
1534         {
1535           r->w2 = dp[-1];
1536           wl = l;
1537         }
1538     }
1539 }
1540
1541
1542 /*
1543  * setpropagatelearn
1544  */
1545
1546 static int
1547 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1548 {
1549   Rule *r;
1550   Id p, d;
1551   int l, why;
1552
1553   if (decision)
1554     {
1555       level++;
1556       if (decision > 0)
1557         solv->decisionmap[decision] = level;
1558       else
1559         solv->decisionmap[-decision] = -level;
1560       queuepush(&solv->decisionq, decision);
1561       queuepush(&solv->decisionq_why, 0);
1562     }
1563   for (;;)
1564     {
1565       r = propagate(solv, level);
1566       if (!r)
1567         break;
1568       if (level == 1)
1569         {
1570           analyze_unsolvable(solv, r, disablerules);
1571           if (disablerules)
1572             return 1;
1573           return 0;
1574         }
1575       printf("conflict with rule #%d\n", (int)(r - solv->rules));
1576       l = analyze(solv, level, r, &p, &d, &why);
1577       if (l >= level || l <= 0)
1578         abort();
1579       printf("reverting decisions (level %d -> %d)\n", level, l);
1580       level = l;
1581       revert(solv, level);
1582       r = addrule(solv, p, d);       /* p requires d */
1583       if (!r)
1584         abort();
1585       if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1586         {
1587           printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1588           abort();
1589         }
1590       queuepush(&solv->learnt_why, why);
1591       if (d)
1592         {
1593           /* at least 2 literals, needs watches */
1594           watch2onhighest(solv, r);
1595           addwatches(solv, r);
1596         }
1597       solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1598       queuepush(&solv->decisionq, p);
1599       queuepush(&solv->decisionq_why, r - solv->rules);
1600       printf("decision: ");
1601       printruleelement(solv, 0, p);
1602       printf("new rule: ");
1603       printrule(solv, r);
1604     }
1605   return level;
1606 }
1607
1608 /*-----------------------------------------------------------------*/
1609 /* Main solver interface */
1610
1611
1612 /*
1613  * solver_create
1614  * create solver structure
1615  *
1616  * pool: all available solvables
1617  * system: installed Solvables
1618  *
1619  *
1620  * Upon solving, rules are created to flag the Solvables
1621  * of the 'system' Source as installed.
1622  */
1623
1624 Solver *
1625 solver_create(Pool *pool, Source *system)
1626 {
1627   Solver *solv;
1628   solv = (Solver *)xcalloc(1, sizeof(Solver));
1629   solv->pool = pool;
1630   solv->system = system;
1631   pool->verbose = 1;
1632
1633   queueinit(&solv->decisionq);
1634   queueinit(&solv->decisionq_why);
1635   queueinit(&solv->problems);
1636   queueinit(&solv->learnt_why);
1637   queueinit(&solv->learnt_pool);
1638
1639   solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1640   solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1641   memset(solv->rules, 0, sizeof(Rule));
1642   solv->nrules = 1;
1643
1644   return solv;
1645 }
1646
1647
1648 /*
1649  * solver_free
1650  */
1651
1652 void
1653 solver_free(Solver *solv)
1654 {
1655   queuefree(&solv->decisionq);
1656   queuefree(&solv->decisionq_why);
1657   queuefree(&solv->learnt_why);
1658   queuefree(&solv->learnt_pool);
1659   xfree(solv->decisionmap);
1660   xfree(solv->rules);
1661   xfree(solv->watches);
1662   xfree(solv->weaksystemrules);
1663   xfree(solv);
1664 }
1665
1666
1667 /*
1668  * reenablerule
1669  * 
1670  * r->w1 was set to 0, now find proper value for w1
1671  */
1672   
1673 static void
1674 reenablerule(Solver *solv, Rule *r)
1675 {
1676   int i;
1677   Id v, l, good;
1678
1679   if (!r->w2)                          /* not a rule, but an assertion */
1680     {
1681       r->w1 = r->p;
1682       return;
1683     }
1684   if (!r->d)
1685     {
1686       if (r->w2 != r->p)
1687         r->w1 = r->p;
1688       else
1689         r->w2 = r->d;                  /* mls: shouldn't this be r->w1 ? */
1690       return;
1691     }
1692   good = 0;
1693                                        /* put it on the first not-false literal */
1694   for (i = -1; ; i++)
1695     {
1696       if (i == -1)
1697         v = r->p;
1698       else
1699         v = solv->pool->whatprovidesdata[r->d + i];
1700       if (!v)
1701         {
1702           printrule(solv, r);
1703           abort();
1704         }
1705       if (v == r->w2)
1706         continue;
1707       l = solv->decisionmap[v > 0 ? v : -v];
1708       if (!l || (v < 0 && l < 0) || (v > 0 && l > 0))
1709         break;
1710     }
1711   r->w1 = v;
1712 }
1713
1714
1715 /*-------------------------------------------------------*/
1716
1717 /*
1718  * run_solver
1719  * 
1720  * all rules have been set up, not actually run the solver
1721  *
1722  */
1723
1724 static void
1725 run_solver(Solver *solv, int disablerules, int doweak)
1726 {
1727   Queue dq;
1728   int systemlevel;
1729   int level, olevel;
1730   Rule *r;
1731   int i, n;
1732   Solvable *s;
1733   Pool *pool = solv->pool;
1734   Id p, *dp;
1735
1736 #if 0
1737   printf("number of rules: %d\n", solv->nrules);
1738 {
1739   int i;
1740   for (i = 0; i < solv->nrules; i++)
1741     {
1742       printrule(solv, solv->rules + i);
1743     }
1744 }
1745 #endif
1746
1747   /* all new rules are learnt after this point */
1748   solv->learntrules = solv->nrules;
1749   /* crate watches lists */
1750   makewatches(solv);
1751
1752   if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1753
1754   /* start SAT algorithm */
1755   level = 1;
1756   systemlevel = level + 1;
1757   if (pool->verbose) printf("solving...\n");
1758
1759   queueinit(&dq);
1760   for (;;)
1761     {
1762       /*
1763        * propagate
1764        */
1765       
1766       if (level == 1)
1767         {
1768           if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1769           if ((r = propagate(solv, level)) != 0)
1770             {
1771               analyze_unsolvable(solv, r, disablerules);
1772               if (disablerules)
1773                 continue;
1774               printf("UNSOLVABLE\n");
1775               queuefree(&dq);
1776               return;
1777             }
1778         }
1779
1780       /*
1781        * system packages
1782        */
1783       
1784       if (level < systemlevel && solv->system->nsolvables)
1785         {
1786           if (!solv->updatesystem)
1787             {
1788               /* try to keep as many packages as possible */
1789               if (pool->verbose) printf("installing system packages\n");
1790               for (i = solv->system->start, n = 0; ; i++, n++)
1791                 {
1792                   if (n == solv->system->nsolvables)
1793                     break;
1794                   if (i == solv->system->start + solv->system->nsolvables)
1795                     i = solv->system->start;
1796                   s = pool->solvables + i;
1797                   if (solv->decisionmap[i] != 0)
1798                     continue;
1799 #if 0
1800                   printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1801 #endif
1802                   olevel = level;
1803                   level = setpropagatelearn(solv, level, i, disablerules);
1804                   if (level == 0)
1805                     {
1806                       printf("UNSOLVABLE\n");
1807                       queuefree(&dq);
1808                       return;
1809                     }
1810                   if (level <= olevel)
1811                     n = 0;
1812                 }
1813             }
1814           if (solv->weaksystemrules)
1815             {
1816               if (pool->verbose) printf("installing weak system packages\n");
1817               for (i = solv->system->start, n = 0; ; i++, n++)
1818                 {
1819                   if (n == solv->system->nsolvables)
1820                     break;
1821                   if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1822                     continue;
1823                   QUEUEEMPTY(&dq);
1824                   if (solv->decisionmap[i] == 0)
1825                     queuepush(&dq, i);
1826                   if (solv->weaksystemrules[i - solv->system->start])
1827                     {
1828                       dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1829                       while ((p = *dp++) != 0)
1830                         {
1831                           if (solv->decisionmap[p] > 0)
1832                             break;
1833                           if (solv->decisionmap[p] == 0)
1834                             queuepush(&dq, p);
1835                         }
1836                       if (p)
1837                         continue;       /* rule is already true */
1838                     }
1839                   if (!dq.count)
1840                     continue;
1841
1842                   if (dq.count > 1)
1843                     prune_to_recommended(solv, &dq);
1844                   if (dq.count > 1)
1845                     prune_best_version_arch(pool, &dq);
1846 #if 0
1847                   s = pool->solvables + dq.elements[0];
1848                   printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1849 #endif
1850                   olevel = level;
1851                   level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
1852                   if (level == 0)
1853                     {
1854                       printf("UNSOLVABLE\n");
1855                       queuefree(&dq);
1856                       return;
1857                     }
1858                   if (level <= olevel)
1859                     {
1860                       n = 0;
1861                       break;
1862                     }
1863                 }
1864               if (n != solv->system->nsolvables)
1865                 continue;
1866             }
1867           systemlevel = level;
1868         }
1869
1870       /*
1871        * decide
1872        */
1873       
1874       if (pool->verbose) printf("deciding unresolved rules\n");
1875       for (i = 1, n = 1; ; i++, n++)
1876         {
1877           if (n == solv->nrules)
1878             break;
1879           if (i == solv->nrules)
1880             i = 1;
1881           r = solv->rules + i;
1882           if (!r->w1)
1883             continue;
1884           QUEUEEMPTY(&dq);
1885           if (r->d == 0)
1886             {
1887               /* binary or unary rule */
1888               /* need two positive undecided literals */
1889               if (r->p < 0 || r->w2 <= 0)
1890                 continue;
1891               if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1892                 continue;
1893               queuepush(&dq, r->p);
1894               queuepush(&dq, r->w2);
1895             }
1896           else
1897             {
1898               /* make sure that
1899                * all negative literals are installed
1900                * no positive literal is installed
1901                * i.e. the rule is not fulfilled and we
1902                * just need to decide on the positive literals
1903                */
1904               if (r->p < 0)
1905                 {
1906                   if (solv->decisionmap[-r->p] <= 0)
1907                     continue;
1908                 }
1909               else
1910                 {
1911                   if (solv->decisionmap[r->p] > 0)
1912                     continue;
1913                   if (solv->decisionmap[r->p] == 0)
1914                     queuepush(&dq, r->p);
1915                 }
1916               dp = pool->whatprovidesdata + r->d;
1917               while ((p = *dp++) != 0)
1918                 {
1919                   if (p < 0)
1920                     {
1921                       if (solv->decisionmap[-p] <= 0)
1922                         break;
1923                     }
1924                   else
1925                     {
1926                       if (solv->decisionmap[p] > 0)
1927                         break;
1928                       if (solv->decisionmap[p] == 0)
1929                         queuepush(&dq, p);
1930                     }
1931                 }
1932               if (p)
1933                 continue;
1934             }
1935           if (dq.count < 2)
1936             {
1937               /* cannot happen as this means that
1938                * the rule is unit */
1939               printrule(solv, r);
1940               abort();
1941             }
1942           prune_to_recommended(solv, &dq);
1943           if (dq.count > 1)
1944             prune_best_version_arch(pool, &dq);
1945           p = dq.elements[dq.count - 1];
1946           s = pool->solvables + p;
1947 #if 0
1948           printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1949 #endif
1950           olevel = level;
1951           level = setpropagatelearn(solv, level, p, disablerules);
1952           if (level == 0)
1953             {
1954               printf("UNSOLVABLE\n");
1955               queuefree(&dq);
1956               return;
1957             }
1958           if (level < systemlevel)
1959             break;
1960           if (level <= olevel)
1961             n = 0;
1962         } /* for(), decide */
1963
1964       if (n != solv->nrules)    /* continue if level < systemlevel */
1965         continue;
1966       
1967       if (doweak && !solv->problems.count)
1968         {
1969           int qcount;
1970
1971           if (pool->verbose) printf("installing recommended packages\n");
1972           QUEUEEMPTY(&dq);
1973           for (i = 1; i < pool->nsolvables; i++)
1974             {
1975               if (solv->decisionmap[i] < 0)
1976                 continue;
1977               if (solv->decisionmap[i] > 0)
1978                 {
1979                   Id *recp, rec, *pp, p;
1980                   s = pool->solvables + i;
1981                   /* installed, check for recommends */
1982                   /* XXX need to special case AND ? */
1983                   if ((recp = s->recommends) != 0)
1984                     {
1985                       while ((rec = *recp++) != 0)
1986                         {
1987                           qcount = dq.count;
1988                           FOR_PROVIDES(p, pp, rec)
1989                             {
1990                               if (solv->decisionmap[p] > 0)
1991                                 {
1992                                   dq.count = qcount;
1993                                   break;
1994                                 }
1995                               else if (solv->decisionmap[p] == 0)
1996                                 queuepushunique(&dq, p);
1997                             }
1998                         }
1999                     }
2000                 }
2001               else
2002                 {
2003                   Id *supp, sup;
2004                   s = pool->solvables + i;
2005                   if (!s->supplements && !s->freshens)
2006                     continue;
2007                   if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
2008                     continue;
2009                   if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
2010                     continue;
2011                   if ((supp = s->supplements) != 0)
2012                     {
2013                       while ((sup = *supp++) != 0)
2014                         if (dep_fulfilled(solv, sup))
2015                           break;
2016                       if (!sup)
2017                         continue;
2018                     }
2019                   if ((supp = s->freshens) != 0)
2020                     {
2021                       while ((sup = *supp++) != 0)
2022                         if (dep_fulfilled(solv, sup))
2023                           break;
2024                       if (!sup)
2025                         continue;
2026                     }
2027                   queuepushunique(&dq, i);
2028                 }
2029             }
2030           if (dq.count)
2031             {
2032               prune_best_version_arch(pool, &dq);
2033               p = dq.elements[dq.count - 1];
2034               s = pool->solvables + p;
2035 #if 1
2036               printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2037 #endif
2038               level = setpropagatelearn(solv, level, p, 0);
2039               continue;
2040             }
2041         }
2042       break;
2043     }
2044   queuefree(&dq);
2045 }
2046
2047   
2048 /*
2049  * refine_suggestion
2050  */
2051   
2052 static void
2053 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2054 {
2055   Rule *r;
2056   int i, j;
2057   Id v;
2058   Queue disabled;
2059   int disabledcnt;
2060
2061   printf("refine_suggestion start\n");
2062   queueinit(&disabled);
2063   QUEUEEMPTY(refined);
2064   queuepush(refined, sug);
2065
2066   /* re-enable all rules but rule "sug" of the problem */
2067   for (i = 0; problem[i]; i++)
2068     {
2069       if (problem[i] == sug)
2070         continue;
2071       r = solv->rules + problem[i];
2072 #if 0
2073       printf("enable ");
2074       printrule(solv, r);
2075 #endif
2076       reenablerule(solv, r);
2077     }
2078   for (;;)
2079     {
2080       revert(solv, 1);          /* XXX move to reset_solver? */
2081       reset_solver(solv);
2082       QUEUEEMPTY(&solv->problems);
2083       run_solver(solv, 0, 0);
2084       if (!solv->problems.count)
2085         {
2086           printf("no more problems!\n");
2087 #if 0
2088           printdecisions(solv);
2089 #endif
2090           break;                /* great, no more problems */
2091         }
2092       disabledcnt = disabled.count;
2093       for (i = 0; i < solv->problems.elements[i]; i++)
2094         {
2095           /* ignore solutions in refined */
2096           v = solv->problems.elements[i];
2097           for (j = 0; problem[j]; j++)
2098             if (problem[j] != sug && problem[j] == v)
2099               break;
2100           if (problem[j])
2101             continue;
2102           queuepush(&disabled, v);
2103         }
2104       if (disabled.count == disabledcnt)
2105         {
2106           /* no solution found, this was an invalid suggestion! */
2107           printf("no solution found!\n");
2108           for (i = 0; i < refined->count; i++)
2109             reenablerule(solv, solv->rules + refined->elements[i]);
2110           refined->count = 0;
2111           break;
2112         }
2113       if (disabled.count == disabledcnt + 1)
2114         {
2115           /* just one solution, add it to refined list */
2116           queuepush(refined, disabled.elements[disabledcnt]);
2117         }
2118       else
2119         {
2120           printf("##############################################   more than one solution found.\n");
2121 #if 1
2122           for (i = 0; i < solv->problems.elements[i]; i++)
2123             {
2124               printrule(solv, solv->rules + solv->problems.elements[i]);
2125             }
2126           printf("##############################################\n");
2127 #endif
2128           /* more than one solution */
2129           /* for now return */
2130         }
2131       for (i = disabledcnt; i < disabled.count; i++)
2132         {
2133           r = solv->rules + disabled.elements[i];;
2134           /* disable it */
2135           r->w1 = 0;
2136 #if 0
2137           printf("disable ");
2138           printrule(solv, r);
2139 #endif
2140         }
2141     }
2142   /* enable refined rules again */
2143   for (i = 0; i < disabled.count; i++)
2144     reenablerule(solv, solv->rules + disabled.elements[i]);
2145   /* disable problem rules again so that we are in the same state as before */
2146   for (i = 0; problem[i]; i++)
2147     {
2148       r = solv->rules + problem[i];
2149       r->w1 = 0;
2150     }
2151   printf("refine_suggestion end\n");
2152 }
2153
2154   
2155 /*
2156  * printdecisions
2157  */
2158   
2159 static const char *
2160 id2rc(Solver *solv, Id id)
2161 {
2162   const char *evr;
2163   if (solv->rc_output != 2)
2164     return "";
2165   evr = id2str(solv->pool, id);
2166   if (*evr < '0' || *evr > '9')
2167     return "0:";
2168   while (*evr >= '0' && *evr <= '9')
2169     evr++;
2170   if (*evr != ':')
2171     return "0:";
2172   return "";
2173 }
2174
2175 void
2176 printdecisions(Solver *solv)
2177 {
2178   Pool *pool = solv->pool;
2179   Id p, *obsoletesmap;
2180   int i;
2181   Solvable *s;
2182
2183   obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2184   for (i = 0; i < solv->decisionq.count; i++)
2185     {
2186       Id obs, *obsp;
2187       Id *pp, n;
2188
2189       n = solv->decisionq.elements[i];
2190       if (n < 0)
2191         continue;
2192       if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2193         continue;
2194       s = pool->solvables + n;
2195       if ((obsp = s->obsoletes) != 0)
2196         while ((obs = *obsp++) != 0)
2197           FOR_PROVIDES(p, pp, obs)
2198             {
2199               if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2200                 {
2201                   obsoletesmap[p] = n;
2202                   obsoletesmap[n]++;
2203                 }
2204             }
2205       FOR_PROVIDES(p, pp, s->name)
2206         if (s->name == pool->solvables[p].name)
2207           {
2208             if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2209               {
2210                 obsoletesmap[p] = n;
2211                 obsoletesmap[n]++;
2212               }
2213           }
2214     }
2215
2216   if (solv->rc_output)
2217     printf(">!> Solution #1:\n");
2218
2219   int installs = 0, uninstalls = 0, upgrades = 0;
2220   
2221   /* print solvables to be erased */
2222
2223   for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2224     {
2225       if (solv->decisionmap[i] > 0)
2226         continue;
2227       if (obsoletesmap[i])
2228         continue;
2229       s = pool->solvables + i;
2230       if (solv->rc_output == 2)
2231         printf(">!> remove  %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2232       else if (solv->rc_output)
2233         printf(">!> remove  %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2234       else
2235         printf("erase   %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2236       uninstalls++;
2237     }
2238
2239   /* print solvables to be installed */
2240
2241   for (i = 0; i < solv->decisionq.count; i++)
2242     {
2243       int j;
2244       p = solv->decisionq.elements[i];
2245       if (p < 0)
2246         continue;
2247       if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2248         continue;
2249       s = pool->solvables + p;
2250
2251       if (!obsoletesmap[p])
2252         {
2253           if (solv->rc_output)
2254             printf(">!> ");
2255           printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2256           if (solv->rc_output != 2)
2257             printf(".%s", id2str(pool, s->arch));
2258           installs++;
2259         }
2260       else if (!solv->rc_output)
2261         {
2262           printf("update  %s-%s.%s  (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2263           for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2264             {
2265               if (obsoletesmap[j] != p)
2266                 continue;
2267               s = pool->solvables + j;
2268               printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2269             }
2270           printf(")");
2271           upgrades++;
2272         }
2273       else
2274         {
2275           Solvable *f, *fn = 0;
2276           for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2277             {
2278               if (obsoletesmap[j] != p)
2279                 continue;
2280               f = pool->solvables + j;
2281               if (fn || f->name != s->name)
2282                 {
2283                   if (solv->rc_output == 2)
2284                     printf(">!> remove  %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2285                   else if (solv->rc_output)
2286                     printf(">!> remove  %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2287                   uninstalls++;
2288                 }
2289               else
2290                 fn = f;
2291             }
2292           if (!fn)
2293             {
2294               printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2295               if (solv->rc_output != 2)
2296                 printf(".%s", id2str(pool, s->arch));
2297               installs++;
2298             }
2299           else
2300             {
2301               if (solv->rc_output == 2)
2302                 printf(">!> upgrade %s-%s => %s-%s%s", id2str(pool, fn->name), id2str(pool, fn->evr), id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2303               else
2304                 printf(">!> upgrade %s-%s.%s => %s-%s.%s", id2str(pool, fn->name), id2str(pool, fn->evr), id2str(pool, fn->arch), id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2305               upgrades++;
2306             }
2307         }
2308       if (solv->rc_output)
2309         {
2310           Source *source = pool_source(pool, s);
2311           if (source)
2312             printf("[%s]", source_name(source));
2313         }
2314       printf("\n");
2315     }
2316
2317   if (solv->rc_output)
2318     printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2319   
2320   xfree(obsoletesmap);
2321 }
2322
2323
2324 /*-----------------------------------------------------------------*/
2325 /* main() */
2326
2327 /*
2328  *
2329  * solve job queue
2330  *
2331  */
2332
2333 void
2334 solve(Solver *solv, Queue *job)
2335 {
2336   Pool *pool = solv->pool;
2337   int i;
2338   Map addedmap;                        /* '1' == have rule for solvable */
2339   Map noupdaterule;                    /* '1' == don't update (scheduled for removal) */
2340   Id how, what, p, *pp, d;
2341   Queue q;
2342   Rule *r;
2343   Solvable *s;
2344
2345   /*
2346    * create basic rule set of all involved packages
2347    * as bitmaps
2348    * 
2349    */
2350
2351   mapinit(&addedmap, pool->nsolvables);
2352   mapinit(&noupdaterule, pool->nsolvables);
2353
2354   queueinit(&q);
2355
2356   /*
2357    * create rules for installed solvables -> keep them installed
2358    * so called: rpm rules
2359    * 
2360    */
2361
2362   for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2363     addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2364
2365   /*
2366    * create install rules
2367    * 
2368    * two passes, as we want to keep the rpm rules distinct from the job rules
2369    * 
2370    */
2371
2372   /*
2373    * solvable rules
2374    *  process job rules for solvables
2375    */
2376   
2377   for (i = 0; i < job->count; i += 2)
2378     {
2379       how = job->elements[i];
2380       what = job->elements[i + 1];
2381
2382       switch(how)
2383         {
2384         case SOLVER_INSTALL_SOLVABLE:
2385           addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2386           break;
2387         case SOLVER_INSTALL_SOLVABLE_NAME:
2388         case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2389           QUEUEEMPTY(&q);
2390           FOR_PROVIDES(p, pp, what)
2391             {
2392                                        /* if by name, ensure that the name matches */
2393               if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2394                 continue;
2395               addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2396             }
2397           break;
2398         case SOLVER_INSTALL_SOLVABLE_UPDATE:
2399           /* dont allow downgrade */
2400           addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2401           break;
2402         }
2403     }
2404
2405   /*
2406    * if unstalls are disallowed, add update rules for every
2407    * installed solvables in the hope to circumvent uninstall
2408    * by upgrading
2409    * 
2410    */
2411   
2412 #if 0
2413   if (!solv->allowuninstall)
2414     {
2415       /* add update rule for every installed package */
2416       for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2417         addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2418     }
2419 #else  /* this is just to add the needed rpm rules to our set */
2420   for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2421     addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2422 #endif
2423
2424   addrulesforsupplements(solv, &addedmap);
2425
2426   /*
2427    * first pass done
2428    * 
2429    * unify existing rules before going over all job rules
2430    * 
2431    */
2432   
2433   unifyrules(solv);     /* remove duplicate rpm rules */
2434
2435   /*
2436    * at this point the system is always solvable,
2437    * as an empty system (remove all packages) is a valid solution
2438    */
2439   if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2440
2441   /*
2442    * now add all job rules
2443    */
2444   
2445   solv->jobrules = solv->nrules;
2446
2447   for (i = 0; i < job->count; i += 2)
2448     {
2449       how = job->elements[i];
2450       what = job->elements[i + 1];
2451       switch(how)
2452         {
2453         case SOLVER_INSTALL_SOLVABLE:                     /* install specific solvable */
2454           if (solv->rc_output) {
2455             Solvable *s = pool->solvables + what;
2456             printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), source_name(pool_source(pool, s)));
2457           }
2458           addrule(solv, what, 0);                         /* install by Id */
2459           break;
2460         case SOLVER_ERASE_SOLVABLE:
2461           addrule(solv, -what, 0);                        /* remove by Id */
2462           MAPSET(&noupdaterule, what);
2463           break;
2464         case SOLVER_INSTALL_SOLVABLE_NAME:                /* install by capability */
2465         case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2466           QUEUEEMPTY(&q);
2467           FOR_PROVIDES(p, pp, what)    /* check all providers */
2468             {
2469                                        /* if by name, ensure that the name matches */
2470               if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2471                 continue;
2472               queuepush(&q, p);
2473             }
2474           if (!q.count) {              /* no provider found -> abort */
2475             fprintf(stderr, "Nothing provides '%s'\n", id2str(pool, what));
2476             /* XXX make this a problem! */
2477             return;
2478             abort();
2479           }
2480
2481           p = queueshift(&q);          /* get first provider */
2482           if (!q.count)
2483             d = 0;                     /* single provider ? -> make assertion */
2484           else
2485             d = pool_queuetowhatprovides(pool, &q);   /* get all providers */
2486           addrule(solv, p, d);         /* add 'requires' rule */
2487           break;
2488         case SOLVER_ERASE_SOLVABLE_NAME:                  /* remove by capability */
2489         case SOLVER_ERASE_SOLVABLE_PROVIDES:
2490           FOR_PROVIDES(p, pp, what)
2491             {
2492                                        /* if by name, ensure that the name matches */
2493               if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2494                 continue;
2495
2496               addrule(solv, -p, 0);  /* add 'remove' rule */
2497               MAPSET(&noupdaterule, p);
2498             }
2499           break;
2500         case SOLVER_INSTALL_SOLVABLE_UPDATE:              /* find update for solvable */
2501           addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2502           break;
2503         }
2504     }
2505
2506   if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2507   
2508   /*
2509    * now add policy rules
2510    * 
2511    */
2512   
2513   solv->systemrules = solv->nrules;
2514
2515   /*
2516    * create rules for updating installed solvables
2517    * 
2518    * (Again ?)
2519    * 
2520    */
2521   
2522   if (!solv->allowuninstall)
2523     {                                  /* loop over all installed solvables */
2524       for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2525       {
2526         if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2527           addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2528         else
2529           addrule(solv, 0, 0);          /* place holder */
2530       }
2531       /* consistency check: we added a rule for _every_ system solvable */
2532       if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2533         abort();
2534     }
2535
2536   if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2537
2538   /* create special weak system rules */
2539   if (solv->system->nsolvables)
2540     {
2541       solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2542       for (i = 0; i < solv->system->nsolvables; i++)
2543         {
2544           findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2545           if (q.count)
2546             solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2547         }
2548     }
2549
2550   /* free unneeded memory */
2551   mapfree(&addedmap);
2552   mapfree(&noupdaterule);
2553   queuefree(&q);
2554
2555   /*
2556    * solve !
2557    * 
2558    */
2559   
2560   run_solver(solv, 1, 1);
2561
2562   /*
2563    *
2564    * print solver result
2565    * 
2566    */
2567
2568   if (pool->verbose) printf("-------------------------------------------------------------\n");
2569
2570   if (solv->problems.count)
2571     {
2572       Queue problems;
2573       Queue solution;
2574       Id *problem;
2575       Id why;
2576       int j;
2577
2578       if (!pool->verbose)
2579         return;
2580       clonequeue(&problems, &solv->problems);
2581       queueinit(&solution);
2582       printf("Encountered problems! Here are the solutions:\n");
2583       problem = problems.elements;
2584       for (i = 0; i < problems.count; i++)
2585         {
2586           Id v = problems.elements[i];
2587           if (v == 0)
2588             {
2589               printf("====================================\n");
2590               problem = problems.elements + i + 1;
2591               continue;
2592             }
2593           refine_suggestion(solv, problem, v, &solution);
2594           for (j = 0; j < solution.count; j++)
2595             {
2596               r = solv->rules + solution.elements[j];
2597               why = solution.elements[j];
2598 #if 0
2599               printrule(solv, r);
2600 #endif
2601               if (why >= solv->jobrules && why < solv->systemrules)
2602                 {
2603                   /* do a sloppy job of analyzing the job rule */
2604                   if (r->p > 0)
2605                     {
2606                       s = pool->solvables + r->p;
2607                       printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2608                     }
2609                   else
2610                     {
2611                       s = pool->solvables - r->p;
2612                       printf("- do not erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2613                     }
2614                 }
2615               else if (why >= solv->systemrules && why < solv->learntrules)
2616                 {
2617                   Solvable *sd = 0;
2618                   s = pool->solvables + solv->system->start + (why - solv->systemrules);
2619                   if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2620                     {
2621                       Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2622                       for (; *dp; dp++)
2623                         if (solv->decisionmap[*dp] > 0)
2624                           {
2625                             sd = pool->solvables + *dp;
2626                             break;
2627                           }
2628                     }
2629                   if (sd)
2630                     {
2631                       printf("- allow downgrade of %s-%s.%s to %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), id2str(pool, sd->name), id2str(pool, sd->evr), id2str(pool, sd->arch));
2632                     }
2633                   else
2634                     {
2635                       printf("- allow deinstallation of %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2636                     }
2637                 }
2638               else
2639                 {
2640                   abort();
2641                 }
2642             }
2643           printf("------------------------------------\n");
2644         }
2645       return;
2646     }
2647
2648   printdecisions(solv);
2649 }
2650
2651
2652 // EOF