xref: /haiku/src/kits/package/solver/SolverProblem.cpp (revision a3e794ae459fec76826407f8ba8c94cd3535f128)
1 /*
2  * Copyright 2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ingo Weinhold <ingo_weinhold@gmx.de>
7  */
8 
9 
10 #include <package/solver/SolverProblem.h>
11 
12 #include <package/solver/SolverPackage.h>
13 #include <package/solver/SolverProblemSolution.h>
14 
15 
16 static const char* const kToStringTexts[] = {
17 	"unspecified problem",
18 	"%source% does not belong to a distupgrade repository",
19 	"%source% has inferior architecture",
20 	"problem with installed package %source%",
21 	"conflicting requests",
22 	"nothing provides requested %dependency%",
23 	"%dependency% is provided by the system",
24 	"dependency problem",
25 	"package %source% is not installable",
26 	"nothing provides %dependency% needed by %source%",
27 	"cannot install both %source% and %target%",
28 	"package %source% conflicts with %dependency% provided by %target%",
29 	"package %source% obsoletes %dependency% provided by %target%",
30 	"installed package %source% obsoletes %dependency% provided by %target%",
31 	"package %source% implicitly obsoletes %dependency% provided by %target%",
32 	"package %source% requires %dependency%, but none of the providers can be "
33 		"installed",
34 	"package %source% conflicts with %dependency% provided by itself"
35 };
36 
37 
38 namespace BPackageKit {
39 
40 
41 BSolverProblem::BSolverProblem(BType type, BSolverPackage* sourcePackage,
42 	BSolverPackage* targetPackage)
43 	:
44 	fType(type),
45 	fSourcePackage(sourcePackage),
46 	fTargetPackage(targetPackage),
47 	fDependency(),
48 	fSolutions(10, true)
49 {
50 }
51 
52 
53 BSolverProblem::BSolverProblem(BType type, BSolverPackage* sourcePackage,
54 	BSolverPackage* targetPackage,
55 	const BPackageResolvableExpression& dependency)
56 	:
57 	fType(type),
58 	fSourcePackage(sourcePackage),
59 	fTargetPackage(targetPackage),
60 	fDependency(dependency),
61 	fSolutions(10, true)
62 {
63 }
64 
65 
66 BSolverProblem::~BSolverProblem()
67 {
68 }
69 
70 
71 BSolverProblem::BType
72 BSolverProblem::Type() const
73 {
74 	return fType;
75 }
76 
77 
78 BSolverPackage*
79 BSolverProblem::SourcePackage() const
80 {
81 	return fSourcePackage;
82 }
83 
84 
85 BSolverPackage*
86 BSolverProblem::TargetPackage() const
87 {
88 	return fTargetPackage;
89 }
90 
91 
92 const BPackageResolvableExpression&
93 BSolverProblem::Dependency() const
94 {
95 	return fDependency;
96 }
97 
98 
99 int32
100 BSolverProblem::CountSolutions() const
101 {
102 	return fSolutions.CountItems();
103 }
104 
105 
106 const BSolverProblemSolution*
107 BSolverProblem::SolutionAt(int32 index) const
108 {
109 	return fSolutions.ItemAt(index);
110 }
111 
112 
113 bool
114 BSolverProblem::AppendSolution(BSolverProblemSolution* solution)
115 {
116 	return fSolutions.AddItem(solution);
117 }
118 
119 
120 BString
121 BSolverProblem::ToString() const
122 {
123 	size_t index = fType;
124 	if (index >= sizeof(kToStringTexts) / sizeof(kToStringTexts[0]))
125 		index = 0;
126 
127 	return BString(kToStringTexts[index])
128 		.ReplaceAll("%source%",
129 			fSourcePackage != NULL
130 				? fSourcePackage->VersionedName().String() : "?")
131 		.ReplaceAll("%target%",
132 			fTargetPackage != NULL
133 				? fTargetPackage->VersionedName().String() : "?")
134 		.ReplaceAll("%dependency%",
135 			fDependency.InitCheck() == B_OK
136 				? fDependency.ToString().String() : "?");
137 }
138 
139 
140 }	// namespace BPackageKit
141