Browse Source

added watertank basic

Kenneth Lausdahl 4 years ago
parent
commit
1336d6943f

+ 110 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/src/be/uantwerpen/ansymo/semanticadaptation/cg/cpp/tests/CgCppModuleTest.xtend

@@ -0,0 +1,110 @@
+/*
+ * generated by Xtext 2.10.0
+ */
+package be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests
+
+import be.uantwerpen.ansymo.semanticadaptation.cg.cpp.CppGenerator
+import be.uantwerpen.ansymo.semanticadaptation.semanticAdaptation.SemanticAdaptation
+import be.uantwerpen.ansymo.semanticadaptation.tests.AbstractSemanticAdaptationTest
+import be.uantwerpen.ansymo.semanticadaptation.tests.SemanticAdaptationInjectorProvider
+import com.google.inject.Inject
+import java.io.File
+import java.util.regex.Pattern
+import org.eclipse.emf.ecore.EObject
+import org.eclipse.emf.ecore.resource.ResourceSet
+import org.eclipse.xtext.generator.IGeneratorContext
+import org.eclipse.xtext.generator.InMemoryFileSystemAccess
+import org.eclipse.xtext.testing.InjectWith
+import org.eclipse.xtext.testing.XtextRunner
+import org.eclipse.xtext.testing.util.ParseHelper
+import org.eclipse.xtext.testing.validation.ValidationTestHelper
+import org.junit.Test
+import org.junit.runner.RunWith
+
+import static org.junit.Assert.assertTrue
+
+@RunWith(XtextRunner)
+@InjectWith(SemanticAdaptationInjectorProvider)
+class CgCppModuleTest extends AbstractSemanticAdaptationTest {
+
+	// @Inject CppGenerator underTest
+	@Inject extension ParseHelper<SemanticAdaptation>
+	@Inject extension  ValidationTestHelper
+
+	@Test def powerwindow_model_only() { __parseNoErrors('test_input/window/') }
+
+	def __parseNoErrors(String filename) {
+
+		val file = new File(filename)
+
+		assertTrue("File not found: " + filename, file.exists);
+
+		var SemanticAdaptation model = null;
+		if (file.isDirectory) {
+			for (f : file.listFiles) {
+				if (model === null) {
+					model = __parse(f.absolutePath)
+				} else {
+					model = __parse(f.absolutePath, model.eResource.resourceSet)
+				}
+				__assertNoParseErrors(model, f.absolutePath)
+			}
+		} else {
+			model = __parse(filename)
+			__assertNoParseErrors(model, filename)
+		}
+
+		val fsa = new InMemoryFileSystemAccess()
+		val IGeneratorContext ctxt = null;
+		new CppGenerator().doGenerate(model.eResource, fsa, ctxt)
+
+		System.out.println(fsa.allFiles)
+	}
+
+	def __parseNoErrorsPrint(String filename) {
+		val root = __parse(filename)
+		print_ast(root)
+		__assertNoParseErrors(root, filename)
+	}
+
+	def __parse(String filename) {
+		return readFile(filename).parse
+	}
+
+	def __parse(String filename, ResourceSet resourceSetToUse) {
+
+		return readFile(filename).parse(resourceSetToUse)
+	}
+
+	def __assertNoParseErrors(EObject root, String filename) {
+		try {
+			root.assertNoErrors
+		} catch (AssertionError e) {
+			val p = Pattern.compile(".*, offset (?<offset>[0-9]+), length (?<length>[0-9]+)")
+			val code = readFile(filename)
+			for (String line : e.message.split("\n")) {
+				val m = p.matcher(line)
+				m.matches()
+				val count = __occurrencesInString(code.subSequence(0, Integer.valueOf(m.group("offset"))).toString(),
+					"\n")
+				print(filename + " at line " + (count + 1) + ": ")
+				println(line)
+			}
+			throw e
+		}
+	}
+
+	def __occurrencesInString(String str, String findstr) {
+		var lastIndex = 0
+		var count = 0
+		while (lastIndex != -1) {
+			lastIndex = str.indexOf(findstr, lastIndex)
+			if (lastIndex != -1) {
+				count++
+				lastIndex += findstr.length()
+			}
+		}
+		return count
+	}
+
+}

+ 69 - 0
DSL_SemanticAdaptation/be.uantwerpen.ansymo.semanticadaptation.cg.cpp.tests/test_input/window/window_sa_canonical.BASE.sa

@@ -0,0 +1,69 @@
+module Window_SA
+
+semantic adaptation reactive mealy window_sa
+
+at "./path/to/Window.fmu"
+for fmu Window window
+input ports displacement (rad), speed (rad/s), reaction_force (N)
+
+
+/*
+In the originial version, no input ports where declared, so all dangling inputs of the original fmus are bound to the input ports of the adapted FMU.
+In the canonical version, the input and output ports are all declared explicitly, and their bindings are implemented in the in/out rules.
+*/
+
+input ports 	reaction_force,
+				displacement,
+				speed;
+
+output ports	disp;
+				tau;
+
+in var 	stored_windowsa_reaction_force,
+		stored_windowsa_displacement,
+		stored_windowsa_speed;
+
+out var stored_window_reaction_torque,
+		stored_window_height;
+
+in rules {
+	true -> {
+		/*
+		is_set checks whether the input value is given in the setValues call of the adapted FMU.
+		Notice that in the canonical version, all ports are prefixed.
+		*/
+		if (is_set(window_sa.reaction_force))
+			stored_windowsa_reaction_force := window_sa.reaction_force;
+	} --> {
+		window.reaction_force := stored_windowsa_reaction_force;
+	};
+    true -> {
+		if (is_set(window_sa.displacement))
+			stored_windowsa_displacement := window_sa.displacement;
+	} --> {
+		window.displacement := stored_windowsa_displacement; 
+	};
+    true -> {
+		if (is_set(window_sa.speed))
+			stored_windowsa_speed := window_sa.speed;
+	} --> {
+		window.speed := stored_windowsa_speed;
+	};
+}
+
+control rules {
+	do_step(window, t, H); // includes update_in rules and update_out (update-in rules do not update state)
+}
+
+out rules {
+	true => {
+		stored_window_reaction_torque := window.reaction_torque;
+	} -> {
+		window_sa.tau := - stored_window_reaction_torque;
+	}
+	true => {
+		stored_window_height := window.height;
+	} -> {
+		window_sa.disp := stored_window_height * 100;
+	}
+}