Commit 02af19b2 authored by Christoph Richter's avatar Christoph Richter
Browse files

MathFunctionFixer: Implemented Chain-of-responsibility pattern

parent 6d8da0d6
Pipeline #56925 passed with stage
in 2 minutes and 25 seconds
package de.monticore.lang.monticar.generator;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.pattern.BaseChainOfResponsibility;
import de.se_rwth.commons.logging.Log;
public abstract class BaseMathFunctionFixerHandler extends BaseChainOfResponsibility<BaseMathFunctionFixerHandler> {
protected abstract boolean canFixMathSymbol(MathExpressionSymbol symbol);
protected abstract void doFixMathFunction(MathExpressionSymbol symbol, BluePrintCPP bluePrintCPP);
public void handleFixMathFunction(MathExpressionSymbol symbol, BluePrintCPP bluePrintCPP) {
if (canFixMathSymbol(symbol)) {
doFixMathFunction(symbol, bluePrintCPP);
} else if (getSuccessor() != null) {
getSuccessor().handleFixMathFunction(symbol, bluePrintCPP);
} else {
Log.info(symbol.getTextualRepresentation(), "Symbol:");
Log.debug(getRole(), "Case not handled!");
}
}
}
......@@ -3,6 +3,7 @@ package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.math._symboltable.expression.*;
import de.monticore.lang.math._symboltable.matrix.*;
import de.monticore.lang.monticar.generator.BaseMathFunctionFixerHandler;
import de.monticore.lang.monticar.generator.MathCommand;
import de.monticore.lang.monticar.generator.Variable;
import de.monticore.lang.monticar.generator.cpp.converter.ComponentConverter;
......@@ -14,8 +15,46 @@ import de.se_rwth.commons.logging.Log;
/**
* @author Sascha Schneiders
*/
public class MathFunctionFixer {
public static void fixMathFunctions(MathExpressionSymbol mathExpressionSymbol, BluePrintCPP bluePrintCPP) {
public class MathFunctionFixer extends BaseMathFunctionFixerHandler {
private static MathFunctionFixer ourInstance = new MathFunctionFixer();
public static MathFunctionFixer getInstance() {
return ourInstance;
}
private MathFunctionFixer() {
}
@Override
public String getRole() {
return "MathFunctionFixer";
}
@Override
protected boolean canFixMathSymbol(MathExpressionSymbol mathExpressionSymbol) {
boolean canHandle = false;
if (mathExpressionSymbol == null
|| mathExpressionSymbol.isAssignmentExpression()
|| mathExpressionSymbol.isMatrixExpression()
|| mathExpressionSymbol.isArithmeticExpression()
|| mathExpressionSymbol.isForLoopExpression()
|| mathExpressionSymbol.isCompareExpression()
|| mathExpressionSymbol.isAssignmentDeclarationExpression()
|| mathExpressionSymbol.isParenthesisExpression()
|| mathExpressionSymbol.isConditionalsExpression()
|| mathExpressionSymbol.isConditionalExpression()
|| mathExpressionSymbol.isPreOperatorExpression()
|| mathExpressionSymbol.isValueExpression()
|| (mathExpressionSymbol.getExpressionID() == MathStringExpression.ID)
|| (mathExpressionSymbol.getExpressionID() == MathChainedExpression.ID)) {
canHandle = true;
}
return canHandle;
}
@Override
protected void doFixMathFunction(MathExpressionSymbol mathExpressionSymbol, BluePrintCPP bluePrintCPP) {
boolean notHandled = true;
if (mathExpressionSymbol == null) {
notHandled = false;
......@@ -55,7 +94,7 @@ public class MathFunctionFixer {
notHandled = false;
} else if (((MathValueExpressionSymbol) mathExpressionSymbol).isNameExpression()) {
notHandled = false;
} else if (((MathValueExpressionSymbol)mathExpressionSymbol).isBooleanExpression()){
} else if (((MathValueExpressionSymbol) mathExpressionSymbol).isBooleanExpression()) {
notHandled = false;
}
} else if (mathExpressionSymbol.getExpressionID() == MathChainedExpression.ID) {
......@@ -68,10 +107,14 @@ public class MathFunctionFixer {
if (notHandled) {
Log.info(mathExpressionSymbol.getClass().getName(), "Symbol name:");
Log.info(mathExpressionSymbol.getTextualRepresentation(), "Symbol:");
Log.debug("Not supported yet","Case not handled");
Log.debug("Not supported yet", "Case not handled");
}
}
public static void fixMathFunctions(MathExpressionSymbol mathExpressionSymbol, BluePrintCPP bluePrintCPP) {
getInstance().doFixMathFunction(mathExpressionSymbol, bluePrintCPP);
}
public static void fixMathFunctions(MathPreOperatorExpressionSymbol mathExpressionSymbol, BluePrintCPP bluePrintCPP) {
fixMathFunctions(mathExpressionSymbol.getMathExpressionSymbol(), bluePrintCPP);
}
......
package de.monticore.lang.monticar.generator.cpp.converter;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.pattern.BaseChainOfResponsibility;
import de.se_rwth.commons.logging.Log;
import java.util.List;
......@@ -10,30 +11,18 @@ import java.util.List;
*
* @author Christoph Richter
*/
public abstract class BaseExecuteMethodGeneratorHandler {
private BaseExecuteMethodGeneratorHandler successor = null;
public BaseExecuteMethodGeneratorHandler getSuccessor() {
return successor;
}
public void setSuccessor(BaseExecuteMethodGeneratorHandler successor) {
this.successor = successor;
}
public abstract class BaseExecuteMethodGeneratorHandler extends BaseChainOfResponsibility<BaseExecuteMethodGeneratorHandler> {
protected abstract boolean canHandleSymbol(MathExpressionSymbol symbol);
protected abstract String doGenerateExecuteCode(MathExpressionSymbol symbol, List<String> includeStrings);
public abstract String getRole();
public String handleGenerateExecuteCode(MathExpressionSymbol symbol, List<String> includeStrings) {
String result = "";
if (canHandleSymbol(symbol)) {
result = doGenerateExecuteCode(symbol, includeStrings);
} else if (successor != null) {
result = successor.handleGenerateExecuteCode(symbol, includeStrings);
} else if (getSuccessor() != null) {
result = getSuccessor().handleGenerateExecuteCode(symbol, includeStrings);
} else {
Log.info(symbol.getTextualRepresentation(), "Symbol:");
Log.debug(getRole(), "Case not handled!");
......
package de.monticore.lang.monticar.pattern;
/**
* Implements the chain-of-responsibility pattern
*
* @author Christoph Richter
*/
public abstract class BaseChainOfResponsibility<T> {
private T successor = null;
public T getSuccessor() {
return successor;
}
public void setSuccessor(T successor) {
this.successor = successor;
}
public abstract String getRole();
}
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