Skip to content
Snippets Groups Projects
Commit c79a05b8 authored by Adam Wardynski's avatar Adam Wardynski
Browse files

Finishing up change $_ -> current sentence pos (internal changes).

parent 4c83c9b7
No related branches found
No related tags found
No related merge requests found
Showing with 44 additions and 53 deletions
...@@ -20,14 +20,14 @@ ApplyOperator::ApplyOperator( ...@@ -20,14 +20,14 @@ ApplyOperator::ApplyOperator(
void ApplyOperator::execute(const ActionExecContext &context) const void ApplyOperator::execute(const ActionExecContext &context) const
{ {
boost::shared_ptr<Position> iter_pos(new Position(0));
boost::shared_ptr<MatchVector> matches = boost::shared_ptr<MatchVector> matches =
boost::dynamic_pointer_cast<MatchVector>(context.variables()->get_fast(_matches)); boost::dynamic_pointer_cast<MatchVector>(context.variables()->get_fast(_matches));
context.sentence_context().goto_start();
while(context.sentence_context().is_current_inside()) { while(context.sentence_context().is_current_inside()) {
// Set initial values of $_ and $m:_M variables for this iteration and launch the match: int orig_pos = context.sentence_context().get_position();
iter_pos->set_value(0); // Set initial value for $m:_M variable for this iteration and launch the match:
matches->clear(); matches->clear();
boost::shared_ptr<Match> match = _match_op->apply(iter_pos, context); boost::shared_ptr<Match> match = _match_op->apply(context);
// Execute the actions only if match isn't empty and all post-conditions are met: // Execute the actions only if match isn't empty and all post-conditions are met:
bool should_act = !match->empty(); bool should_act = !match->empty();
for(size_t i = 0; should_act && i < _conditions->size(); ++i) { for(size_t i = 0; should_act && i < _conditions->size(); ++i) {
...@@ -40,12 +40,8 @@ void ApplyOperator::execute(const ActionExecContext &context) const ...@@ -40,12 +40,8 @@ void ApplyOperator::execute(const ActionExecContext &context) const
} }
} }
// Inner operators (match and its conditions) are responsible for properly // Inner operators (match and its conditions) are responsible for properly
// advancing $_ variable. // advancing current sentence position.
// After an iteration, we increase current sentence position by the value of $_ BOOST_ASSERT(context.sentence_context().get_position() > orig_pos);
// and repeat until current sentence position goes outside.
BOOST_ASSERT(iter_pos->get_value() > 0);
context.sentence_context().set_position(
context.sentence_context().get_position() + iter_pos->get_value());
} }
} }
......
...@@ -11,17 +11,17 @@ ConjConditions::ConjConditions(const std::vector< boost::shared_ptr<const MatchC ...@@ -11,17 +11,17 @@ ConjConditions::ConjConditions(const std::vector< boost::shared_ptr<const MatchC
BOOST_ASSERT(_conditions.size() > 0); BOOST_ASSERT(_conditions.size() > 0);
} }
MatchResult ConjConditions::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const MatchResult ConjConditions::apply(const ActionExecContext& context) const
{ {
boost::shared_ptr<MatchVector> matches(new MatchVector()); boost::shared_ptr<MatchVector> matches(new MatchVector());
int orig_iter_pos = iter_pos->get_value(); int orig_pos = context.sentence_context().get_position();
foreach (const boost::shared_ptr<const MatchCondition>& cond, _conditions) { foreach (const boost::shared_ptr<const MatchCondition>& cond, _conditions) {
MatchResult res = cond->apply(iter_pos, context); MatchResult res = cond->apply(context);
if(res.matched()) { if(res.matched()) {
matches->append(res.get_match()); matches->append(res.get_match());
} else { } else {
iter_pos->set_value(orig_iter_pos); context.sentence_context().set_position(orig_pos);
return MatchResult(); return MatchResult();
} }
} }
......
...@@ -22,10 +22,10 @@ public: ...@@ -22,10 +22,10 @@ public:
} }
/** /**
* Applies the condition to the given execution context. * Applies the condition to the given execution context.
* If match is found, the current iter Position "$_" is increased * If match is found, the current position in sentence is increased
* by one (the size of token match, which is always one). * by one (the size of token match, which is always one).
*/ */
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const; MatchResult apply(const ActionExecContext& context) const;
/** /**
* @returns String representation of the MatchCondition * @returns String representation of the MatchCondition
......
...@@ -10,14 +10,14 @@ OptionalMatch::OptionalMatch(const boost::shared_ptr<ConjConditions>& conditions ...@@ -10,14 +10,14 @@ OptionalMatch::OptionalMatch(const boost::shared_ptr<ConjConditions>& conditions
BOOST_ASSERT(_conditions); BOOST_ASSERT(_conditions);
} }
MatchResult OptionalMatch::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const MatchResult OptionalMatch::apply(const ActionExecContext& context) const
{ {
int orig_pos = iter_pos->get_value(); int orig_pos = context.sentence_context().get_position();
MatchResult res = _conditions->apply(iter_pos, context); MatchResult res = _conditions->apply(context);
if (res.matched()) { if (res.matched()) {
return res; return res;
} }
iter_pos->set_value(orig_pos); context.sentence_context().set_position(orig_pos);
return MatchResult(boost::make_shared<Match>()); return MatchResult(boost::make_shared<Match>());
} }
......
...@@ -25,11 +25,11 @@ public: ...@@ -25,11 +25,11 @@ public:
* If the match, was successfull the result consists of "true" * If the match, was successfull the result consists of "true"
* and the match. * and the match.
* If there was no match, "true" is returned anyway, but with a default Match. * If there was no match, "true" is returned anyway, but with a default Match.
* If match is found, the current iter Position "$_" is increased * If match is found, the current sentence Position is increased
* as to point one token after all the matched tokens, otherwise * as to point one token after all the matched tokens, otherwise
* it stays unchanged. * it stays unchanged.
*/ */
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const; MatchResult apply(const ActionExecContext& context) const;
/** /**
* @returns String representation of the MatchCondition * @returns String representation of the MatchCondition
......
...@@ -10,20 +10,20 @@ RepeatedMatch::RepeatedMatch(const boost::shared_ptr<ConjConditions>& conditions ...@@ -10,20 +10,20 @@ RepeatedMatch::RepeatedMatch(const boost::shared_ptr<ConjConditions>& conditions
BOOST_ASSERT(_conditions); BOOST_ASSERT(_conditions);
} }
MatchResult RepeatedMatch::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const MatchResult RepeatedMatch::apply(const ActionExecContext& context) const
{ {
boost::shared_ptr<MatchVector> match(new MatchVector()); boost::shared_ptr<MatchVector> match(new MatchVector());
int orig_pos = iter_pos->get_value(); int orig_pos = context.sentence_context().get_position();
MatchResult res = _conditions->apply(iter_pos, context); MatchResult res = _conditions->apply(context);
if (res.matched()) { if (res.matched()) {
do { do {
match->append(res.get_match()); match->append(res.get_match());
res = _conditions->apply(iter_pos, context); res = _conditions->apply(context);
} while (res.matched()); } while (res.matched());
return MatchResult(match); return MatchResult(match);
} }
else { else {
iter_pos->set_value(orig_pos); context.sentence_context().set_position(orig_pos);
return MatchResult(); return MatchResult();
} }
} }
......
...@@ -26,11 +26,11 @@ public: ...@@ -26,11 +26,11 @@ public:
* and the MatchVector constructed out of individual match vectors coming * and the MatchVector constructed out of individual match vectors coming
* from each successful repetition. * from each successful repetition.
* If there was no match, "false" is returned along with a default Match. * If there was no match, "false" is returned along with a default Match.
* If match is found, the current iter Position "$_" is increased * If match is found, the current sentence Position is increased
* as to point one token after all the matched tokens, otherwise * as to point one token after all the matched tokens, otherwise
* it stays unchanged. * it stays unchanged.
*/ */
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const; MatchResult apply(const ActionExecContext& context) const;
/** /**
* @returns String representation of the MatchCondition * @returns String representation of the MatchCondition
......
...@@ -19,17 +19,17 @@ std::string TokenCondition::to_string(const Corpus2::Tagset& tagset) const ...@@ -19,17 +19,17 @@ std::string TokenCondition::to_string(const Corpus2::Tagset& tagset) const
return _predicate->to_string(tagset); return _predicate->to_string(tagset);
} }
MatchResult TokenCondition::apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const MatchResult TokenCondition::apply(const ActionExecContext& context) const
{ {
int orig_iter = iter_pos->get_value(); int orig_iter = context.sentence_context().get_position();
if (_predicate->apply(context)->get_value()) { if (_predicate->apply(context)->get_value()) {
boost::shared_ptr<Match> match(new TokenMatch(context.sentence_context().get_abs_position(*iter_pos))); boost::shared_ptr<Match> match(new TokenMatch(context.sentence_context().get_position()));
// increase the $_ variable by one after successful token match // increase current sentence position by one after successful token match
iter_pos->set_value(orig_iter + 1); context.sentence_context().set_position(orig_iter + 1);
return MatchResult(match); return MatchResult(match);
} }
else { else {
iter_pos->set_value(orig_iter); context.sentence_context().set_position(orig_iter);
return MatchResult(); return MatchResult();
} }
} }
......
...@@ -20,10 +20,10 @@ public: ...@@ -20,10 +20,10 @@ public:
std::string name() const; std::string name() const;
/** /**
* Applies the condition to the given execution context. * Applies the condition to the given execution context.
* If match is found, the current iter Position "$_" is increased * If match is found, the current sentence Position is increased
* by one (the size of token match, which is always one). * by one (the size of token match, which is always one).
*/ */
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const; MatchResult apply(const ActionExecContext& context) const;
/** /**
* @returns String representation of the Action * @returns String representation of the Action
......
#ifndef LIBWCCL_OPS_MATCH_MATCHCONDITION_H #ifndef LIBWCCL_OPS_MATCH_MATCHCONDITION_H
#define LIBWCCL_OPS_MATCH_MATCHCONDITION_H #define LIBWCCL_OPS_MATCH_MATCHCONDITION_H
#include <libwccl/ops/funexeccontext.h> #include <libwccl/ops/actionexeccontext.h>
#include <libwccl/ops/expression.h> #include <libwccl/ops/expression.h>
#include <libwccl/ops/match/matchresult.h> #include <libwccl/ops/match/matchresult.h>
...@@ -19,10 +19,10 @@ public: ...@@ -19,10 +19,10 @@ public:
virtual std::string name() const = 0; virtual std::string name() const = 0;
/** /**
* Applies the condition to the given execution context. * Applies the condition to the given execution context.
* If match is found, the current iter position "$_" is * If match is found, the current sentence position is
* set to position right after the match. * set to position right after the match.
*/ */
virtual MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const = 0; virtual MatchResult apply(const ActionExecContext& context) const = 0;
}; };
} /* end ns Wccl */ } /* end ns Wccl */
......
...@@ -3,16 +3,14 @@ ...@@ -3,16 +3,14 @@
namespace Wccl { namespace Wccl {
boost::shared_ptr<Match> MatchOperator::apply( boost::shared_ptr<Match> MatchOperator::apply(const ActionExecContext& context) const
boost::shared_ptr<Position> iter_pos,
const FunExecContext& context) const
{ {
int orig_iter_pos = iter_pos->get_value(); int orig_pos = context.sentence_context().get_position();
MatchResult res = _conditions->apply(iter_pos, context); MatchResult res = _conditions->apply(context);
if(res.matched()) { if(res.matched()) {
return res.get_match(); return res.get_match();
} }
iter_pos->set_value(orig_iter_pos + 1); context.sentence_context().set_position(orig_pos + 1);
return boost::make_shared<Match>(); return boost::make_shared<Match>();
} }
......
...@@ -31,16 +31,13 @@ public: ...@@ -31,16 +31,13 @@ public:
/** /**
* Applies the operator to the given context. * Applies the operator to the given context.
* @returns Vector of matches corresponding to match conditions, * @returns Vector of matches corresponding to match conditions,
* if all conditions were met. In such case position $_ points one position * if all conditions were met. In such case current sentence position
* ahead of matched tokens. * points to one position ahead of matched tokens.
* Empty MatchVector is returned if any of the conditions was not met. * Empty MatchVector is returned if any of the conditions was not met.
* In such case Position $_ is advanced by 1. * In such case current Position in sentence is advanced by 1.
* @param iter_pos Pointer to the variable $_ from context's Variables
* @param context Execution context - current sentence and Variables to operate on * @param context Execution context - current sentence and Variables to operate on
*/ */
boost::shared_ptr<Match> apply( boost::shared_ptr<Match> apply(const ActionExecContext& context) const;
boost::shared_ptr<Position> iter_pos,
const FunExecContext& context) const;
protected: protected:
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment