Skip to content
Snippets Groups Projects
Commit 2fddc450 authored by Paweł Kędzia's avatar Paweł Kędzia
Browse files

Merge branch 'master' of nlp.pwr.wroc.pl:wccl

parents 2131e3e5 bcbd29c4
Branches
No related tags found
No related merge requests found
...@@ -5,24 +5,22 @@ ...@@ -5,24 +5,22 @@
namespace Wccl { namespace Wccl {
ApplyOperator::ApplyOperator( ApplyOperator::ApplyOperator(
const VariableAccessor<Position>& cur_iter_pos,
const VariableAccessor<Match>& matches, const VariableAccessor<Match>& matches,
const boost::shared_ptr<const MatchOperator>& match_op, const boost::shared_ptr<const MatchOperator>& match_op,
const std::vector<boost::shared_ptr<const MatchAction> >& actions, const boost::shared_ptr<const std::vector<boost::shared_ptr<MatchAction> > >& actions,
const std::vector<boost::shared_ptr<const Function<Bool> > >& conditions) const boost::shared_ptr<const std::vector<boost::shared_ptr<Function<Bool> > > >& conditions)
: _cur_iter_pos(cur_iter_pos), : _matches(matches),
_matches(matches),
_match_op(match_op), _match_op(match_op),
_actions(actions), _actions(actions),
_conditions(conditions) _conditions(conditions)
{ {
BOOST_ASSERT(_match_op); BOOST_ASSERT(_match_op);
BOOST_ASSERT(actions.size() > 0); BOOST_ASSERT(actions->size() > 0);
} }
void ApplyOperator::execute(const ActionExecContext &context) const void ApplyOperator::execute(const ActionExecContext &context) const
{ {
boost::shared_ptr<Position> iter_pos = context.variables()->get_fast(_cur_iter_pos); 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));
while(context.sentence_context().is_current_inside()) { while(context.sentence_context().is_current_inside()) {
...@@ -32,12 +30,12 @@ void ApplyOperator::execute(const ActionExecContext &context) const ...@@ -32,12 +30,12 @@ void ApplyOperator::execute(const ActionExecContext &context) const
boost::shared_ptr<Match> match = _match_op->apply(iter_pos, context); boost::shared_ptr<Match> match = _match_op->apply(iter_pos, 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) {
should_act = _conditions[i]->apply(context)->get_value(); should_act = (*_conditions)[i]->apply(context)->get_value();
} }
if (should_act) { if (should_act) {
matches->append(match); // the match goes to $m:_M[0] matches->append(match); // the match goes to $m:_M[0]
foreach (const boost::shared_ptr<const MatchAction>& action, _actions) { foreach (const boost::shared_ptr<MatchAction>& action, *_actions) {
action->execute(context); action->execute(context);
} }
} }
...@@ -55,22 +53,22 @@ std::string ApplyOperator::to_string(const Corpus2::Tagset& tagset) const ...@@ -55,22 +53,22 @@ std::string ApplyOperator::to_string(const Corpus2::Tagset& tagset) const
{ {
std::ostringstream ostream; std::ostringstream ostream;
ostream << name() << "(" << _match_op->to_string(tagset) << ", "; ostream << name() << "(" << _match_op->to_string(tagset) << ", ";
if (!_conditions.empty()) { if (!_conditions->empty()) {
ostream << "cond("; ostream << "cond(";
for(size_t i = 0; i < _conditions.size(); ++i) { for(size_t i = 0; i < _conditions->size(); ++i) {
if (i != 0) { if (i != 0) {
ostream << ", "; ostream << ", ";
} }
ostream << _conditions[i]->to_string(tagset); ostream << (*_conditions)[i]->to_string(tagset);
} }
ostream << "), "; ostream << "), ";
} }
ostream << "actions("; ostream << "actions(";
for(size_t i = 0; i < _actions.size(); ++i) { for(size_t i = 0; i < _actions->size(); ++i) {
if (i != 0) { if (i != 0) {
ostream << ", "; ostream << ", ";
} }
ostream << _actions[i]->to_string(tagset); ostream << (*_actions)[i]->to_string(tagset);
} }
ostream << "))"; ostream << "))";
return ostream.str(); return ostream.str();
...@@ -79,22 +77,22 @@ std::string ApplyOperator::to_string(const Corpus2::Tagset& tagset) const ...@@ -79,22 +77,22 @@ std::string ApplyOperator::to_string(const Corpus2::Tagset& tagset) const
std::ostream& ApplyOperator::write_to(std::ostream &ostream) const std::ostream& ApplyOperator::write_to(std::ostream &ostream) const
{ {
ostream << name() << "(" << *_match_op << ", "; ostream << name() << "(" << *_match_op << ", ";
if (!_conditions.empty()) { if (!_conditions->empty()) {
ostream << "cond("; ostream << "cond(";
for(size_t i = 0; i < _conditions.size(); ++i) { for(size_t i = 0; i < _conditions->size(); ++i) {
if (i != 0) { if (i != 0) {
ostream << ", "; ostream << ", ";
} }
ostream << *_conditions[i]; ostream << *(*_conditions)[i];
} }
ostream << "), "; ostream << "), ";
} }
ostream << "actions("; ostream << "actions(";
for(size_t i = 0; i < _actions.size(); ++i) { for(size_t i = 0; i < (*_actions).size(); ++i) {
if (i != 0) { if (i != 0) {
ostream << ", "; ostream << ", ";
} }
ostream << *_actions[i]; ostream << *(*_actions)[i];
} }
ostream << "))"; ostream << "))";
return ostream; return ostream;
......
#ifndef LIBWCCL_OPS_MATCH_APPLYOPERATOR_H #ifndef LIBWCCL_OPS_MATCH_APPLYOPERATOR_H
#define LIBWCCL_OPS_MATCH_APPLYOPERATOR_H #define LIBWCCL_OPS_MATCH_APPLYOPERATOR_H
#include <libwccl/ops/function.h> #include <libwccl/ops/functions/bool/predicates/logicalpredicate.h>
#include <libwccl/ops/match/matchoperator.h> #include <libwccl/ops/match/matchoperator.h>
#include <libwccl/ops/match/matchaction.h> #include <libwccl/ops/match/matchaction.h>
namespace Wccl { namespace Wccl {
/** /**
...@@ -14,20 +15,19 @@ namespace Wccl { ...@@ -14,20 +15,19 @@ namespace Wccl {
class ApplyOperator : public Expression class ApplyOperator : public Expression
{ {
public: public:
typedef LogicalPredicate::BoolFunctionPtrVector BoolFunctionPtrVector;
/** /**
* @param cur_iter_pos Accessor to the "$_" variable
* @param matches Accessor to the "$m:_M" variable * @param matches Accessor to the "$m:_M" variable
* @param match_op "match" operator for apply * @param match_op "match" operator for apply
* @param actions "actions" section of apply, should not be empty * @param actions "actions" section of apply, should not be empty
* @param conditions "cond" section of apply, empty by default * @param conditions "cond" section of apply, empty by default
*/ */
ApplyOperator( ApplyOperator(
const VariableAccessor<Position>& cur_iter_pos,
const VariableAccessor<Match>& matches, const VariableAccessor<Match>& matches,
const boost::shared_ptr<const MatchOperator>& match_op, const boost::shared_ptr<const MatchOperator>& match_op,
const std::vector<boost::shared_ptr<const MatchAction> >& actions, const boost::shared_ptr<const std::vector<boost::shared_ptr<MatchAction> > >& actions,
const std::vector<boost::shared_ptr<const Function<Bool> > >& conditions = (std::vector<boost::shared_ptr<const Function<Bool> > >())); const boost::shared_ptr<const BoolFunctionPtrVector>& conditions
= boost::shared_ptr<const BoolFunctionPtrVector>(new BoolFunctionPtrVector()));
/** /**
* @returns Name of the operator. * @returns Name of the operator.
...@@ -58,11 +58,10 @@ protected: ...@@ -58,11 +58,10 @@ protected:
virtual void execute(const ActionExecContext &context) const; virtual void execute(const ActionExecContext &context) const;
private: private:
const VariableAccessor<Position> _cur_iter_pos;
const VariableAccessor<Match> _matches; const VariableAccessor<Match> _matches;
const boost::shared_ptr<const MatchOperator> _match_op; const boost::shared_ptr<const MatchOperator> _match_op;
const std::vector<boost::shared_ptr<const MatchAction> > _actions; const boost::shared_ptr<const std::vector<boost::shared_ptr<MatchAction> > > _actions;
const std::vector<boost::shared_ptr<const Function<Bool> > > _conditions; const boost::shared_ptr<const std::vector<boost::shared_ptr<Function<Bool> > > > _conditions;
}; };
} /* end ns Wccl */ } /* end ns Wccl */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment