Commit e3e235c8 authored by Sascha Niklas Schneiders's avatar Sascha Niklas Schneiders
Browse files

current stream array/matrix support

parent d9f4c5a9
......@@ -19,7 +19,7 @@
<se-commons.version>1.7.7</se-commons.version>
<mc.grammars.assembly.version>0.0.6-SNAPSHOT</mc.grammars.assembly.version>
<SIUnit.version>0.0.10-SNAPSHOT</SIUnit.version>
<Common-MontiCar.version>0.0.10-SNAPSHOT</Common-MontiCar.version>
<Common-MontiCar.version>0.0.11-SNAPSHOT</Common-MontiCar.version>
<Embedded-MontiArc.version>0.0.11-SNAPSHOT</Embedded-MontiArc.version>
<Embedded-MontiArc-Behaviour.version>0.0.11-SNAPSHOT</Embedded-MontiArc-Behaviour.version>
<Math.version>0.0.11-SNAPSHOT</Math.version>
......
package de.monticore.lang.monticar.generator;
/**
* @author Sascha Schneiders
*/
public class MatrixInstance {
}
......@@ -14,11 +14,7 @@ import de.monticore.lang.monticar.generator.cpp.viewmodel.check.ComponentCheckVi
import de.monticore.lang.monticar.generator.cpp.viewmodel.check.IOutputPortCheck;
import de.monticore.lang.monticar.generator.cpp.viewmodel.check.RangeOutputPortCheck;
import de.monticore.lang.monticar.literals2._ast.ASTBooleanLiteral;
import de.monticore.lang.monticar.streamunits._ast.ASTNamedStreamUnits;
import de.monticore.lang.monticar.streamunits._ast.ASTPrecisionNumber;
import de.monticore.lang.monticar.streamunits._ast.ASTStream;
import de.monticore.lang.monticar.streamunits._ast.ASTStreamInstruction;
import de.monticore.lang.monticar.streamunits._ast.ASTStreamValue;
import de.monticore.lang.monticar.streamunits._ast.*;
import de.monticore.lang.monticar.streamunits._symboltable.ComponentStreamUnitsSymbol;
import de.monticore.lang.monticar.streamunits._symboltable.NamedStreamUnitsSymbol;
import de.monticore.lang.monticar.streamunits._visitor.StreamUnitsVisitor;
......@@ -196,7 +192,7 @@ public final class TestsGeneratorCPP {
private static FileContent getCatchLib() {
InputStream resource = TestsGeneratorCPP.class.getResourceAsStream("/vendor/catch.hpp");
String body = new Scanner(resource, "UTF-8").useDelimiter("\\A").next();
String body = new Scanner(resource).useDelimiter("\\A").next();
return new FileContent(body, TESTS_DIRECTORY_NAME + "/catch.hpp");
}
......@@ -236,6 +232,18 @@ public final class TestsGeneratorCPP {
result = RangeOutputPortCheck.from(baseValue, baseValue);
}
}
@Override
public void visit(ASTMatrixPair node) {
//TODO handle matrices
//node.getValuePairs()
}
/*@Override
public void visit(ASTArrayValues node) {
//TODO handle port arrays
}*/
}
private static final class ASTStreamValue2InputPortValue implements StreamUnitsVisitor {
......@@ -259,5 +267,17 @@ public final class TestsGeneratorCPP {
}
result = Double.toString(unitNumber.getNumber().get().doubleValue());
}
@Override
public void visit(ASTMatrixPair node) {
//TODO handle matrices
//node.getValuePairs()
//node.get
}
/*@Override
public void visit(ASTArrayValues node) {
//TODO handle port arrays
}*/
}
}
......@@ -25,6 +25,9 @@ public class AbstractSymtabTest extends AbstractSymtab {
public static void testFilesAreEqual(List<File> files, String restPath) {
for (File f : files) {
File fileTarget = new File("./src/test/resources/results/" + restPath + f.getName());
if (!fileTarget.exists()) {
fileTarget = new File("./src/test/resources/results/" + restPath + "test/" + f.getName());
}
Log.debug("" + fileTarget.exists(), "Exists");
Log.debug(f.getName() + " " + fileTarget.getName(), "Comparing");
assertTrue(areBothFilesEqual(f, fileTarget));
......
......@@ -33,4 +33,21 @@ public class TestsGenTest extends AbstractSymtabTest {
List<File> files = generatorCPP.generateFiles(symTab, componentSymbol, symTab);
assertEquals(15, files.size());
}
@Test
public void testSimpleMatrixComponent() throws IOException {
TaggingResolver symTab = createSymTabAndTaggingResolver(MODELS_DIR_PATH.toString());
ExpandedComponentInstanceSymbol componentSymbol = symTab.<ExpandedComponentInstanceSymbol>resolve(
"test.simpleMatrixComponent",
ExpandedComponentInstanceSymbol.KIND
).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setModelsDirPath(MODELS_DIR_PATH);
generatorCPP.setGenerateTests(true);
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/test/simpleMatrixComponent");
List<File> files = generatorCPP.generateFiles(symTab, componentSymbol, symTab);
String restPath = "test/simpleMatrixComponent/";
testFilesAreEqual(files, restPath);
}
}
#ifndef HELPER_H
#define HELPER_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <stdarg.h>
#include <initializer_list>
class Helper
{
public:
static void init()
{
string_vector argv(2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main(2, argv.c_str_vec(), 1);
//octave_debug=1;
//feval ("pkg", ovl ("load", "all"), 0);
}
static octave_value_list convertToOctaveValueList(double a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(Matrix a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(RowVector a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(ColumnVector a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(double a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(std::initializer_list<double> args)
{
octave_value_list in;
int counter = 0;
for(double element : args) {
in(counter) = octave_value(element);
++counter;
}
return in;
}
static octave_value_list convertToOctaveValueList(Matrix a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(RowVector a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(ColumnVector a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list callOctaveFunction(octave_value_list in, std::string functionName,int argsOut)
{
/*octave_idx_type n = 2;
octave_value_list in;
for(octave_idx_type i = 0; i < n; i++)
in(i) = octave_value(5 * (i + 2));
octave_value_list out = feval("gcd", in, 1);
if(!error_state && out.length() > 0)
std::cout << "GCD of [" << in(0).int_value() << ", " << in(1).int_value() << "] is " << out(0).int_value()
<< std::endl;
else
std::cout << "invalid\n";
clean_up_and_exit(0);*/
/* if(functionName=="eigs")
return feval(functionName, in, 2);
else if(functionName=="kmeans")
return feval(functionName, in, 2);
*/
return feval(functionName, in, argsOut);
}
static int callOctaveFunctionIntFirstResult(octave_value_list in, std::string functionName, int argsOut)
{
// printf("callOctaveFunctionIntFirstResult pre return functionName: %s\n",functionName.c_str());
return callOctaveFunction(in, functionName,argsOut)(0).int_value();
}
static double callOctaveFunctionDoubleFirstResult(octave_value_list in, std::string functionName, int argsOut)
{
// printf("callOctaveFunctionDoubleFirstResult pre return functionName: %s\n",functionName.c_str());
return callOctaveFunction(in, functionName,argsOut)(0).double_value();
}
static Matrix callOctaveFunctionMatrixFirstResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(0).matrix_value();
}
static ColumnVector callOctaveFunctionColumnVectorFirstResult(octave_value_list in, std::string functionName, int argsOut)
{
printf("pre Call %s\n", functionName.c_str());
try {
in=octave_value_list();
octave_value_list list = callOctaveFunction(in, functionName,argsOut);
printf("post Call %s\n", functionName.c_str());
return list(0).array_value().as_column();
} catch(const std::exception& e) {
printf("%s\n", e.what());
}
return ColumnVector();
}
static RowVector callOctaveFunctionRowVectorFirstResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(0).array_value().as_row();
}
static int callOctaveFunctionIntSecondResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(1).int_value();
}
static double callOctaveFunctionDoubleSecondResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(1).double_value();
}
static Matrix callOctaveFunctionMatrixSecondResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(1).matrix_value();
}
static ColumnVector callOctaveFunctionColumnVectorSecondResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(1).array_value().as_column();
}
static RowVector callOctaveFunctionRowVectorSecondResult(octave_value_list in, std::string functionName, int argsOut)
{
return callOctaveFunction(in, functionName,argsOut)(1).array_value().as_row();
}
static Matrix getMatrixFromOctaveListFirstResult(octave_value_list list){
return list(0).matrix_value();
}
static RowVector getRowVectorFromOctaveListFirstResult(octave_value_list list){
return list(0).array_value().as_row();
}
static ColumnVector getColumnVectorFromOctaveListFirstResult(octave_value_list list){
return list(0).array_value().as_column();
}
static double getDoubleFromOctaveListFirstResult(octave_value_list list){
return list(0).double_value();
}
static int getIntFromOctaveListFirstResult(octave_value_list list){
return list(0).int_value();
}
};
#endif // HELPER_H
\ No newline at end of file
#ifndef TEST_SIMPLEMATRIXCOMPONENT_TEST
#define TEST_SIMPLEMATRIXCOMPONENT_TEST
#include "catch.hpp"
#include "../test_simpleMatrixComponent.h"
TEST_CASE("test.SimpleMatrixComponentTest1", "[test_simpleMatrixComponent]") {
test_simpleMatrixComponent component;
component.init();
component.status = 1.0;
component.execute();
component.status = 2.0;
component.execute();
component.status = 3.0;
component.execute();
}
#endif
#ifndef TESTS_MAIN
#define TESTS_MAIN
#include "../Helper.h"
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
int main(int argc, char* argv[]) {
Helper::init();
Catch::Session session;
int returnCode = session.applyCommandLine(argc, argv);
if (returnCode != 0) {
octave_quit();
return returnCode;
}
int numFailed = session.run();
octave_quit();
return numFailed;
}
#include "test_simpleMatrixComponent_test.hpp"
#endif
#ifndef TEST_SIMPLEMATRIXCOMPONENT
#define TEST_SIMPLEMATRIXCOMPONENT
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "octave/oct.h"
class test_simpleMatrixComponent{
public:
double status;
Matrix direction;
RowVector CONSTANTCONSTANTVECTOR0;
RowVector CONSTANTCONSTANTVECTOR1;
RowVector CONSTANTCONSTANTVECTOR2;
void init()
{
direction=Matrix(1,3);
CONSTANTCONSTANTVECTOR0 = RowVector(3);
CONSTANTCONSTANTVECTOR0(0,0) = 1;
CONSTANTCONSTANTVECTOR0(0,1) = 0;
CONSTANTCONSTANTVECTOR0(0,2) = 0;
CONSTANTCONSTANTVECTOR1 = RowVector(3);
CONSTANTCONSTANTVECTOR1(0,0) = 0;
CONSTANTCONSTANTVECTOR1(0,1) = 1;
CONSTANTCONSTANTVECTOR1(0,2) = 0;
CONSTANTCONSTANTVECTOR2 = RowVector(3);
CONSTANTCONSTANTVECTOR2(0,0) = 0;
CONSTANTCONSTANTVECTOR2(0,1) = 0;
CONSTANTCONSTANTVECTOR2(0,2) = 1;
}
void execute()
{
if((status == 1)){
direction = CONSTANTCONSTANTVECTOR0;
}
else if((status == 2)){
direction = CONSTANTCONSTANTVECTOR1;
}
else {
direction = CONSTANTCONSTANTVECTOR2;
}
}
};
#endif
package test;
component SimpleMatrixComponent{
ports in N1 status,
out Q^{1,3} direction;
implementation Math{
if status == 1
direction = [1, 0, 0];
elseif status == 2
direction = [0, 1, 0];
else
direction = [0, 0, 1];
end
}
}
\ No newline at end of file
package test;
stream SimpleMatrixComponentTest1 for SimpleMatrixComponent{
status: 1 tick 2 tick 3;
direction: [1, 0, 0] tick [0, 1, 0] tick [0, 0, 1];
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment