From ed5f2256317a0d0172908614b68dbb8213e772ad Mon Sep 17 00:00:00 2001
From: Adam Wardynski <award@.(B-4.4.46a)>
Date: Fri, 11 Mar 2011 16:25:15 +0100
Subject: [PATCH] TokenCondition - wrapper for L0 predicates, matches single
 token under "$_"

---
 libwccl/CMakeLists.txt                        |  1 +
 .../ops/match/conditions/tokencondition.cpp   | 41 ++++++++++++++++
 libwccl/ops/match/conditions/tokencondition.h | 47 +++++++++++++++++++
 3 files changed, 89 insertions(+)
 create mode 100644 libwccl/ops/match/conditions/tokencondition.cpp
 create mode 100644 libwccl/ops/match/conditions/tokencondition.h

diff --git a/libwccl/CMakeLists.txt b/libwccl/CMakeLists.txt
index 500aa12..09413f3 100644
--- a/libwccl/CMakeLists.txt
+++ b/libwccl/CMakeLists.txt
@@ -54,6 +54,7 @@ SET(libwccl_STAT_SRC
 	ops/functions/tset/catfilter.cpp
 	ops/functions/tset/getsymbols.cpp
 	ops/functions/tset/getsymbolsinrange.cpp
+	ops/match/conditions/tokencondition.cpp
 	ops/rulesequence.cpp
 	ops/tagaction.cpp
 	ops/tagactions/delete.cpp
diff --git a/libwccl/ops/match/conditions/tokencondition.cpp b/libwccl/ops/match/conditions/tokencondition.cpp
new file mode 100644
index 0000000..e6c0b0f
--- /dev/null
+++ b/libwccl/ops/match/conditions/tokencondition.cpp
@@ -0,0 +1,41 @@
+#include <libwccl/ops/match/conditions/tokencondition.h>
+#include <libwccl/values/tokenmatch.h>
+
+namespace Wccl {
+
+
+TokenCondition::TokenCondition(const boost::shared_ptr<Function<Bool> >& predicate)
+	: _predicate(predicate)
+{
+}
+
+std::string TokenCondition::name() const
+{
+	return _predicate->raw_name();
+}
+
+std::string TokenCondition::to_string(const Corpus2::Tagset& tagset) const
+{
+	return _predicate->to_string(tagset);
+}
+
+MatchResult TokenCondition::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const
+{
+	if (_predicate->apply(context)->get_value()) {
+		boost::shared_ptr<Match> match = boost::shared_ptr<Match>(new TokenMatch(
+				context.sentence_context().get_abs_position(*iter_pos)));
+		// increase the $_ variable by one after successful token match
+		iter_pos->set_value(iter_pos->get_value() + 1);
+		return MatchResult(true, match);
+	}
+	else {
+		return MatchResult();
+	}
+}
+
+std::ostream& TokenCondition::write_to(std::ostream& os) const
+{
+	return os << *_predicate;
+}
+
+} /* end ns Wccl */
diff --git a/libwccl/ops/match/conditions/tokencondition.h b/libwccl/ops/match/conditions/tokencondition.h
new file mode 100644
index 0000000..94f0bf2
--- /dev/null
+++ b/libwccl/ops/match/conditions/tokencondition.h
@@ -0,0 +1,47 @@
+#ifndef LIBWCCL_OPS_MATCH_CONDITIONS_TOKENCODITION_H
+#define LIBWCCL_OPS_MATCH_CONDITIONS_TOKENCODITION_H
+
+#include <libwccl/ops/match/matchcondition.h>
+#include <libwccl/ops/function.h>
+
+namespace Wccl {
+
+/**
+ * Class for conditions of MatchOperator that operate on tokens,
+ * wrapping WCCL Predicates
+ */
+class TokenCondition : public MatchCondition
+{
+public:
+	TokenCondition(const boost::shared_ptr<Function<Bool> >& predicate);
+	/**
+	 * @returns Name of the condition.
+	 */
+	std::string name() const;
+	/**
+	 * Applies the condition to the given execution context.
+	 * If match is found, the current iter Position "$_" is increased
+	 * by one (the size of token match, which is always one).
+	 */
+	MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const;
+
+	/**
+	 * @returns String representation of the Action
+	 */
+	std::string to_string(const Corpus2::Tagset& tagset) const;
+
+protected:
+	/**
+	 * Writes string representation of the TokenCondition to
+	 * an output stream.
+	 * @returns Stream written to.
+	 * @note May be incomplete and/or containt internal info.
+	 */
+	std::ostream& write_to(std::ostream& ostream) const;
+private:
+	const boost::shared_ptr< const Function<Bool> > _predicate;
+};
+
+} /* end ns Wccl */
+
+#endif // LIBWCCL_OPS_MATCH_CONDITIONS_TOKENCODITION_H
-- 
GitLab