Wildmeshing Toolkit
Loading...
Searching...
No Matches
Expression.hpp
1#pragma once
2
3#include <cctype>
4#include <iostream>
5#include <memory>
6#include <set>
7#include <stdexcept>
8#include <string>
9
10#include "../ConnectedComponent.hpp"
11
12namespace wmtk::components::image_simulation::expression_parser {
13
18{
19public:
20 virtual ~Expression() = default;
21
29 virtual bool eval(const CellTag& tags) const = 0;
30
37 virtual std::string to_string() const = 0;
38
42 virtual bool contains_not() const { return false; }
43
47 virtual bool contains_and() const { return false; }
48
52 virtual bool contains_or() const { return false; }
53
57 bool contains_only_and() const { return !contains_or() && !contains_not(); }
61 bool contains_only_or() const { return !contains_and() && !contains_not(); }
65 bool contains_only_not() const { return !contains_and() && !contains_or(); }
66
74 virtual CellTag tags_involved() const = 0;
75 virtual std::set<std::string> tag_names_involved() const = 0;
76};
77
78using ExpressionPtr = std::unique_ptr<Expression>;
79
83class TagExpr : public Expression
84{
85public:
86 explicit TagExpr(int64_t tag, const std::string& name)
87 : m_tag(tag)
88 , m_name(name)
89 {}
90
91 bool eval(const CellTag& tags) const override { return tags.count(m_tag) != 0; }
92 std::string to_string() const override { return "\"" + m_name + "\""; }
93 CellTag tags_involved() const override { return {m_tag}; }
94 std::set<std::string> tag_names_involved() const override { return {m_name}; }
95
96private:
97 int64_t m_tag;
98 std::string m_name;
99};
100
104class EmptyExpr : public Expression
105{
106public:
107 EmptyExpr() = default;
108
109 bool eval(const CellTag& tags) const override { return tags.empty(); }
110 std::string to_string() const override { return "\"_\""; }
111 CellTag tags_involved() const override { return {}; }
112 std::set<std::string> tag_names_involved() const override { return {}; }
113};
114
118class NotExpr : public Expression
119{
120public:
121 explicit NotExpr(ExpressionPtr expr)
122 : m_expr(std::move(expr))
123 {}
124
125 bool eval(const CellTag& tags) const override { return !m_expr->eval(tags); }
126 std::string to_string() const override { return "!(" + m_expr->to_string() + ")"; }
127
128 bool contains_not() const override { return true; }
129 bool contains_and() const override { return m_expr->contains_and(); }
130 bool contains_or() const override { return m_expr->contains_or(); }
131 CellTag tags_involved() const override { return m_expr->tags_involved(); }
132 std::set<std::string> tag_names_involved() const override
133 {
134 return m_expr->tag_names_involved();
135 }
136
137private:
138 ExpressionPtr m_expr;
139};
140
144class AndExpr : public Expression
145{
146public:
147 AndExpr(ExpressionPtr left, ExpressionPtr right)
148 : m_left(std::move(left))
149 , m_right(std::move(right))
150 {}
151
152 bool eval(const CellTag& tags) const override
153 {
154 return m_left->eval(tags) && m_right->eval(tags);
155 }
156 std::string to_string() const override
157 {
158 return "(" + m_left->to_string() + " & " + m_right->to_string() + ")";
159 }
160
161 bool contains_not() const override { return m_left->contains_not() || m_right->contains_not(); }
162 bool contains_and() const override { return true; }
163 bool contains_or() const override { return m_left->contains_or() || m_right->contains_or(); }
164 CellTag tags_involved() const override
165 {
166 CellTag tags = m_left->tags_involved();
167 CellTag right_tags = m_right->tags_involved();
168 tags.insert(right_tags.begin(), right_tags.end());
169 return tags;
170 }
171
172 std::set<std::string> tag_names_involved() const override
173 {
174 auto names = m_left->tag_names_involved();
175 auto right_names = m_right->tag_names_involved();
176 names.insert(right_names.begin(), right_names.end());
177 return names;
178 }
179
180private:
181 ExpressionPtr m_left;
182 ExpressionPtr m_right;
183};
184
188class OrExpr : public Expression
189{
190public:
191 OrExpr(ExpressionPtr left, ExpressionPtr right)
192 : m_left(std::move(left))
193 , m_right(std::move(right))
194 {}
195
196 bool eval(const CellTag& tags) const override
197 {
198 return m_left->eval(tags) || m_right->eval(tags);
199 }
200
201 std::string to_string() const override
202 {
203 return "(" + m_left->to_string() + " | " + m_right->to_string() + ")";
204 }
205
206 bool contains_not() const override { return m_left->contains_not() || m_right->contains_not(); }
207 bool contains_and() const override { return m_left->contains_and() || m_right->contains_and(); }
208 bool contains_or() const override { return true; }
209 CellTag tags_involved() const override
210 {
211 CellTag tags = m_left->tags_involved();
212 CellTag right_tags = m_right->tags_involved();
213 tags.insert(right_tags.begin(), right_tags.end());
214 return tags;
215 }
216 std::set<std::string> tag_names_involved() const override
217 {
218 auto names = m_left->tag_names_involved();
219 auto right_names = m_right->tag_names_involved();
220 names.insert(right_names.begin(), right_names.end());
221 return names;
222 }
223
224private:
225 ExpressionPtr m_left;
226 ExpressionPtr m_right;
227};
228
229} // namespace wmtk::components::image_simulation::expression_parser
bool contains_or() const override
Checks if the expression contains a logical OR operation.
Definition Expression.hpp:163
std::string to_string() const override
Converts the expression to its string representation. Mainly for debugging purposes.
Definition Expression.hpp:156
CellTag tags_involved() const override
Returns the set of tags involved in this expression.
Definition Expression.hpp:164
bool contains_not() const override
Checks if the expression contains a logical NOT operation.
Definition Expression.hpp:161
bool eval(const CellTag &tags) const override
Evaluates the expression against a given set of tags.
Definition Expression.hpp:152
bool contains_and() const override
Checks if the expression contains a logical AND operation.
Definition Expression.hpp:162
std::string to_string() const override
Converts the expression to its string representation. Mainly for debugging purposes.
Definition Expression.hpp:110
bool eval(const CellTag &tags) const override
Evaluates the expression against a given set of tags.
Definition Expression.hpp:109
CellTag tags_involved() const override
Returns the set of tags involved in this expression.
Definition Expression.hpp:111
Base class for boolean expression tree nodes used to evaluate cell tags.
Definition Expression.hpp:18
virtual bool contains_or() const
Checks if the expression contains a logical OR operation.
Definition Expression.hpp:52
virtual bool eval(const CellTag &tags) const =0
Evaluates the expression against a given set of tags.
virtual CellTag tags_involved() const =0
Returns the set of tags involved in this expression.
virtual bool contains_and() const
Checks if the expression contains a logical AND operation.
Definition Expression.hpp:47
bool contains_only_and() const
Checks if the expression contains only logical AND operations or none.
Definition Expression.hpp:57
bool contains_only_not() const
Checks if the expression contains only logical NOT operations or none.
Definition Expression.hpp:65
bool contains_only_or() const
Checks if the expression contains only logical OR operations or none.
Definition Expression.hpp:61
virtual std::string to_string() const =0
Converts the expression to its string representation. Mainly for debugging purposes.
virtual bool contains_not() const
Checks if the expression contains a logical NOT operation.
Definition Expression.hpp:42
bool contains_or() const override
Checks if the expression contains a logical OR operation.
Definition Expression.hpp:130
bool eval(const CellTag &tags) const override
Evaluates the expression against a given set of tags.
Definition Expression.hpp:125
bool contains_not() const override
Checks if the expression contains a logical NOT operation.
Definition Expression.hpp:128
std::string to_string() const override
Converts the expression to its string representation. Mainly for debugging purposes.
Definition Expression.hpp:126
bool contains_and() const override
Checks if the expression contains a logical AND operation.
Definition Expression.hpp:129
CellTag tags_involved() const override
Returns the set of tags involved in this expression.
Definition Expression.hpp:131
bool eval(const CellTag &tags) const override
Evaluates the expression against a given set of tags.
Definition Expression.hpp:196
bool contains_and() const override
Checks if the expression contains a logical AND operation.
Definition Expression.hpp:207
bool contains_not() const override
Checks if the expression contains a logical NOT operation.
Definition Expression.hpp:206
std::string to_string() const override
Converts the expression to its string representation. Mainly for debugging purposes.
Definition Expression.hpp:201
CellTag tags_involved() const override
Returns the set of tags involved in this expression.
Definition Expression.hpp:209
bool contains_or() const override
Checks if the expression contains a logical OR operation.
Definition Expression.hpp:208
std::string to_string() const override
Converts the expression to its string representation. Mainly for debugging purposes.
Definition Expression.hpp:92
CellTag tags_involved() const override
Returns the set of tags involved in this expression.
Definition Expression.hpp:93
bool eval(const CellTag &tags) const override
Evaluates the expression against a given set of tags.
Definition Expression.hpp:91