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

VarGetter, operator getting value of a variable.

It is used wherever in WCCL variable is seen in read-only context, i.e. outside of operators that are setting its value like setvar.
parent 80d0cdfc
No related branches found
No related tags found
No related merge requests found
#ifndef LIBWCCL_OPS_VARGETTER_H
#define LIBWCCL_OPS_VARGETTER_H
#include <libwccl/ops/functions.h>
namespace Wccl {
/**
* Operator that returns value of a variable of given type T
*/
template<class T>
class VarGetter : public Function<T> {
public:
VarGetter(const VariableAccesor<T>& var_acc)
: var_acc_(var_acc)
{
}
/**
* @returns Operator name for variable getter, we assume "$", although real
* string representation depends on given Value's var_repr function
*/
virtual const std::string raw_operator_name() const {
return "$";
}
/**
* @returns String representation of the variable getter which is
* equal to string representation of the underlying variable.
*/
virtual std::string to_raw_string() const {
return T::var_repr(var_acc_.get_name());
}
/**
* @returns String representation of the variable getter which is
* equal to string representation of the underlying variable.
*/
virtual std::string to_string(const Corpus2::Tagset&) const {
return T::var_repr(var_acc_.get_name());
}
protected:
/**
* Return value held by the underlying variable.
*/
virtual BaseRetValPtr apply_internal(const FunExecContext& context) const {
return context.variables()->get_fast(var_acc_);
}
private:
const VariableAccesor<T> var_acc_;
};
} /* end ns Wccl */
#endif // LIBWCCL_OPS_VARGETTER_H
......@@ -15,6 +15,7 @@ add_executable(tests
strsetfunctions.cpp
values.cpp
varaccess.cpp
vargetter.cpp
variables.cpp
)
......
#include <boost/test/unit_test.hpp>
#include <boost/bind.hpp>
#include <libwccl/ops/vargetter.h>
#include <libwccl/values/bool.h>
#include <libwccl/values/tset.h>
#include <libwccl/values/strset.h>
#include <libwccl/ops/constant.h>
using namespace Wccl;
BOOST_AUTO_TEST_SUITE(vargetter)
struct VarGetFix
{
VarGetFix()
: sc(boost::make_shared<Corpus2::Sentence>()),
cx(sc, boost::make_shared<Variables>()),
tagset(),
true_value(true),
false_value(false),
true_constant(true_value),
false_constant(false_value)
{
cx.variables()->put("True_bool", true_value);
cx.variables()->put("False_bool", false_value);
}
SentenceContext sc;
FunExecContext cx;
Corpus2::Tagset tagset;
Bool true_value;
Bool false_value;
Constant<Bool> true_constant;
Constant<Bool> false_constant;
};
BOOST_FIXTURE_TEST_CASE(bool_apply, VarGetFix)
{
VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool");
VarGetter<Bool> var_getter_t(acc_t);
BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value());
VariableAccesor<Bool> acc_f = cx.variables()->create_accesor<Bool>("False_bool");
VarGetter<Bool> var_getter_f(acc_f);
BOOST_CHECK_EQUAL(false, var_getter_f.apply(cx)->get_value());
}
BOOST_FIXTURE_TEST_CASE(bool_apply_acc_destruct, VarGetFix)
{
boost::shared_ptr<VarGetter<Bool> > var_getter;
{
VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool");
var_getter.reset(new VarGetter<Bool>(acc_t));
}
BOOST_CHECK_EQUAL(true, var_getter->apply(cx)->get_value());
}
BOOST_FIXTURE_TEST_CASE(bool_mod, VarGetFix)
{
VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool");
VarGetter<Bool> var_getter_t(acc_t);
BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value());
cx.variables()->get<Bool>("True_bool")->set_value(false);
BOOST_CHECK_EQUAL(false, var_getter_t.apply(cx)->get_value());
cx.variables()->put("True_bool", Bool(true));
BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value());
cx.variables()->set("True_bool", false_value);
BOOST_CHECK_EQUAL(false, var_getter_t.apply(cx)->get_value());
cx.variables()->set("True_bool", Bool(true));
BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value());
}
//---- To String -----
BOOST_FIXTURE_TEST_CASE(bool_varget_to_string, VarGetFix)
{
VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool");
VarGetter<Bool> var_getter_t(acc_t);
BOOST_CHECK_EQUAL(Bool::var_repr("True_bool"), var_getter_t.to_string(tagset));
}
BOOST_FIXTURE_TEST_CASE(bool_varget_to_raw_string, VarGetFix)
{
VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool");
VarGetter<Bool> var_getter_t(acc_t);
BOOST_CHECK_EQUAL(Bool::var_repr("True_bool"), var_getter_t.to_raw_string());
}
BOOST_AUTO_TEST_SUITE_END()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment