Commit fc0c3c1e authored by Malte Heithoff's avatar Malte Heithoff

Basic Library

parent 91b6b4e6
Pipeline #392533 failed with stage
in 1 minute and 23 seconds
......@@ -235,6 +235,8 @@ public class GeneratorCPP implements EMAMGenerator {
// setGenerateMainClass(true);
}
addSemantics(taggingResolver, componentInstanceSymbol);
String lastNameWithoutArrayPart = "";
if (!streamTestGenerationMode) {
for (EMAComponentInstanceSymbol instanceSymbol : componentInstanceSymbol.getSubComponents()) {
......@@ -327,16 +329,10 @@ public class GeneratorCPP implements EMAMGenerator {
EMAComponentInstanceSymbol.KIND).isPresent()) {
EMAComponentInstanceSymbol componentInstanceSymbol = (EMAComponentInstanceSymbol) taggingResolver.resolve(componentFullName,
EMAComponentInstanceSymbol.KIND).get();
ExecutionSemantics semantics = new ExecutionSemantics(taggingResolver, componentInstanceSymbol);
semantics.addExecutionSemantics();
fileContents.addAll(generateStrings(taggingResolver, componentInstanceSymbol));
}
}
} else {
ExecutionSemantics semantics = new ExecutionSemantics(taggingResolver, rootModel);
semantics.addExecutionSemantics();
searchForCVEverywhere(componentSymbol, taggingResolver);
fileContents = generateStrings(taggingResolver, componentSymbol);
}
......@@ -365,6 +361,13 @@ public class GeneratorCPP implements EMAMGenerator {
return files;
}
public void addSemantics(TaggingResolver taggingResolver, EMAComponentInstanceSymbol component) {
if (component.getOrderOutput().isEmpty()) {
ExecutionSemantics semantics = new ExecutionSemantics(taggingResolver, component);
semantics.addExecutionSemantics();
}
}
protected List<File> generateCMakeFiles(EMAComponentInstanceSymbol componentInstanceSymbol) {
List<File> files = new ArrayList<>();
if (componentInstanceSymbol != null) {
......
......@@ -5,6 +5,8 @@ package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.instanceStructure.EMAComponentInstanceSymbol;
import de.monticore.lang.monticar.generator.cpp.resolver.Resolver;
import de.monticore.lang.monticar.generator.order.simulator.AbstractSymtab;
import de.monticore.lang.monticar.semantics.Constants;
import de.monticore.lang.monticar.semantics.util.BasicLibrary;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Log;
import org.apache.commons.cli.CommandLine;
......@@ -303,7 +305,7 @@ public final class GeneratorCppCli {
Path modelsDirPath = Paths.get(cliArgs.getOptionValue(OPTION_MODELS_PATH.getOpt()));
String rootModelName = cliArgs.getOptionValue(OPTION_ROOT_MODEL.getOpt());
String outputPath = cliArgs.getOptionValue(OPTION_OUTPUT_PATH.getOpt());
TaggingResolver symTab = AbstractSymtab.createSymTabAndTaggingResolver(modelsDirPath.toString());
TaggingResolver symTab = getSymTabAndTaggingResolver(modelsDirPath);
Resolver resolver = new Resolver(symTab);
EMAComponentInstanceSymbol componentSymbol = resolveSymbol(resolver, rootModelName);
......@@ -391,6 +393,12 @@ public final class GeneratorCppCli {
}
private static TaggingResolver getSymTabAndTaggingResolver(Path modelsDirPath) {
BasicLibrary.extract();
return AbstractSymtab.createSymTabAndTaggingResolver(modelsDirPath.toString(),
Constants.SYNTHESIZED_COMPONENTS_ROOT, BasicLibrary.BASIC_LIBRARY_ROOT);
}
private static EMAComponentInstanceSymbol resolveSymbol(Resolver resolver, String rootModelName) {
EMAComponentInstanceSymbol componentSymbol = resolver.getExpandedComponentInstanceSymbol(rootModelName).orElse(null);
if (componentSymbol == null) {
......
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Add<T,N1 n=2> {
port
in T summand[n],
out T sum1;
implementation Math{
sum1 = sum(summand);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Constant<T>(T cvalue) {
port
out T value;
implementation Math{
value = cvalue;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Division<T> {
port
in T dividend,
in T divisor,
out T division;
implementation Math{
division * divisor == dividend;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Gain<T>(T gainValue) {
port
in T input,
out T output;
implementation Math{
output = input * gainValue;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Max<T,N1 n=2> {
port
in T in1[n],
out T maximum;
implementation Math{
T maxVal = in1(1);
for i=1:n
maxVal=max(maxVal,in1(i));
end
maximum=maxVal;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Min<T,N1 n=2> {
port
in T input[n],
out T minimum;
implementation Math{
T minVal=input(1);
for i=1:n
minVal=min(minVal,input(i));
end
minimum=minVal;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Mod<T> {
port
in T input1,
in T input2,
out T output;
implementation Math{
output == modulo(input1,input2);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Multiplication<T,N1 n=2> {
port
in T factor[n],
out T product1;
implementation Math{
product1 == product(factor);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
/* The Saturation block imposes upper and lower bounds on a signal. When the input signal is within the range specified by the Lower limit and Upper limit parameters, the input signal passes through unchanged. When the input signal is outside these bounds, the signal is clipped to the upper or lower bound.
*/
component Saturation<T> (T minValue, T maxValue){
port
in T input,
out T output;
implementation Math {
output = min(max(input, minValue), maxValue);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.arithmetic;
component Subtract<T> {
ports
in T minuend,
in T subtrahend,
out T difference;
implementation Math {
difference = minuend - subtrahend;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.basicLibrary;
non-df component Delay<T>(T dvalue = 0) {
port
in T input,
out T output;
implementation Math {
static delayed = dvalue;
output = dvalue;
dvalue = input;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.basicLibrary;
component LookUp<T, N1 n = 1> (Q^{1,n} lookuptable) {
ports
in T input,
out T output;
implementation Math{
output = lookuptable(input);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.differential;
component Derivation<T>(T dvalue = 0) {
port
in T input,
out T output;
implementation Math{
input(t=0) = dvalue;
diff(input) == output;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.differential;
component Integrator<T>(T dvalue = 0) {
port
in T input,
out T output;
implementation Math{
output(t=0) = dvalue;
diff(output) == input;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component And<N1 n=2> {
port
in B input[n],
out B output;
implementation Math{
output = and(input);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component Equals<T> {
port
in T input1,
in T input2,
out B output;
implementation Math{
output=equal(input1,input2);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component Greater<T> {
port
in T input1,
in T input2,
out B output;
implementation Math{
output=input1>input2;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component GreaterEquals<T> {
port
in T input1,
in T input2,
out B output;
implementation Math{
output=input1>=input2;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component Less<T> {
port
in T input1,
in T input2,
out B output;
implementation Math{
output=input1<input2;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component LessEquals<T> {
port
in T input1,
in T input2,
out B output;
implementation Math{
output=input1<=input2;
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component Not<T> {
port
in T input,
out T output;
implementation Math{
if input==0
output=1;
else
output=0;
end
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.semantics.library.logic;
component Or<T,N1 n=2> {
port
in B input[n],
out B output;
implementation Math{
T outVal=input(1);
for i=1:n
outVal=or(outVal,input(i));
end
output=outVal;
}
}
/* (c) https://github.com/MontiCore/monticore */
package basicLibrary;
component Switch<T=B> { // if no generic is given, then default type B is taken
port
in T input1, //if
in B cond, // condition
in T input2, //else
out T output;
implementation Math{
if cond
output=input1;
else
output=input2;
end
}
}
import argparse
from sympy import symbols, sympify, solve
def main(equations, symbolNames):
symbols_ = {}
for symbolName in symbolNames:
symbols_[symbolName] = symbols(symbolName)
equs = sympify(equations, locals(), evaluate=False)
solutions = solve(equs, symbols_)
i = 0
for sol in solutions.keys():
print(sol, "=", solutions[sol])
parser = argparse.ArgumentParser(description='Solve DAE symbolic if possible')
parser.add_argument('system', metavar='eq', type=str, nargs='+')
parser.add_argument('--symbols', metavar='y', type=str, nargs='+')
args = parser.parse_args()
main(args.system, args.symbols)
\ No newline at end of file
import argparse
import re
from sympy import symbols, Function, dsolve, sympify, Derivative, solve
def reduceSystem(equs, ics):
differentialEquations = []
algebraicEquations = []
differentialFunctions = []
algebraicFunctions = []
for eq in equs:
ds = eq.atoms(Derivative)
if len(ds) == 0:
algebraicEquations.append(eq)
else:
differentialEquations.append(eq)
for d in ds:
fs = d.atoms(Function)
differentialFunctions.extend(fs)
if len(algebraicEquations) == 0:
return differentialEquations
for eq in equs:
fs = eq.atoms(Function)
for f in fs:
if not f in differentialFunctions and f not in algebraicFunctions:
algebraicFunctions.append(f)
if len(algebraicFunctions) == 0:
return differentialEquations
sol = solve(algebraicEquations, algebraicFunctions)
# check if solution is present
newEqus = []
for eq in differentialEquations:
for f, s in sol.items():
eq = eq.subs(f, s)
newEqus.append(eq)
newIcs = {}
for f in ics.keys():
for d in differentialFunctions:
if f.func == d.func:
newIcs[f] = ics[f]
return newEqus, newIcs, algebraicEquations, algebraicFunctions, differentialFunctions
def main(equations, symbolNames, functionNames, startValues):
symbols_ = {}
for symbolName in symbolNames:
symbols_[symbolName] = symbols(symbolName)
functions_ = {}
for functionName in functionNames:
functions_[functionName] = Function(functionName)
ics = {}
for functionName in startValues.keys():
function = functions_[functionName]
ics[function(0)] = startValues[functionName]
equs = sympify(equations, locals(), evaluate=False)
equs, ics, algebraicEquations, algebraicFunctions, differentialFunctions = reduceSystem(equs, ics)
solutions = dsolve(equs, ics=ics)
equationsToSolve = []
functionsToSolve = []
equationsToSolve.extend(algebraicEquations)
equationsToSolve.extend(solutions)
functionsToSolve.extend(algebraicFunctions)
functionsToSolve.extend(differentialFunctions)
sol = solve(equationsToSolve, functionsToSolve)
for solution in solutions:
print(str(solution.args[0]).replace('(' + time + ')', ''), "=", solution.args[1])
for f in algebraicFunctions:
print(str(f).replace('(' + time + ')', ''), "=", str(sol[f]).replace('(' + time + ')', ''))
parser = argparse.ArgumentParser(description='Solve DAE symbolic if possible')
parser.add_argument('system', metavar='eq', type=str, nargs='+')
parser.add_argument('--functions', metavar='f', type=str, nargs='+')
parser.add_argument('--y0', metavar='y0', type=float, nargs='+')
args = parser.parse_args()
if (len(args.y0) > len(args.functions)):
raise Exception('Arguments for y0 cannot be larger than arguments for functions')
ics = {}
for i in range(len(args.y0)):
ics[args.functions[i]] = args.y0[i]
time = 'current_Time'
system = []
for eq in args.system:
newEq = eq
for f in args.functions:
pat = re.compile(r'\b' + f + r'\b')
newEq = pat.sub(f + '(' + time + ')', newEq)
system.append(newEq)
main(system, [time], args.functions, ics)
\ No newline at end of file