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
Branches
No related tags found
No related merge requests found
Showing with 44 additions and 53 deletions
......@@ -20,14 +20,14 @@ ApplyOperator::ApplyOperator(
void ApplyOperator::execute(const ActionExecContext &context) const
{
boost::shared_ptr<Position> iter_pos(new Position(0));
boost::shared_ptr<MatchVector> matches =
boost::dynamic_pointer_cast<MatchVector>(context.variables()->get_fast(_matches));
context.sentence_context().goto_start();
while(context.sentence_context().is_current_inside()) {
// Set initial values of $_ and $m:_M variables for this iteration and launch the match:
iter_pos->set_value(0);
int orig_pos = context.sentence_context().get_position();
// Set initial value for $m:_M variable for this iteration and launch the match:
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:
bool should_act = !match->empty();
for(size_t i = 0; should_act && i < _conditions->size(); ++i) {
......@@ -40,12 +40,8 @@ void ApplyOperator::execute(const ActionExecContext &context) const
}
}
// Inner operators (match and its conditions) are responsible for properly
// advancing $_ variable.
// After an iteration, we increase current sentence position by the value of $_
// 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());
// advancing current sentence position.
BOOST_ASSERT(context.sentence_context().get_position() > orig_pos);
}
}
......
......@@ -11,17 +11,17 @@ ConjConditions::ConjConditions(const std::vector< boost::shared_ptr<const MatchC
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());
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) {
MatchResult res = cond->apply(iter_pos, context);
MatchResult res = cond->apply(context);
if(res.matched()) {
matches->append(res.get_match());
} else {
iter_pos->set_value(orig_iter_pos);
context.sentence_context().set_position(orig_pos);
return MatchResult();
}
}
......
......@@ -22,10 +22,10 @@ public:
}
/**
* 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).
*/
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const;
MatchResult apply(const ActionExecContext& context) const;
/**
* @returns String representation of the MatchCondition
......
......@@ -10,14 +10,14 @@ OptionalMatch::OptionalMatch(const boost::shared_ptr<ConjConditions>& 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();
MatchResult res = _conditions->apply(iter_pos, context);
int orig_pos = context.sentence_context().get_position();
MatchResult res = _conditions->apply(context);
if (res.matched()) {
return res;
}
iter_pos->set_value(orig_pos);
context.sentence_context().set_position(orig_pos);
return MatchResult(boost::make_shared<Match>());
}
......
......@@ -25,11 +25,11 @@ public:
* If the match, was successfull the result consists of "true"
* and the 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
* 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
......
......@@ -10,20 +10,20 @@ RepeatedMatch::RepeatedMatch(const boost::shared_ptr<ConjConditions>& 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());
int orig_pos = iter_pos->get_value();
MatchResult res = _conditions->apply(iter_pos, context);
int orig_pos = context.sentence_context().get_position();
MatchResult res = _conditions->apply(context);
if (res.matched()) {
do {
match->append(res.get_match());
res = _conditions->apply(iter_pos, context);
res = _conditions->apply(context);
} while (res.matched());
return MatchResult(match);
}
else {
iter_pos->set_value(orig_pos);
context.sentence_context().set_position(orig_pos);
return MatchResult();
}
}
......
......@@ -26,11 +26,11 @@ public:
* and the MatchVector constructed out of individual match vectors coming
* from each successful repetition.
* 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
* 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
......
......@@ -19,17 +19,17 @@ 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
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()) {
boost::shared_ptr<Match> match(new TokenMatch(context.sentence_context().get_abs_position(*iter_pos)));
// increase the $_ variable by one after successful token match
iter_pos->set_value(orig_iter + 1);
boost::shared_ptr<Match> match(new TokenMatch(context.sentence_context().get_position()));
// increase current sentence position by one after successful token match
context.sentence_context().set_position(orig_iter + 1);
return MatchResult(match);
}
else {
iter_pos->set_value(orig_iter);
context.sentence_context().set_position(orig_iter);
return MatchResult();
}
}
......
......@@ -20,10 +20,10 @@ public:
std::string name() const;
/**
* 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).
*/
MatchResult apply(boost::shared_ptr<Position> iter_pos, const FunExecContext& context) const;
MatchResult apply(const ActionExecContext& context) const;
/**
* @returns String representation of the Action
......
#ifndef 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/match/matchresult.h>
......@@ -19,10 +19,10 @@ public:
virtual std::string name() const = 0;
/**
* 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.
*/
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 */
......
......@@ -3,16 +3,14 @@
namespace Wccl {
boost::shared_ptr<Match> MatchOperator::apply(
boost::shared_ptr<Position> iter_pos,
const FunExecContext& context) const
boost::shared_ptr<Match> MatchOperator::apply(const ActionExecContext& context) const
{
int orig_iter_pos = iter_pos->get_value();
MatchResult res = _conditions->apply(iter_pos, context);
int orig_pos = context.sentence_context().get_position();
MatchResult res = _conditions->apply(context);
if(res.matched()) {
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>();
}
......
......@@ -31,16 +31,13 @@ public:
/**
* Applies the operator to the given context.
* @returns Vector of matches corresponding to match conditions,
* if all conditions were met. In such case position $_ points one position
* ahead of matched tokens.
* if all conditions were met. In such case current sentence position
* points to one position ahead of matched tokens.
* Empty MatchVector is returned if any of the conditions was not met.
* In such case Position $_ is advanced by 1.
* @param iter_pos Pointer to the variable $_ from context's Variables
* In such case current Position in sentence is advanced by 1.
* @param context Execution context - current sentence and Variables to operate on
*/
boost::shared_ptr<Match> apply(
boost::shared_ptr<Position> iter_pos,
const FunExecContext& context) const;
boost::shared_ptr<Match> apply(const ActionExecContext& context) const;
protected:
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment