<commit_before>package rzehan.shared.engine;

import rzehan.shared.engine.evaluationFunctions.*;
import rzehan.shared.engine.exceptions.ValidatorException;
import rzehan.shared.engine.exceptions.VariableNotDefinedException;
import rzehan.shared.engine.validationFunctions.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.V;

/**
 * Created by martin on 20.10.16.
 */
public class Engine {

    private final ProvidedVarsManager providedVarsManager;

    private final Map<String, ValueDefinition> valueDefinitionsByVarName = new HashMap<>();
    private final Map<String, Object> valuesByVarName = new HashMap<>();
    private final Map<String, Pattern> patternsByVarName = new HashMap<>();
    private final RulesManager rulesManager = new RulesManager();

    public Engine(ProvidedVarsManager providedVarsManager) {
        this.providedVarsManager = providedVarsManager;
    }


    //build methods for creating stuff

    public ValueDefinition buildValueDefinition(ValueType type, EvaluationFunction evaluationFunction) {
        ValueDefinition definition = new ValueDefinition(type, evaluationFunction);
        return definition;
    }

    public Pattern.Expression buildExpression(boolean caseSensitive, String originalRegexp) {
        return new Pattern.Expression(this, caseSensitive, originalRegexp);
    }

    public Pattern buildPattern(Pattern.Expression... expressions) {
        return new Pattern(this, expressions);
    }

    public Pattern buildPattern(List<Pattern.Expression> expressions) {
        return new Pattern(this, expressions);
    }

    public EvaluationFunction buildEvaluationFunction(String name) {
        if (name.equals("PROVIDED_FILE")) {
            return new EfProvidedFile(this);
        } else if (name.equals("PROVIDED_STRING")) {
            return new EfProvidedString(this);
        } else if (name.equals("PROVIDED_INTEGER")) {
            return new EfProvidedInteger(this);
        } else if (name.equals("RETURN_FIRST_FILE_FROM_LIST")) {
            return new EfReturnFirstFileFromFileList(this);
        } else if (name.equals("FIND_FILES_IN_DIR_BY_PATTERN")) {
            return new EfFindFilesInDirByPattern(this);
        } else {
            throw new RuntimeException(String.format("vyhodnocovací funkce %s neexistuje", name));
        }
    }

    public ValidationFunction buildValidationFunction(String name) {
        //TODO: vlasnte java 8 umi switch pres Stringy
        if (name.equals("CHECK_FILE_LIST_EXACT_SIZE")) {
            return new VfCheckFileListExactSize(this);
        } else if (name.equals("CHECK_FILENAME_MATCHES_PATTERN")) {
            return new VfCheckFilenameMatchesPattern(this);
        } else if (name.equals("CHECK_ALL_FILENAMES_MATCH_PATTERN")) {
            return new VfCheckAllFilenamesMatchePattern(this);
        } else if (name.equals("CHECK_FILE_IS_DIR")) {
            return new VfCheckFileIsDir(this);
        } else if (name.equals("CHECK_FILE_IS_NOT_DIR")) {
            return new VfCheckFileIsNotDir(this);
        } else if (name.equals("CHECK_NO_FILE_IS_DIR")) {
            return new VfCheckNoFileIsDir(this);
        } else if (name.equals("CHECK_NO_OTHER_FILES_IN_DIR")) {
            return new VfCheckNoOtherFilesInDir(this);
        } else if (name.equals("CHECK_ALL_FILE_LISTS_SAME_SIZE")) {
            return new VfCheckAllFileListsSameSize(this);
        } else if (name.equals("CHECK_FILENAMES_LENGTHS_SAME")) {
            return new VfCheckFilenamesLengthsSame(this);
        } else if (name.equals("CHECK_CHECKSUM_GENERATED_BY_GRAMMAR")) {
            return new VfCheckChecksumGeneratedByGrammar(this);
        } else if (name.equals("CHECK_CHECKSUM_ALL_PATHS_MATCH_FILES")) {
            return new VfCheckChecksumAllPathsMatchFiles(this);
        } else if (name.equals("CHECK_CHECKSUM_ALL_CHECKSUMS_MATCH")) {
            return new VfCheckChecksumAllChecksumsMatch(this);
        } else if (name.equals("CHECK_XML_WELL_BUILT")) {
            return new VfCheckXmlWellBuilt(this);
        } else if (name.equals("CHECK_XML_VALID_BY_XSD")) {
            return new VfCheckXmlValidByXsd(this);
        } else {
            throw new RuntimeException(String.format("validační funkce %s neexistuje", name));
        }
    }

    public RulesSection buildRuleSection(String sectionName) {
        return new RulesSection(sectionName);
    }

    public Rule buildRule(String ruleName, Rule.Level error, ValidationFunction function) {
        return new Rule(ruleName, error, function);
    }

    //register methods for defining variables (values, patterns, ruleSections, rules)

    public void registerValue(String valueVariableName, Object value) {
        //TODO: check if not defined already
        valuesByVarName.put(valueVariableName, value);
    }

    public void registerValueDefinition(String valueVariableName, ValueDefinition definition) {
        //TODO: check if not defined already
        valueDefinitionsByVarName.put(valueVariableName, definition);
    }

    public void registerPattern(String patternVariableName, Pattern pattern) {
        //TODO: check if not defined already
        patternsByVarName.put(patternVariableName, pattern);
    }


    public void registerRuleSection(RulesSection section) {<commit_msg>switch(String) namisto if, else if, else<commit_after>package rzehan.shared.engine;

import rzehan.shared.engine.evaluationFunctions.*;
import rzehan.shared.engine.exceptions.ValidatorException;
import rzehan.shared.engine.exceptions.VariableNotDefinedException;
import rzehan.shared.engine.validationFunctions.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static javafx.scene.input.KeyCode.V;

/**
 * Created by martin on 20.10.16.
 */
public class Engine {

    private final ProvidedVarsManager providedVarsManager;

    private final Map<String, ValueDefinition> valueDefinitionsByVarName = new HashMap<>();
    private final Map<String, Object> valuesByVarName = new HashMap<>();
    private final Map<String, Pattern> patternsByVarName = new HashMap<>();
    private final RulesManager rulesManager = new RulesManager();

    public Engine(ProvidedVarsManager providedVarsManager) {
        this.providedVarsManager = providedVarsManager;
    }


    //build methods for creating stuff

    public ValueDefinition buildValueDefinition(ValueType type, EvaluationFunction evaluationFunction) {
        ValueDefinition definition = new ValueDefinition(type, evaluationFunction);
        return definition;
    }

    public Pattern.Expression buildExpression(boolean caseSensitive, String originalRegexp) {
        return new Pattern.Expression(this, caseSensitive, originalRegexp);
    }

    public Pattern buildPattern(Pattern.Expression... expressions) {
        return new Pattern(this, expressions);
    }

    public Pattern buildPattern(List<Pattern.Expression> expressions) {
        return new Pattern(this, expressions);
    }

    public EvaluationFunction buildEvaluationFunction(String name) {
        switch (name) {
            case "PROVIDED_FILE":
                return new EfProvidedFile(this);
            case "PROVIDED_STRING":
                return new EfProvidedString(this);
            case "PROVIDED_INTEGER":
                return new EfProvidedInteger(this);
            case "RETURN_FIRST_FILE_FROM_LIST":
                return new EfReturnFirstFileFromFileList(this);
            case "FIND_FILES_IN_DIR_BY_PATTERN":
                return new EfFindFilesInDirByPattern(this);
            default:
                throw new RuntimeException(String.format("vyhodnocovací funkce %s neexistuje", name));
        }
    }

    public ValidationFunction buildValidationFunction(String name) {
        switch (name) {
            case "CHECK_FILE_LIST_EXACT_SIZE":
                return new VfCheckFileListExactSize(this);
            case "CHECK_FILENAME_MATCHES_PATTERN":
                return new VfCheckFilenameMatchesPattern(this);
            case "CHECK_ALL_FILENAMES_MATCH_PATTERN":
                return new VfCheckAllFilenamesMatchePattern(this);
            case "CHECK_FILE_IS_DIR":
                return new VfCheckFileIsDir(this);
            case "CHECK_FILE_IS_NOT_DIR":
                return new VfCheckFileIsNotDir(this);
            case "CHECK_NO_FILE_IS_DIR":
                return new VfCheckNoFileIsDir(this);
            case "CHECK_NO_OTHER_FILES_IN_DIR":
                return new VfCheckNoOtherFilesInDir(this);
            case "CHECK_ALL_FILE_LISTS_SAME_SIZE":
                return new VfCheckAllFileListsSameSize(this);
            case "CHECK_FILENAMES_LENGTHS_SAME":
                return new VfCheckFilenamesLengthsSame(this);
            case "CHECK_CHECKSUM_GENERATED_BY_GRAMMAR":
                return new VfCheckChecksumGeneratedByGrammar(this);
            case "CHECK_CHECKSUM_ALL_PATHS_MATCH_FILES":
                return new VfCheckChecksumAllPathsMatchFiles(this);
            case "CHECK_CHECKSUM_ALL_CHECKSUMS_MATCH":
                return new VfCheckChecksumAllChecksumsMatch(this);
            case "CHECK_XML_WELL_BUILT":
                return new VfCheckXmlWellBuilt(this);
            case "CHECK_XML_VALID_BY_XSD":
                return new VfCheckXmlValidByXsd(this);
            default:
                throw new RuntimeException(String.format("validační funkce %s neexistuje", name));
        }
    }

    public RulesSection buildRuleSection(String sectionName) {
        return new RulesSection(sectionName);
    }

    public Rule buildRule(String ruleName, Rule.Level error, ValidationFunction function) {
        return new Rule(ruleName, error, function);
    }

    //register methods for defining variables (values, patterns, ruleSections, rules)

    public void registerValue(String valueVariableName, Object value) {
        //TODO: check if not defined already
        valuesByVarName.put(valueVariableName, value);
    }

    public void registerValueDefinition(String valueVariableName, ValueDefinition definition) {
        //TODO: check if not defined already
        valueDefinitionsByVarName.put(valueVariableName, definition);
    }

    public void registerPattern(String patternVariableName, Pattern pattern) {
        //TODO: check if not defined already
        patternsByVarName.put(patternVariableName, pattern);
    }


    public void registerRuleSection(RulesSection section) {
{'fraction_non_alphanumeric': 0.07987522450137063, 'fraction_numerical': 0.0012288496077133945, 'mean_word_length': 3.235388310648519, 'pattern_counts': {'":': 19, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7587742', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2820, 'n_words': 646}