Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / solver / detail / SolutionAction.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* SolutionAction.cc
3  *
4  * Easy-to use interface to the ZYPP dependency resolver
5  *
6  * Copyright (C) 2000-2002 Ximian, Inc.
7  * Copyright (C) 2005 SUSE Linux Products GmbH
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License,
11  * version 2, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307, USA.
22  */
23
24 #define ZYPP_USE_RESOLVER_INTERNALS
25
26 #include "zypp/solver/detail/Resolver.h"
27 #include "zypp/solver/detail/SolutionAction.h"
28 #include "zypp/solver/detail/SolverQueueItem.h"
29 #include "zypp/Capabilities.h"
30 #include "zypp/base/Logger.h"
31
32 /////////////////////////////////////////////////////////////////////////
33 namespace zypp
34 { ///////////////////////////////////////////////////////////////////////
35   ///////////////////////////////////////////////////////////////////////
36   namespace solver
37   { /////////////////////////////////////////////////////////////////////
38     /////////////////////////////////////////////////////////////////////
39     namespace detail
40     { ///////////////////////////////////////////////////////////////////
41
42 using namespace std;
43
44 IMPL_PTR_TYPE(SolutionAction);
45
46 //---------------------------------------------------------------------------
47
48 SolutionAction::SolutionAction()
49 {
50 }
51
52
53 SolutionAction::~SolutionAction()
54 {
55 }
56
57
58 //---------------------------------------------------------------------------
59
60 ostream &
61 TransactionSolutionAction::dumpOn( ostream& os) const
62 {
63     os << "TransactionSolutionAction: ";
64     switch (_action) {
65         case KEEP:                      os << "Keep " << _item; break;
66         case INSTALL:                   os << "Install " << _item; break;
67         case REMOVE:                    os << "Remove " << _item; break;
68         case UNLOCK:                    os << "Unlock " << _item; break;
69         case LOCK:                      os << "Lock " << _item; break;
70         case REMOVE_EXTRA_REQUIRE:      os << "Remove require " << _capability; break;
71         case REMOVE_EXTRA_CONFLICT:     os << "Remove conflict " << _capability; break;
72         case ADD_SOLVE_QUEUE_ITEM:      os << "Add SolveQueueItem " <<  _solverQueueItem; break;
73         case REMOVE_SOLVE_QUEUE_ITEM:   os << "Remove SolveQueueItem " <<  _solverQueueItem; break;
74     }
75     return os;
76 }
77
78
79 ostream&
80 operator<<( ostream& os, const SolutionActionList & actionlist)
81 {
82     for (SolutionActionList::const_iterator iter = actionlist.begin(); iter != actionlist.end(); ++iter) {
83         os << *(*iter);
84         os << endl;
85     }
86     return os;
87 }
88
89 //---------------------------------------------------------------------------
90
91 ostream &
92 InjectSolutionAction::dumpOn( ostream& os ) const
93 {
94     os << "InjectSolutionAction: ";
95     switch (_kind) {
96         case WEAK:      os << "Weak"; break;
97         default: os << "Wrong kind"; break;
98     }
99     os << " ";
100     os << _item;
101     return os;
102 }
103
104 //---------------------------------------------------------------------------
105
106
107 ostream &
108 SolutionAction::dumpOn( std::ostream & os ) const
109 {
110     os << "SolutionAction<";
111     os << "not specified";
112     os << "> ";
113     return os;
114 }
115
116
117 bool
118 TransactionSolutionAction::execute(ResolverInternal & resolver) const
119 {
120     bool ret = true;
121     switch (action()) {
122         case KEEP:
123             _item.status().resetTransact (ResStatus::USER);
124             ret = _item.status().setTransact (false, ResStatus::APPL_HIGH); // APPL_HIGH: Locking should not be saved permanently
125             break;
126         case INSTALL:
127             if (_item.status().isToBeUninstalled())
128                 ret = _item.status().setTransact (false, ResStatus::USER);
129             else
130                 _item.status().setToBeInstalled (ResStatus::USER);
131             break;
132         case REMOVE:
133             if (_item.status().isToBeInstalled()) {
134                 _item.status().setTransact (false,ResStatus::USER);
135                 _item.status().setLock (true,ResStatus::USER); // no other dependency can set it again
136             } else if (_item.status().isInstalled())
137                 _item.status().setToBeUninstalled (ResStatus::USER);
138             else
139                 _item.status().setLock (true,ResStatus::USER); // no other dependency can set it again
140             break;
141         case UNLOCK:
142             ret = _item.status().setLock (false, ResStatus::USER);
143             if (!ret) ERR << "Cannot unlock " << _item << endl;
144             break;
145         case LOCK:
146             _item.status().resetTransact (ResStatus::USER);
147             ret = _item.status().setLock (true, ResStatus::APPL_HIGH); // APPL_HIGH: Locking should not be saved permanently
148             if (!ret) ERR << "Cannot lock " << _item << endl;
149             break;
150         case REMOVE_EXTRA_REQUIRE:
151             resolver.removeExtraRequire (_capability);
152             break;
153         case REMOVE_EXTRA_CONFLICT:
154             resolver.removeExtraConflict (_capability);
155             break;
156         case ADD_SOLVE_QUEUE_ITEM:
157             resolver.addQueueItem(_solverQueueItem);
158             break;
159         case REMOVE_SOLVE_QUEUE_ITEM:
160             resolver.removeQueueItem(_solverQueueItem);
161             break;
162         default:
163             ERR << "Wrong TransactionKind" << endl;
164             ret = false;
165     }
166     return ret;
167 }
168
169 bool
170 InjectSolutionAction::execute(ResolverInternal & resolver) const
171 {
172     switch (_kind) {
173         case WEAK:
174             // set item dependencies to weak
175             resolver.addWeak (_item);
176             break;
177         default:
178             ERR << "No valid InjectSolutionAction kind found" << endl;
179             return false;
180     }
181
182     return true;
183 }
184
185       ///////////////////////////////////////////////////////////////////
186     };// namespace detail
187     /////////////////////////////////////////////////////////////////////
188     /////////////////////////////////////////////////////////////////////
189   };// namespace solver
190   ///////////////////////////////////////////////////////////////////////
191   ///////////////////////////////////////////////////////////////////////
192 };// namespace zypp
193 /////////////////////////////////////////////////////////////////////////