diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt index 320dbd262b77e9addc9e7db690bdee5fe1619a6b..ed0cd667dd5734c79e8d7fdfb653caf52dc0eb72 100644 --- a/libwccl/CMakeLists.txt +++ b/libwccl/CMakeLists.txt @@ -29,6 +29,10 @@ SET(libwccl_STAT_SRC exception.cpp ops/formatters.cpp ops/functions/bool/iteration.cpp + ops/functions/bool/iterations/atleast.cpp + ops/functions/bool/iterations/leftlook.cpp + ops/functions/bool/iterations/only.cpp + ops/functions/bool/iterations/rightlook.cpp ops/functions/bool/predicate.cpp ops/functions/bool/predicates/and.cpp ops/functions/bool/predicates/logicalpredicate.cpp diff --git a/libwccl/ops/functions/bool/iterations/atleast.cpp b/libwccl/ops/functions/bool/iterations/atleast.cpp new file mode 100644 index 0000000000000000000000000000000000000000..671368239f3b6b7afcd7f7ccea12f8767119434d --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/atleast.cpp @@ -0,0 +1,48 @@ +#include <libwccl/ops/functions/bool/iterations/atleast.h> +#include <sstream> + +namespace Wccl { + +bool AtLeast::iterate( + int left, + int right, + Wccl::Position &p, + const Wccl::FunExecContext &context) const +{ + int left_to_match = min_matches_; + for(int i = left; i <= right; ++i) { + p.set_value(i); + if(evaluating_expr_->apply(context)) { + if(--left_to_match == 0) { + return true; + } + } + } + return false; +} + +std::string AtLeast::to_string(const Corpus2::Tagset& tagset) const +{ + std::ostringstream ss; + ss << name(tagset) << "(" + << left_pos_expr_->to_string(tagset) << ", " + << right_pos_expr_->to_string(tagset) << ", " + << Position::var_repr(iter_var_acc_.get_name()) << ", " + << evaluating_expr_->to_string(tagset) << ", " + << min_matches_ << ")"; + return ss.str(); +} + +std::string AtLeast::to_raw_string() const +{ + std::ostringstream ss; + ss << raw_name() << "(" + << left_pos_expr_->to_raw_string() << ", " + << right_pos_expr_->to_raw_string() << ", " + << Position::var_repr(iter_var_acc_.get_name()) << ", " + << evaluating_expr_->to_raw_string() << ", " + << min_matches_ << ")"; + return ss.str(); +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/iterations/atleast.h b/libwccl/ops/functions/bool/iterations/atleast.h new file mode 100644 index 0000000000000000000000000000000000000000..fa1f644efd06e6cc3a275b35e4b347d11ce6f034 --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/atleast.h @@ -0,0 +1,70 @@ +#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_ATLEAST_H +#define LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_ATLEAST_H + +#include <libwccl/ops/functions/bool/iteration.h> + +namespace Wccl { + +/** + * Iterative operator "atleast", checking if there + * is some minimum number of positions within + * given range, that make evaluating condition return true + */ +class AtLeast : public Iteration +{ +public: + AtLeast( + const PosFunctionPtr& left_pos_expr, + const PosFunctionPtr& right_pos_expr, + const VariableAccessor<Position>& iter_var_acc, + const BoolFunctionPtr& evaluating_expr, + int min_matches) + : Iteration(left_pos_expr, right_pos_expr, iter_var_acc, evaluating_expr), + min_matches_(min_matches) + { + } + + /** + * @returns Name of the function: "atleast". + */ + std::string raw_name() const { + return "atleast"; + } + + /** + * @returns String reperesentation of AtLeast Operator: + * atleast(left_pos_expr, right_pos_expr, variable, eval_expr, min_matches) + */ + std::string to_string(const Corpus2::Tagset& tagset) const; + + /** + * @returns String reperesentation of AtLeast Operator. This is + * default representation of + * atleast(raw_left_p_expr, raw_right_p_expr, var, raw_eval_expr, min_matches) + * @note This version doesn't require a Tagset, but may + * be incomplete and/or contain internal info. + */ + std::string to_raw_string() const; + +protected: + /** + * @returns True, if for at least min_matches positions + * within range the evaluating expression returned true. + * False otherwise. + * @note Upon success, the iteration variable is + * set to the "min_matches"th position in range (that is the + * position that made the stop condition true in this case). + */ + bool iterate( + int left, + int right, + Position &p, + const FunExecContext &context) const; +private: + int min_matches_; +}; + + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_ATLEAST_H diff --git a/libwccl/ops/functions/bool/iterations/leftlook.cpp b/libwccl/ops/functions/bool/iterations/leftlook.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5439bec505f019875b85ad1e225037a8c188369f --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/leftlook.cpp @@ -0,0 +1,20 @@ +#include <libwccl/ops/functions/bool/iterations/leftlook.h> + +namespace Wccl { + +bool LeftLook::iterate( + int left, + int right, + Wccl::Position &p, + const Wccl::FunExecContext &context) const +{ + for(int i = left; i <= right; ++i) { + p.set_value(i); + if(evaluating_expr_->apply(context)) { + return true; + } + } + return false; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/iterations/leftlook.h b/libwccl/ops/functions/bool/iterations/leftlook.h new file mode 100644 index 0000000000000000000000000000000000000000..5c07a39c3c828e5d85c14810830ca375536e56cd --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/leftlook.h @@ -0,0 +1,53 @@ +#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_LEFTLOOK_H +#define LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_LEFTLOOK_H + +#include <libwccl/ops/functions/bool/iteration.h> + +namespace Wccl { + +/** + * Iterative operator "llook", which looks for a first + * position from left that makes evaluating expression return true. + */ +class LeftLook : public Iteration +{ +public: + LeftLook( + const PosFunctionPtr& left_pos_expr, + const PosFunctionPtr& right_pos_expr, + const VariableAccessor<Position>& iter_var_acc, + const BoolFunctionPtr& evaluating_expr) + : Iteration(left_pos_expr, right_pos_expr, iter_var_acc, evaluating_expr) + { + } + + /** + * @returns Name of the function: "llook". + */ + std::string raw_name() const { + return "llook"; + } + +protected: + /** + * @returns True if, when scanning from left, + * a position within range is found that makes + * the evaluating function return true. False + * otherwise. + * @note Upon success, the iteration variable is + * set to the first position in range from left + * that has made evaluation function return true + * (that is the position that made the stop condition + * true in this case). + */ + bool iterate( + int left, + int right, + Position &p, + const FunExecContext &context) const; +}; + + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_LEFTLOOK_H diff --git a/libwccl/ops/functions/bool/iterations/only.cpp b/libwccl/ops/functions/bool/iterations/only.cpp new file mode 100644 index 0000000000000000000000000000000000000000..291d25f9ce5893b49f3973c87ed4224ea6b5b014 --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/only.cpp @@ -0,0 +1,20 @@ +#include <libwccl/ops/functions/bool/iterations/only.h> + +namespace Wccl { + +bool Only::iterate( + int left, + int right, + Wccl::Position &p, + const Wccl::FunExecContext &context) const +{ + for(int i = left; i <= right; ++i) { + p.set_value(i); + if(!evaluating_expr_->apply(context)) { + return false; + } + } + return true; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/iterations/only.h b/libwccl/ops/functions/bool/iterations/only.h new file mode 100644 index 0000000000000000000000000000000000000000..dce58cba7dcf366487b80057fa120c22bfc98651 --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/only.h @@ -0,0 +1,51 @@ +#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_ONLY_H +#define LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_ONLY_H + +#include <libwccl/ops/functions/bool/iteration.h> + +namespace Wccl { + +/** + * Iterative operator "only", which mandates that + * evaluating expression should evaluate to true + * on all positions in range. + */ +class Only : public Iteration +{ +public: + Only( + const PosFunctionPtr& left_pos_expr, + const PosFunctionPtr& right_pos_expr, + const VariableAccessor<Position>& iter_var_acc, + const BoolFunctionPtr& evaluating_expr) + : Iteration(left_pos_expr, right_pos_expr, iter_var_acc, evaluating_expr) + { + } + + /** + * @returns Name of the function: "only". + */ + std::string raw_name() const { + return "only"; + } + +protected: + /** + * @returns True, if for all positions within range + * the evaluating expression returned true. False + * otherwise. + * @note Upon success, the iteration variable is + * set to the last position in range (that is the + * position that made the stop condition true in this case). + */ + bool iterate( + int left, + int right, + Position &p, + const FunExecContext &context) const; +}; + + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_ONLY_H diff --git a/libwccl/ops/functions/bool/iterations/rightlook.cpp b/libwccl/ops/functions/bool/iterations/rightlook.cpp new file mode 100644 index 0000000000000000000000000000000000000000..613c62285ccc25c95ff81bfde194e705daa5d27c --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/rightlook.cpp @@ -0,0 +1,20 @@ +#include <libwccl/ops/functions/bool/iterations/rightlook.h> + +namespace Wccl { + +bool RightLook::iterate( + int left, + int right, + Wccl::Position &p, + const Wccl::FunExecContext &context) const +{ + for(int i = right; i >= left; --i) { + p.set_value(i); + if(evaluating_expr_->apply(context)) { + return true; + } + } + return false; +} + +} /* end ns Wccl */ diff --git a/libwccl/ops/functions/bool/iterations/rightlook.h b/libwccl/ops/functions/bool/iterations/rightlook.h new file mode 100644 index 0000000000000000000000000000000000000000..b39251affb8f3defb9c9b2d81c60c3dcf17518bc --- /dev/null +++ b/libwccl/ops/functions/bool/iterations/rightlook.h @@ -0,0 +1,53 @@ +#ifndef LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_RIGHTLOOK_H +#define LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_RIGHTLOOK_H + +#include <libwccl/ops/functions/bool/iteration.h> + +namespace Wccl { + +/** + * Iterative operator "rlook", which looks for a first + * position from right that makes evaluating expression return true. + */ +class RightLook : public Iteration +{ +public: + RightLook( + const PosFunctionPtr& left_pos_expr, + const PosFunctionPtr& right_pos_expr, + const VariableAccessor<Position>& iter_var_acc, + const BoolFunctionPtr& evaluating_expr) + : Iteration(left_pos_expr, right_pos_expr, iter_var_acc, evaluating_expr) + { + } + + /** + * @returns Name of the function: "rlook". + */ + std::string raw_name() const { + return "rlook"; + } + +protected: + /** + * @returns True if, when scanning from right, + * a position within range is found that makes + * the evaluating function return true. False + * otherwise. + * @note Upon success, the iteration variable is + * set to the first position in range from right + * that has made evaluation function return true + * (that is the position that made the stop condition + * true in this case). + */ + bool iterate( + int left, + int right, + Position &p, + const FunExecContext &context) const; +}; + + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_FUNCTIONS_BOOL_ITERATIONS_RIGHTLOOK_H