Browse Source

Integrate naming service into C

René Beckmann 7 years ago
parent
commit
f053dd4ad6

+ 2 - 1
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/CCodeGeneratorModule.java

@@ -32,6 +32,7 @@ import org.yakindu.sct.model.sgen.GeneratorEntry;
 import org.yakindu.sct.model.sgraph.Statechart;
 
 import com.google.inject.Binder;
+import com.google.inject.Singleton;
 import com.google.inject.name.Names;
 /**
  * 
@@ -44,7 +45,7 @@ public class CCodeGeneratorModule implements IGeneratorModule {
 	public void configure(GeneratorEntry entry, Binder binder) {
 		binder.bind(GeneratorEntry.class).toInstance(entry);
 		binder.bind(IExecutionFlowGenerator.class).to(CGenerator.class);
-		binder.bind(INamingService.class).to(CNamingService.class);
+		binder.bind(INamingService.class).to(CTreeNamingService.class).in(Singleton.class);
 		binder.bind(ICodegenTypeSystemAccess.class).to(CTypeSystemAccess.class);
 		binder.bind(IncludeProvider.class).to(StandardIncludeProvider.class);
 		bindIGenArtifactConfigurations(entry, binder);

+ 145 - 0
plugins/org.yakindu.sct.generator.c/src/org/yakindu/sct/generator/c/CTreeNamingService.xtend

@@ -0,0 +1,145 @@
+/**
+  Copyright (c) 2014-2015 committers of YAKINDU Statechart Tools.
+  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:
+  	Markus Mühlbrandt - Initial contribution and API
+*/
+
+package org.yakindu.sct.generator.c
+
+import com.google.inject.Inject
+import java.util.Arrays
+import java.util.Map
+import org.yakindu.base.base.NamedElement
+import org.yakindu.sct.generator.c.extensions.GenmodelEntries
+import org.yakindu.sct.model.sexec.ExecutionFlow
+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.naming.tree.TreeNamingService
+import org.yakindu.sct.model.sgen.GeneratorEntry
+import org.yakindu.sct.model.sgraph.State
+import org.yakindu.sct.model.sgraph.Statechart
+import org.yakindu.sct.model.stext.stext.TimeEventSpec
+
+import static org.yakindu.sct.generator.c.CKeywords.*
+
+public class CTreeNamingService extends TreeNamingService {
+	
+	@Inject
+	extension GenmodelEntries
+	@Inject
+	extension SExecExtensions
+	@Inject
+	var GeneratorEntry entry
+	
+	override void initializeNamingService(Statechart statechart) {
+		if (entry.identifierLength !== null) {
+			setMaxLength(entry.identifierLength)
+		}
+		
+		if (entry.separator !== null) {
+			setSeparator(entry.separator.charAt(0))
+		}
+		
+		super.initializeNamingService(statechart)
+	}
+	
+	override void initializeNamingService(ExecutionFlow flow) {
+		if (entry.identifierLength !== null) {
+			setMaxLength(entry.identifierLength)
+		}
+		
+		if (entry.separator !== null) {
+			setSeparator(entry.separator.charAt(0))
+		}
+		
+		super.initializeNamingService(flow)
+	}
+	
+	override Map<NamedElement, String> getShortNameMap(ExecutionFlow flow) {
+		if (entry.identifierLength !== null) {
+			setMaxLength(entry.identifierLength)
+		}
+		
+		if (entry.separator !== null) {
+			setSeparator(entry.separator.charAt(0))
+		}
+		
+		return super.getShortNameMap(flow)
+	}
+	
+	override Map<NamedElement, String> getShortNameMap(Statechart statechart) {
+		if (entry.identifierLength !== null) {
+			setMaxLength(entry.identifierLength)
+		}
+		
+		if (entry.separator !== null) {
+			setSeparator(entry.separator.charAt(0))
+		}
+		
+		return super.getShortNameMap(statechart)
+	}
+	
+	override protected prefix(Step it) {
+		val prefix = newArrayList
+		if (entry.statemachinePrefix != null) {
+			prefix.add(entry.statemachinePrefix)
+		} else {
+			prefix.add(flow.name.toFirstLower)
+		}
+		prefix.add(switch (it) {
+			case isCheckFunction:"check"
+			case isEntryAction: "enact"
+			case isExitAction: "exact"
+			case isEffect: "effect"
+			case isEnterSequence: "enseq"
+			case isDeepEnterSequence: "dhenseq"
+			case isShallowEnterSequence: "shenseq"
+			case isExitSequence: "exseq"
+			case isReactSequence: "react"
+			default: ""
+		})
+		return prefix
+	}
+	
+	override protected prefix(ExecutionState it) {
+		if (entry.statemachinePrefix.nullOrEmpty) {
+			#[super.prefix(it).head?.toFirstUpper]			
+		} else {
+			#[entry.statemachinePrefix]
+		}
+	}
+	
+	override protected prefix(State it) {
+		if (entry.statemachinePrefix.nullOrEmpty) {
+			super.prefix(it).toFirstUpper
+		} else {
+			#[entry.statemachinePrefix]
+		}
+	}
+	
+	override protected prefix(TimeEventSpec it, NamedElement element) {
+		if (entry.statemachinePrefix.nullOrEmpty) {
+			super.prefix(it, element).toFirstUpper
+		} else {
+			#[entry.statemachinePrefix]
+		}
+	}
+	
+	override asEscapedIdentifier(String it) {
+		var s = it
+		if (s.isKeyword) {
+			s = s + separator +'ID'
+		}
+		return s.asIdentifier
+	}
+	
+	override boolean isKeyword(String name) {
+		return !Arrays::asList(C_KEYWORDS).findFirst[it.equalsIgnoreCase(name)].nullOrEmpty
+	}
+}

+ 1 - 1
plugins/org.yakindu.sct.model.sexec/src/org/yakindu/sct/model/sexec/naming/tree/StringTreeNode.xtend

@@ -79,7 +79,7 @@ class StringTreeNode {
 			return newNode;
 		}
 
-		var firstString = sList.get(0); // first element to search in own children. If not found, create new one.
+		var firstString = sList.get(0) ?: ""; // first element to search in own children. If not found, create new one.
 		var rest = sList.subList(1, sList.size()); // the rest of the array that the child's addStringList function is called with
 
 		for (child : children) // search for child that fits

+ 28 - 9
plugins/org.yakindu.sct.model.sexec/src/org/yakindu/sct/model/sexec/naming/tree/TreeNamingService.xtend

@@ -33,6 +33,7 @@ import org.yakindu.sct.model.sgraph.State
 import org.yakindu.sct.model.sgraph.Statechart
 import org.yakindu.sct.model.sgraph.Vertex
 import org.yakindu.sct.model.stext.stext.TimeEventSpec
+import com.google.inject.Singleton
 
 /** New implementation of the naming service for various identifiers used in the generated code. 
  * It is responsible for identifier construction depending on the thing to be named including different strategies 
@@ -243,7 +244,7 @@ class TreeNamingService implements INamingService {
 		return map
 	}
 
-	def protected suffix(Step it) {
+	def protected List<String> suffix(Step it) {
 		var l = new ArrayList<String>();
 
 		switch (it) {
@@ -281,27 +282,27 @@ class TreeNamingService implements INamingService {
 		return l;
 	}
 
-	def protected prefix(Step it) {
+	def protected List<String> prefix(Step it) {
 		return new ArrayList<String>();
 	}
 
-	def protected prefix(ExecutionState it) {
+	def protected List<String> prefix(ExecutionState it) {
 		var l = new ArrayList<String>();
 		// l.add(flow.name);
 		return l;
 	}
 
-	def protected suffix(ExecutionState it) {
+	def protected List<String> suffix(ExecutionState it) {
 		return new ArrayList<String>();
 	}
 
-	def protected prefix(TimeEventSpec it, NamedElement element) {
+	def protected List<String> prefix(TimeEventSpec it, NamedElement element) {
 		var l = new ArrayList<String>();
 		// l.add(activeFlow.name);
 		return l;
 	}
 
-	def protected suffix(TimeEventSpec it, NamedElement element) {
+	def protected List<String> suffix(TimeEventSpec it, NamedElement element) {
 		var l = new ArrayList<String>();
 		switch (element) {
 			Statechart: {
@@ -314,17 +315,17 @@ class TreeNamingService implements INamingService {
 		return l;
 	}
 
-	def protected prefix(State it) {
+	def protected List<String> prefix(State it) {
 		var l = new ArrayList<String>();
 		// l.add(activeStatechart.name);
 		return l;
 	}
 
-	def protected prefix(Vertex it) {
+	def protected List<String> prefix(Vertex it) {
 		return new ArrayList<String>();
 	}
 
-	def protected suffix(Vertex it) {
+	def protected List<String> suffix(Vertex it) {
 		return new ArrayList<String>();
 	}
 	
@@ -340,4 +341,22 @@ class TreeNamingService implements INamingService {
 		}
 		result
 	}
+	
+	def protected List<String> toFirstUpper(List<String> segments) {
+		val result = newArrayList
+		if(segments.nullOrEmpty) {
+			return result
+		}
+		else {
+			for(var i = 0; i < segments.size(); i++) {
+				if(i == 0) {
+					result.add(segments.get(i).toFirstUpper)
+				} else {
+					result.add(segments.get(i))
+				}
+			}
+		}
+		
+		return result
+	}
 }