瀏覽代碼

[YAKHMI-1105] added wrangling tests. Testing for same shortnames from executionFlow and statechart in DefaultNamingService, added getter and setter for separator and max length.

malknet42 11 年之前
父節點
當前提交
3fee2a0d7d

+ 59 - 25
plugins/org.yakindu.sct.model.sexec/src/org/yakindu/sct/model/sexec/naming/DefaultNamingService.xtend

@@ -1,28 +1,28 @@
 package org.yakindu.sct.model.sexec.naming
 
+import java.util.Comparator
+import java.util.HashMap
+import java.util.List
+import java.util.Map
 import javax.inject.Inject
+import org.eclipse.emf.ecore.EObject
+import org.eclipse.xtext.naming.IQualifiedNameProvider
 import org.yakindu.base.base.NamedElement
-import java.util.Map
 import org.yakindu.sct.model.sexec.ExecutionFlow
-import java.util.HashMap
-import org.yakindu.sct.model.sexec.extensions.SExecExtensions
-import java.util.Comparator
-import org.yakindu.sct.model.sexec.Step
+import org.yakindu.sct.model.sexec.ExecutionNode
 import org.yakindu.sct.model.sexec.ExecutionScope
-import org.yakindu.sct.model.sexec.transformation.StatechartExtensions
-import org.eclipse.xtext.naming.IQualifiedNameProvider
 import org.yakindu.sct.model.sexec.ExecutionState
+import org.yakindu.sct.model.sexec.Step
+import org.yakindu.sct.model.sexec.extensions.SExecExtensions
+import org.yakindu.sct.model.sexec.transformation.SgraphExtensions
+import org.yakindu.sct.model.sexec.transformation.StatechartExtensions
+import org.yakindu.sct.model.sgraph.CompositeElement
+import org.yakindu.sct.model.sgraph.Region
 import org.yakindu.sct.model.sgraph.State
-import org.yakindu.sct.model.stext.stext.TimeEventSpec
 import org.yakindu.sct.model.sgraph.Statechart
-import org.yakindu.sct.model.sgraph.CompositeElement
 import org.yakindu.sct.model.sgraph.Vertex
-import org.yakindu.sct.model.sexec.transformation.SgraphExtensions
-import org.yakindu.sct.model.sexec.ExecutionNode
-import org.eclipse.emf.ecore.EObject
-import java.util.List
-import org.yakindu.sct.model.sgraph.Region
 import org.yakindu.sct.model.stext.naming.StextNameProvider
+import org.yakindu.sct.model.stext.stext.TimeEventSpec
 
 class StepDepthComparator implements Comparator<Step> {
 	@Inject
@@ -58,8 +58,11 @@ class DefaultNamingService implements INamingService {
 	@Inject extension StepDepthComparator stepDepthComparator
 	@Inject extension ExecutionScopeDepthComparator executionScopeDepthComparator
 	@Inject extension NamingHelper
-	
+
 	@Inject private StextNameProvider provider
+	
+	// from public class org.yakindu.sct.generator.c.features.CDefaultFeatureValueProvider extends		
+	private static final String VALID_IDENTIFIER_REGEX = "[_a-zA-Z][_a-zA-Z0-9]*";	
 
 	var protected int maxLength = 0
 
@@ -288,6 +291,7 @@ class DefaultNamingService implements INamingService {
 				shortName = element.fqElementName(separator).asIdentifier.getHash(maxLength)
 			}
 		} else {
+
 			//use full qualified name
 			shortName = element.createShortName(prefix, suffix, nameList, NameShorteningStrategy::FQN_NAME, separator)
 		}
@@ -347,7 +351,8 @@ class DefaultNamingService implements INamingService {
 
 	def protected dispatch String elementName(NamedElement it, NameShorteningStrategy nameShorteningType) {
 		switch nameShorteningType {
-			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(1).toString(separator.toString)
+			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(1).
+				toString(separator.toString)
 			case NameShorteningStrategy::SHORT_NAME: return name
 			case NameShorteningStrategy::REMOVE_VOWELS: return name?.removeVowels
 			case NameShorteningStrategy::INDEX_POSITION: return name?.removeVowels
@@ -356,7 +361,8 @@ class DefaultNamingService implements INamingService {
 
 	def protected dispatch String elementName(ExecutionScope it, NameShorteningStrategy nameShorteningType) {
 		switch nameShorteningType {
-			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(2).toString(separator.toString)
+			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(2).
+				toString(separator.toString)
 			case NameShorteningStrategy::SHORT_NAME: return name
 			case NameShorteningStrategy::REMOVE_VOWELS: return name?.removeVowels
 			case NameShorteningStrategy::INDEX_POSITION: return asIndexPosition
@@ -365,7 +371,8 @@ class DefaultNamingService implements INamingService {
 
 	def protected dispatch String elementName(ExecutionState it, NameShorteningStrategy nameShorteningType) {
 		switch nameShorteningType {
-			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(2).toString(separator.toString)
+			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(2).
+				toString(separator.toString)
 			case NameShorteningStrategy::SHORT_NAME: return simpleName
 			case NameShorteningStrategy::REMOVE_VOWELS: return simpleName.removeVowels
 			case NameShorteningStrategy::INDEX_POSITION: return asIndexPosition
@@ -374,30 +381,35 @@ class DefaultNamingService implements INamingService {
 
 	def protected dispatch String elementName(ExecutionNode it, NameShorteningStrategy nameShorteningType) {
 		switch nameShorteningType {
-			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(2).toString(separator.toString)
+			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(2).
+				toString(separator.toString)
 			case NameShorteningStrategy::SHORT_NAME: return simpleName
 			case NameShorteningStrategy::REMOVE_VOWELS: return simpleName
 			case NameShorteningStrategy::INDEX_POSITION: return simpleName
 		}
 	}
-	
+
 	def protected dispatch String elementName(Region it, NameShorteningStrategy nameShorteningType) {
 		switch nameShorteningType {
-			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(1).toString(separator.toString)
+			case NameShorteningStrategy::FQN_NAME:
+				return provider.getFullyQualifiedName(it).skipFirst(1).toString(separator.toString)
 			case NameShorteningStrategy::SHORT_NAME: {
-				if (name.nullOrEmpty) {		
+				if (name.nullOrEmpty) {
 					return provider.getFullyQualifiedName(it).lastSegment.toString.substring(1)
 				}
 				return name
 			}
-			case NameShorteningStrategy::REMOVE_VOWELS: return name?.removeVowels
-			case NameShorteningStrategy::INDEX_POSITION: return asSGraphIndexPosition
+			case NameShorteningStrategy::REMOVE_VOWELS:
+				return name?.removeVowels
+			case NameShorteningStrategy::INDEX_POSITION:
+				return asSGraphIndexPosition
 		}
 	}
 
 	def protected dispatch String elementName(Vertex it, NameShorteningStrategy nameShorteningType) {
 		switch nameShorteningType {
-			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(1).toString(separator.toString)
+			case NameShorteningStrategy::FQN_NAME: return provider.getFullyQualifiedName(it).skipFirst(1).
+				toString(separator.toString)
 			case NameShorteningStrategy::SHORT_NAME: return name
 			case NameShorteningStrategy::REMOVE_VOWELS: return name?.removeVowels
 			case NameShorteningStrategy::INDEX_POSITION: return asSGraphIndexPosition
@@ -435,4 +447,26 @@ class DefaultNamingService implements INamingService {
 	def protected removeVowels(String it) {
 		replaceAll('[aeiou]', '')
 	}
+
+	override public setMaxLength(int length) {
+		maxLength = length
+	}
+
+	override public setSeparator(char sep) {
+		// Check if Prefix is ok		
+		var String sepString =  sep+""		
+		if(!(sepString.matches(VALID_IDENTIFIER_REGEX))){
+			throw new IllegalArgumentException
+		}
+		separator = sep
+	}
+
+	override public getMaxLength() {
+		return maxLength
+	}
+
+	override public getSeparator() {
+		return separator
+	}
+
 }

+ 31 - 0
plugins/org.yakindu.sct.model.sexec/src/org/yakindu/sct/model/sexec/naming/INamingService.java

@@ -76,4 +76,35 @@ public interface INamingService {
 	 * @return
 	 */
 	boolean isKeyword(String string);
+	
+	/**
+	 * Sets the maximum length for shorted names. 
+	 * 
+	 * @param int
+	 */
+	void setMaxLength (int length);
+	
+	/**
+	 * Sets the separator for shorted names.
+	 * 
+	 * @param char
+	 */
+	void setSeparator(char sep);
+	
+	
+	/**
+	 * Returns the maximum length of shorted names.
+	 * 
+	 * @return int
+	 */
+	int getMaxLength();
+	
+	/**
+	 * Returns the separator for shorted names. 
+	 * 
+	 * @return char 
+	 */
+	char getSeparator();
+	
+	
 }

+ 1 - 0
test-plugins/org.yakindu.sct.model.sexec.test/src/org/yakindu/sct/model/sexec/transformation/test/AllTests.java

@@ -6,6 +6,7 @@ import org.junit.runners.Suite.SuiteClasses;
 
 @RunWith(Suite.class)
 @SuiteClasses({ 
+		DefaultNamingServiceTest.class,
 		FlowOptimizer_ExecutionEntryTest.class, 
 		HistoryTest.class,
 		LocalReactionTest.class,

+ 218 - 16
test-plugins/org.yakindu.sct.model.sexec.test/src/org/yakindu/sct/model/sexec/transformation/test/DefaultNamingServiceTest.java

@@ -6,33 +6,39 @@ import static org.junit.Assert.fail;
 import java.util.Collections;
 import java.util.List;
 
-import org.junit.Ignore;
+import org.junit.Before;
 import org.junit.Test;
 import org.yakindu.sct.model.sexec.ExecutionFlow;
 import org.yakindu.sct.model.sexec.ExecutionState;
 import org.yakindu.sct.model.sexec.naming.INamingService;
+import org.yakindu.sct.model.sexec.transformation.FlowOptimizer;
 import org.yakindu.sct.model.sgraph.State;
 import org.yakindu.sct.model.sgraph.Statechart;
 import org.yakindu.sct.test.models.SCTUnitTestModels;
 
 import com.google.inject.Inject;
 
+
 public class DefaultNamingServiceTest extends ModelSequencerTest {
 
 	@Inject
 	protected SCTUnitTestModels testModels;
 
+	@Inject
+	FlowOptimizer optimizer;
+
 	@Inject
 	protected INamingService statechartNamingService;
 
 	@Inject
 	protected INamingService flowNamingService;
-	
-	@Ignore
-	@Test
-	public void testDefaultNamingServiceState_NameMatch() {
 
-		List<Statechart> statecharts = Collections.emptyList();
+	private List<Statechart> statecharts;
+
+	@Before
+	public void setupNamingService() {
+
+		statecharts = Collections.emptyList();
 
 		try {
 			statecharts = testModels.loadAllStatecharts();
@@ -40,24 +46,220 @@ public class DefaultNamingServiceTest extends ModelSequencerTest {
 			fail(e.getMessage());
 		}
 
+		optimizer.inlineReactions(true);
+		optimizer.inlineExitActions(true);
+		optimizer.inlineEntryActions(true);
+		optimizer.inlineEnterSequences(true);
+		optimizer.inlineExitSequences(true);
+		optimizer.inlineChoices(true);
+		optimizer.inlineEntries(true);
+		optimizer.inlineEnterRegion(true);
+		optimizer.inlineExitRegion(true);
+
+		// TODO: Why does PerformanceTest doesn't work?
+		Statechart statecharttoRemove = null;
+		for (Statechart sct : statecharts) {
+			if (sct.getName().equals("PerformanceTest")) {
+				statecharttoRemove = sct;
+			}
+		}
+		statecharts.remove(statecharttoRemove);
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_0() {
+
 		for (Statechart statechart : statecharts) {
+
 			// Transform statechart
 			ExecutionFlow flow = sequencer.transform(statechart);
-			if (statecharts.indexOf(statechart) == 4) {
-				System.out.println();
-			}
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(0);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(0);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_31() {
+
+		for (Statechart statechart : statecharts) {
+
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(31);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(31);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_15() {
+
+		for (Statechart statechart : statecharts) {
+
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(15);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(15);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_7() {
+
+		for (Statechart statechart : statecharts) {
+
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(7);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(7);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_4() {
+
+		for (Statechart statechart : statecharts) {
+
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(4);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(4);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_3() {
+
+		for (Statechart statechart : statecharts) {
+
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(3);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(3);
+			flowNamingService.setSeparator('_');
+
 			// Initialize naming services for statechart and ExecutionFlow
 			statechartNamingService.initializeNamingService(statechart);
 			flowNamingService.initializeNamingService(flow);
 
-			for (ExecutionState state : flow.getStates()) {
-				if (state.getSourceElement() instanceof State) {
-					assertEquals(flowNamingService.getShortName(state),
-							statechartNamingService
-									.getShortName((State) state
-											.getSourceElement()));
-				}
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_2() {
+
+		for (Statechart statechart : statecharts) {
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(2);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(2);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	@Test
+	public void testDefaultNamingServiceState_NameMatch_1() {
+
+		for (Statechart statechart : statecharts) {
+			// Transform statechart
+			ExecutionFlow flow = sequencer.transform(statechart);
+			flow = optimizer.transform(flow);
+
+			statechartNamingService.setMaxLength(1);
+			statechartNamingService.setSeparator('_');
+			flowNamingService.setMaxLength(1);
+			flowNamingService.setSeparator('_');
+
+			// Initialize naming services for statechart and ExecutionFlow
+			statechartNamingService.initializeNamingService(statechart);
+			flowNamingService.initializeNamingService(flow);
+
+			// Check for equality
+			checkNameEquality(flow, statechartNamingService, flowNamingService);
+		}
+	}
+
+	private void checkNameEquality(ExecutionFlow flow,
+			INamingService statechartNamingService,
+			INamingService flowNamingService) {
+
+		for (ExecutionState state : flow.getStates()) {
+			if (state.getSourceElement() instanceof State) {
+				assertEquals(flowNamingService.getShortName(state),
+						statechartNamingService.getShortName((State) state
+								.getSourceElement()));
+
 			}
 		}
 	}
+
 }

+ 14 - 1
test-plugins/org.yakindu.sct.test.models/src/org/yakindu/sct/test/models/SCTUnitTestModels.java

@@ -78,7 +78,9 @@ public class SCTUnitTestModels extends AbstractTestModelsUtil {
 	public static final String TRANSITIONS_WITHOUT_CONDITION = "TransitionWithoutCondition.sct";
 	public static final String VALUED_EVENTS = "ValuedEvents.sct";
 	public static final String VARIABLE_INITIALIZATION = "VariableInitialization.sct";
+	public static final String ZBRIDGE_SERVER_SYNC = "ZBridgeServerSync.sct";
 
+	
 	@Override
 	public String getModelDirectory() {
 		return TESTMODEL_DIR;
@@ -104,5 +106,16 @@ public class SCTUnitTestModels extends AbstractTestModelsUtil {
 			}
 		});
 	}
-
+	
+	public Statechart loadStatechartByFilename(String requestedStatechart) throws Exception {
+		Statechart result = null;		
+		Field[] fields = getClass().getFields();
+		for (Field field : fields) {			
+			String value = (String) field.get(this);
+			if (value.endsWith(requestedStatechart)) {
+				result  = (loadStatechartFromResource(value));
+			}			
+		}
+		return result;
+	}
 }