#include "rdf_rule_core.h"
#include "rdf_date_time.h"
#include "rule_expression.h"
#include "rule_expression_names.h"
namespace test_literal1 {
using namespace std;
using rdf::index_type;
using rdf::resource;
using rdf::literal;
using rdf::internal::resource_base_map;
using rdf::internal::to_literal;
using rdf::date_type_from_string;
using rdf::time_type_from_string;
using rdf::duration_type_from_string;
using rule::internal::expression_rule_term_ptr_type;
using rule::internal::fa;
using rule::internal::fa_ptr_type;
using rule::internal::relation_row_map;
using rule::F_add_aa;
using rule::F_add_xa;
using rule::F_eq_ax;
using rule::F_str_ends_with_aa;
using rule::F_str_starts_with_aa;
using rule::F_str_to_upper_a;
using rule::F_eq_xa;
using rule::F_str_to_lower_a;
using rule::F_and_xx;
using rule::F_str_eq_no_case_aa;
using rule::F_get_age_as_of_aa;
using rule::F_sub_aa;
void assert_val(literal const& val, literal const& expected_val)
{
cout << val << " - ";
if(&val == &expected_val) cout << "ok";
else {
cout << "ERROR - expecting "<< expected_val << "(of type "<<expected_val.get_type_name()<<", but got type "<<val.get_type_name()<<")";
}
cout << "\n";
};
std::string assert_val(bool const val, bool const expected_val)
{
std::string s;
if(val) s = "true - ";
else s = "false - ";
if(val == expected_val) s += "ok";
else s += "ERROR";
return s;
};
int test(int argc, char *argv[])
{
cout << "\n---test_literal1-------------------------------------------------------------------------------------\n";
resource_base_map resources;
cout << "Creating literals:\n";
index_type const& i_michel = resources.create_literal_as_index("Michel");
literal const& l_michel = to_literal(i_michel);
cout << l_michel << "\n";
index_type const& i_50 = resources.create_literal_as_index("50 days", int(50));
literal const& l_50 = to_literal(i_50);
cout << l_50 << "\n";
index_type const& i_49_9f = resources.create_literal_as_index("$49.90", float(49.9));
literal const& l_49_9f = to_literal(i_49_9f);
cout << l_49_9f << "\n";
index_type const& date1 = resources.create_literal_as_index("date1", date_type_from_string("2007-01-09"));
literal const& l_date1 = to_literal(date1);
cout << l_date1 << "\n";
index_type const& time1 = resources.create_literal_as_index("time1", time_type_from_string("2007-01-10 16:30:00"));
literal const& l_time1 = to_literal(time1);
cout << l_time1 << "\n";
index_type const& duration1 = resources.create_literal_as_index("duration1", duration_type_from_string("16:30:00"));
literal const& l_duration1 = to_literal(duration1);
cout << l_duration1 << "\n";
cout << "\nLet's try to find them back\n\n";
cout << "looking for 'Michel' content: ";
assert_val(resources.get_literal("Michel"), l_michel);
cout << "looking for '50 days' content: ";
assert_val(resources.get_literal("50 days"), l_50);
cout << "looking for '$49.90' content: ";
assert_val(resources.get_literal("$49.90"), l_49_9f);
cout << "looking for 'date1' content: ";
assert_val(resources.get_literal("date1"), l_date1);
cout << "looking for 'time1' content: ";
assert_val(resources.get_literal("time1"), l_time1);
cout << "looking for 'duration1' content: ";
assert_val(resources.get_literal("duration1"), l_duration1);
cout << endl;
relation_row_map row;
cout << "Let's check that (time1 == (date1 + 1) + duration1) holds true. . .";
fa_ptr_type fa_date1_p(new fa(l_date1));
fa_ptr_type fa_time1_p(new fa(l_time1));
fa_ptr_type fa_duration1_p(new fa(l_duration1));
fa_ptr_type fa_1_p(new fa(resources.create_literal("1", int(1))));
expression_rule_term_ptr_type et1_p(new F_add_aa(fa_date1_p, fa_1_p));
expression_rule_term_ptr_type et2_p(new F_add_xa(et1_p , fa_duration1_p));
expression_rule_term_ptr_type et3_p(new F_eq_ax (fa_time1_p, et2_p));
cout << assert_val(et3_p->eval(NULL, row), true) << endl;
cout << "(date1 + 1) is " << (*et1_p)(NULL, row) << endl;
cout << "((date1 + 1) + duration1) is " << (*et2_p)(NULL, row) << endl;
cout << "time1 is " << (*fa_time1_p)(NULL, row) << endl;
cout << "(time1 == (date1 + 1) + duration1) is " << (*et3_p)(NULL, row) << endl;
cout << endl;
cout << "Check that ('Michel' ends_with 'chel') holds true. . .";
fa_ptr_type fa_michel_p(new fa(l_michel));
literal const&l_chel = resources.create_literal("chel", "chel");
fa_ptr_type fa_chel_p(new fa(l_chel));
et1_p.reset(new F_str_ends_with_aa(fa_michel_p, fa_chel_p));
cout << assert_val(et1_p->eval(NULL, row), true) << endl;
cout << endl;
cout << "Check that ('Michel' starts_with 'M') holds true. . .";
literal const& l_M = resources.create_literal("M", "M");
fa_ptr_type fa_M_p(new fa(l_M));
et1_p.reset(new F_str_starts_with_aa(fa_michel_p, fa_M_p));
cout << assert_val(et1_p->eval(NULL, row), true) << endl;
cout << endl;
literal const& l_MONTREAL = resources.create_literal("MONTREAL", "MONTREAL");
literal const& l_montreal = resources.create_literal("montreal", "montreal");
literal const& l_mONtREAL = resources.create_literal("mONtREAL", "mONtREAL");
cout << "Check that ((((str_to_upper 'mONtREAL') == 'MONTREAL') and ((str_to_lower 'mONtREAL') == 'montreal')) and ('MONTREAL' str_eq_no_case 'montreal')) holds true. . .";
fa_ptr_type fa_MONTREAL_p(new fa(l_MONTREAL));
fa_ptr_type fa_montreal_p(new fa(l_montreal));
fa_ptr_type fa_mONtREAL_p(new fa(l_mONtREAL));
et1_p.reset(new F_str_to_upper_a(fa_mONtREAL_p));
et2_p.reset(new F_eq_xa(et1_p, fa_MONTREAL_p));
et3_p.reset(new F_str_to_lower_a(fa_mONtREAL_p));
expression_rule_term_ptr_type et4_p(new F_eq_xa (et3_p, fa_montreal_p));
expression_rule_term_ptr_type et5_p(new F_and_xx (et2_p, et4_p));
expression_rule_term_ptr_type et6_p(new F_str_eq_no_case_aa (fa_MONTREAL_p, fa_montreal_p));
expression_rule_term_ptr_type et7_p(new F_and_xx (et5_p, et6_p));
cout << assert_val(et7_p->eval(NULL, row), true) << endl;
cout << endl;
cout << "Check that Sophie's age as of 2007-01-10 is 6: (('2000-07-27' get_age_as_of '2007-01-10') == 6) holds true. . .";
literal const& l_2000 = resources.create_literal("2000-07-27", date_type_from_string("2000-07-27"));
literal const& l_2007 = resources.create_literal("2007-01-10", date_type_from_string("2007-01-10"));
literal const& l_6 = resources.create_literal("6", int(6));
fa_ptr_type fa_2000_p(new fa(l_2000));
fa_ptr_type fa_2007_p(new fa(l_2007));
fa_ptr_type fa_6_p(new fa(l_6));
et1_p.reset(new F_get_age_as_of_aa(fa_2000_p, fa_2007_p));
et2_p.reset(new F_eq_xa(et1_p, fa_6_p));
cout << assert_val(et2_p->eval(NULL, row), true) << endl;
cout << "('2000-07-27' get_age_as_of '2007-01-10') is " << (*et1_p)(NULL, row) << endl;
cout << "6 is " << (*fa_6_p)(NULL, row) << endl;
cout << "(('2000-07-27' get_age_as_of '2007-01-10') == 6) is " << (*et2_p)(NULL, row) << endl;
cout << endl;
cout << "Check (('2007-01-10' - '08:00:00') == '2007-01-09 16:00:00) holds true. . .";
literal const& l_8h = resources.create_literal("08:00:00", duration_type_from_string("08:00:00"));
literal const& l_time2 = resources.create_literal("2007-01-09 16:00:00", time_type_from_string("2007-01-09 16:00:00"));
fa_ptr_type fa_8h_p(new fa(l_8h));
fa_ptr_type fa_time2_p(new fa(l_time2));
et1_p.reset(new F_sub_aa(fa_2007_p, fa_8h_p));
et2_p.reset(new F_eq_xa(et1_p, fa_time2_p));
cout << assert_val(et2_p->eval(NULL, row), true) << endl;
cout << "('2007-01-10' - '08:00:00') is " << (*et1_p)(NULL, row) << endl;
cout << endl;
cout << "\n. . .done!\n";
cout << " ---------------------------------------------------------------------------------------------------------\n";
return EXIT_SUCCESS;
};
}; // namespace test_...