src/GuileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 27 Jan 2019 17:57:03 +0100
branchv_0
changeset 5 17bb45570099
parent 4 9b4fe4bc5f0f
child 6 4062b8436838
permissions -rw-r--r--
ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications

/**
 * Relational pipes
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include <memory>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <locale>
#include <codecvt>
#include <regex>
#include <assert.h>

#include <libguile.h>

#include <relpipe/reader/typedefs.h>
#include <relpipe/reader/TypeId.h>
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h>
#include <relpipe/reader/handlers/AttributeMetadata.h>

#include <relpipe/writer/Factory.h>

#include <relpipe/cli/RelpipeCLIException.h>

namespace relpipe {
namespace tr {
namespace guile {

using namespace std;
using namespace relpipe;
using namespace relpipe::reader;
using namespace relpipe::reader::handlers;

class GuileHandler : public RelationalReaderValueHadler {
private:
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings or use always UTF-8 between C++ and Guile

	shared_ptr<writer::RelationalWriter> relationalWriter;

	wregex relationNameRegEx;

	vector<AttributeMetadata> currentReaderMetadata;
	vector<writer::AttributeMetadata> currentWriterMetadata;
	vector<string_t> currentRecord;
	integer_t currentAttributeIndex = 0;
	boolean_t includeCurrentRecord = false;
	boolean_t filterCurrentRelation = false;
	string_t guileCodeWhereCondition;

	/**
	 * @param attributeName name from relational pipe
	 * @return variable name in Guile
	 */
	string_t a2v(const string_t& attributeName) {
		// TODO: escape spaces and special characters
		return L"$" + attributeName;
	}

	SCM toGuileSymbol(const string_t& name) {
		return scm_string_to_symbol(scm_from_locale_string(convertor.to_bytes(name).c_str()));
	}

	SCM evalGuileCode(const string_t& value) {
		return scm_eval_string(toGuileValue(&value, typeid (string_t), TypeId::STRING));
	}

	SCM toGuileValue(const void* value, const std::type_info& typeInfo, TypeId type) {
		switch (type) {
			case TypeId::BOOLEAN:
			{
				assert(typeInfo == typeid (boolean_t));
				auto* typedValue = static_cast<const boolean_t*> (value);
				return *typedValue ? SCM_BOOL_T : SCM_BOOL_F;
			}
			case TypeId::INTEGER:
			{
				assert(typeInfo == typeid (integer_t));
				auto* typedValue = static_cast<const integer_t*> (value);
				return scm_from_uint64(*typedValue);
			}
			case TypeId::STRING:
			{
				assert(typeInfo == typeid (string_t));
				auto* typedValue = static_cast<const string_t*> (value);
				return scm_from_locale_string(convertor.to_bytes(*typedValue).c_str());
			}
			default:
				throw cli::RelpipeCLIException(L"Unsupported type in toGuileValue()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
		}
	}

	void defineGuileVariable(const string_t& name, const void* value, const std::type_info& typeInfo, TypeId type) {
		scm_define(toGuileSymbol(name), toGuileValue(value, typeInfo, type));
	}

	void undefineGuileVariable(const string_t& name) {
		scm_define(toGuileSymbol(name), scm_make_undefined_variable()); // undefined != (define n)
		// TODO: or use: scm_variable_unset_x() ?
	}

	void writeGuileValueToAttribute(const writer::AttributeMetadata& attribute) {
		string_t variableName = a2v(attribute.attributeName);
		SCM guileValue = scm_eval_string(toGuileValue(&variableName, typeid (variableName), TypeId::STRING));

		switch (attribute.typeId) {
			case writer::TypeId::BOOLEAN:
			{
				boolean_t value = scm_to_bool(guileValue);
				return relationalWriter->writeAttribute(&value, typeid (value));
			}
			case writer::TypeId::INTEGER:
			{
				integer_t value = scm_to_uint64(guileValue);
				return relationalWriter->writeAttribute(&value, typeid (value));
			}
			case writer::TypeId::STRING:
			{
				char* ch = scm_to_locale_string(guileValue);
				string_t value = convertor.from_bytes(ch);
				free(ch);
				return relationalWriter->writeAttribute(&value, typeid (value));
			}
			default:
				throw cli::RelpipeCLIException(L"Unsupported type in writeGuileValueToAttribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
		}
	}

public:

	GuileHandler(ostream& output, const vector<string_t>& arguments) {
		relationalWriter.reset(writer::Factory::create(output));

		// TODO: options and parser
		if (arguments.size() == 2) {
			relationNameRegEx = wregex(arguments[0]);
			guileCodeWhereCondition = arguments[1];
		} else {
			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-guile <relationNameRegExp> <whereConditionGuileCode>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
		}
	}

	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
		for (auto attribute : currentReaderMetadata) undefineGuileVariable(attribute.getAttributeName());
		currentReaderMetadata = attributes;
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
		// TODO: allow structural changes during transformation
		// TODO: clear Guile variables for attributes from previous relation
		currentWriterMetadata.clear();
		for (AttributeMetadata readerMetadata : attributes) {
			currentWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
		}

		currentRecord.resize(attributes.size());
		filterCurrentRelation = regex_match(name, relationNameRegEx);

		relationalWriter->startRelation(name, currentWriterMetadata, true);
	}

	void attribute(const void* value, const std::type_info& type) override {
		if (filterCurrentRelation) {
			defineGuileVariable(a2v(currentReaderMetadata[currentAttributeIndex].getAttributeName()), value, type, currentReaderMetadata[currentAttributeIndex].getTypeId());

			currentAttributeIndex++;

			if (currentAttributeIndex > 0 && currentAttributeIndex % currentReaderMetadata.size() == 0) {
				includeCurrentRecord = scm_to_bool(evalGuileCode(guileCodeWhereCondition));
				if (includeCurrentRecord) for (auto attribute : currentWriterMetadata) writeGuileValueToAttribute(attribute);
				includeCurrentRecord = false;
			}

			currentAttributeIndex = currentAttributeIndex % currentReaderMetadata.size();
		} else {
			relationalWriter->writeAttribute(value, type);
		}
	}

	void endOfPipe() {

	}

};

}
}
}