Simplifies transformation initialization using the VIATRA MT API

Change-Id: I8821f00bbbd3af4014b34d41bd835b97cf951e01
Signed-off-by: Zoltan Ujhelyi <zoltan.ujhelyi@incquerylabs.com>
diff --git a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/CPS2DeploymentBatchViatra.xtend b/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/CPS2DeploymentBatchViatra.xtend
index eef5afb..cf16b9c 100644
--- a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/CPS2DeploymentBatchViatra.xtend
+++ b/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/CPS2DeploymentBatchViatra.xtend
@@ -15,13 +15,26 @@
 import org.apache.log4j.Logger
 import org.eclipse.viatra.examples.cps.traceability.CPSToDeployment
 import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.CpsXformM2M
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.rules.RuleProvider
 import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
 import org.eclipse.viatra.transformation.runtime.emf.transformation.batch.BatchTransformation
 import org.eclipse.viatra.transformation.runtime.emf.transformation.batch.BatchTransformationStatements
 
-import static com.google.common.base.Preconditions.*
+import static org.eclipse.viatra.query.runtime.matchers.util.Preconditions.*
 import org.eclipse.viatra.transformation.debug.configuration.TransformationDebuggerConfiguration
+import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.Identifiable
+import org.eclipse.viatra.examples.cps.traceability.CPS2DeploymentTrace
+import org.eclipse.viatra.examples.cps.deployment.BehaviorTransition
+import org.eclipse.viatra.examples.cps.deployment.DeploymentApplication
+import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.ActionPair
+import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.HostInstance
+import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.ApplicationInstance
+import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.AppInstanceWithStateMachine
+import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.State
+import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.Transition
+import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory
+import org.eclipse.viatra.examples.cps.deployment.DeploymentFactory
+import org.eclipse.viatra.examples.cps.traceability.TraceabilityFactory
+import org.eclipse.viatra.examples.cps.deployment.DeploymentHost
 
 class CPS2DeploymentBatchViatra {
 	extension Logger logger = Logger.getLogger("cps.xform.m2m.batch.viatra")
@@ -29,14 +42,17 @@
     /* Transformation-related extensions */
 
     extension CpsXformM2M cpsXformM2M = CpsXformM2M.instance
-    extension RuleProvider ruleProvider
     extension BatchTransformation transformation
     extension BatchTransformationStatements statements
     
+    extension BatchTransformationRuleFactory = new BatchTransformationRuleFactory
+    protected extension DeploymentFactory depFactory = DeploymentFactory.eINSTANCE
+    protected extension TraceabilityFactory traceFactory = TraceabilityFactory.eINSTANCE
+    
     CPSToDeployment mapping
     ViatraQueryEngine engine
     
-    private var initialized = false;
+    var initialized = false;
 
     def initialize(CPSToDeployment cps2dep, ViatraQueryEngine engine) {
         initialize(cps2dep, engine, false, null)
@@ -51,7 +67,6 @@
         if (!initialized) {
             this.mapping = cps2dep
             this.engine = engine
-            ruleProvider = new RuleProvider(engine, cps2dep)
             
             val transformationBuilder = BatchTransformation.forEngine(engine)
 
@@ -98,4 +113,150 @@
         transformation = null
         return
     }
+    
+    val hostRule = createRule(HostInstance.instance).name("HostRule").action[
+                val cpsHostInstance = it.hostInstance
+                val nodeIp = it.hostInstance.nodeIp
+                debug('''Mapping host with IP: «nodeIp»''')
+                val deploymentHost = createDeploymentHost => [
+                    ip = nodeIp
+                ]
+                mapping.deployment.hosts += deploymentHost
+                mapping.traces += createCPS2DeploymentTrace => [
+                    cpsElements += cpsHostInstance
+                    deploymentElements += deploymentHost
+                ]
+            ].build
+    val applicationRule = createRule(ApplicationInstance.instance).name("ApplicationRule").action[
+                val cpsApplicationInstance = it.appInstance
+                val appId = it.appInstance.identifier
+                
+                val cpsHostInstance = cpsApplicationInstance.allocatedTo
+                val depHost = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsHostInstance).filter(DeploymentHost).head
+                
+                debug('''Mapping application with ID: «appId»''')
+                val deploymentApplication = createDeploymentApplication => [
+                    id = appId
+                ]
+                
+                
+                mapping.traces += createCPS2DeploymentTrace => [
+                    cpsElements += cpsApplicationInstance
+                    deploymentElements += deploymentApplication
+                ]
+                depHost.applications += deploymentApplication
+                debug('''Mapped application with ID: «appId»''')
+            ].build
+    val stateMachineRule = createRule(AppInstanceWithStateMachine.instance).name("StateMachineRule").action[
+                val cpsApplicationInstance = it.appInstance
+                val cpsStateMachine = it.stateMachine
+                
+                val depApplication = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsApplicationInstance).filter(DeploymentApplication).head
+                debug('''Mapping state machine with ID: «cpsStateMachine.identifier»''')
+                val depBehavior = createDeploymentBehavior => [
+                    description = cpsStateMachine.identifier
+                ]
+                depApplication.behavior = depBehavior
+                
+                val trace = getTraceForCPSElement(cpsStateMachine)
+                if (trace === null){
+                    mapping.traces += createCPS2DeploymentTrace => [
+                        cpsElements += cpsStateMachine
+                        deploymentElements += depBehavior
+                    ]
+                } else {
+                    trace.deploymentElements += depBehavior
+                }
+                
+            ].build
+    val stateRule = createRule(State.instance).name("StateRule").action[
+                val cpsStateMachine = it.stateMachine
+                val cpsAppInstance = it.appInstance
+                val cpsState = it.state
+                
+                debug('''Mapping state with ID: «cpsState.identifier»''')
+                val behaviorState = createBehaviorState => [
+                    description = cpsState.identifier
+                ]
+                
+                val appInstanceTrace = getTraceForCPSElement(cpsAppInstance)
+                val depApplication = appInstanceTrace.deploymentElements.filter(DeploymentApplication).head
+                val depBehavior = depApplication.behavior
+                depBehavior.states += behaviorState
+                
+                val trace = getTraceForCPSElement(cpsState)
+                if (trace === null) {
+                    mapping.traces += createCPS2DeploymentTrace => [
+                        cpsElements += cpsState
+                        deploymentElements += behaviorState
+                    ]
+                } else {
+                    trace.deploymentElements += behaviorState
+                }
+                
+                if (cpsStateMachine.initial == cpsState) {
+                    depBehavior.current = behaviorState
+                }
+            ].build
+    val transitionRule = createRule(Transition.instance).name("TransitionRule").action[
+                val cpsAppInstance = it.appInstance
+                val cpsState = it.sourceState
+                val cpsTargetState = it.targetState
+                val cpsTransition = it.transition  
+                
+                debug('''Mapping transition with ID: «cpsTransition.identifier»''')
+                val behaviorTransition = createBehaviorTransition => [
+                    description = cpsTransition.identifier
+                ]
+                
+                val appInstanceTrace = getTraceForCPSElement(cpsAppInstance)
+                val depApplication = appInstanceTrace.deploymentElements.filter(DeploymentApplication).head
+                val depBehavior = depApplication.behavior
+                depBehavior.transitions += behaviorTransition
+                
+                val trace = getTraceForCPSElement(cpsTransition)
+                if (trace === null){
+                    mapping.traces += createCPS2DeploymentTrace => [
+                        cpsElements += cpsTransition
+                        deploymentElements += behaviorTransition
+                    ]
+                } else {
+                    trace.deploymentElements += behaviorTransition
+                }
+                
+                val depTargetState = depBehavior.states.filter[description == cpsTargetState.identifier].head
+                val depSourceState = depBehavior.states.filter[description == cpsState.identifier].head
+                
+                depSourceState.outgoing += behaviorTransition
+                behaviorTransition.to = depTargetState
+            ].build
+    val actionRule = createRule(ActionPair.instance).name("ActionRule").action[
+                val cpsSendTransition = sendTransition
+                val cpsSendAppInstance = sendAppInstance
+                val cpsWaitTransition = waitTransition
+                val cpsWaitAppInstance = waitAppInstance
+                
+                debug('''Mapping trigger between transitions: «cpsSendTransition.identifier» and «cpsWaitTransition.identifier»''')
+                val sendTransitionTrace = getTraceForCPSElement(cpsSendTransition)
+                val sendAppInstanceTrace = getTraceForCPSElement(cpsSendAppInstance)
+                
+                val depSendApp = sendAppInstanceTrace.deploymentElements.filter(DeploymentApplication).head
+                val depSendTransition = sendTransitionTrace.deploymentElements.filter(BehaviorTransition).findFirst[
+                    depSendApp == it.eContainer.eContainer
+                ]
+                
+                val waitTransitionTrace = getTraceForCPSElement(cpsWaitTransition)
+                val waitAppInstanceTrace = getTraceForCPSElement(cpsWaitAppInstance)
+                
+                val depWaitApp = waitAppInstanceTrace.deploymentElements.filter(DeploymentApplication).head
+                val depWaitTransition = waitTransitionTrace.deploymentElements.filter(BehaviorTransition).findFirst[
+                    depWaitApp == it.eContainer.eContainer
+                ]
+                
+                depSendTransition.trigger += depWaitTransition
+            ].build
+    
+    def getTraceForCPSElement(Identifiable cpsElement) {
+        engine.cps2depTrace.getAllValuesOftrace(null, cpsElement, null).filter(CPS2DeploymentTrace).head
+    }
 }
\ No newline at end of file
diff --git a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/rules/RuleProvider.xtend b/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/rules/RuleProvider.xtend
deleted file mode 100644
index 7dc8b4d..0000000
--- a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/batch/viatra/rules/RuleProvider.xtend
+++ /dev/null
@@ -1,241 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014-2016 IncQuery Labs Ltd.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     Akos Horvath, Abel Hegedus, Zoltan Ujhelyi, Daniel Segesdi - initial API and implementation
- *******************************************************************************/
-package org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.rules
-
-import org.apache.log4j.Logger
-import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.Identifiable
-import org.eclipse.viatra.examples.cps.deployment.DeploymentApplication
-import org.eclipse.viatra.examples.cps.deployment.DeploymentFactory
-import org.eclipse.viatra.examples.cps.deployment.DeploymentHost
-import org.eclipse.viatra.examples.cps.traceability.CPS2DeploymentTrace
-import org.eclipse.viatra.examples.cps.traceability.CPSToDeployment
-import org.eclipse.viatra.examples.cps.traceability.TraceabilityFactory
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.ActionPair
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.AppInstanceWithStateMachine
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.ApplicationInstance
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.CpsXformM2M
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.HostInstance
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.State
-import org.eclipse.viatra.examples.cps.xform.m2m.batch.viatra.patterns.Transition
-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.viatra.examples.cps.deployment.BehaviorTransition
-
-class RuleProvider {
-	extension Logger logger = Logger.getLogger("cps.xform.m2m.batch.viatra")
-	extension CpsXformM2M cpsXformM2M = CpsXformM2M.instance
-	extension BatchTransformationRuleFactory = new BatchTransformationRuleFactory
-	extension IModelManipulations manipulation
-	protected extension DeploymentFactory depFactory = DeploymentFactory.eINSTANCE
-	protected extension TraceabilityFactory traceFactory = TraceabilityFactory.eINSTANCE
-	CPSToDeployment mapping
-	ViatraQueryEngine engine
-	
-	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> hostRule
-	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> applicationRule
-	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> stateMachineRule
-	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> stateRule
-	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> transitionRule
-	BatchTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> actionRule
-	
-	new(ViatraQueryEngine engine, CPSToDeployment deployment) {
-		this.mapping = deployment
-		this.engine = engine
-		manipulation = new SimpleModelManipulations(engine)
-	}
-	
-	
-	public def getHostRule() {
-		if (hostRule === null) {
-			hostRule = createRule(HostInstance.instance).name("HostRule").action[
-				val cpsHostInstance = it.hostInstance
-				val nodeIp = it.hostInstance.nodeIp
-				debug('''Mapping host with IP: «nodeIp»''')
-				val deploymentHost = createDeploymentHost => [
-					ip = nodeIp
-				]
-				mapping.deployment.hosts += deploymentHost
-				mapping.traces += createCPS2DeploymentTrace => [
-					cpsElements += cpsHostInstance
-					deploymentElements += deploymentHost
-				]
-			].build
-		}
-		return hostRule
-	}
-	
-	public def getApplicationRule() {
-		if (applicationRule === null) {
-			applicationRule = createRule(ApplicationInstance.instance).name("ApplicationRule").action[
-				val cpsApplicationInstance = it.appInstance
-				val appId = it.appInstance.identifier
-				
-				val cpsHostInstance = cpsApplicationInstance.allocatedTo
-				val depHost = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsHostInstance).filter(DeploymentHost).head
-				
-				debug('''Mapping application with ID: «appId»''')
-				val deploymentApplication = createDeploymentApplication => [
-					id = appId
-				]
-				
-				
-				mapping.traces += createCPS2DeploymentTrace => [
-					cpsElements += cpsApplicationInstance
-					deploymentElements += deploymentApplication
-				]
-				depHost.applications += deploymentApplication
-				debug('''Mapped application with ID: «appId»''')
-			].build
-		}
-		return applicationRule
-	}
-	
-	public def getStateMachineRule() {
-		if (stateMachineRule === null) {
-			stateMachineRule = createRule(AppInstanceWithStateMachine.instance).name("StateMachineRule").action[
-				val cpsApplicationInstance = it.appInstance
-				val cpsStateMachine = it.stateMachine
-				
-				val depApplication = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsApplicationInstance).filter(DeploymentApplication).head
-				debug('''Mapping state machine with ID: «cpsStateMachine.identifier»''')
-				val depBehavior = createDeploymentBehavior => [
-					description = cpsStateMachine.identifier
-				]
-				depApplication.behavior = depBehavior
-				
-				val trace = getTraceForCPSElement(cpsStateMachine)
-				if (trace === null){
-					mapping.traces += createCPS2DeploymentTrace => [
-						cpsElements += cpsStateMachine
-						deploymentElements += depBehavior
-					]
-				} else {
-					trace.deploymentElements += depBehavior
-				}
-				
-			].build
-		}
-		return stateMachineRule
-	}
-	
-	public def getStateRule() {
-		if (stateRule === null) {
-			stateRule = createRule(State.instance).name("StateRule").action[
-				val cpsStateMachine = it.stateMachine
-				val cpsAppInstance = it.appInstance
-				val cpsState = it.state
-				
-				debug('''Mapping state with ID: «cpsState.identifier»''')
-				val behaviorState = createBehaviorState => [
-					description = cpsState.identifier
-				]
-				
-				val appInstanceTrace = getTraceForCPSElement(cpsAppInstance)
-				val depApplication = appInstanceTrace.deploymentElements.filter(DeploymentApplication).head
-				val depBehavior = depApplication.behavior
-				depBehavior.states += behaviorState
-				
-				val trace = getTraceForCPSElement(cpsState)
-				if (trace === null) {
-					mapping.traces += createCPS2DeploymentTrace => [
-						cpsElements += cpsState
-						deploymentElements += behaviorState
-					]
-				} else {
-					trace.deploymentElements += behaviorState
-				}
-				
-				if (cpsStateMachine.initial == cpsState) {
-					depBehavior.current = behaviorState
-				}
-			].build
-		}
-		return stateRule
-	}
-	
-	public def getTransitionRule() {
-		if (transitionRule === null) {
-			transitionRule = createRule(Transition.instance).name("TransitionRule").action[
-				val cpsAppInstance = it.appInstance
-				val cpsState = it.sourceState
-				val cpsTargetState = it.targetState
-				val cpsTransition = it.transition  
-				
-				debug('''Mapping transition with ID: «cpsTransition.identifier»''')
-				val behaviorTransition = createBehaviorTransition => [
-					description = cpsTransition.identifier
-				]
-				
-				val appInstanceTrace = getTraceForCPSElement(cpsAppInstance)
-				val depApplication = appInstanceTrace.deploymentElements.filter(DeploymentApplication).head
-				val depBehavior = depApplication.behavior
-				depBehavior.transitions += behaviorTransition
-				
-				val trace = getTraceForCPSElement(cpsTransition)
-				if (trace === null){
-					mapping.traces += createCPS2DeploymentTrace => [
-						cpsElements += cpsTransition
-						deploymentElements += behaviorTransition
-					]
-				} else {
-					trace.deploymentElements += behaviorTransition
-				}
-				
-				val depTargetState = depBehavior.states.filter[description == cpsTargetState.identifier].head
-				val depSourceState = depBehavior.states.filter[description == cpsState.identifier].head
-				
-				depSourceState.outgoing += behaviorTransition
-				behaviorTransition.to = depTargetState
-			].build
-		}
-		return transitionRule
-	}
-	
-	public def getActionRule() {
-		if (actionRule === null) {
-			actionRule = createRule(ActionPair.instance).name("ActionRule").action[
-				val cpsSendTransition = sendTransition
-				val cpsSendAppInstance = sendAppInstance
-				val cpsWaitTransition = waitTransition
-				val cpsWaitAppInstance = waitAppInstance
-				
-				debug('''Mapping trigger between transitions: «cpsSendTransition.identifier» and «cpsWaitTransition.identifier»''')
-				val sendTransitionTrace = getTraceForCPSElement(cpsSendTransition)
-				val sendAppInstanceTrace = getTraceForCPSElement(cpsSendAppInstance)
-				
-				val depSendApp = sendAppInstanceTrace.deploymentElements.filter(DeploymentApplication).head
-				val depSendTransition = sendTransitionTrace.deploymentElements.filter(BehaviorTransition).findFirst[
-					depSendApp == it.eContainer.eContainer
-				]
-				
-				val waitTransitionTrace = getTraceForCPSElement(cpsWaitTransition)
-				val waitAppInstanceTrace = getTraceForCPSElement(cpsWaitAppInstance)
-				
-				val depWaitApp = waitAppInstanceTrace.deploymentElements.filter(DeploymentApplication).head
-				val depWaitTransition = waitTransitionTrace.deploymentElements.filter(BehaviorTransition).findFirst[
-					depWaitApp == it.eContainer.eContainer
-				]
-				
-				depSendTransition.trigger += depWaitTransition
-			].build
-		}
-		return actionRule
-	}
-	
-	def getTraceForCPSElement(Identifiable cpsElement) {
-		engine.cps2depTrace.getAllValuesOftrace(null, cpsElement, null).filter(CPS2DeploymentTrace).head
-	}
-}
\ No newline at end of file
diff --git a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/CPS2DeploymentTransformationViatra.xtend b/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/CPS2DeploymentTransformationViatra.xtend
index 34720c1..b777f46 100644
--- a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/CPS2DeploymentTransformationViatra.xtend
+++ b/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/CPS2DeploymentTransformationViatra.xtend
@@ -15,27 +15,48 @@
 import org.apache.log4j.Logger
 import org.eclipse.viatra.examples.cps.traceability.CPSToDeployment
 import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.CpsXformM2M
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.rules.RuleProvider
 import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
 import org.eclipse.viatra.transformation.evm.api.Scheduler.ISchedulerFactory
 import org.eclipse.viatra.transformation.runtime.emf.transformation.eventdriven.EventDrivenTransformation
 
-import static com.google.common.base.Preconditions.*
+import static org.eclipse.viatra.query.runtime.matchers.util.Preconditions.*
 import org.eclipse.viatra.transformation.debug.configuration.TransformationDebuggerConfiguration
 import org.eclipse.viatra.transformation.evm.specific.resolver.InvertedDisappearancePriorityConflictResolver
+import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.IModelManipulations
+import org.eclipse.viatra.examples.cps.deployment.DeploymentPackage
+import org.eclipse.viatra.examples.cps.traceability.TraceabilityPackage
+import org.eclipse.viatra.transformation.runtime.emf.rules.eventdriven.EventDrivenTransformationRuleFactory
+import org.eclipse.viatra.transformation.evm.specific.crud.CRUDActivationStateEnum
+import org.eclipse.viatra.examples.cps.deployment.BehaviorTransition
+import org.eclipse.viatra.examples.cps.deployment.DeploymentApplication
+import org.eclipse.viatra.transformation.evm.specific.Lifecycles
+import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.TriggerPair
+import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.Transition
+import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.State
+import org.eclipse.viatra.examples.cps.deployment.DeploymentBehavior
+import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.StateMachine
+import org.eclipse.viatra.examples.cps.deployment.DeploymentHost
+import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.ApplicationInstance
+import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.HostInstance
+import org.eclipse.viatra.examples.cps.deployment.BehaviorState
+import org.eclipse.viatra.transformation.runtime.emf.modelmanipulation.SimpleModelManipulations
 
 class CPS2DeploymentTransformationViatra {
 
     extension Logger logger = Logger.getLogger("cps.xform.m2m.incr.viatra")
     extension CpsXformM2M cpsXformM2M = CpsXformM2M.instance
-    extension RuleProvider ruleProvider
+
+    extension IModelManipulations manipulation
+    extension DeploymentPackage depPackage = DeploymentPackage::eINSTANCE
+    extension TraceabilityPackage trPackage = TraceabilityPackage::eINSTANCE
+    extension EventDrivenTransformationRuleFactory ruleFactory = new EventDrivenTransformationRuleFactory
 
     CPSToDeployment cps2dep
     ViatraQueryEngine engine
     EventDrivenTransformation transform
     ISchedulerFactory factory;
 
-    private var initialized = false;
+    var initialized = false;
 
     def initialize(CPSToDeployment cps2dep, ViatraQueryEngine engine) {
         initialize(cps2dep, engine, false, null)
@@ -54,11 +75,11 @@
             debug("Preparing queries on engine.")
             var watch = Stopwatch.createStarted
             prepare(engine)
+            manipulation = new SimpleModelManipulations(engine)
             info('''Prepared queries on engine («watch.elapsed(TimeUnit.MILLISECONDS)» ms)''')
 
             info("Preparing transformation rules.")
             watch = Stopwatch.createStarted
-            ruleProvider = new RuleProvider(engine, cps2dep)
             createTransformation(isDebuggable, debugName)
             info('''Prepared transformation rules («watch.elapsed(TimeUnit.MILLISECONDS)» ms)''')
             initialized = true
@@ -113,4 +134,284 @@
         transform = null
         return
     }
+    
+    val hostRule = createRule(HostInstance.instance).name("HostRule").action(
+                CRUDActivationStateEnum.CREATED) [  
+                debug('''Mapping host with IP: «hostInstance.nodeIp»''')
+            
+                val deploymentHost = cps2dep.deployment.createChild(deployment_Hosts, deploymentHost)
+                deploymentHost.set(deploymentHost_Ip, hostInstance.nodeIp)
+                
+                val hostTrace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
+                hostTrace.addTo(CPS2DeploymentTrace_CpsElements, hostInstance)
+                hostTrace.addTo(CPS2DeploymentTrace_DeploymentElements, deploymentHost)
+                
+            ].action(CRUDActivationStateEnum.UPDATED) [
+                // Optional.get should be always work here
+                val depHost = engine.cps2depTrace.getOneArbitraryMatch(cps2dep, null, hostInstance, null).get.depElement as DeploymentHost
+                debug('''Updating mapped host with IP: «depHost.ip»''')
+                depHost.set(deploymentHost_Ip, hostInstance.nodeIp)
+                debug('''Updated mapped host with IP: «depHost.ip»''')
+            ].action(CRUDActivationStateEnum.DELETED) [
+                engine.cps2depTrace.getOneArbitraryMatch(cps2dep, null, hostInstance, null).ifPresent[traceMatch |
+                        logger.debug('''Removing host with IP: «hostInstance.nodeIp»''')
+                        cps2dep.deployment.remove(deployment_Hosts, traceMatch.depElement)
+                        cps2dep.remove(CPSToDeployment_Traces, traceMatch.trace)
+                        logger.debug('''Removed host with IP: «hostInstance.nodeIp»''')
+                ]
+            ].addLifeCycle(Lifecycles.getDefault(true, true))
+            .build
+    val applicationRule = createRule(ApplicationInstance.instance).name("ApplicationRule").action(
+                CRUDActivationStateEnum.CREATED) [
+                val depHost = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance.allocatedTo).
+                    filter(DeploymentHost).head
+                debug('''Mapping application with ID: «appInstance.identifier»''')
+                val deploymentApplication = depHost.createChild(deploymentHost_Applications, deploymentApplication)
+                deploymentApplication.set(deploymentApplication_Id, appInstance.identifier)
+                
+                val hostTrace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
+                hostTrace.addTo(CPS2DeploymentTrace_CpsElements, appInstance)
+                hostTrace.addTo(CPS2DeploymentTrace_DeploymentElements, deploymentApplication)
+                debug('''Mapped application with ID: «appInstance.identifier»''')
+            ].action(CRUDActivationStateEnum.UPDATED) [
+                val depApp = engine.cps2depTrace.getOneArbitraryMatch(cps2dep, null, appInstance, null).get.depElement as DeploymentApplication
+                if (depApp.id != appInstance.identifier)
+                    depApp.set(deploymentApplication_Id, appInstance.identifier)
+            ].action(CRUDActivationStateEnum.DELETED) [
+                val trace = engine.cps2depTrace.getAllValuesOftrace(null, appInstance, null).head
+                val depApp = trace.deploymentElements.head as DeploymentApplication
+                engine.allocatedDeploymentApplication.getAllValuesOfdepHost(depApp).head.remove(deploymentHost_Applications, depApp)
+                cps2dep.remove(CPSToDeployment_Traces, trace)
+            ].addLifeCycle(Lifecycles.getDefault(true, true))
+            .build
+    val stateMachineRule = createRule(StateMachine.instance).name("StateMachineRule").action(
+                CRUDActivationStateEnum.CREATED) [
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
+                    DeploymentApplication).head
+                debug('''Mapping state machine with ID: «stateMachine.identifier»''')
+                val behavior = depApp.createChild(deploymentApplication_Behavior, deploymentBehavior) as DeploymentBehavior
+                behavior.set(deploymentElement_Description, stateMachine.identifier)
+                depApp.set(deploymentApplication_Behavior, behavior)
+                val traces = engine.cps2depTrace.getAllValuesOftrace(null, stateMachine, null)
+                if (traces.empty) {
+                    trace('''Creating new trace for state machine''')
+                    val trace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
+                    trace.addTo(CPS2DeploymentTrace_CpsElements, stateMachine)
+                    trace.addTo(CPS2DeploymentTrace_DeploymentElements, behavior)
+
+                } else {
+                    trace('''Adding new behavior to existing trace''')
+                    traces.head.addTo(CPS2DeploymentTrace_DeploymentElements, behavior)
+                }
+                debug('''Mapped state machine with ID: «stateMachine.identifier»''')
+            ].action(CRUDActivationStateEnum.UPDATED) [
+                val smId = stateMachine.identifier
+                debug('''Updating mapped state machine with ID: «smId»''')
+                val depSMs = engine.cps2depTrace.getAllValuesOfdepElement(null, null, stateMachine).filter(
+                    DeploymentBehavior)
+                depSMs.forEach [
+                    if (description != smId) {
+                        trace('''ID changed to «smId»''')
+                        set(deploymentElement_Description, smId)
+                    }
+                ]
+                debug('''Updated mapped state machine with ID: «smId»''')
+            ].action(CRUDActivationStateEnum.DELETED) [
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).head as DeploymentApplication;
+                val depBehavior = depApp.behavior
+                val smId = depBehavior.description
+
+                logger.debug('''Removing state machine with ID: «smId»''')
+                depApp.set(deploymentApplication_Behavior, null)
+
+                val smTrace = engine.cps2depTrace.getAllValuesOftrace(null, stateMachine, null).head
+                smTrace.remove(CPS2DeploymentTrace_DeploymentElements, depBehavior)
+                if (smTrace.deploymentElements.empty) {
+                    trace('''Removing empty trace''')
+                    cps2dep.remove(CPSToDeployment_Traces, smTrace)
+                }
+                logger.debug('''Removed state machine with ID: «smId»''')
+            ].addLifeCycle(Lifecycles.getDefault(true, true))
+            .build
+    val stateRule = createRule(State.instance).name("StateRule").action(
+                CRUDActivationStateEnum.CREATED) [
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).head as DeploymentApplication
+                debug('''Mapping state with ID: «state.identifier»''')
+                val depState = depApp.behavior.createChild(deploymentBehavior_States, behaviorState)
+                depState.set(deploymentElement_Description, state.identifier)
+                if (stateMachine.initial == state) {
+                    depApp.behavior.set(deploymentBehavior_Current, depState)
+                }
+                
+                val traces = engine.cps2depTrace.getAllValuesOftrace(null, state, null)
+                if (traces.empty) {
+                    trace('''Creating new trace for state ''')
+                    val trace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
+                    trace.addTo(CPS2DeploymentTrace_CpsElements, state)
+                    trace.addTo(CPS2DeploymentTrace_DeploymentElements, depState)
+                } else {
+                    trace('''Adding new state to existing trace''')
+                    traces.head.addTo(CPS2DeploymentTrace_DeploymentElements, depState)
+                }
+                debug('''Mapped state with ID: «state.identifier»''')
+            ].action(CRUDActivationStateEnum.UPDATED) [
+                debug('''Updating mapped state with ID: «state.identifier»''')
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
+                    DeploymentApplication).head
+                val depState = engine.cps2depTrace.getAllValuesOfdepElement(null, null, state).filter(BehaviorState).
+                    findFirst[depApp.behavior.states.contains(it)]  
+                val depBehavior = depApp.behavior
+                
+                if (depState.description != state.identifier) {
+                    trace('''ID changed to «state.identifier»''')
+                    depState.set(deploymentElement_Description, state.identifier)
+                }
+                
+                if (state == stateMachine.initial) {
+                    if (depBehavior.current != depState) {
+                        trace('''Current state changed to «state.identifier»''')
+                        depBehavior.set(deploymentBehavior_Current, depState)
+                    }
+                }
+                debug('''Updated mapped state with ID: «state.identifier»''')
+            ].action(CRUDActivationStateEnum.DELETED) [
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).head as DeploymentApplication
+                val depBehavior = depApp.behavior
+                val depState = engine.cps2depTrace.getAllValuesOfdepElement(null, null, state).filter(BehaviorState).
+                    findFirst[depApp.behavior.states.contains(it)];
+                val stateId = depState.description
+                
+                logger.debug('''Removing state with ID: «stateId»''')
+                if (depBehavior !== null) {
+                    depBehavior.remove(deploymentBehavior_States, depState)
+                    if (depBehavior.current == depState) {
+                        depBehavior.set(deploymentBehavior_Current, null)
+                    }
+                }
+                val smTrace = engine.cps2depTrace.getAllValuesOftrace(null, state, null).head
+                smTrace.remove(CPS2DeploymentTrace_DeploymentElements, depState)
+                if (smTrace.deploymentElements.empty) {
+                    trace('''Removing empty trace''')
+                    cps2dep.remove(CPSToDeployment_Traces, smTrace)
+                }
+                logger.debug('''Removed state with ID: «stateId»''')
+            ].addLifeCycle(Lifecycles.getDefault(true, true))
+            .build
+    val transitionRule = createRule(Transition.instance).name("TransitionRule").action(
+                CRUDActivationStateEnum.CREATED) [
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
+                    DeploymentApplication).head
+                val transition = transition
+                val transitionId = transition.identifier
+                
+                debug('''Mapping transition with ID: «transitionId»''')
+                val depTransition = depApp.behavior.createChild(deploymentBehavior_Transitions, behaviorTransition)
+                depTransition.set(deploymentElement_Description, transitionId)
+
+                val tempDepSources = engine.cps2depTrace.getAllValuesOfdepElement(null, null, srcState);
+                val depSource = depApp.behavior.states.findFirst[tempDepSources.contains(it)]
+
+                depSource.addTo(behaviorState_Outgoing, depTransition)
+                val tempDepTargets = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition.targetState);
+                val depTarget = depApp.behavior.states.findFirst[tempDepTargets.contains(it)]
+                
+                depTransition.set(behaviorTransition_To, depTarget)
+                val traces = engine.cps2depTrace.getAllValuesOftrace(null, transition, null)
+                if (traces.empty) {
+                    trace('''Creating new trace for transition ''')
+                    val trace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
+                    trace.addTo(CPS2DeploymentTrace_CpsElements, transition)
+                    trace.addTo(CPS2DeploymentTrace_DeploymentElements, depTransition)
+                } else {
+                    trace('''Adding new transition to existing trace''')
+                    traces.head.addTo(CPS2DeploymentTrace_DeploymentElements, depTransition)
+                }
+                debug('''Mapped transition with ID: «transitionId»''')
+            ].action(CRUDActivationStateEnum.UPDATED) [
+                val transition = transition
+                val trId = transition.identifier
+                debug('''Updating mapped transition with ID: «trId»''')
+                
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
+                    DeploymentApplication).head
+                val depTransitions = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition).filter(
+                    BehaviorTransition).toSet
+                val depTransition = depApp.behavior.transitions.findFirst[depTransitions.contains(it)]
+                val oldDesc = depTransition.description
+                if (oldDesc != trId) {
+                    trace('''ID changed to «oldDesc»''')
+                    depTransition.set(deploymentElement_Description, trId)
+                }
+                val tempDepSources = engine.cps2depTrace.getAllValuesOfdepElement(null, null, srcState)
+                val depSource = depApp.behavior.states.findFirst[tempDepSources.contains(it)]
+                val tempDepTargets = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition.targetState);
+                val depTarget = depApp.behavior.states.findFirst[tempDepTargets.contains(it)]
+                
+                if (!depSource.outgoing.contains(depTransition)) {
+                    trace('''Source state changed to «depSource.description»''')
+                    depSource.addTo(behaviorState_Outgoing, depTransition)
+                }
+                if (depTransition.to != depTarget) {
+                    trace('''Target state changed to «depTarget.description»''')
+                    depTransition.set(behaviorTransition_To, depTarget)
+                }
+                debug('''Updated mapped transition with ID: «trId»''')
+            ].action(CRUDActivationStateEnum.DELETED) [
+                val transition = transition
+                val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
+                    DeploymentApplication).head
+                val depTransitions = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition).filter(
+                    BehaviorTransition).toSet
+                val depTransition = engine.depBehaviorsStateAndTransitions.
+                    getAllValuesOfdepTransition(depApp.behavior, null).findFirst[depTransitions.contains(it)]
+                val trId = depTransition.description
+                logger.debug('''Removing transition with ID: «trId»''')
+                depTransition.set(behaviorTransition_To, null)
+                val tempDepSources = engine.cps2depTrace.getAllValuesOfdepElement(null, null, srcState)
+                val depSource = depApp.behavior.states.findFirst[tempDepSources.contains(it)]
+                depSource?.remove(behaviorState_Outgoing, depTransition)
+                depApp.behavior.remove(deploymentBehavior_Transitions, depTransition)
+                val smTrace = engine.cps2depTrace.getAllValuesOftrace(null, transition, null).head
+                smTrace.remove(CPS2DeploymentTrace_DeploymentElements, depTransition)
+                if (smTrace.deploymentElements.empty) {
+                    trace('''Removing empty trace''')
+                    cps2dep.remove(CPSToDeployment_Traces, smTrace)
+                }
+                logger.debug('''Removed transition with ID: «trId»''')
+            ].addLifeCycle(Lifecycles.getDefault(true, true))
+            .build
+    val triggerRule = createRule(TriggerPair.instance).name("TriggerRule").action(
+                CRUDActivationStateEnum.CREATED) [
+                val depAppTrigger = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTrigger).
+                    filter(DeploymentApplication).head
+                val depAppTarget = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTarget).
+                    filter(DeploymentApplication).head
+                val sendTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTrigger).filter(
+                    BehaviorTransition).findFirst[depAppTrigger.behavior.transitions.contains(it)]
+                val waitTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTarget).filter(
+                    BehaviorTransition).findFirst[depAppTarget.behavior.transitions.contains(it)]
+                debug('''Mapping trigger between «sendTr.description» and «waitTr.description»''')
+                if (!sendTr.trigger.contains(waitTr)) {
+                    trace('''Adding new trigger''')
+                    sendTr.addTo(behaviorTransition_Trigger, waitTr)
+                }
+                debug('''Mapped trigger between «sendTr.description» and «waitTr.description»''')
+            ].action(CRUDActivationStateEnum.DELETED) [
+                val depAppTrigger = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTrigger).
+                    filter(DeploymentApplication).head
+                val depAppTarget = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTarget).
+                    filter(DeploymentApplication).head
+                val sendTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTrigger).filter(
+                    BehaviorTransition).findFirst[depAppTrigger.behavior.transitions.contains(it)]
+                val waitTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTarget).filter(
+                    BehaviorTransition).findFirst[depAppTarget.behavior.transitions.contains(it)]
+                debug('''Removing trigger between «sendTr.description» and «waitTr.description»''')
+                if (sendTr.trigger.contains(waitTr)) {
+                    trace('''Removing existing trigger''')
+                    sendTr.remove(behaviorTransition_Trigger, waitTr)
+                }
+                debug('''Removed trigger between «sendTr.description» and «waitTr.description»''')
+            ].addLifeCycle(Lifecycles.getDefault(true, true))
+            .build
+    
 }
diff --git a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/rules/RuleProvider.xtend b/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/rules/RuleProvider.xtend
deleted file mode 100644
index 8c0878a..0000000
--- a/cps/transformations/org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra/src/org/eclipse/viatra/examples/cps/xform/m2m/incr/viatra/rules/RuleProvider.xtend
+++ /dev/null
@@ -1,378 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014-2016 IncQuery Labs Ltd.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- *     Akos Horvath, Abel Hegedus, Zoltan Ujhelyi, Peter Lunk, Istvan David - initial API and implementation
- *******************************************************************************/
-package org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.rules
-
-import org.apache.log4j.Logger
-import org.eclipse.viatra.examples.cps.deployment.BehaviorState
-import org.eclipse.viatra.examples.cps.deployment.BehaviorTransition
-import org.eclipse.viatra.examples.cps.deployment.DeploymentApplication
-import org.eclipse.viatra.examples.cps.deployment.DeploymentBehavior
-import org.eclipse.viatra.examples.cps.deployment.DeploymentHost
-import org.eclipse.viatra.examples.cps.deployment.DeploymentPackage
-import org.eclipse.viatra.examples.cps.traceability.CPS2DeploymentTrace
-import org.eclipse.viatra.examples.cps.traceability.CPSToDeployment
-import org.eclipse.viatra.examples.cps.traceability.TraceabilityPackage
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.ApplicationInstance
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.CpsXformM2M
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.HostInstance
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.StateMachine
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.State
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.Transition
-import org.eclipse.viatra.examples.cps.xform.m2m.incr.viatra.patterns.TriggerPair
-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.evm.specific.Lifecycles
-import org.eclipse.viatra.transformation.evm.specific.crud.CRUDActivationStateEnum
-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.eventdriven.EventDrivenTransformationRule
-import org.eclipse.viatra.transformation.runtime.emf.rules.eventdriven.EventDrivenTransformationRuleFactory
-
-public class RuleProvider {
-
-	extension Logger logger = Logger.getLogger("cps.xform.m2m.incr.viatra")
-	extension CpsXformM2M cpsXformM2M = CpsXformM2M.instance
-	extension IModelManipulations manipulation
-	extension DeploymentPackage depPackage = DeploymentPackage::eINSTANCE
-	extension TraceabilityPackage trPackage = TraceabilityPackage::eINSTANCE
-	extension EventDrivenTransformationRuleFactory factory = new EventDrivenTransformationRuleFactory
-	CPSToDeployment cps2dep
-	ViatraQueryEngine engine
-
-	EventDrivenTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> hostRule
-	EventDrivenTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> applicationRule
-	EventDrivenTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> stateMachineRule
-	EventDrivenTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> stateRule
-	EventDrivenTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> transitionRule
-	EventDrivenTransformationRule<? extends IPatternMatch, ? extends ViatraQueryMatcher<?>> triggerRule
-
-	new(ViatraQueryEngine engine, CPSToDeployment cps2dep) {
-		this.engine = engine
-		this.cps2dep = cps2dep
-		manipulation = new SimpleModelManipulations(engine)
-	}
-
-	public def getHostRule() {
-		if (hostRule === null) {
-			hostRule = createRule(HostInstance.instance).name("HostRule").action(
-				CRUDActivationStateEnum.CREATED) [	
-				debug('''Mapping host with IP: «hostInstance.nodeIp»''')
-			
-				val deploymentHost = cps2dep.deployment.createChild(deployment_Hosts, deploymentHost)
-				deploymentHost.set(deploymentHost_Ip, hostInstance.nodeIp)
-				
-				val hostTrace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
-				hostTrace.addTo(CPS2DeploymentTrace_CpsElements, hostInstance)
-				hostTrace.addTo(CPS2DeploymentTrace_DeploymentElements, deploymentHost)
-				
-			].action(CRUDActivationStateEnum.UPDATED) [
-				// Optional.get should be always work here
-				val depHost = engine.cps2depTrace.getOneArbitraryMatch(cps2dep, null, hostInstance, null).get.depElement as DeploymentHost
-				debug('''Updating mapped host with IP: «depHost.ip»''')
-				depHost.set(deploymentHost_Ip, hostInstance.nodeIp)
-				debug('''Updated mapped host with IP: «depHost.ip»''')
-			].action(CRUDActivationStateEnum.DELETED) [
-				engine.cps2depTrace.getOneArbitraryMatch(cps2dep, null, hostInstance, null).ifPresent[traceMatch |
-        				logger.debug('''Removing host with IP: «hostInstance.nodeIp»''')
-        				cps2dep.deployment.remove(deployment_Hosts, traceMatch.depElement)
-        				cps2dep.remove(CPSToDeployment_Traces, traceMatch.trace)
-        				logger.debug('''Removed host with IP: «hostInstance.nodeIp»''')
-				]
-			].addLifeCycle(Lifecycles.getDefault(true, true)).build
-
-		}
-		return hostRule
-	}
-
-	public def getApplicationRule() {
-		if (applicationRule === null) {
-
-			applicationRule = createRule(ApplicationInstance.instance).name("ApplicationRule").action(
-				CRUDActivationStateEnum.CREATED) [
-				val depHost = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance.allocatedTo).
-					filter(DeploymentHost).head
-				debug('''Mapping application with ID: «appInstance.identifier»''')
-				val deploymentApplication = depHost.createChild(deploymentHost_Applications, deploymentApplication)
-				deploymentApplication.set(deploymentApplication_Id, appInstance.identifier)
-				
-				val hostTrace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
-				hostTrace.addTo(CPS2DeploymentTrace_CpsElements, appInstance)
-				hostTrace.addTo(CPS2DeploymentTrace_DeploymentElements, deploymentApplication)
-				debug('''Mapped application with ID: «appInstance.identifier»''')
-			].action(CRUDActivationStateEnum.UPDATED) [
-				val depApp = engine.cps2depTrace.getOneArbitraryMatch(cps2dep, null, appInstance, null).get.depElement as DeploymentApplication
-				if (depApp.id != appInstance.identifier)
-					depApp.set(deploymentApplication_Id, appInstance.identifier)
-			].action(CRUDActivationStateEnum.DELETED) [
-				val trace = engine.cps2depTrace.getAllValuesOftrace(null, appInstance, null).head as CPS2DeploymentTrace
-				val depApp = trace.deploymentElements.head as DeploymentApplication
-				engine.allocatedDeploymentApplication.getAllValuesOfdepHost(depApp).head.remove(deploymentHost_Applications, depApp)
-				cps2dep.remove(CPSToDeployment_Traces, trace)
-			].addLifeCycle(Lifecycles.getDefault(true, true)).build
-
-		}
-		return applicationRule
-	}
-
-	public def getStateMachineRule() {
-		if (stateMachineRule === null) {
-			stateMachineRule = createRule(StateMachine.instance).name("StateMachineRule").action(
-				CRUDActivationStateEnum.CREATED) [
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
-					DeploymentApplication).head
-				debug('''Mapping state machine with ID: «stateMachine.identifier»''')
-				val behavior = depApp.createChild(deploymentApplication_Behavior, deploymentBehavior) as DeploymentBehavior
-				behavior.set(deploymentElement_Description, stateMachine.identifier)
-				depApp.set(deploymentApplication_Behavior, behavior)
-				val traces = engine.cps2depTrace.getAllValuesOftrace(null, stateMachine, null)
-				if (traces.empty) {
-					trace('''Creating new trace for state machine''')
-					val trace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
-					trace.addTo(CPS2DeploymentTrace_CpsElements, stateMachine)
-					trace.addTo(CPS2DeploymentTrace_DeploymentElements, behavior)
-
-				} else {
-					trace('''Adding new behavior to existing trace''')
-					traces.head.addTo(CPS2DeploymentTrace_DeploymentElements, behavior)
-				}
-				debug('''Mapped state machine with ID: «stateMachine.identifier»''')
-			].action(CRUDActivationStateEnum.UPDATED) [
-				val smId = stateMachine.identifier
-				debug('''Updating mapped state machine with ID: «smId»''')
-				val depSMs = engine.cps2depTrace.getAllValuesOfdepElement(null, null, stateMachine).filter(
-					DeploymentBehavior)
-				depSMs.forEach [
-					if (description != smId) {
-						trace('''ID changed to «smId»''')
-						set(deploymentElement_Description, smId)
-					}
-				]
-				debug('''Updated mapped state machine with ID: «smId»''')
-			].action(CRUDActivationStateEnum.DELETED) [
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).head as DeploymentApplication;
-				val depBehavior = depApp.behavior
-				val smId = depBehavior.description
-
-				logger.debug('''Removing state machine with ID: «smId»''')
-				depApp.set(deploymentApplication_Behavior, null)
-
-				val smTrace = engine.cps2depTrace.getAllValuesOftrace(null, stateMachine, null).head
-				smTrace.remove(CPS2DeploymentTrace_DeploymentElements, depBehavior)
-				if (smTrace.deploymentElements.empty) {
-					trace('''Removing empty trace''')
-					cps2dep.remove(CPSToDeployment_Traces, smTrace)
-				}
-				logger.debug('''Removed state machine with ID: «smId»''')
-			].addLifeCycle(Lifecycles.getDefault(true, true)).build
-
-		}
-		return stateMachineRule
-	}
-
-	public def getStateRule() {
-		if (stateRule === null) {
-			stateRule = createRule(State.instance).name("StateRule").action(
-				CRUDActivationStateEnum.CREATED) [
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).head as DeploymentApplication
-				debug('''Mapping state with ID: «state.identifier»''')
-				val depState = depApp.behavior.createChild(deploymentBehavior_States, behaviorState)
-				depState.set(deploymentElement_Description, state.identifier)
-				if (stateMachine.initial == state) {
-					depApp.behavior.set(deploymentBehavior_Current, depState)
-				}
-				
-				val traces = engine.cps2depTrace.getAllValuesOftrace(null, state, null)
-				if (traces.empty) {
-					trace('''Creating new trace for state ''')
-					val trace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
-					trace.addTo(CPS2DeploymentTrace_CpsElements, state)
-					trace.addTo(CPS2DeploymentTrace_DeploymentElements, depState)
-				} else {
-					trace('''Adding new state to existing trace''')
-					traces.head.addTo(CPS2DeploymentTrace_DeploymentElements, depState)
-				}
-				debug('''Mapped state with ID: «state.identifier»''')
-			].action(CRUDActivationStateEnum.UPDATED) [
-				debug('''Updating mapped state with ID: «state.identifier»''')
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
-					DeploymentApplication).head
-				val depState = engine.cps2depTrace.getAllValuesOfdepElement(null, null, state).filter(BehaviorState).
-					findFirst[depApp.behavior.states.contains(it)]	
-				val depBehavior = depApp.behavior
-				
-				if (depState.description != state.identifier) {
-					trace('''ID changed to «state.identifier»''')
-					depState.set(deploymentElement_Description, state.identifier)
-				}
-				
-				if (state == stateMachine.initial) {
-					if (depBehavior.current != depState) {
-						trace('''Current state changed to «state.identifier»''')
-						depBehavior.set(deploymentBehavior_Current, depState)
-					}
-				}
-				debug('''Updated mapped state with ID: «state.identifier»''')
-			].action(CRUDActivationStateEnum.DELETED) [
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).head as DeploymentApplication
-				val depBehavior = depApp.behavior
-				val depState = engine.cps2depTrace.getAllValuesOfdepElement(null, null, state).filter(BehaviorState).
-					findFirst[depApp.behavior.states.contains(it)];
-				val stateId = depState.description
-				
-				logger.debug('''Removing state with ID: «stateId»''')
-				if (depBehavior !== null) {
-					depBehavior.remove(deploymentBehavior_States, depState)
-					if (depBehavior.current == depState) {
-						depBehavior.set(deploymentBehavior_Current, null)
-					}
-				}
-				val smTrace = engine.cps2depTrace.getAllValuesOftrace(null, state, null).head
-				smTrace.remove(CPS2DeploymentTrace_DeploymentElements, depState)
-				if (smTrace.deploymentElements.empty) {
-					trace('''Removing empty trace''')
-					cps2dep.remove(CPSToDeployment_Traces, smTrace)
-				}
-				logger.debug('''Removed state with ID: «stateId»''')
-			].addLifeCycle(Lifecycles.getDefault(true, true)).build
-
-		}
-		return stateRule
-	}
-
-	public def getTransitionRule() {
-		if (transitionRule === null) {
-			transitionRule = createRule(Transition.instance).name("TransitionRule").action(
-				CRUDActivationStateEnum.CREATED) [
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
-					DeploymentApplication).head
-				val transition = transition
-				val transitionId = transition.identifier
-				
-				debug('''Mapping transition with ID: «transitionId»''')
-				val depTransition = depApp.behavior.createChild(deploymentBehavior_Transitions, behaviorTransition)
-				depTransition.set(deploymentElement_Description, transitionId)
-
-				val tempDepSources = engine.cps2depTrace.getAllValuesOfdepElement(null, null, srcState);
-				val depSource = depApp.behavior.states.findFirst[tempDepSources.contains(it)]
-
-				depSource.addTo(behaviorState_Outgoing, depTransition)
-				val tempDepTargets = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition.targetState);
-				val depTarget = depApp.behavior.states.findFirst[tempDepTargets.contains(it)]
-				
-				depTransition.set(behaviorTransition_To, depTarget)
-				val traces = engine.cps2depTrace.getAllValuesOftrace(null, transition, null)
-				if (traces.empty) {
-					trace('''Creating new trace for transition ''')
-					val trace = cps2dep.createChild(CPSToDeployment_Traces, CPS2DeploymentTrace)
-					trace.addTo(CPS2DeploymentTrace_CpsElements, transition)
-					trace.addTo(CPS2DeploymentTrace_DeploymentElements, depTransition)
-				} else {
-					trace('''Adding new transition to existing trace''')
-					traces.head.addTo(CPS2DeploymentTrace_DeploymentElements, depTransition)
-				}
-				debug('''Mapped transition with ID: «transitionId»''')
-			].action(CRUDActivationStateEnum.UPDATED) [
-				val transition = transition
-				val trId = transition.identifier
-				debug('''Updating mapped transition with ID: «trId»''')
-				
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
-					DeploymentApplication).head
-				val depTransitions = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition).filter(
-					BehaviorTransition).toSet
-				val depTransition = depApp.behavior.transitions.findFirst[depTransitions.contains(it)]
-				val oldDesc = depTransition.description
-				if (oldDesc != trId) {
-					trace('''ID changed to «oldDesc»''')
-					depTransition.set(deploymentElement_Description, trId)
-				}
-				val tempDepSources = engine.cps2depTrace.getAllValuesOfdepElement(null, null, srcState)
-				val depSource = depApp.behavior.states.findFirst[tempDepSources.contains(it)]
-				val tempDepTargets = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition.targetState);
-				val depTarget = depApp.behavior.states.findFirst[tempDepTargets.contains(it)]
-				
-				if (!depSource.outgoing.contains(depTransition)) {
-					trace('''Source state changed to «depSource.description»''')
-					depSource.addTo(behaviorState_Outgoing, depTransition)
-				}
-				if (depTransition.to != depTarget) {
-					trace('''Target state changed to «depTarget.description»''')
-					depTransition.set(behaviorTransition_To, depTarget)
-				}
-				debug('''Updated mapped transition with ID: «trId»''')
-			].action(CRUDActivationStateEnum.DELETED) [
-				val transition = transition
-				val depApp = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstance).filter(
-					DeploymentApplication).head
-				val depTransitions = engine.cps2depTrace.getAllValuesOfdepElement(null, null, transition).filter(
-					BehaviorTransition).toSet
-				val depTransition = engine.depBehaviorsStateAndTransitions.
-					getAllValuesOfdepTransition(depApp.behavior, null).findFirst[depTransitions.contains(it)]
-				val trId = depTransition.description
-				logger.debug('''Removing transition with ID: «trId»''')
-				depTransition.set(behaviorTransition_To, null)
-				val tempDepSources = engine.cps2depTrace.getAllValuesOfdepElement(null, null, srcState)
-				val depSource = depApp.behavior.states.findFirst[tempDepSources.contains(it)]
-				depSource?.remove(behaviorState_Outgoing, depTransition)
-				depApp.behavior.remove(deploymentBehavior_Transitions, depTransition)
-				val smTrace = engine.cps2depTrace.getAllValuesOftrace(null, transition, null).head
-				smTrace.remove(CPS2DeploymentTrace_DeploymentElements, depTransition)
-				if (smTrace.deploymentElements.empty) {
-					trace('''Removing empty trace''')
-					cps2dep.remove(CPSToDeployment_Traces, smTrace)
-				}
-				logger.debug('''Removed transition with ID: «trId»''')
-			].addLifeCycle(Lifecycles.getDefault(true, true)).build
-
-		}
-		return transitionRule
-	}
-
-	public def getTriggerRule() {
-		if (triggerRule === null) {
-			triggerRule = createRule(TriggerPair.instance).name("TriggerRule").action(
-				CRUDActivationStateEnum.CREATED) [
-				val depAppTrigger = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTrigger).
-					filter(DeploymentApplication).head
-				val depAppTarget = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTarget).
-					filter(DeploymentApplication).head
-				val sendTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTrigger).filter(
-					BehaviorTransition).findFirst[depAppTrigger.behavior.transitions.contains(it)]
-				val waitTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTarget).filter(
-					BehaviorTransition).findFirst[depAppTarget.behavior.transitions.contains(it)]
-				debug('''Mapping trigger between «sendTr.description» and «waitTr.description»''')
-				if (!sendTr.trigger.contains(waitTr)) {
-					trace('''Adding new trigger''')
-					sendTr.addTo(behaviorTransition_Trigger, waitTr)
-				}
-				debug('''Mapped trigger between «sendTr.description» and «waitTr.description»''')
-			].action(CRUDActivationStateEnum.DELETED) [
-				val depAppTrigger = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTrigger).
-					filter(DeploymentApplication).head
-				val depAppTarget = engine.cps2depTrace.getAllValuesOfdepElement(null, null, appInstanceTarget).
-					filter(DeploymentApplication).head
-				val sendTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTrigger).filter(
-					BehaviorTransition).findFirst[depAppTrigger.behavior.transitions.contains(it)]
-				val waitTr = engine.cps2depTrace.getAllValuesOfdepElement(null, null, cpsTarget).filter(
-					BehaviorTransition).findFirst[depAppTarget.behavior.transitions.contains(it)]
-				debug('''Removing trigger between «sendTr.description» and «waitTr.description»''')
-				if (sendTr.trigger.contains(waitTr)) {
-					trace('''Removing existing trigger''')
-					sendTr.remove(behaviorTransition_Trigger, waitTr)
-				}
-				debug('''Removed trigger between «sendTr.description» and «waitTr.description»''')
-			].addLifeCycle(Lifecycles.getDefault(true, true)).build
-
-		}
-		return triggerRule
-	}
-}