Skip to content

Commit

Permalink
Support for tuple types
Browse files Browse the repository at this point in the history
Also test cases recompiled
  • Loading branch information
jesusc committed Oct 17, 2019
1 parent 546a475 commit 36320ca
Show file tree
Hide file tree
Showing 20 changed files with 1,301 additions and 2,032 deletions.
9 changes: 9 additions & 0 deletions plugins/lintra.atlcompiler/src/a2l/utils/TypeError.java
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
package a2l.utils;

public class TypeError extends RuntimeException {

public TypeError(String msg) {
super(msg);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 208,8 @@ public JTypeRef createTypeRef(Type type, boolean isMutable) {
}

@Override
public JTypeRef createTypeRef(Type type, Supplier<JTypeRef> baseType) {
return createTypeRef(type, false, false, baseType);
public JTypeRef createTypeRef(Type type, Supplier<JTypeRef> baseType, boolean isMutable) {
return createTypeRef(type, false, isMutable, baseType);
}

protected JTypeRef createTypeRef(Type type, boolean isContainedType, boolean isMutable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,7 @@
import anatlyzer.atlext.OCL.OperationCallExp;
import anatlyzer.atlext.OCL.OperatorCallExp;
import anatlyzer.atlext.OCL.OrderedSetExp;
import anatlyzer.atlext.OCL.RealExp;
import anatlyzer.atlext.OCL.SequenceExp;
import anatlyzer.atlext.OCL.SetExp;
import anatlyzer.atlext.OCL.StringExp;
Expand Down Expand Up @@ -54,6 55,7 @@ public interface IOclCompiler {
void inBooleanExp(BooleanExp self);
void inStringExp(StringExp self);
void inIntegerExp(IntegerExp self);
void inRealExp(RealExp self);
void inEnumLiteralExp(EnumLiteralExp self);
void inOclUndefined(OclUndefinedExp self);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,7 @@ public interface ITyping {
JTypeRef createTypeRef(Type type);
JTypeRef createTypeRef(Type type, boolean isMutable);

JTypeRef createTypeRef(Type type, Supplier<JTypeRef> baseType);
JTypeRef createTypeRef(Type type, Supplier<JTypeRef> baseType, boolean isMutable);

JType getType(String type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 95,7 @@
import anatlyzer.atlext.OCL.OperatorCallExp;
import anatlyzer.atlext.OCL.OrderedSetExp;
import anatlyzer.atlext.OCL.PropertyCallExp;
import anatlyzer.atlext.OCL.RealExp;
import anatlyzer.atlext.OCL.SequenceExp;
import anatlyzer.atlext.OCL.SetExp;
import anatlyzer.atlext.OCL.StringExp;
Expand Down Expand Up @@ -1024,6 1025,11 @@ public void inIntegerExp(IntegerExp self) {
ocl.inIntegerExp(self);
}

@Override
public void inRealExp(RealExp self) {
ocl.inRealExp(self);
}

@Override
public void inSequenceExp(SequenceExp self) {
ocl.inSequenceExp(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 40,7 @@
import a2l.optimiser.anatlyzerext.MutableCollectionOperationCallExp;
import a2l.optimiser.anatlyzerext.MutableIteratorExp;
import a2l.optimiser.anatlyzerext.NavRefAsSet;
import a2l.utils.TypeError;
import anatlyzer.atl.analyser.namespaces.MetamodelNamespace;
import anatlyzer.atl.model.TypeUtils;
import anatlyzer.atl.model.TypingModel;
Expand All @@ -60,7 61,6 @@
import anatlyzer.atl.types.Type;
import anatlyzer.atl.types.UnionType;
import anatlyzer.atl.util.ATLUtils;
import anatlyzer.atl.util.TypeError;
import anatlyzer.atl.util.UnsupportedTranslation;
import anatlyzer.atlext.ATL.BindingStat;
import anatlyzer.atlext.ATL.ContextHelper;
Expand All @@ -86,6 86,7 @@
import anatlyzer.atlext.OCL.OperatorCallExp;
import anatlyzer.atlext.OCL.OrderedSetExp;
import anatlyzer.atlext.OCL.PropertyCallExp;
import anatlyzer.atlext.OCL.RealExp;
import anatlyzer.atlext.OCL.SequenceExp;
import anatlyzer.atlext.OCL.SetExp;
import anatlyzer.atlext.OCL.StringExp;
Expand All @@ -109,6 110,7 @@
import lintra.atlcompiler.javagen.JExpression;
import lintra.atlcompiler.javagen.JForeach;
import lintra.atlcompiler.javagen.JInvoke;
import lintra.atlcompiler.javagen.JInvokeStatic;
import lintra.atlcompiler.javagen.JMetaType;
import lintra.atlcompiler.javagen.JMethod;
import lintra.atlcompiler.javagen.JParameter;
Expand Down Expand Up @@ -544,7 546,10 @@ protected void translateCollectionOperation(OperationCallExp self) {
}
};

JVariableDeclaration newVar = gen.addLocalVar(env.currentBlock(), "op", typ.createTypeRef(self.getInferredType(), createBaseType));
System.out.println(self.getLocation() " - " self.getOperationName());
boolean isMutable = AstAnnotations.isMutable(self);

JVariableDeclaration newVar = gen.addLocalVar(env.currentBlock(), "op", typ.createTypeRef(self.getInferredType(), createBaseType, isMutable));
List<JStatement> stms = createCommentedList(self);
stms.addAll(env.getStatements(self.getSource()));

Expand Down Expand Up @@ -629,11 634,21 @@ protected void translateCollectionOperation(OperationCallExp self) {
// case "flatten":
// stms.add( createAssignment(newVar, srcVarName ".flatMap(x_ -> x_)") );
// break;
case "first":
stms.add( createAssignment(newVar, srcVarName ".isEmpty() ? null : " srcVarName ".get(0)") );
case "first":
// We check the type, and it it is a primitive type we do a hacky conversion...
if (self.getInferredType() instanceof IntegerType) {
// Use MAX_INTEGER as the representation of OclUndefined... Not perfect but should do
stms.add( createAssignment(newVar, srcVarName ".isEmpty() ? Integer.MAX_VALUE : " srcVarName ".get(0)") );
} else {
stms.add( createAssignment(newVar, srcVarName ".isEmpty() ? null : " srcVarName ".get(0)") );
}
break;
case "last":
stms.add( createAssignment(newVar, srcVarName ".isEmpty() ? null : " srcVarName ".last()") );
case "last":
if (self.getInferredType() instanceof IntegerType) {
stms.add( createAssignment(newVar, srcVarName ".isEmpty() ? Integer.MAX_VALUE : " srcVarName ".get(0)") );
} else {
stms.add( createAssignment(newVar, srcVarName ".isEmpty() ? null : " srcVarName ".last()") );
}
break;
case "asSet":
stms.add( createAssignment(newVar, "javaslang.collection.HashSet.ofAll(" srcVarName ")" ));
Expand Down Expand Up @@ -892,8 907,6 @@ public void toIteratorExp(IteratorExp self, boolean isMutable) {

JVariableDeclaration colVar = env.getVar(self.getSource()); // This returns a collection

// create a new var for the result and initialize
JVariableDeclaration resultVar = createResultVar(self, stms, isMutable);

String name = self.getName();
if ( name.equals("sortedBy") ) {
Expand All @@ -909,11 922,22 @@ public void toIteratorExp(IteratorExp self, boolean isMutable) {
Set<JVariableDeclaration> varSet = closureStatements.stream().filter(s -> s instanceof JAssignment).map(s -> ((JAssignment) s).getDeclaration()).collect(Collectors.toSet());
closure.getLocalVars().addAll(varSet);

JInvoke invoke = createInvoke("sortBy", CreationHelpers.refVar(colVar), closure);

JAssignment sort = createAssignment(resultVar, invoke);
stms.add(sort);

JVariableDeclaration resultVar;
if (isMutable) {
// TODO: CHECK THAT IF THIS IS MUTABLE THEN WE JUST SORT THE COLLECTION DIRECTLY, BUT NOT SURE HOW...
JInvokeStatic invoke = CreationHelpers.createInvokeStatic("a2l.runtime.stdlib.Collections.sort", CreationHelpers.refVar(colVar), closure);

stms.add(CreationHelpers.createExpresionStatement(invoke));
// Use the collection variable as result because we are modifying the collection in place
resultVar = colVar;
} else {
resultVar = createResultVar(self, stms, isMutable);

JInvoke invoke = createInvoke("sortBy", CreationHelpers.refVar(colVar), closure);

JAssignment sort = createAssignment(resultVar, invoke);
stms.add(sort);
}
// // Add the body to the foreach
// addStm(foreach, env.getStatements(self.getBody()));
// JAssignment append = createAssignment(resultVar, resultVar.getName() ".prepend(" env.getVar(self.getBody()).getName() ")");
Expand All @@ -922,7 946,10 @@ public void toIteratorExp(IteratorExp self, boolean isMutable) {
env.bind(self, resultVar, stms);
return;
}


// create a new var for the result and initialize
JVariableDeclaration resultVar = createResultVar(self, stms, isMutable);

// - put the statements within the body
JForeach foreach = createForeach(colVar, env.getVar(it));
stms.add(foreach);
Expand Down Expand Up @@ -1163,6 1190,13 @@ public void inIntegerExp(IntegerExp self) {
env.bind(self, newVar, Collections.singletonList( createAssignment(newVar, self.getIntegerSymbol() "")));
}

@Override
public void inRealExp(RealExp self) {
JVariableDeclaration newVar = gen.addLocalVar(env.currentBlock(), "tmp", typ.createTypeRef("double"));
env.bind(self, newVar, Collections.singletonList( createAssignment(newVar, self.getRealSymbol() "")));
}


@Override
public void inTupleExp(TupleExp self) {
TupleTypeInformation type = getType(self);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 25,11 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
projects.put("/a2l.tests.imdb/transformations/FindCouples.atl", "a2l.tests.findcouples");
projects.put("/a2l.tests.java2uml/transformations/java2uml_deps.atl", "a2l.tests.java2uml.umldeps");
projects.put("/a2l.tests.java2graph/transformations/Java2Graph.atl", "a2l.tests.java2graph");
projects.put("/a2l.tests.airquality/transformations/AirQualityReport.atl", "a2l.tests.airquality");

List<TransformationToCompile> trafos = new ArrayList<CompileTestCasesHandler.TransformationToCompile>();
List<TransformationToCompile> trafosNoOps = new ArrayList<CompileTestCasesHandler.TransformationToCompile>();

for (Entry<String, String> path : projects.entrySet()) {
TransformationToCompile t = new TransformationToCompile(path.getKey());
HashSet<Optimisation> optimisations = new HashSet<Optimisation>();
Expand Down
1 change: 1 addition & 0 deletions plugins/lintra2.lib/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 6,7 @@ Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: a2l.runtime,
a2l.runtime.lintra,
a2l.runtime.stdlib,
com.google.common.cache,
com.google.common.collect,
javaslang,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 58,7 @@ public void execute() throws InterruptedException {
Scheduler scheduler = new Scheduler(numWorkers, numChunks);

Worker firstWorker = null;

increase = 1;
for (long i = 0; i < numWorkers; i ) {
Worker worker = new Worker("Worker " i, i, increase, scheduler);
if ( firstWorker == null ) {
Expand Down Expand Up @@ -99,7 99,7 @@ public void execute() throws InterruptedException {
}

public void postprocessing(IGlobalContext context) {
context.getGlobalTrace().pack();
// context.getGlobalTrace().pack();

for (ITransformation2 t : partialTrafos) {
t.doSequentialPostprocessing();
Expand Down
16 changes: 16 additions & 0 deletions plugins/lintra2.lib/src-engine/a2l/runtime/stdlib/Collections.java
Original file line number Diff line number Diff line change
@@ -0,0 1,16 @@
package a2l.runtime.stdlib;

import java.util.List;
import java.util.function.Function;

public class Collections {

@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> void sort(List<T> list, Function<T, Comparable<? extends Object>> map) {
java.util.Collections.sort(list, (v1, v2) -> {
Comparable<? extends Object> c1 = map.apply(v1);
Comparable<? extends Object> c2 = map.apply(v2);
return ((Comparable<Object>) c1).compareTo((Comparable<Object>) c2);
});
}
}
Loading

0 comments on commit 36320ca

Please sign in to comment.