77d3b394a4435e20eff80b65f31d114ef5780608
[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 #include "zypp/solver/detail/Resolver.h"
25 #include "zypp/solver/detail/SolutionAction.h"
26 #include "zypp/Capabilities.h"
27 #include "zypp/base/Logger.h"
28
29 /////////////////////////////////////////////////////////////////////////
30 namespace zypp
31 { ///////////////////////////////////////////////////////////////////////
32   ///////////////////////////////////////////////////////////////////////
33   namespace solver
34   { /////////////////////////////////////////////////////////////////////
35     /////////////////////////////////////////////////////////////////////
36     namespace detail
37     { ///////////////////////////////////////////////////////////////////
38
39 using namespace std;
40
41 IMPL_PTR_TYPE(SolutionAction);
42 IMPL_PTR_TYPE(TransactionSolutionAction);
43 IMPL_PTR_TYPE(InjectSolutionAction);
44
45 //---------------------------------------------------------------------------
46
47 SolutionAction::SolutionAction()
48 {
49 }
50
51
52 SolutionAction::~SolutionAction()
53 {
54 }
55
56
57 //---------------------------------------------------------------------------
58
59 ostream &
60 TransactionSolutionAction::dumpOn( ostream& os) const
61 {
62     os << "TransactionSolutionAction: ";
63     switch (_action) {
64         case KEEP:                      os << "Keep " << _item; break;
65         case INSTALL:                   os << "Install " << _item; break;
66         case REMOVE:                    os << "Remove " << _item; break;
67         case UNLOCK:                    os << "Unlock " << _item; break;
68         case LOCK:                      os << "Lock " << _item; break;
69         case REMOVE_EXTRA_REQUIRE:      os << "Remove require " << _capability; break;
70         case REMOVE_EXTRA_CONFLICT:     os << "Remove conflict " << _capability; break;
71         case ADD_SOLVE_QUEUE_ITEM:      os << "Add SolveQueueItem " <<  _solverQueueItem; break;
72         case REMOVE_SOLVE_QUEUE_ITEM:   os << "Remove SolveQueueItem " <<  _solverQueueItem; break;
73     }
74     return os;
75 }
76
77
78 ostream&
79 operator<<( ostream& os, const SolutionActionList & actionlist)
80 {
81     for (SolutionActionList::const_iterator iter = actionlist.begin(); iter != actionlist.end(); ++iter) {
82         os << *(*iter);
83         os << endl;
84     }
85     return os;
86 }
87
88
89 ostream&
90 operator<<( ostream& os, const CSolutionActionList & actionlist)
91 {
92     for (CSolutionActionList::const_iterator iter = actionlist.begin(); iter != actionlist.end(); ++iter) {
93         os << *(*iter);
94         os << endl;
95     }
96     return os;
97 }
98
99 //---------------------------------------------------------------------------
100
101 ostream &
102 InjectSolutionAction::dumpOn( ostream& os ) const
103 {
104     os << "InjectSolutionAction: ";
105     switch (_kind) {
106         case WEAK:      os << "Weak"; break;
107         default: os << "Wrong kind"; break;
108     }
109     os << " ";
110     os << _item;
111     return os;
112 }
113
114 //---------------------------------------------------------------------------
115
116
117 ostream &
118 SolutionAction::dumpOn( std::ostream & os ) const
119 {
120     os << "SolutionAction<";
121     os << "not specified";
122     os << "> ";
123     return os;
124 }
125
126
127 bool
128 TransactionSolutionAction::execute(Resolver & resolver) const
129 {
130     bool ret = true;
131     switch (action()) {
132         case KEEP:
133             _item.status().resetTransact (ResStatus::USER);
134             ret = _item.status().setTransact (false, ResStatus::APPL_HIGH); // APPL_HIGH: Locking should not be saved permanently
135             break;
136         case INSTALL:
137             if (_item.status().isToBeUninstalled())
138                 ret = _item.status().setTransact (false, ResStatus::USER);
139             else
140                 _item.status().setToBeInstalled (ResStatus::USER);
141             break;
142         case REMOVE:
143             if (_item.status().isToBeInstalled()) {
144                 _item.status().setTransact (false,ResStatus::USER);
145                 _item.status().setLock (true,ResStatus::USER); // no other dependency can set it again
146             } else if (_item.status().isInstalled())
147                 _item.status().setToBeUninstalled (ResStatus::USER);
148             else
149                 _item.status().setLock (true,ResStatus::USER); // no other dependency can set it again
150             break;
151         case UNLOCK:
152             ret = _item.status().setLock (false, ResStatus::USER);
153             if (!ret) ERR << "Cannot unlock " << _item << endl;
154             break;
155         case LOCK:
156             _item.status().resetTransact (ResStatus::USER);
157             ret = _item.status().setLock (true, ResStatus::APPL_HIGH); // APPL_HIGH: Locking should not be saved permanently
158             if (!ret) ERR << "Cannot lock " << _item << endl;
159             break;
160         case REMOVE_EXTRA_REQUIRE:
161             resolver.removeExtraRequire (_capability);
162             break;
163         case REMOVE_EXTRA_CONFLICT:
164             resolver.removeExtraConflict (_capability);
165             break;
166         case ADD_SOLVE_QUEUE_ITEM:
167             resolver.addQueueItem(_solverQueueItem);
168             break;
169         case REMOVE_SOLVE_QUEUE_ITEM:
170             resolver.removeQueueItem(_solverQueueItem);
171             break;
172         default:
173             ERR << "Wrong TransactionKind" << endl;
174             ret = false;
175     }
176     return ret;
177 }
178
179 bool
180 InjectSolutionAction::execute(Resolver & resolver) const
181 {
182     switch (_kind) {
183         case WEAK:
184             // set item dependencies to weak
185             resolver.addWeak (_item);
186             break;
187         default:
188             ERR << "No valid InjectSolutionAction kind found" << endl;
189             return false;
190     }
191
192     return true;
193 }
194
195       ///////////////////////////////////////////////////////////////////
196     };// namespace detail
197     /////////////////////////////////////////////////////////////////////
198     /////////////////////////////////////////////////////////////////////
199   };// namespace solver
200   ///////////////////////////////////////////////////////////////////////
201   ///////////////////////////////////////////////////////////////////////
202 };// namespace zypp
203 /////////////////////////////////////////////////////////////////////////