Skip to content
Snippets Groups Projects
Select Git revision
  • 24c70cad75f57acc2745b4abdfc7ad1573d638ea
  • master default protected
  • vertical_relations
  • lu_without_semantic_frames
  • hierarchy
  • additional-unification-filters
  • v0.1.1
  • v0.1.0
  • v0.0.9
  • v0.0.8
  • v0.0.7
  • v0.0.6
  • v0.0.5
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
17 results

tests.py

Blame
  • conditional.h 4.51 KiB
    #ifndef LIBWCCL_OPS_FUNCTIONS_CONDITIONAL_H
    #define LIBWCCL_OPS_FUNCTIONS_CONDITIONAL_H
    
    #include <boost/format.hpp>
    #include <libwccl/ops/functions/constant.h>
    
    namespace Wccl {
    
    /**
     * Template class for conditional operators, returning value
     * depending on evaluation of some predicate.
     * This class is targeted towards if..then..else expression
     */
    template<class T>
    class Conditional : public Function<T>
    {
    public:
    	typedef boost::shared_ptr<const Function<T> > ArgFunctionPtr;
    	typedef boost::shared_ptr<const Function<Bool> > BoolFunctionPtr;
    	
    	Conditional(
    		const BoolFunctionPtr& cond_expr,
    		const ArgFunctionPtr& iftrue_expr,
    		const ArgFunctionPtr& iffalse_expr = Default())
    		: cond_expr_(cond_expr), iftrue_expr_(iftrue_expr), iffalse_expr_(iffalse_expr)
    	{
    		BOOST_ASSERT(cond_expr_);
    		BOOST_ASSERT(iftrue_expr_);
    		BOOST_ASSERT(iffalse_expr_);
    	}
    
    	/**
    	 * String representation of conditional operator in form of:
    	 * "if(cond_expr_string, iftrue_expr_string, iffalse_expr_string)"
    	 */
    	std::string to_string(const Corpus2::Tagset& tagset) const;
    
    	/**
    	 * @returns Name of the function: "if"
    	 */
    	std::string raw_name() const {
    		return "if";
    	}
    
    protected:
    	const BoolFunctionPtr cond_expr_;
    	const ArgFunctionPtr iftrue_expr_;
    	const ArgFunctionPtr iffalse_expr_;
    
    	/**
    	 * Evaluate the predicate. If it is true, evaluate and return value of
    	 * iftrue_expression. If predicate is false, evalute and return value
    	 * of iffalse_expression.
    	 */
    	FunctionBase::BaseRetValPtr apply_internal(const FunExecContext& context) const;
    
    	/**
    	 * Writes raw string representation of conditional operator in form of:
    	 * "if(cond_expr_raw_s, iftrue_expr_raw_s, iffalse_expr_raw_s)"
    	 * This version does not require tagset, but may be inclomplete
    	 * and/or contain internal info.
    	 * @returns Stream written to.
    	 */
    	virtual std::ostream& write_to(std::ostream& ostream) const;
    
    private:
    	static const ArgFunctionPtr Default() {
    		return detail::DefaultFunction<T>();
    	}
    };