<commit_before>package com.incquerylabs.smarthome.model.rulegenerator.rules

import org.eclipse.core.resources.ResourcesPlugin
import com.incquerylabs.smarthome.model.rulegenerator.SmartHomeRulesMatcher
import org.apache.log4j.Logger
import org.eclipse.viatra.query.runtime.api.IPatternMatch
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.IModelManipulations
import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.SimpleModelManipulations
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory
import org.eclipse.core.resources.IResource
import java.io.ByteArrayInputStream
import com.incquerylabs.smarthome.model.rulegenerator.GeneralRulesMatcher
import com.incquerylabs.smarthome.model.rulegenerator.ChangeStateSwitchRulesMatcher
import com.incquerylabs.smarthome.model.rulegenerator.ChangeStateDimmerRulesMatcher

class RuleProvider {

    extension IModelManipulations manipulation
    extension BatchTransformationRuleFactory = new BatchTransformationRuleFactory
    extension Logger logger = Logger.getLogger("com.incquerylabs.smarthome.model.rulegenerator.rules")

    BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> smarthomeRule
    BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> generalRulesRule
	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> changeStateSwitchRulesRule
	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> changeStateDimmerRulesRule
	
    private var ruleId = 0;
    private static final String destinationProject = "com.incquerylabs.smarthome.demorules.generated";
    private static final String rulePackageName = "genrules";

    ViatraQueryEngine engine

    new(ViatraQueryEngine engine) {
        this.engine = engine
        manipulation = new SimpleModelManipulations(engine)
    }

    def writeToFile(String fileName, String fileContent) {
        val workspace = ResourcesPlugin.getWorkspace()
        
        val root = workspace.getRoot()
        val project = root.getProject(destinationProject)
        val folder = project.getFolder("src/main/resources/gen-rules")
        val file = folder.getFile(fileName)

        if (!project.exists()) {
            project.create(null)
        }
        if (!project.isOpen()) {
            project.open(null)
        }
        if (!folder.exists()) {
            folder.create(IResource.NONE, true, null)<commit_msg>Rule generator now empties the folder before generation<commit_after>package com.incquerylabs.smarthome.model.rulegenerator.rules

import org.eclipse.core.resources.ResourcesPlugin
import com.incquerylabs.smarthome.model.rulegenerator.SmartHomeRulesMatcher
import org.apache.log4j.Logger
import org.eclipse.viatra.query.runtime.api.IPatternMatch
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.IModelManipulations
import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.SimpleModelManipulations
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory
import org.eclipse.core.resources.IResource
import java.io.ByteArrayInputStream
import com.incquerylabs.smarthome.model.rulegenerator.GeneralRulesMatcher
import com.incquerylabs.smarthome.model.rulegenerator.ChangeStateSwitchRulesMatcher
import com.incquerylabs.smarthome.model.rulegenerator.ChangeStateDimmerRulesMatcher

class RuleProvider {

    extension IModelManipulations manipulation
    extension BatchTransformationRuleFactory = new BatchTransformationRuleFactory
    extension Logger logger = Logger.getLogger("com.incquerylabs.smarthome.model.rulegenerator.rules")

    BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> smarthomeRule
    BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> generalRulesRule
	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> changeStateSwitchRulesRule
	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> changeStateDimmerRulesRule
	
    private var ruleId = 0;
    private static final String destinationProject = "com.incquerylabs.smarthome.demorules.generated";
    private static final String rulePackageName = "genrules";

    ViatraQueryEngine engine

    new(ViatraQueryEngine engine) {
        this.engine = engine
        manipulation = new SimpleModelManipulations(engine)
        
        val workspace = ResourcesPlugin.getWorkspace()
        
        val root = workspace.getRoot()
        val project = root.getProject(destinationProject)
        val folder = project.getFolder("src/main/resources/gen-rules")
        if (!project.exists()) {
            project.create(null)
        }
        if (!project.isOpen()) {
            project.open(null)
        }
        if (folder.exists()) {
        	folder.delete(true, null);
        }
        folder.create(IResource.NONE, true, null)
        workspace.save(true,null)
    }

    def writeToFile(String fileName, String fileContent) {
        val workspace = ResourcesPlugin.getWorkspace()
        
        val root = workspace.getRoot()
        val project = root.getProject(destinationProject)
        val folder = project.getFolder("src/main/resources/gen-rules")
        val file = folder.getFile(fileName)

        if (!project.exists()) {
            project.create(null)
        }
        if (!project.isOpen()) {
            project.open(null)
        }
        if (!folder.exists()) {
            folder.create(IResource.NONE, true, null)
{'fraction_non_alphanumeric': 0.08421400264200793, 'fraction_numerical': 0.0006605019815059445, 'mean_word_length': 4.341269841269841, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '3322844', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1646, 'n_words': 282}