summaryrefslogtreecommitdiff
path: root/src/generic/apt/aptitude_resolver.h
blob: a15502a7d4cfec3770436da75ebe898e9b061ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// aptitude_resolver.h                  -*-c++-*-
//
// 
//   Copyright (C) 2005, 2008 Daniel Burrows

//   This program is free software; you can redistribute it and/or
//   modify it under the terms of the GNU General Public License as
//   published by the Free Software Foundation; either version 2 of
//   the License, or (at your option) any later version.

//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//   General Public License for more details.

//   You should have received a copy of the GNU General Public License
//   along with this program; see the file COPYING.  If not, write to
//   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//   Boston, MA 02111-1307, USA.
//
// 
//


#ifndef APTITUDE_RESOLVER_H
#define APTITUDE_RESOLVER_H

#include "aptitude_resolver_universe.h"

#include <generic/apt/matching/pattern.h>
#include <generic/problemresolver/problemresolver.h>

#include <generic/util/immset.h>

/** \brief Glue code to make the resolver talk to the core aptitude classes.
 *
 *  
 *  General comment on how the iterators are handled: basically the
 *  technique is (generally) to have a normalize() routine that
 *  advances the current iterator(s) to the next "interesting"
 *  iterator.  For instance, broken_dep_iterator::normalize() moves to
 *  the next broken dependency (sort of).  If the current iterator is
 *  already interesting, nothing happens.  This is used on
 *  initialization and in operator++ (after advancing the iterator a
 *  single step manually).
 * 
 *  \file aptitude_resolver.h
 */

namespace aptitude
{
  namespace matching
  {
    class pattern;
  }
}

class aptitude_resolver:public generic_problem_resolver<aptitude_universe>
{
  imm::map<package, action> keep_all_solution;

  void add_full_replacement_score(const pkgCache::VerIterator &src,
				  const pkgCache::PkgIterator &real_target,
				  const pkgCache::VerIterator &provider,
				  int full_replacement_score,
				  int undo_full_replacement_score);

public:
  class resolver_hint
  {
  public:
    /** \brief The type of hint represented by this object. */
    enum hint_type
      {
	/** \brief A hint that one or more package versions should be
	 *  rejected.
	 */
	reject,
	/** \brief A hint that one or more package versions should be
	 *   mandated.
	 */
	mandate,
	/** \brief A hint that one or more package versions should
	 *  have their scores adjusted by some amount.
	 */
	tweak_score
      };

  private:
    hint_type type;
    int score;
    cwidget::util::ref_ptr<aptitude::matching::pattern> target;
    std::string version;

    resolver_hint(hint_type _type, int _score,
		  const cwidget::util::ref_ptr<aptitude::matching::pattern> &_target,
		  const std::string &_version)
      : type(_type), score(_score), target(_target), version(_version)
    {
    }

  public:
    resolver_hint()
      : type((hint_type)-1), score(-1), target(NULL), version()
    {
    }

    ~resolver_hint();

    /** \brief Create a hint that rejects a version or versions of a package. */
    static resolver_hint make_reject(const cwidget::util::ref_ptr<aptitude::matching::pattern> &target,
				     const std::string &version)
    {
      return resolver_hint(reject, 0, target, version);
    }

    /** \brief Create a hint that mandates a version or versions of a package. */
    static resolver_hint make_mandate(const cwidget::util::ref_ptr<aptitude::matching::pattern> &target,
				      const std::string &version)
    {
      return resolver_hint(mandate, 0, target, version);
    }

    /** \brief Create a hint that adjust the score of a package. */
    static resolver_hint make_tweak_score(const cwidget::util::ref_ptr<aptitude::matching::pattern> &target,
					  const std::string &version,
					  int score)
    {
      return resolver_hint(tweak_score, score, target, version);
    }

    /** \brief Parse a resolver hint definition.
     *
     *  Definitions have the form ACTION TARGET [VERSION].  ACTION is
     *  either a number (which will be added to the score of the
     *  selected version), or the special strings "reject" or
     *  "accept".  If TARGET is a match pattern (specifically, if the
     *  portion of the remaining string that parses as a match pattern
     *  includes a question mark or tilde), then it will be treated as
     *  such; otherwise it is the name of the package to match.
     *  VERSION is the version of TARGET that is to be tweaked.  If
     *  VERSION is not present, all versions of the package (except
     *  the removal version) that match TARGET will be selected.  If
     *  VERSION has the form "/<archive>" then the version of the
     *  package from that archive will be selected.  If VERSION is
     *  ":UNINST" then the not-installed version of the package will be
     *  selected.
     */
    static resolver_hint parse(const std::string &definition);

    /** \brief Get the type of this hint.
     *
     *  \sa hint_type
     */
    hint_type get_type() const { return type; }

    /** \brief For score-tweaking hints, get the number of points to be
     *  added to the version's score.
     */
    int get_score() const { return score; }

    /** \brief Return the pattern identifying the package or packages
     *  to be adjusted.
     */
    const cwidget::util::ref_ptr<aptitude::matching::pattern> &
    get_target() const { return target; }

    /** \brief Return the version selected by this hint.
     *
     *  \todo Perhaps this should just test whether a version matches
     *  instead?
     */
    const std::string &get_version() const { return version; }
  };

  aptitude_resolver(int step_score, int broken_score,
		    int unfixed_soft_score,
		    int infinity,
		    int resolution_score,
		    const std::vector<resolver_hint> &hints,
		    aptitudeDepCache *cache);

  /** \brief Return \b true if the given version will break a hold or
   *  install a forbidden version.
   */
  bool is_break_hold(const version &v) const;

  /** Assign scores to all packages and all package versions according
   *  to its arguments.  All scores are assigned with add_score, so
   *  this can be easily combined with other policies.
   *
   * \param preserve_score the score to assign to the version that the
   * user selected.
   *
   * \param auto_score the score to assign to automatically assigned
   * actions.  By making this smaller than preserve_score you can bias
   * the system towards overriding automatic decisions rather than
   * user actions.
   *
   * \param remove_score the score to assign to removing a package
   * against the user's wishes.
   *
   * \param keep_score the score to assign to cancelling actions on a
   * package against the user's wishes.
   *
   * \param install_score the score to assign to removing a package
   * against the user's wishes.
   *
   * \param upgrade_score the score to assign to upgrading a package
   * against the user's wishes.
   *
   * \param non_default_score the score to assign to installing a
   * non-default version of a package (such as a downgrade or an
   * experimental version).
   *
   * \param essential_remove an additional modification applied to the
   * removal of an essential package (typically used to deep-six such
   * solutions by, eg, setting it to -100000)
   *
   * \param full_replacement_score the score for removing a package p
   * and installing a package that fully replaces p (i.e., conflicts,
   * provides, and replaces it).
   *
   * \param undo_full_replacement_score the score for installing a
   * package p and removing a package that fully replaces p.
   *
   * \param break_hold_score an additional modification applied to
   * solutions that break a hold or violate a forbidding.
   *
   * \param allow_break_holds_and_forbids   if false, versions that
   * would break a package hold or install a forbidden version are
   * rejected up-front.
   */
  void add_action_scores(int preserve_score, int auto_score,
			 int remove_score, int keep_score,
			 int install_score, int upgrade_score,
			 int non_default_score, int essential_remove,
			 int full_replacement_score,
			 int undo_full_replacement_score,
			 int break_hold_score,
			 bool allow_break_holds_and_forbids);

  /** Score packages/versions according to their priorities.  Normally
   *  you want important>=required>=standard>=optional>=extra.
   *
   *  \param important score modification for Important versions
   *  \param required score modification for Required versions
   *  \param standard score modification for Standard versions
   *  \param optional score modification for Optional versions
   *  \param extra score modification for Extra versions
   */
  void add_priority_scores(int important, int required, int standard,
			   int optional, int extra);

  /** \return the "keep-all" solution, the solution that cancels
   *  all of the user's planned actions.
   */
  imm::map<package, action> get_keep_all_solution() const;
};

#endif