<commit_before>/*
 *    Copyright 2011 The 99 Software Foundation
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.nnsoft.t2t.configuration;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.nnsoft.t2t.core.Rule;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.Var;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author <NAME> ( <EMAIL> )
 */
public class ConfigurationManager {

    private static ConfigurationManager instance;

    public static ConfigurationManager getInstance(File file) {
        if( instance == null) {
            instance = new ConfigurationManager(file);
        }
        return instance;
    }

    private ConfigurationManager(File file) {
        if(file == null)
            throw new IllegalArgumentException("Configuration file path cannot be null");
        if(!file.exists()) {
            throw new IllegalArgumentException("Configuration file: '" +
                    file.getPath() + "' does not exists");
        }
        XMLConfiguration xmlConfiguration;
        try {
            xmlConfiguration = new XMLConfiguration(file.getAbsolutePath());
        } catch (ConfigurationException e) {
            throw new RuntimeException("Error while loading XMLConfiguration", e);
        }
        configuration = new MigratorConfiguration();
        configuration.setSourceGraph(getSourceGraph(xmlConfiguration));
        configuration.setSourceConnection(getSourceConnection(xmlConfiguration));
        configuration.setDestinationGraph(getDestinationGraph(xmlConfiguration));
        configuration.setDestinationConnection(getDestinationConnection(xmlConfiguration));
        configuration.setCommitRate(getCommitRate(xmlConfiguration));
        configuration.setActiveFiltering(isActiveFiltering(xmlConfiguration));
        configuration.setRules(getRules(xmlConfiguration));
        configuration.setNamespaceMappings(getNamespaceMappings(xmlConfiguration));
        configuration.setSlicingClass(getSlicingClass(xmlConfiguration));
    }

    private String getSlicingClass(XMLConfiguration xmlConfiguration) {
        HierarchicalConfiguration source = xmlConfiguration.configurationAt("source");
        String slicingClass = source.getString("slicing-class");
        return slicingClass;
    }

    private Set<NamespaceMapping> getNamespaceMappings(XMLConfiguration xmlConfiguration) {
        Set<NamespaceMapping> result = new HashSet<NamespaceMapping>();
        HierarchicalConfiguration rules = xmlConfiguration.configurationAt("namespace-mappings");
        List<HierarchicalConfiguration> mappings = rules.configurationsAt("mapping");
        for(HierarchicalConfiguration mapping : mappings) {
            String from = mapping.getString("from");
            String to = mapping.getString("to");
            result.add(new NamespaceMapping(new URIImpl(from), new URIImpl(to)));
        }
        return result;
    }

    private boolean isActiveFiltering(XMLConfiguration xmlConfiguration) {
        return xmlConfiguration.getBoolean("active-filtering");
    }

    private Set<Rule> getRules(XMLConfiguration xmlConfiguration) {
        Set<Rule> result = new HashSet<Rule>();
        HierarchicalConfiguration rules = xmlConfiguration.configurationAt("rules");
        List<HierarchicalConfiguration> rulesList = rules.configurationsAt("rule");
        for(HierarchicalConfiguration rule : rulesList) {
            result.add(getRule(rule));
        }
        return result;
    }

    private Rule getRule(HierarchicalConfiguration rule) {
        HierarchicalConfiguration match = rule.configurationAt("match");
        String pattern = match.getString("pattern");
        StatementPattern matchStatementPattern = parsePattern(pattern);

        Set<StatementPattern> applyStatementPatterns = new HashSet<StatementPattern>();
        HierarchicalConfiguration apply = rule.configurationAt("apply");
        List<HierarchicalConfiguration> patterns = apply.configurationsAt("patterns");
        for(HierarchicalConfiguration applyPattern : patterns) {
            applyStatementPatterns.add(parsePattern(applyPattern.getString("pattern")));
        }
        return new Rule(matchStatementPattern, applyStatementPatterns);
    }

    private StatementPattern parsePattern(String string) {
        String rawVars[] = string.trim().split(" ");
        Var clensedVars[] = new Var[rawVars.length];
        int index = 0;
        for(String var : rawVars) {
            if(var.startsWith("?")) {<commit_msg>suppressed unchecked warnings<commit_after>/*
 *    Copyright 2011 The 99 Software Foundation
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.nnsoft.t2t.configuration;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.nnsoft.t2t.core.Rule;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.algebra.StatementPattern;
import org.openrdf.query.algebra.Var;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author <NAME> ( <EMAIL> )
 */
public class ConfigurationManager {

    private static ConfigurationManager instance;

    public static ConfigurationManager getInstance(File file) {
        if( instance == null) {
            instance = new ConfigurationManager(file);
        }
        return instance;
    }

    private ConfigurationManager(File file) {
        if(file == null)
            throw new IllegalArgumentException("Configuration file path cannot be null");
        if(!file.exists()) {
            throw new IllegalArgumentException("Configuration file: '" +
                    file.getPath() + "' does not exists");
        }
        XMLConfiguration xmlConfiguration;
        try {
            xmlConfiguration = new XMLConfiguration(file.getAbsolutePath());
        } catch (ConfigurationException e) {
            throw new RuntimeException("Error while loading XMLConfiguration", e);
        }
        configuration = new MigratorConfiguration();
        configuration.setSourceGraph(getSourceGraph(xmlConfiguration));
        configuration.setSourceConnection(getSourceConnection(xmlConfiguration));
        configuration.setDestinationGraph(getDestinationGraph(xmlConfiguration));
        configuration.setDestinationConnection(getDestinationConnection(xmlConfiguration));
        configuration.setCommitRate(getCommitRate(xmlConfiguration));
        configuration.setActiveFiltering(isActiveFiltering(xmlConfiguration));
        configuration.setRules(getRules(xmlConfiguration));
        configuration.setNamespaceMappings(getNamespaceMappings(xmlConfiguration));
        configuration.setSlicingClass(getSlicingClass(xmlConfiguration));
    }

    private String getSlicingClass(XMLConfiguration xmlConfiguration) {
        HierarchicalConfiguration source = xmlConfiguration.configurationAt("source");
        String slicingClass = source.getString("slicing-class");
        return slicingClass;
    }

    private Set<NamespaceMapping> getNamespaceMappings(XMLConfiguration xmlConfiguration) {
        Set<NamespaceMapping> result = new HashSet<NamespaceMapping>();
        HierarchicalConfiguration rules = xmlConfiguration.configurationAt("namespace-mappings");
        @SuppressWarnings("unchecked") // type known
        List<HierarchicalConfiguration> mappings = rules.configurationsAt("mapping");
        for(HierarchicalConfiguration mapping : mappings) {
            String from = mapping.getString("from");
            String to = mapping.getString("to");
            result.add(new NamespaceMapping(new URIImpl(from), new URIImpl(to)));
        }
        return result;
    }

    private boolean isActiveFiltering(XMLConfiguration xmlConfiguration) {
        return xmlConfiguration.getBoolean("active-filtering");
    }

    private Set<Rule> getRules(XMLConfiguration xmlConfiguration) {
        Set<Rule> result = new HashSet<Rule>();
        HierarchicalConfiguration rules = xmlConfiguration.configurationAt("rules");
        @SuppressWarnings("unchecked") // type known
        List<HierarchicalConfiguration> rulesList = rules.configurationsAt("rule");
        for(HierarchicalConfiguration rule : rulesList) {
            result.add(getRule(rule));
        }
        return result;
    }

    private Rule getRule(HierarchicalConfiguration rule) {
        HierarchicalConfiguration match = rule.configurationAt("match");
        String pattern = match.getString("pattern");
        StatementPattern matchStatementPattern = parsePattern(pattern);

        Set<StatementPattern> applyStatementPatterns = new HashSet<StatementPattern>();
        HierarchicalConfiguration apply = rule.configurationAt("apply");
        @SuppressWarnings("unchecked")
        List<HierarchicalConfiguration> patterns = apply.configurationsAt("patterns");
        for(HierarchicalConfiguration applyPattern : patterns) {
            applyStatementPatterns.add(parsePattern(applyPattern.getString("pattern")));
        }
        return new Rule(matchStatementPattern, applyStatementPatterns);
    }

    private StatementPattern parsePattern(String string) {
        String rawVars[] = string.trim().split(" ");
        Var clensedVars[] = new Var[rawVars.length];
        int index = 0;
        for(String var : rawVars) {
            if(var.startsWith("?")) {
{'fraction_non_alphanumeric': 0.09070735090152567, 'fraction_numerical': 0.002404068423485899, 'mean_word_length': 3.6301369863013697, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 38}, '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': '6203408', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2583, 'n_words': 613}