Lattice Land Parsing Library
Loading...
Searching...
No Matches
solver_output.hpp
Go to the documentation of this file.
1//
2// Created by falque on 05/06/24.
3//
4
5#ifndef OUTPUT_H
6#define OUTPUT_H
7#include <functional>
8#include <peglib.h>
9#include <lala/logic/ast.hpp>
10
11
12namespace lala {
13
14enum class OutputType {
15 XCSP,
17};
18
19
20template<class Allocator>
22 using bstring = battery::string<Allocator>;
23 template<class T> using bvector = battery::vector<T, Allocator>;
24 using array_dim_t = bvector<battery::tuple<size_t,size_t>>;
25 using F = TFormula<Allocator>;
26
27 bvector<bstring> output_vars;
28 // For each array, we store its output dimension characteristics and the list of the variables in the array.
29 bvector<battery::tuple<bstring, array_dim_t, bvector<bstring>>> output_arrays;
30
31 OutputType type;
32 std::string join_str(const bvector<bstring>& vec, const std::string& separator, std::function<std::string(const bstring&)> toString) const {
33 std::string result;
34 for (size_t i = 0; i < vec.size(); ++i) {
35 result += toString(vec[i]);
36 if (i < vec.size() - 1) {
37 result += separator;
38 }
39 }
40 return result;
41 }
42
43
44public:
45 template <class Alloc2>
46 friend class SolverOutput;
47
48 CUDA SolverOutput(const Allocator& alloc)
49 : output_vars(alloc)
50 , output_arrays(alloc),type(OutputType::FLATZINC)
51 {}
52
53 CUDA SolverOutput(const Allocator& alloc,OutputType outputType)
54 : output_vars(alloc)
55 , output_arrays(alloc),type(outputType)
56 {}
57
60
61 template <class Alloc>
63 output_vars = other.output_vars;
64 output_arrays = other.output_arrays;
65 type = other.type;
66 return *this;
67 }
68
69 template<class Alloc2>
70 CUDA SolverOutput(const SolverOutput<Alloc2>& other, const Allocator& allocator = Allocator{})
71 : output_vars(other.output_vars, allocator)
72 , output_arrays(other.output_arrays, allocator),type(other.type)
73 {}
74
75 void add_array_var(const std::string& name, const bstring& var_name, const peg::SemanticValues& sv) {
76 int idx = -1;
77 auto array_name = bstring(name.data());
78 for(int i = 0; i < output_arrays.size(); ++i) {
79 if(battery::get<0>(output_arrays[i]) == array_name) {
80 idx = i;
81 break;
82 }
83 }
84 if(idx == -1) {
85 output_arrays.push_back(battery::make_tuple<bstring, array_dim_t, bvector<bstring>>(bstring(array_name), {}, {}));
86 idx = static_cast<int>(output_arrays.size()) - 1;
87 // Add the dimension of the array.
88 for(int i = 0; i < sv.size(); ++i) {
89 auto range = std::any_cast<F>(sv[i]);
90 for(int j = 0; j < range.s().size(); ++j) {
91 const auto& itv = range.s()[j];
92 battery::get<1>(output_arrays[idx]).push_back(battery::make_tuple(battery::get<0>(itv).z(), battery::get<1>(itv).z()));
93 }
94 }
95 }
96 battery::get<2>(output_arrays[idx]).push_back(var_name);
97 }
98
99 void add_var(const bstring& var_name) {
100 output_vars.push_back(var_name);
101 }
102 bvector<bstring> getOutputVars(){
103 return output_vars;
104 }
106 template <class Alloc, class B, class Env>
107 CUDA void print_variable(const LVar<Alloc>& vname, const Env& benv, const B& b) const {
108 const auto& x = *(benv.variable_of(vname));
109 x.sort.print_value(b.project(x.avars[0]));
110 }
111 };
112
113 template <class Env, class A, class S>
114 CUDA void print_solution_flat_zinc(const Env& env, const A& sol, const S& simplifier = SimplifierIdentity{}) const {
115 for(int i = 0; i < output_vars.size(); ++i) {
116 printf("%s=", output_vars[i].data());
117 simplifier.print_variable(output_vars[i], env, sol);
118 printf(";\n");
119 }
120 for(int i = 0; i < output_arrays.size(); ++i) {
121 const auto& dims = battery::get<1>(output_arrays[i]);
122 const auto& array_vars = battery::get<2>(output_arrays[i]);
123 printf("%s=array%" PRIu64 "d(", battery::get<0>(output_arrays[i]).data(), dims.size());
124 for(int j = 0; j < dims.size(); ++j) {
125 printf("%" PRIu64 "..%" PRIu64 ",", battery::get<0>(dims[j]), battery::get<1>(dims[j]));
126 }
127 printf("[");
128 for(int j = 0; j < array_vars.size(); ++j) {
129 simplifier.print_variable(array_vars[j], env, sol);
130 if(j+1 != array_vars.size()) {
131 printf(",");
132 }
133 }
134 printf("]);\n");
135 }
136 }
137
138 template<class Env, class A, class S>
139 CUDA void print_solution_xml(const Env& env, const A& sol, const S& simplifier = SimplifierIdentity{}) const {
140 auto vars = join_str(output_vars, " ", [](const bstring& s) -> std::string { return s.data(); });
141 printf("v <instantiation> <list>%s</list> <values>", vars.c_str());
142 for (int i = 0; i < output_vars.size(); ++i) {
143 simplifier.print_variable(output_vars[i], env, sol);
144 if(i+1 != output_vars.size()) {
145 printf(" ");
146 }
147 }
148 printf("</values> </instantiation>\n");
149 }
150
151
152
153 template <class Env, class A, class S>
154 CUDA void print_solution(const Env& env, const A& sol, const S& simplifier = SimplifierIdentity{}) const {
155 if(type == OutputType::FLATZINC) {
156 print_solution_flat_zinc(env, sol, simplifier);
157 }else{
158 print_solution_xml(env, sol, simplifier);
159 }
160 }
161
162};
163
164} // namespace lala
165#endif //OUTPUT_H
Definition solver_output.hpp:105
Definition solver_output.hpp:21
CUDA SolverOutput(const SolverOutput< Alloc2 > &other, const Allocator &allocator=Allocator{})
Definition solver_output.hpp:70
CUDA SolverOutput(const Allocator &alloc, OutputType outputType)
Definition solver_output.hpp:53
void add_var(const bstring &var_name)
Definition solver_output.hpp:99
CUDA SolverOutput(const Allocator &alloc)
Definition solver_output.hpp:48
CUDA void print_solution(const Env &env, const A &sol, const S &simplifier=SimplifierIdentity{}) const
Definition solver_output.hpp:154
SolverOutput(SolverOutput &&)=default
CUDA SolverOutput< Allocator > & operator=(const SolverOutput< Alloc > &other)
Definition solver_output.hpp:62
CUDA void print_solution_xml(const Env &env, const A &sol, const S &simplifier=SimplifierIdentity{}) const
Definition solver_output.hpp:139
void add_array_var(const std::string &name, const bstring &var_name, const peg::SemanticValues &sv)
Definition solver_output.hpp:75
CUDA void print_solution_flat_zinc(const Env &env, const A &sol, const S &simplifier=SimplifierIdentity{}) const
Definition solver_output.hpp:114
bvector< bstring > getOutputVars()
Definition solver_output.hpp:102
SolverOutput< Allocator > & operator=(const SolverOutput< Allocator > &)=default
Definition flatzinc_parser.hpp:21
OutputType
Definition solver_output.hpp:14