Skip to content

Commit a0db490

Browse files
Add causal link handling in solver_core and server classes
- Introduced add_causal_link method in solver_core to manage causal relationships between flaws and resolvers. - Implemented causal_link_added method in server to notify clients when a causal link is established. - Updated basic_solver to utilize the new causal link functionality.
1 parent 0ee84bd commit a0db490

5 files changed

Lines changed: 32 additions & 0 deletions

File tree

include/server/basic_solver_server.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace ratio
2525
void current_flaw(std::optional<std::reference_wrapper<ratio::flaw>> f) noexcept override;
2626
void current_resolver(std::optional<std::reference_wrapper<ratio::resolver>> r) noexcept override;
2727

28+
void causal_link_added(const flaw &f, const resolver &r) override;
29+
2830
private:
2931
std::unordered_set<network::ws_server_session_base *> clients;
3032
};

include/solver_core.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ namespace ratio
168168
return r_ref;
169169
}
170170

171+
virtual void add_causal_link(flaw &f, resolver &r) noexcept;
172+
171173
virtual void solve() = 0;
172174

173175
[[nodiscard]] bool match(riddle::term &lhs, riddle::term &rhs) const;
@@ -234,6 +236,16 @@ namespace ratio
234236
* @param The current resolver being applied.
235237
*/
236238
virtual void current_resolver([[maybe_unused]] std::optional<std::reference_wrapper<ratio::resolver>>) noexcept {}
239+
240+
/**
241+
* @brief Notifies when a causal link has been added.
242+
*
243+
* This function is called when a causal link has been added. It is a virtual function that can be overridden by derived classes to perform specific actions when a causal link is added.
244+
*
245+
* @param flaw The flaw that is the source of the causal link.
246+
* @param resolver The resolver that is the destination of the causal link.
247+
*/
248+
virtual void causal_link_added([[maybe_unused]] const flaw &, [[maybe_unused]] const resolver &) {}
237249
#endif
238250

239251
protected:

src/basic_solver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ namespace ratio
288288
{
289289
execute(get_solver().new_eq(static_cast<atom_flaw &>(flw).get_atom(), atm));
290290
add_ac_constraint(get_ac().new_assign(utils::variable(static_cast<riddle::atom &>(*static_cast<atom_flaw &>(flw).get_atom()).get_sigma()), arc_consistency::solver::False));
291+
get_solver().add_causal_link(static_cast<atom &>(*atm).get_flaw(), *this);
291292
}
292293

293294
json::json unify_atom::to_json() const

src/server/basic_solver_server.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,12 @@ namespace ratio
8989
for (auto client : clients)
9090
client->send(msg);
9191
}
92+
93+
void server::causal_link_added(const ratio::flaw &f, const ratio::resolver &r)
94+
{
95+
auto j_msg = json::json{{"msg_type", "causal_link_added"}, {"flaw", f.get_id()}, {"resolver", r.get_id()}};
96+
auto msg = j_msg.dump();
97+
for (auto client : clients)
98+
client->send(msg);
99+
}
92100
} // namespace ratio

src/solver_core.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#ifdef ORATIO_ENABLE_LISTENERS
77
#define CURRENT_FLAW(f) current_flaw(f)
88
#define CURRENT_RESOLVER(r) current_resolver(r)
9+
#define NEW_CAUSAL_LINK(f, r) causal_link_added(f, r)
910
#else
1011
#define CURRENT_FLAW(f)
1112
#define CURRENT_RESOLVER(r)
13+
#define NEW_CAUSAL_LINK(f, r)
1214
#endif
1315

1416
namespace ratio
@@ -166,6 +168,13 @@ namespace ratio
166168
throw std::runtime_error("Invalid type");
167169
}
168170

171+
void solver_core::add_causal_link(flaw &f, resolver &r) noexcept
172+
{
173+
f.supports.push_back(r);
174+
r.preconditions.push_back(f);
175+
NEW_CAUSAL_LINK(f, r);
176+
}
177+
169178
bool solver_core::match(riddle::term &lhs, riddle::term &rhs) const
170179
{
171180
if (&lhs == &rhs) // the terms are the same, so they match..

0 commit comments

Comments
 (0)