Browse Source

canonical generator skeleton

Cláudio Gomes 6 years ago
parent
commit
ba33111dd6

+ 69 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/oracles/power_window_case_study/lazy.BASE.sa

@@ -0,0 +1,69 @@
+semantic adaptation reactive moore LazySA lazy_sa_BASE
+at "./path/to/LazySA.fmu"
+
+	for inner fmu Controller controller
+	at "./path/to/Controller.fmu"
+	with input ports obj_detected, passenger_up, passenger_down, passenger_stop, driver_up, driver_down, driver_stop
+	with output ports up, down, stop
+
+input ports obj_detected -> controller.obj_detected,
+			passenger_up -> controller.passenger_up,
+			passenger_down -> controller.passenger_down,
+			passenger_stop -> controller.passenger_stop,
+			driver_up -> controller.driver_up,
+			driver_down -> controller.driver_down,
+			driver_stop -> controller.driver_stop
+
+output ports up, down, stop
+
+param 	INIT_OBJ_DETECTED := false,
+		INIT_PASSENGER_UP := false,
+		INIT_PASSENGER_DOWN := false,
+		INIT_PASSENGER_STOP := false,
+		INIT_DRIVER_UP := false,
+		INIT_DRIVER_DOWN := false,
+		INIT_DRIVER_STOP := false;
+
+control var	tn := -1.0,
+			tl := -1.0,
+			prev_obj_detected := INIT_OBJ_DETECTED,
+			prev_passenger_up := INIT_PASSENGER_UP,
+			prev_passenger_down := INIT_PASSENGER_DOWN,
+			prev_passenger_stop := INIT_PASSENGER_STOP,
+			prev_driver_up := INIT_DRIVER_UP,
+			prev_driver_down := INIT_DRIVER_DOWN,
+			prev_driver_stop := INIT_DRIVER_STOP;
+
+control rules {
+	if (tl < 0.0){
+		tl := t;
+	}
+	
+	var step_size := min(H, tn - t); 
+	if (lazy_sa.obj_detected != prev_obj_detected or
+		lazy_sa.passenger_up != prev_passenger_up or
+		lazy_sa.passenger_down != prev_passenger_down or
+		lazy_sa.passenger_stop != prev_passenger_stop or
+		lazy_sa.driver_up != prev_driver_up or
+		lazy_sa.driver_down != prev_driver_down or
+		lazy_sa.driver_stop != prev_driver_stop or
+		(t+H) >= tn
+	){
+		var step_to_be_done := (t+H-tl);
+		var step_done := do_step(controller, t, step_to_be_done); 
+		tn := tl + step_done + get_next_time_step(controller); 
+		step_size := tl + step_done - t; 
+		tl := tl + step_done; 
+	}
+	
+	prev_obj_detected := lazy_sa.obj_detected;
+	prev_passenger_up := lazy_sa.passenger_up;
+	prev_passenger_down := lazy_sa.passenger_down;
+	prev_passenger_stop := lazy_sa.passenger_stop;
+	prev_driver_up := lazy_sa.driver_up;
+	prev_driver_down := lazy_sa.driver_down;
+	prev_driver_stop := lazy_sa.driver_stop;
+	
+	return step_size;
+}
+

+ 1 - 1
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/AbstractSemanticAdaptationTest.xtend

@@ -61,7 +61,7 @@ abstract class AbstractSemanticAdaptationTest {
 		visited.add(element.hashCode)
 		element.eClass.EAllStructuralFeatures.forEach[f | 
 			val feature = element.eGet(f)
-			if (feature != null) {
+			if (feature !== null) {
 				if (feature instanceof EList<?>) {
 					(feature as EObjectEList<?>).forEach[e | 
 						print(ind(indent+1) + f.name + "(" + feature.indexOf(e) + "): ")

+ 0 - 2
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/BasicParserTest.xtend

@@ -9,10 +9,8 @@ import org.eclipse.xtext.testing.validation.ValidationTestHelper
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
 import java.io.File
 import java.util.List
-import org.eclipse.xtext.util.Arrays
 import org.eclipse.xtext.validation.Issue
 import com.google.common.base.Predicate
-import static org.junit.Assert.fail
 import static com.google.common.collect.Iterables.isEmpty
 import static com.google.common.collect.Iterables.filter
 import org.eclipse.xtext.diagnostics.Severity

+ 12 - 18
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.tests/src/be/uantwerpen/ansymo/semanticadaptation/tests/SemanticAdaptationGeneratorTest.xtend

@@ -3,14 +3,14 @@
  */
 package be.uantwerpen.ansymo.semanticadaptation.tests
 
-import org.junit.Test
 import com.google.inject.Inject
-import org.junit.runner.RunWith
-import java.io.File
-import org.eclipse.xtext.testing.XtextRunner
 import org.eclipse.xtext.testing.InjectWith
+import org.eclipse.xtext.testing.XtextRunner
+import org.eclipse.xtext.util.IAcceptor
 import org.eclipse.xtext.xbase.testing.CompilationTestHelper
-import org.junit.Ignore
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.eclipse.xtext.xbase.testing.CompilationTestHelper.Result
 
 @RunWith(XtextRunner)
 @InjectWith(SemanticAdaptationInjectorProvider)
@@ -18,20 +18,14 @@ class SemanticAdaptationGeneratorTest extends AbstractSemanticAdaptationTest{
 	
 	@Inject extension CompilationTestHelper
 	
-	@Ignore @Test def powerwindow_model_only() { __generate('input/powerwindow_model_only.sa') }
-	@Ignore @Test def powerwindow_algebraic_loop_delay_BASE() { __generate('input/powerwindow_algebraic_loop_delay_BASE.sa') }
-	@Ignore @Test def powerwindow_algebraic_loop_delay() { __generate('input/powerwindow_algebraic_loop_delay.sa') }
-	@Ignore @Test def powerwindow_algebraic_loop_iteration_BASE() { __generate('input/powerwindow_algebraic_loop_iteration_BASE.sa') }
-	@Ignore @Test def powerwindow_algebraic_loop_iteration() { __generate('input/powerwindow_algebraic_loop_iteration.sa') }
-	@Ignore @Test def powerwindow_controller_delay() { __generate('input/powerwindow_controller_delay.sa') }
-	@Ignore @Test def powerwindow_controller_delay_BASE() { __generate('input/powerwindow_controller_delay_BASE.sa') }
-	@Ignore @Test def powerwindow_multi_rate() { __generate('input/powerwindow_multi_rate.sa') }
-	@Ignore @Test def powerwindow_multi_rate_BASE() { __generate('input/powerwindow_multi_rate_BASE.sa') }
-	@Ignore @Test def powerwindow() { __generate('input/powerwindow.sa') }
+	@Test def powerwindow_model_only() { __generate('input/power_window_case_study/lazy.sa') }
 	
 	def void __generate(String filename) {
-		val f = new File(filename)
-		readFile(filename).assertCompilesTo('oracles/test1.txt'/*readFile('oracles/' + f.name)*/)
+		//readFile(filename).assertCompilesTo('oracles/power_window_case_study/lazy.BASE.sa')
+		
+		readFile(filename).compile(new IAcceptor<CompilationTestHelper.Result>(){
+			override accept(Result t) { }
+		})
+		
 	}
-
 }

+ 2 - 2
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/src/be/uantwerpen/ansymo/semanticadaptation/SemanticAdaptationRuntimeModule.xtend

@@ -5,7 +5,7 @@ package be.uantwerpen.ansymo.semanticadaptation
 
 import be.uantwerpen.ansymo.semanticadaptation.valueconverter.SemanticAdaptationValueConverters
 import be.uantwerpen.ansymo.semanticadaptation.scoping.SemanticAdaptationScopeProvider
-import be.uantwerpen.ansymo.semanticadaptation.generator.SemanticAdaptationPythonGenerator
+import be.uantwerpen.ansymo.semanticadaptation.generator.SemanticAdaptationCanonicalGenerator
 
 /**
  * Use this class to register components to be used at runtime / without the Equinox extension registry.
@@ -19,6 +19,6 @@ class SemanticAdaptationRuntimeModule extends AbstractSemanticAdaptationRuntimeM
 		return SemanticAdaptationScopeProvider
 	}
 	override bindIGenerator2() {
-		return SemanticAdaptationPythonGenerator
+		return SemanticAdaptationCanonicalGenerator
 	}
 }

+ 81 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/src/be/uantwerpen/ansymo/semanticadaptation/generator/SemanticAdaptationCanonicalGenerator.xtend

@@ -0,0 +1,81 @@
+/*
+ * generated by Xtext 2.10.0
+ */
+package be.uantwerpen.ansymo.semanticadaptation.generator
+
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
+import java.io.ByteArrayOutputStream
+import org.eclipse.emf.ecore.resource.Resource
+import org.eclipse.xtext.generator.AbstractGenerator
+import org.eclipse.xtext.generator.IFileSystemAccess2
+import org.eclipse.xtext.generator.IGeneratorContext
+
+/**
+ * Generates code from your model files on save.
+ * 
+ * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
+ */
+class SemanticAdaptationCanonicalGenerator extends AbstractGenerator {
+	
+	String CANONICAL_EXT = ".BASE.sa"
+	String NAME_SUFFIX = "_BASE"
+	
+	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
+		println("Generating canonical semantic adaptation for file " + resource.URI.toFileString() + "...")
+		
+		println("Resource URI information:")
+		println("\t resource.URI.lastSegment = " + resource.URI.lastSegment())
+		println("\t resource.URI.trimFileExtension = " + resource.URI.trimFileExtension())
+		
+		println("______________________________File Read______________________________")
+		var outputByteArray = new ByteArrayOutputStream()
+		resource.save(outputByteArray, null)
+		println(outputByteArray.toString())
+		outputByteArray.close()
+		println("__________________________________________________________________________")
+		
+		// Create file name for the canonical sa file
+		var fileNameWithoutExt = resource.URI.trimFileExtension().lastSegment()
+		var canonicalFileName = fileNameWithoutExt + CANONICAL_EXT
+		println("canonicalFileName = " + canonicalFileName)
+		
+		// Create in memory representation of canonical SA file
+		var adaptations = resource.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation);
+		if (adaptations.size > 1){
+			throw new Exception("Only one semantic adaptation is supported per .sa file")
+		}
+		var adaptation = adaptations.head
+		
+		println("Checking if file is already a canonical version...")
+		if (adaptation.name.indexOf(NAME_SUFFIX) == -1){
+			println("It is not.")
+			
+			adaptation.name = adaptation.name + NAME_SUFFIX
+			
+			adaptation.canonicalize
+						
+			outputByteArray = new ByteArrayOutputStream()
+			adaptation.eResource.save(outputByteArray,null)
+			
+			println("______________________________Generated file______________________________")
+			println(outputByteArray.toString())
+			println("__________________________________________________________________________")
+			
+			fsa.generateFile(canonicalFileName, outputByteArray.toString())
+			println("File " + canonicalFileName + " written.")
+			outputByteArray.close()
+			
+			println("Generating canonical semantic adaptation for file " + resource.URI + "... DONE.")
+			
+		} else {
+			println("It is already a canonical version.")
+			println("Nothing to do.")
+		}
+
+	}
+	
+	def canonicalize(Adaptation sa){
+		
+	}
+}

+ 4 - 5
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation/src/be/uantwerpen/ansymo/semanticadaptation/generator/SemanticAdaptationPythonGenerator.xtend

@@ -4,7 +4,6 @@
 package be.uantwerpen.ansymo.semanticadaptation.generator
 
 import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.Adaptation
-import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
 import org.eclipse.emf.ecore.resource.Resource
 import org.eclipse.xtext.generator.AbstractGenerator
 import org.eclipse.xtext.generator.IFileSystemAccess2
@@ -20,10 +19,10 @@ class SemanticAdaptationPythonGenerator extends AbstractGenerator {
 	public final static String FILENAME = 'output.test.txt'
 
 	override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
-		for (Adaptation a : resource.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation)) {
-			fsa.generateFile(FILENAME, a.compile)
-			println(fsa.readTextFile(FILENAME))
-		}
+//		for (Adaptation a : resource.allContents.toIterable.filter(SemanticAdaptation).last.elements.filter(Adaptation)) {
+//			fsa.generateFile(FILENAME, a.compile)
+//			println(fsa.readTextFile(FILENAME))
+//		}
 	}
 	
 	def compile(Adaptation a) '''