Skip to content

Commit

Permalink
Added implicit for block migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Jan 5, 2024
1 parent eb3af0e commit 87802dc
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.acceleo.AcceleoFactory;
import org.eclipse.acceleo.ExpressionStatement;
import org.eclipse.acceleo.Statement;
import org.eclipse.acceleo.aql.migration.MigrationException;
import org.eclipse.acceleo.aql.migration.converters.ModuleConverter.ImpliciteSelfFrame;
import org.eclipse.acceleo.aql.migration.converters.utils.OperationUtils;
import org.eclipse.acceleo.aql.migration.converters.utils.TypeUtils;
import org.eclipse.acceleo.model.mtl.ForBlock;
import org.eclipse.acceleo.model.mtl.MtlPackage;
import org.eclipse.acceleo.model.mtl.Query;
import org.eclipse.acceleo.model.mtl.QueryInvocation;
import org.eclipse.acceleo.model.mtl.Template;
import org.eclipse.acceleo.model.mtl.TemplateInvocation;
import org.eclipse.acceleo.query.ast.AstFactory;
import org.eclipse.acceleo.query.ast.AstPackage;
Expand Down Expand Up @@ -103,9 +105,9 @@ public final class ExpressionConverter extends AbstractConverter {
private static final String SELF_VARIABLE_NAME = "self";

/**
* The flag stating whether we should resolve the self variable.
* The current implicit self variable name stack.
*/
private boolean resolveSelf = true;
private Deque<ImpliciteSelfFrame> implicitSelfStack;

/**
* The trget folder {@link Path}.
Expand Down Expand Up @@ -146,11 +148,13 @@ public Map<Call, String> getJavaServiceCalls() {
*
* @param input
* the expression to convert
* @param implicitSelfStack
* the current implicit self variable name stack
* @return the statement
*/
public Statement convertToStatement(OCLExpression input) {
public Statement convertToStatement(OCLExpression input, Deque<ImpliciteSelfFrame> implicitSelfStack) {
Statement output = AcceleoFactory.eINSTANCE.createExpressionStatement();
((ExpressionStatement)output).setExpression(convertToExpression(input, false));
((ExpressionStatement)output).setExpression(convertToExpression(input, implicitSelfStack));
return output;
}

Expand All @@ -159,19 +163,18 @@ public Statement convertToStatement(OCLExpression input) {
*
* @param inputExpression
* the expression to convert
* @param allowSelf
* if <true>, won't resolve self for this expression
* @param implicitSelfStack
* the current implicit self variable name stack
* @return the Acceleo 4 expression
*/
public org.eclipse.acceleo.Expression convertToExpression(OCLExpression inputExpression,
boolean allowSelf) {
Deque<ImpliciteSelfFrame> implicitSelfStack) {
org.eclipse.acceleo.Expression outputExpression = AcceleoFactory.eINSTANCE.createExpression();
if (allowSelf) {
this.resolveSelf = false;
}
outputExpression.setAst(createAstResult((Expression)convert(inputExpression)));
if (allowSelf) {
this.resolveSelf = true;
this.implicitSelfStack = implicitSelfStack;
try {
outputExpression.setAst(createAstResult((Expression)convert(inputExpression)));
} finally {

}
return outputExpression;
}
Expand Down Expand Up @@ -301,9 +304,8 @@ private Expression caseVariableExp(VariableExp input) {
// } else {
output = AstFactory.eINSTANCE.createVarRef();
String variableName = input.getReferredVariable().getName();
if (resolveSelf && SELF_VARIABLE_NAME.equals(variableName)) {
Variable variable = findVariable(input);
variableName = variable.getName();
if (implicitSelfStack != null && SELF_VARIABLE_NAME.equals(variableName)) {
variableName = implicitSelfStack.peekLast().getImplicitSelfName();
}
((VarRef)output).setVariableName(variableName);
// }
Expand Down Expand Up @@ -374,10 +376,10 @@ private Expression convertCurrentCall(OperationCallExp input) {
org.eclipse.ocl.expressions.OCLExpression<EClassifier> firstArgument = input.getArgument().get(0);
if (firstArgument instanceof IntegerLiteralExp) {
int index = ((IntegerLiteralExp)firstArgument).getIntegerSymbol();
variableName = getCurrentVariableName(input, index);
variableName = getCurrentVariableName(index);
} else if (firstArgument instanceof TypeExp) {
final EClassifier eClassifier = ((TypeExp)firstArgument).getReferredType();
variableName = getCurrentVariableName(input, eClassifier);
variableName = getCurrentVariableName(eClassifier);
} else {
variableName = null;
}
Expand All @@ -396,48 +398,54 @@ private Expression convertCurrentCall(OperationCallExp input) {
return res;
}

private String getCurrentVariableName(OperationCallExp input, EClassifier eClassifier) {
private String getCurrentVariableName(EClassifier eClassifier) {
String res = null;

EObject container = input.eContainer();
while (container != null) {
if (container instanceof ForBlock) {
final ForBlock forBlock = (ForBlock)container;
final Iterator<ImpliciteSelfFrame> it = implicitSelfStack.descendingIterator();
while (it.hasNext()) {
final ImpliciteSelfFrame frame = it.next();
if (frame.getInput() instanceof ForBlock) {
final ForBlock forBlock = (ForBlock)frame.getInput();
final EClassifier forType = forBlock.getLoopVariable().getType();
if (eClassifier == forType || (eClassifier instanceof EClass && forType instanceof EClass
&& ((EClass)forType).isSuperTypeOf((EClass)eClassifier))) {
res = forBlock.getLoopVariable().getName();
res = frame.getImplicitSelfName();
break;
}
}
container = container.eContainer();
}

return res;
}

private String getCurrentVariableName(OperationCallExp input, int index) {
private String getCurrentVariableName(int index) {
String res = null;

int localIndex = index;
EObject container = input.eContainer();
ForBlock lastFor = null;
while (container != null) {
if (container instanceof ForBlock) {
final ForBlock forBlock = (ForBlock)container;
final Iterator<ImpliciteSelfFrame> it = implicitSelfStack.descendingIterator();
ImpliciteSelfFrame forParentFrame = null;
boolean wasForFrame = false;
while (it.hasNext()) {
final ImpliciteSelfFrame frame = it.next();
if (wasForFrame) {
forParentFrame = frame;
}
if (frame.getInput() instanceof ForBlock) {
if (localIndex == 0) {
res = forBlock.getLoopVariable().getName();
res = frame.getImplicitSelfName();
break;
} else {
localIndex--;
wasForFrame = true;
}
localIndex--;
lastFor = forBlock;
}
container = container.eContainer();

if (localIndex > 0 && lastFor != null) {
res = findVariable(lastFor).getName();
} else {
wasForFrame = false;
}
}
if (res == null && forParentFrame != null) {
res = forParentFrame.getImplicitSelfName();
}

return res;
}

Expand Down Expand Up @@ -811,19 +819,4 @@ private Expression convertCollectionLiteralExp(CollectionLiteralExp input) {
return output;
}

// TODO use accurate rules here
private static Variable findVariable(EObject context) {
Variable variable = null;
EObject validParent = context.eContainer();
while (validParent != null && !(validParent instanceof Template || validParent instanceof Query)) {
validParent = validParent.eContainer();
}
if (validParent instanceof Template) {
variable = ((Template)validParent).getParameter().get(0);
} else if (validParent instanceof Query) {
variable = ((Query)validParent).getParameter().get(0);
}
return variable;
}

}

0 comments on commit 87802dc

Please sign in to comment.