Skip to content
Snippets Groups Projects
rulesequence.cpp 944 B
Newer Older
#include <libwccl/ops/rulesequence.h>
#include <libpwrutils/foreach.h>


namespace Wccl {

Bool RuleSequence::execute_once(const boost::shared_ptr<Corpus2::Sentence>& sentence)
{
	if(!sentence || sentence->empty()) {
		throw InvalidArgument(
				"sentence",
				"Received an empty sentence.");
	}
	Bool changed(false);
	SentenceContext sc(sentence);
	while(sc.is_current_inside()) {
Adam Wardynski's avatar
Adam Wardynski committed
		foreach (TagRule& rule, *this) {
			if (rule.execute(sc).get_value()) {
				changed.set_value(true);
			}
		}
		sc.advance();
	}
	return changed;
}

int RuleSequence::execute_until_done(const boost::shared_ptr<Corpus2::Sentence>& sentence, int max_iter)
{
	if(max_iter < 0) {
		throw InvalidArgument(
				"max_iter",
				"Supplied a negative value for maximum number of iterations.");
	}
	int iter_no = 0;
	while(iter_no < max_iter) {
		++iter_no;
		if (!execute_once(sentence).get_value()) {
			return iter_no;
		}
	}
	return iter_no;
}

} /* end ns Wccl */