author | František Kučera <franta-hg@frantovo.cz> |
Sun, 23 May 2021 22:40:59 +0200 | |
branch | v_0 |
changeset 9 | 215f62cc655f |
parent 8 | fc8b94bccfc5 |
permissions | -rw-r--r-- |
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info) |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <regex> |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <locale> |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
21 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
#include <relpipe/common/type/typedefs.h> |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
#include "Mode.h" |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
namespace relpipe { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
namespace tr { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
namespace infertypes { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
class DataMode : public Mode { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
private: |
7
b8f130c7998e
DataMode: use pattern constants
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
32 |
static const std::wregex BOOLEAN_PATTERN; |
b8f130c7998e
DataMode: use pattern constants
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
33 |
static const std::wregex INTEGER_PATTERN; |
6
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
34 |
relpipe::common::type::StringX name; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
35 |
std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
36 |
std::vector<relpipe::common::type::StringX> values; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
37 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
38 |
bool matches(int attributeIndex, const std::wregex& pattern) { |
8
fc8b94bccfc5
DataMode: support also negative integers + back to the correct value index
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
39 |
for (int record = 0, attributeCount = attributes.size(), limit = values.size() / attributeCount; record < limit; record++) if (!std::regex_match(values[record * attributeCount + attributeIndex], pattern)) return false; |
6
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
40 |
return true; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
41 |
} |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
42 |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
public: |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
DataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
} |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
6
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
49 |
this->name = name; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
50 |
this->attributes = attributes; |
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
} |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
void attribute(const relpipe::common::type::StringX& value) override { |
6
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
54 |
values.push_back(value); |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
55 |
} |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
56 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
57 |
virtual ~DataMode() { |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
58 |
std::vector<bool> booleans(attributes.size(), true); |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
59 |
std::vector<bool> integers(attributes.size(), true); |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
60 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
61 |
for (int i = 0, limit = attributes.size(); i < limit; i++) { |
7
b8f130c7998e
DataMode: use pattern constants
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
62 |
booleans[i] = matches(i, BOOLEAN_PATTERN); |
b8f130c7998e
DataMode: use pattern constants
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
63 |
integers[i] = matches(i, INTEGER_PATTERN); |
6
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
64 |
} |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
65 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
66 |
vector<writer::AttributeMetadata> writerMetadata; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
67 |
for (int i = 0, limit = attributes.size(); i < limit; i++) { |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
68 |
relpipe::reader::handlers::AttributeMetadata& am = attributes[i]; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
69 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
70 |
relpipe::writer::TypeId type; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
71 |
if (booleans[i]) type = relpipe::writer::TypeId::BOOLEAN; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
72 |
else if (integers[i]) type = relpipe::writer::TypeId::INTEGER; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
73 |
else type = relpipe::writer::TypeId::STRING; |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
74 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
75 |
writerMetadata.push_back({am.getAttributeName(), type}); |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
76 |
} |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
77 |
|
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
78 |
relationalWriter->startRelation(name, writerMetadata, true); |
779897b055c6
implement DataMode – scans all values and recognizes boolean and integer attributes
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
79 |
for (relpipe::common::type::StringX& value : values) relationalWriter->writeAttribute(value); |
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
80 |
} |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
81 |
}; |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
82 |
|
7
b8f130c7998e
DataMode: use pattern constants
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
83 |
const std::wregex DataMode::BOOLEAN_PATTERN = std::wregex(L"true|false"); |
8
fc8b94bccfc5
DataMode: support also negative integers + back to the correct value index
František Kučera <franta-hg@frantovo.cz>
parents:
7
diff
changeset
|
84 |
const std::wregex DataMode::INTEGER_PATTERN = std::wregex(L"-?[0-9]+"); |
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
85 |
|
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
86 |
} |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
87 |
} |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
88 |
} |