package ua.ansymo.hintco.test

import org.junit.Test
import ua.ansymo.hintco.ModelStorage
import ua.ansymo.hintco.CosimUnitInstance
import ua.ansymo.hintco.HierarchicalUnitPort
import ua.ansymo.hintco.InputPortInstance
import ua.ansymo.hintco.InterpolationAdaptation
import ua.ansymo.hintco.MultiRateAdaptation
import ua.ansymo.hintco.PortInstance
import ua.ansymo.hintco.PowerBondAdaptation

import static org.junit.Assert.*
import static ua.ansymo.hintco.ModelQuery.*

class ModelTest {
	
	val loader = new ModelStorage()
	
	@Test
	def void getAdaptedTest(){
		val src = loader.loadCandidates("instances/model_test.xmi")
		
		val plant = findIded("EMAPlantNoLoad_FixedEuler_1Em6", src) as CosimUnitInstance
		assertTrue(plant.selectedAdaptations().head instanceof MultiRateAdaptation)
		assertTrue((plant.selectedAdaptations().head as MultiRateAdaptation).rate == 5)
		
		val torque = findIded("torque_input@expseu_", src) as InputPortInstance
		assertTrue(torque.selectedAdaptations().head instanceof InterpolationAdaptation)
	}
	
	@Test
	def void getDualTest(){
		val src = loader.loadCandidates("instances/case_study_double_loop_pbond.hintco")
		
		val plant = findIded("EMAPlantNoLoad_FixedEuler_1Em6", src) as CosimUnitInstance
		
		val sensor = findIded("LoadNSensor_FixedEuler_1Em6", src) as CosimUnitInstance
		
		assertTrue((plant.adaptation as PowerBondAdaptation).dual === sensor.adaptation)
		assertTrue((sensor.adaptation as PowerBondAdaptation).dual === plant.adaptation)
	}
	
	
	@Test
	def void isInputTest(){
		val src = loader.loadCandidates("instances/add_constraints_test.hintco")
		
		assertTrue((findIded("Bip", src) as HierarchicalUnitPort).input)
		assertTrue((findIded("B2ip2", src) as HierarchicalUnitPort).input)
		assertFalse((findIded("B2op", src) as HierarchicalUnitPort).input)
		assertFalse((findIded("Bop", src) as HierarchicalUnitPort).input)
	}
	
	@Test
	def void findSmallestScenarioContainingPortsTest(){
		val src = loader.loadCandidates("instances/add_constraints_test.hintco")
		
		var res = findLeastAncestorScenarioTriple(
								findIded("Aop", src) as PortInstance, 	
								findIded("Cip", src) as PortInstance
							)
		assertEquals(findIded("R", src),res.get(0))
		assertEquals(findIded("A", src),res.get(1))
		assertEquals(findIded("C", src),res.get(2))
		
		res = findLeastAncestorScenarioTriple(
								findIded("Aop", src) as PortInstance, 	
								findIded("B21ip1", src) as PortInstance
							)
		assertEquals(findIded("R", src),res.get(0))
		assertEquals(findIded("A", src),res.get(1))
		assertEquals(findIded("B", src),res.get(2))
		
		res = findLeastAncestorScenarioTriple(
								findIded("B21ip1", src) as PortInstance, 	
								findIded("Aop", src) as PortInstance
							)
		assertEquals(findIded("R", src),res.get(0))
		assertEquals(findIded("B", src),res.get(1))
		assertEquals(findIded("A", src),res.get(2))
		
		res = findLeastAncestorScenarioTriple(
								findIded("B21ip1", src) as PortInstance, 	
								findIded("B1op2", src) as PortInstance
							)
		assertEquals(findIded("B", src),res.get(0))
		assertEquals(findIded("B2", src),res.get(1))
		assertEquals(findIded("B1", src),res.get(2))
	}
	
}