Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / solver / detail / SolverQueueItemInstallOneOf.cc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* SolverQueueItem.cc
3  *
4  * Copyright (C) 2008 SUSE Linux Products GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 extern "C"
21 {
22 #include <solv/solver.h>
23 }
24
25 #define ZYPP_USE_RESOLVER_INTERNALS
26
27 #include "zypp/base/Logger.h"
28 #include "zypp/solver/detail/SolverQueueItemInstallOneOf.h"
29 #include "zypp/sat/Pool.h"
30
31 /////////////////////////////////////////////////////////////////////////
32 namespace zypp
33 { ///////////////////////////////////////////////////////////////////////
34   ///////////////////////////////////////////////////////////////////////
35   namespace solver
36   { /////////////////////////////////////////////////////////////////////
37     /////////////////////////////////////////////////////////////////////
38     namespace detail
39     { ///////////////////////////////////////////////////////////////////
40
41 using namespace std;
42
43 IMPL_PTR_TYPE(SolverQueueItemInstallOneOf);
44
45 //---------------------------------------------------------------------------
46
47 std::ostream &
48 SolverQueueItemInstallOneOf::dumpOn( std::ostream & os ) const
49 {
50     os << "[" << (_soft?"Soft":"") << "InstallOneOf: ";
51     for (PoolItemList::const_iterator iter = _oneOfList.begin();
52          iter != _oneOfList.end();
53          iter++)
54         os << *iter;
55     os << "]";
56
57     return os;
58 }
59
60 //---------------------------------------------------------------------------
61
62 SolverQueueItemInstallOneOf::SolverQueueItemInstallOneOf (const ResPool & pool, const PoolItemList & itemList,
63                                                           bool soft)
64     : SolverQueueItem (QUEUE_ITEM_TYPE_INSTALL_ONE_OF, pool)
65     , _oneOfList (itemList)
66     , _soft (soft)
67 {
68 }
69
70
71 SolverQueueItemInstallOneOf::~SolverQueueItemInstallOneOf()
72 {
73 }
74
75 //---------------------------------------------------------------------------
76
77 bool SolverQueueItemInstallOneOf::addRule (sat::detail::CQueue & q)
78 {
79     bool ret = true;
80     MIL << "Install one of " << (_soft ? "(soft):" : ":")<< endl;
81     Queue qs;
82
83     if (_soft) {
84         queue_push( &(q), SOLVER_INSTALL | SOLVER_SOLVABLE_ONE_OF | SOLVER_WEAK);
85     } else {
86         queue_push( &(q), SOLVER_INSTALL | SOLVER_SOLVABLE_ONE_OF );
87     }
88
89     queue_init(&qs);
90     for (PoolItemList::const_iterator iter = _oneOfList.begin(); iter != _oneOfList.end(); iter++) {
91         Id id = (*iter)->satSolvable().id();
92         if (id == ID_NULL) {
93             ERR << *iter << " not found" << endl;
94             ret = false;
95         } else {
96             MIL << "    candidate:" << *iter << " with the SAT-Pool ID: " << id << endl;
97             queue_push( &(qs), id );
98         }
99     }
100     sat::Pool satPool( sat::Pool::instance() );
101     queue_push( &(q), pool_queuetowhatprovides(satPool.get(), &qs));
102     queue_free(&qs);
103
104     return ret;
105 }
106
107 SolverQueueItem_Ptr
108 SolverQueueItemInstallOneOf::copy (void) const
109 {
110     SolverQueueItemInstallOneOf_Ptr new_installOneOf = new SolverQueueItemInstallOneOf (pool(), _oneOfList);
111     new_installOneOf->SolverQueueItem::copy(this);
112     new_installOneOf->_soft = _soft;
113
114     return new_installOneOf;
115 }
116
117 int
118 SolverQueueItemInstallOneOf::cmp (SolverQueueItem_constPtr item) const
119 {
120     int cmp = this->compare (item);
121     if (cmp != 0)
122         return cmp;
123     SolverQueueItemInstallOneOf_constPtr install = dynamic_pointer_cast<const SolverQueueItemInstallOneOf>(item);
124
125     return (_oneOfList == install->_oneOfList) ? 0 : -1; // more evaluation would be not useful
126 }
127
128
129 //---------------------------------------------------------------------------
130
131
132 ///////////////////////////////////////////////////////////////////
133     };// namespace detail
134     /////////////////////////////////////////////////////////////////////
135     /////////////////////////////////////////////////////////////////////
136   };// namespace solver
137   ///////////////////////////////////////////////////////////////////////
138   ///////////////////////////////////////////////////////////////////////
139 };// namespace zypp
140 /////////////////////////////////////////////////////////////////////////