implement DataMode – scans all values and recognizes boolean and integer attributes v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 23 May 2021 21:28:26 +0200
branchv_0
changeset 6 779897b055c6
parent 5 9ddc9462e25b
child 7 b8f130c7998e
implement DataMode – scans all values and recognizes boolean and integer attributes
src/DataMode.h
src/Mode.h
--- a/src/DataMode.h	Sun May 23 21:22:02 2021 +0200
+++ b/src/DataMode.h	Sun May 23 21:28:26 2021 +0200
@@ -29,15 +29,52 @@
 
 class DataMode : public Mode {
 private:
+	relpipe::common::type::StringX name;
+	std::vector<relpipe::reader::handlers::AttributeMetadata> attributes;
+	std::vector<relpipe::common::type::StringX> values;
+
+	bool matches(int attributeIndex, const std::wregex& pattern) {
+		for (int record = 0, attributeCount = attributes.size(), limit = values.size() / attributeCount; record < limit; record++) if (!std::regex_match(values[record * attributeIndex], pattern)) return false;
+		return true;
+	}
+
 public:
 
 	DataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) {
 	}
 
 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
+		this->name = name;
+		this->attributes = attributes;
 	}
 
 	void attribute(const relpipe::common::type::StringX& value) override {
+		values.push_back(value);
+	}
+
+	virtual ~DataMode() {
+		std::vector<bool> booleans(attributes.size(), true);
+		std::vector<bool> integers(attributes.size(), true);
+
+		for (int i = 0, limit = attributes.size(); i < limit; i++) {
+			booleans[i] = matches(i, std::wregex(L"true|false"));
+			integers[i] = matches(i, std::wregex(L"[0-9]+"));
+		}
+
+		vector<writer::AttributeMetadata> writerMetadata;
+		for (int i = 0, limit = attributes.size(); i < limit; i++) {
+			relpipe::reader::handlers::AttributeMetadata& am = attributes[i];
+
+			relpipe::writer::TypeId type;
+			if (booleans[i]) type = relpipe::writer::TypeId::BOOLEAN;
+			else if (integers[i]) type = relpipe::writer::TypeId::INTEGER;
+			else type = relpipe::writer::TypeId::STRING;
+
+			writerMetadata.push_back({am.getAttributeName(), type});
+		}
+
+		relationalWriter->startRelation(name, writerMetadata, true);
+		for (relpipe::common::type::StringX& value : values) relationalWriter->writeAttribute(value);
 	}
 };
 
--- a/src/Mode.h	Sun May 23 21:22:02 2021 +0200
+++ b/src/Mode.h	Sun May 23 21:28:26 2021 +0200
@@ -23,6 +23,7 @@
 namespace tr {
 namespace infertypes {
 
+// TODO: use RelationalWriter interface instead?
 class Mode {
 protected:
 	shared_ptr<writer::RelationalWriter> relationalWriter;