Commit 9c621eca by Maarten L. Hekkelman

more condition work

parent ab9c4d94
...@@ -216,6 +216,7 @@ set(project_sources ...@@ -216,6 +216,7 @@ set(project_sources
${PROJECT_SOURCE_DIR}/src/TlsParser.cpp ${PROJECT_SOURCE_DIR}/src/TlsParser.cpp
${PROJECT_SOURCE_DIR}/src/v2/category.cpp ${PROJECT_SOURCE_DIR}/src/v2/category.cpp
${PROJECT_SOURCE_DIR}/src/v2/condition.cpp
${PROJECT_SOURCE_DIR}/src/v2/dictionary_parser.cpp ${PROJECT_SOURCE_DIR}/src/v2/dictionary_parser.cpp
${PROJECT_SOURCE_DIR}/src/v2/item.cpp ${PROJECT_SOURCE_DIR}/src/v2/item.cpp
${PROJECT_SOURCE_DIR}/src/v2/parser.cpp ${PROJECT_SOURCE_DIR}/src/v2/parser.cpp
......
...@@ -40,7 +40,6 @@ namespace cif::v2 ...@@ -40,7 +40,6 @@ namespace cif::v2
class category class category
{ {
public: public:
friend class row_handle; friend class row_handle;
template <typename, typename...> template <typename, typename...>
...@@ -122,6 +121,26 @@ class category ...@@ -122,6 +121,26 @@ class category
const std::string &name() const { return m_name; } const std::string &name() const { return m_name; }
iset fields() const
{
if (m_validator == nullptr)
throw std::runtime_error("No Validator specified");
if (m_cat_validator == nullptr)
m_validator->reportError("undefined Category", true);
iset result;
for (auto &iv : m_cat_validator->mItemValidators)
result.insert(iv.mTag);
return result;
}
const Validator *get_validator() const { return m_validator; }
const ValidateCategory *get_cat_validator() const { return m_cat_validator; }
// --------------------------------------------------------------------
reference front() reference front()
{ {
return {*this, *m_head}; return {*this, *m_head};
...@@ -199,7 +218,7 @@ class category ...@@ -199,7 +218,7 @@ class category
} }
// -------------------------------------------------------------------- // --------------------------------------------------------------------
conditional_iterator_proxy<category> find(condition &&cond) conditional_iterator_proxy<category> find(condition &&cond)
{ {
return find(begin(), std::forward<condition>(cond)); return find(begin(), std::forward<condition>(cond));
...@@ -330,7 +349,23 @@ class category ...@@ -330,7 +349,23 @@ class category
return *h.begin(); return *h.begin();
} }
bool exists(condition &&cond) const; bool exists(condition &&cond) const
{
bool result = false;
cond.prepare(*this);
for (auto r : *this)
{
if (cond(r))
{
result = true;
break;
}
}
return result;
}
// -------------------------------------------------------------------- // --------------------------------------------------------------------
...@@ -626,6 +661,8 @@ class category ...@@ -626,6 +661,8 @@ class category
std::string m_name; std::string m_name;
std::vector<item_column> m_columns; std::vector<item_column> m_columns;
const Validator *m_validator = nullptr;
const ValidateCategory *m_cat_validator = nullptr;
row *m_head = nullptr, *m_tail = nullptr; row *m_head = nullptr, *m_tail = nullptr;
}; };
......
...@@ -37,6 +37,13 @@ namespace cif::v2 ...@@ -37,6 +37,13 @@ namespace cif::v2
{ {
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// let's make life easier
iset get_category_fields(const category &cat);
uint16_t get_column_ix(const category &cat, std::string_view col);
bool is_column_type_uchar(const category &cat, std::string_view col);
// --------------------------------------------------------------------
// some more templates to be able to do querying // some more templates to be able to do querying
namespace detail namespace detail
...@@ -147,7 +154,10 @@ namespace detail ...@@ -147,7 +154,10 @@ namespace detail
{ {
} }
void prepare(const category &c) override; void prepare(const category &c) override
{
m_item_ix = get_column_ix(c, m_item_tag);
}
bool test(row_handle r) const override bool test(row_handle r) const override
{ {
...@@ -173,7 +183,11 @@ namespace detail ...@@ -173,7 +183,11 @@ namespace detail
{ {
} }
void prepare(const category &c) override; void prepare(const category &c) override
{
m_item_ix = get_column_ix(c, m_item_tag);
m_icase = is_column_type_uchar(c, m_item_tag);
}
bool test(row_handle r) const override bool test(row_handle r) const override
{ {
...@@ -201,7 +215,10 @@ namespace detail ...@@ -201,7 +215,10 @@ namespace detail
{ {
} }
void prepare(const category &c) override; void prepare(const category &c) override
{
m_item_ix = get_column_ix(c, m_item_tag);
}
bool test(row_handle r) const override bool test(row_handle r) const override
{ {
......
...@@ -43,10 +43,4 @@ class row_handle; ...@@ -43,10 +43,4 @@ class row_handle;
class item; class item;
class item_handle; class item_handle;
// --------------------------------------------------------------------
// let's make life easier
std::vector<std::string> get_category_fields(const category &cat);
} // namespace cif::v2 } // namespace cif::v2
\ No newline at end of file
...@@ -349,7 +349,8 @@ class conditional_iterator_proxy ...@@ -349,7 +349,8 @@ class conditional_iterator_proxy
size_t size() const { return std::distance(begin(), end()); } size_t size() const { return std::distance(begin(), end()); }
// row front() { return *begin(); } row_handle front() { return *begin(); }
// row_handle back() { return *begin(); }
CategoryType &category() const { return *mCat; } CategoryType &category() const { return *mCat; }
......
...@@ -171,8 +171,8 @@ class Validator ...@@ -171,8 +171,8 @@ class Validator
Validator(const Validator &rhs) = delete; Validator(const Validator &rhs) = delete;
Validator &operator=(const Validator &rhs) = delete; Validator &operator=(const Validator &rhs) = delete;
Validator(Validator &&rhs); Validator(Validator &&rhs) = default;
Validator &operator=(Validator &&rhs); Validator &operator=(Validator &&rhs) = default;
friend class dictionary_parser; friend class dictionary_parser;
......
...@@ -274,9 +274,4 @@ category::iterator category::erase_impl(const_iterator pos) ...@@ -274,9 +274,4 @@ category::iterator category::erase_impl(const_iterator pos)
// return iterator(*this, cur); // return iterator(*this, cur);
} }
std::vector<std::string> get_category_fields(const category &cat)
{
return {};
}
} // namespace cif::v2 } // namespace cif::v2
\ No newline at end of file
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2022 NKI/AVL, Netherlands Cancer Institute
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cif++/v2/category.hpp>
#include <cif++/v2/condition.hpp>
namespace cif::v2
{
iset get_category_fields(const category &cat)
{
return cat.fields();
}
uint16_t get_column_ix(const category &cat, std::string_view col)
{
return cat.get_column_ix(col);
}
bool is_column_type_uchar(const category &cat, std::string_view col)
{
bool result = false;
auto cv = cat.get_cat_validator();
if (cv)
{
auto iv = cv->getValidatorForItem(col);
if (iv != nullptr and iv->mType != nullptr)
{
auto type = iv->mType;
result = type->mPrimitiveType == DDL_PrimitiveType::UChar;
}
}
return result;
}
} // namespace cif::v2
...@@ -31,11 +31,16 @@ ...@@ -31,11 +31,16 @@
#include <cif++/v2/validate.hpp> #include <cif++/v2/validate.hpp>
#include <cif++/v2/dictionary_parser.hpp> #include <cif++/v2/dictionary_parser.hpp>
namespace cif
{
extern int VERBOSE; extern int VERBOSE;
}
namespace cif::v2 namespace cif::v2
{ {
using cif::VERBOSE;
ValidationError::ValidationError(const std::string &msg) ValidationError::ValidationError(const std::string &msg)
: mMsg(msg) : mMsg(msg)
{ {
...@@ -218,13 +223,6 @@ const ValidateItem *ValidateCategory::getValidatorForItem(std::string_view tag) ...@@ -218,13 +223,6 @@ const ValidateItem *ValidateCategory::getValidatorForItem(std::string_view tag)
// -------------------------------------------------------------------- // --------------------------------------------------------------------
// Validator::Validator(std::string_view name, std::istream &is)
// : mName(name)
// {
// DictParser p(*this, is);
// p.loadDictionary();
// }
void Validator::addTypeValidator(ValidateType &&v) void Validator::addTypeValidator(ValidateType &&v)
{ {
auto r = mTypeValidators.insert(std::move(v)); auto r = mTypeValidators.insert(std::move(v));
......
...@@ -315,75 +315,75 @@ _test.name ...@@ -315,75 +315,75 @@ _test.name
// BOOST_CHECK_EQUAL(n, 1); // BOOST_CHECK_EQUAL(n, 1);
} }
// // -------------------------------------------------------------------- // --------------------------------------------------------------------
// BOOST_AUTO_TEST_CASE(ut2) BOOST_AUTO_TEST_CASE(ut2)
// { {
// // using namespace mmcif; // using namespace mmcif;
// auto f = R"(data_TEST auto f = R"(data_TEST
// # #
// loop_ loop_
// _test.id _test.id
// _test.name _test.name
// _test.value _test.value
// 1 aap 1.0 1 aap 1.0
// 2 noot 1.1 2 noot 1.1
// 3 mies 1.2 3 mies 1.2
// )"_cf; )"_cf;
// auto &db = f.firstDatablock(); auto &db = f.front();
// BOOST_CHECK(db.getName() == "TEST"); BOOST_CHECK(db.name() == "TEST");
// auto &test = db["test"]; auto &test = db["test"];
// BOOST_CHECK(test.size() == 3); BOOST_CHECK(test.size() == 3);
// int n = 0; int n = 0;
// for (auto r : test.find(cif::Key("name") == "aap")) for (auto r : test.find(cif::v2::key("name") == "aap"))
// { {
// BOOST_CHECK(++n == 1); BOOST_CHECK(++n == 1);
// BOOST_CHECK(r["id"].as<int>() == 1); BOOST_CHECK(r["id"].as<int>() == 1);
// BOOST_CHECK(r["name"].as<std::string>() == "aap"); BOOST_CHECK(r["name"].as<std::string>() == "aap");
// BOOST_CHECK(r["value"].as<float>() == 1.0); BOOST_CHECK(r["value"].as<float>() == 1.0);
// } }
// auto t = test.find(cif::Key("id") == 1); auto t = test.find(cif::v2::key("id") == 1);
// BOOST_CHECK(not t.empty()); BOOST_CHECK(not t.empty());
// BOOST_CHECK(t.front()["name"].as<std::string>() == "aap"); BOOST_CHECK(t.front()["name"].as<std::string>() == "aap");
// auto t2 = test.find(cif::Key("value") == 1.2); auto t2 = test.find(cif::v2::key("value") == 1.2);
// BOOST_CHECK(not t2.empty()); BOOST_CHECK(not t2.empty());
// BOOST_CHECK(t2.front()["name"].as<std::string>() == "mies"); BOOST_CHECK(t2.front()["name"].as<std::string>() == "mies");
// } }
// BOOST_AUTO_TEST_CASE(ut3) BOOST_AUTO_TEST_CASE(ut3)
// { {
// using namespace cif::literals; using namespace cif::v2::literals;
// auto f = R"(data_TEST auto f = R"(data_TEST
// # #
// loop_ loop_
// _test.id _test.id
// _test.name _test.name
// _test.value _test.value
// 1 aap 1.0 1 aap 1.0
// 2 noot 1.1 2 noot 1.1
// 3 mies 1.2 3 mies 1.2
// 4 boom . 4 boom .
// 5 roos ? 5 roos ?
// )"_cf; )"_cf;
// auto &db = f.firstDatablock(); auto &db = f.front();
// BOOST_CHECK(db.getName() == "TEST"); BOOST_CHECK(db.name() == "TEST");
// auto &test = db["test"]; auto &test = db["test"];
// BOOST_CHECK(test.size() == 5); BOOST_CHECK(test.size() == 5);
// BOOST_CHECK(test.exists("value"_key == cif::null)); BOOST_CHECK(test.exists("value"_key == cif::v2::null));
// BOOST_CHECK(test.find("value"_key == cif::null).size() == 2); BOOST_CHECK(test.find("value"_key == cif::v2::null).size() == 2);
// } }
// // -------------------------------------------------------------------- // // --------------------------------------------------------------------
...@@ -528,8 +528,8 @@ _test.name ...@@ -528,8 +528,8 @@ _test.name
// std::istream is_data(&data_buffer); // std::istream is_data(&data_buffer);
// f.load(is_data); // f.load(is_data);
// auto &cat1 = f.firstDatablock()["cat_1"]; // auto &cat1 = f.front()["cat_1"];
// auto &cat2 = f.firstDatablock()["cat_2"]; // auto &cat2 = f.front()["cat_2"];
// BOOST_CHECK(cat1.size() == 3); // BOOST_CHECK(cat1.size() == 3);
// BOOST_CHECK(cat2.size() == 3); // BOOST_CHECK(cat2.size() == 3);
...@@ -649,7 +649,7 @@ _test.name ...@@ -649,7 +649,7 @@ _test.name
// std::istream is_data(&data_buffer); // std::istream is_data(&data_buffer);
// f.load(is_data); // f.load(is_data);
// auto &cat1 = f.firstDatablock()["cat_1"]; // auto &cat1 = f.front()["cat_1"];
// BOOST_CHECK(cat1.size() == 3); // BOOST_CHECK(cat1.size() == 3);
...@@ -808,8 +808,8 @@ _test.name ...@@ -808,8 +808,8 @@ _test.name
// std::istream is_data(&data_buffer); // std::istream is_data(&data_buffer);
// f.load(is_data); // f.load(is_data);
// auto &cat1 = f.firstDatablock()["cat_1"]; // auto &cat1 = f.front()["cat_1"];
// auto &cat2 = f.firstDatablock()["cat_2"]; // auto &cat2 = f.front()["cat_2"];
// // check a rename in parent and child // // check a rename in parent and child
...@@ -1021,8 +1021,8 @@ _test.name ...@@ -1021,8 +1021,8 @@ _test.name
// std::istream is_data(&data_buffer); // std::istream is_data(&data_buffer);
// f.load(is_data); // f.load(is_data);
// auto &cat1 = f.firstDatablock()["cat_1"]; // auto &cat1 = f.front()["cat_1"];
// auto &cat2 = f.firstDatablock()["cat_2"]; // auto &cat2 = f.front()["cat_2"];
// // check a rename in parent and child // // check a rename in parent and child
...@@ -1235,8 +1235,8 @@ _test.name ...@@ -1235,8 +1235,8 @@ _test.name
// std::istream is_data(&data_buffer); // std::istream is_data(&data_buffer);
// f.load(is_data); // f.load(is_data);
// auto &cat1 = f.firstDatablock()["cat_1"]; // auto &cat1 = f.front()["cat_1"];
// auto &cat2 = f.firstDatablock()["cat_2"]; // auto &cat2 = f.front()["cat_2"];
// // -------------------------------------------------------------------- // // --------------------------------------------------------------------
// // check iterate children // // check iterate children
...@@ -1347,7 +1347,7 @@ _test.name ...@@ -1347,7 +1347,7 @@ _test.name
// 5 ? // 5 ?
// )"_cf; // )"_cf;
// auto &db = f.firstDatablock(); // auto &db = f.front();
// for (auto r : db["test"].find(cif::Key("id") == 1)) // for (auto r : db["test"].find(cif::Key("id") == 1))
// { // {
...@@ -1404,7 +1404,7 @@ _test.name ...@@ -1404,7 +1404,7 @@ _test.name
// 5 ? // 5 ?
// )"_cf; // )"_cf;
// auto &db = f.firstDatablock(); // auto &db = f.front();
// // query tests // // query tests
...@@ -1439,7 +1439,7 @@ _test.name ...@@ -1439,7 +1439,7 @@ _test.name
// 5 ? // 5 ?
// )"_cf; // )"_cf;
// auto &db = f.firstDatablock(); // auto &db = f.front();
// // query tests // // query tests
// for (const auto &[id, name] : db["test"].find<int, std::optional<std::string>>(cif::All(), "id", "name")) // for (const auto &[id, name] : db["test"].find<int, std::optional<std::string>>(cif::All(), "id", "name"))
...@@ -1668,9 +1668,9 @@ _test.name ...@@ -1668,9 +1668,9 @@ _test.name
// std::istream is_data(&data_buffer); // std::istream is_data(&data_buffer);
// f.load(is_data); // f.load(is_data);
// auto &cat1 = f.firstDatablock()["cat_1"]; // auto &cat1 = f.front()["cat_1"];
// auto &cat2 = f.firstDatablock()["cat_2"]; // auto &cat2 = f.front()["cat_2"];
// auto &cat3 = f.firstDatablock()["cat_3"]; // auto &cat3 = f.front()["cat_3"];
// cat3.update_value("name"_key == "aap" and "num"_key == 1, "name", "aapje"); // cat3.update_value("name"_key == "aap" and "num"_key == 1, "name", "aapje");
...@@ -2036,7 +2036,7 @@ _test.name ...@@ -2036,7 +2036,7 @@ _test.name
// _test.text ?? // _test.text ??
// )"_cf; // )"_cf;
// auto &db1 = data1.firstDatablock(); // auto &db1 = data1.front();
// auto &test1 = db1["test"]; // auto &test1 = db1["test"];
// BOOST_CHECK_EQUAL(test1.size(), 1); // BOOST_CHECK_EQUAL(test1.size(), 1);
...@@ -2052,7 +2052,7 @@ _test.name ...@@ -2052,7 +2052,7 @@ _test.name
// auto data2 = cif::File(ss); // auto data2 = cif::File(ss);
// auto &db2 = data2.firstDatablock(); // auto &db2 = data2.front();
// auto &test2 = db2["test"]; // auto &test2 = db2["test"];
// BOOST_CHECK_EQUAL(test2.size(), 1); // BOOST_CHECK_EQUAL(test2.size(), 1);
...@@ -2077,7 +2077,7 @@ _test.name ...@@ -2077,7 +2077,7 @@ _test.name
// boo.data_.whatever // boo.data_.whatever
// )"_cf; // )"_cf;
// auto &db1 = data1.firstDatablock(); // auto &db1 = data1.front();
// auto &test1 = db1["test"]; // auto &test1 = db1["test"];
// struct T { // struct T {
...@@ -2107,7 +2107,7 @@ _test.name ...@@ -2107,7 +2107,7 @@ _test.name
// auto data2 = cif::File(ss); // auto data2 = cif::File(ss);
// auto &db2 = data2.firstDatablock(); // auto &db2 = data2.front();
// auto &test2 = db2["test"]; // auto &test2 = db2["test"];
// BOOST_CHECK_EQUAL(test2.size(), sizeof(kS) / sizeof(T)); // BOOST_CHECK_EQUAL(test2.size(), sizeof(kS) / sizeof(T));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment