Skip to content

Commit

Permalink
Footprint generator was wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
jesusc committed Jan 14, 2020
1 parent 747eef9 commit 6e74696
Show file tree
Hide file tree
Showing 25 changed files with 432 additions and 427 deletions.
75 changes: 50 additions & 25 deletions plugins/lintra.atlcompiler/src/a2l/compiler/FootprintGenerator.java
Original file line number Diff line number Diff line change
@@ -1,7 1,5 @@
package a2l.compiler;

import static linda.atlcompiler.CreationHelpers.createTextExp;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -14,18 12,17 @@
import a2l.driver.IMetaDriver;
import a2l.utils.A2LUtils;
import anatlyzer.atl.analyser.IAnalyserResult;
import anatlyzer.atl.analyser.namespaces.MetamodelNamespace;
import anatlyzer.atl.footprint.TrafoMetamodelData;
import anatlyzer.atl.model.ATLModel;
import anatlyzer.atl.types.Metaclass;
import anatlyzer.atl.util.ATLUtils;
import anatlyzer.atl.util.ATLUtils.ModelInfo;
import anatlyzer.atlext.ATL.InPatternElement;
import anatlyzer.atlext.ATL.MatchedRule;
import anatlyzer.atlext.ATL.Module;
import anatlyzer.atlext.ATL.SimpleInPatternElement;
import linda.atlcompiler.CreationHelpers;
import linda.atlcompiler.ITyping;
import linda.atlcompiler.LindaTyping;
import lintra.atlcompiler.javagen.JClass;
import lintra.atlcompiler.javagen.JConditional;
import lintra.atlcompiler.javagen.JConditionalBlock;
import lintra.atlcompiler.javagen.JMethod;
import lintra.atlcompiler.javagen.JParameter;
import lintra.atlcompiler.javagen.JavaGenModel;
Expand All @@ -49,8 46,25 @@ public FootprintGenerator(IAnalyserResult result, DriverConfiguration driverConf
public JavaGenModel generate(String basePkg) {
Module m = result.getATLModel().getModule();

List<MatchedRule> matchedRules = ATLUtils.getAllMatchedRules(result.getATLModel());

Set<EClass> allFootprintClasses = new HashSet<>();
HashMap<EClass, IMetaDriver> class2driver = new HashMap<EClass, IMetaDriver>();

for (MatchedRule mr : matchedRules) {
for (InPatternElement ipe : mr.getInPattern().getElements()) {
SimpleInPatternElement sipe = (SimpleInPatternElement) ipe;
if ( sipe.getInferredType() instanceof Metaclass ) {
Metaclass metaclass = ((Metaclass) sipe.getInferredType());
allFootprintClasses.add(metaclass.getKlass());
IMetaDriver d = drivers.get(metaclass.getModel().getName());
class2driver.put(metaclass.getKlass(), d);
}
}
}


/*
List<ModelInfo> inputs = ATLUtils.getModelInfo(result.getATLModel()).stream().filter(ModelInfo::isInput).collect(Collectors.toList());
for(ModelInfo i : inputs) {
MetamodelNamespace mm = result.getNamespaces().getNamespace(i.getMetamodelName());
Expand All @@ -59,14 73,16 @@ public JavaGenModel generate(String basePkg) {
TrafoMetamodelData footprint = new TrafoMetamodelData(result.getATLModel(), mm);
Set<EClass> classes = footprint.getDirectUsedClasses();
allFootprintClasses.addAll(classes);
IMetaDriver d = drivers.get(i.getMetamodelName());
for (EClass eClass : classes) {
class2driver.put(eClass, d);
}
}

*/


JavaGenModel jmodel = JavagenFactory.eINSTANCE.createJavaGenModel();
Expand Down Expand Up @@ -99,7 115,7 @@ private void createIsFootprint(JClass gclass, Set<EClass> allFootprintClasses, H
param1.setType(typ.createTypeRef("Object"));
method.getParameters().add(param1);

JConditional conditional = JavagenFactory.eINSTANCE.createJConditional();

List<EClass> sorted = allFootprintClasses.stream().sorted((c1, c2) -> {
if ( c1 == c2 )
return 0;
Expand All @@ -108,26 124,35 @@ private void createIsFootprint(JClass gclass, Set<EClass> allFootprintClasses, H
}
return -1;
}).collect(Collectors.toList());

for(EClass e : sorted) {
JConditionalBlock cond = JavagenFactory.eINSTANCE.createJConditionalBlock();


// Optimise a bit by generating a large "or"
String largeOr = sorted.stream().map(e -> {
IMetaDriver driver = class2driver.get(e);
String qName = driver.getClassQName(e);

//cond.setExpr(createTextExp("o.getClass() == " qName ".class"));
cond.setExpr(createTextExp("o instanceof " qName));

cond.getStatements().add(CreationHelpers.createReturn(CreationHelpers.createTextExp("true")));
conditional.getConditions().add(cond);
}
return "(o instanceof " qName ")";
}).collect(Collectors.joining(" || "));

JConditionalBlock cond = JavagenFactory.eINSTANCE.createJConditionalBlock();
cond.getStatements().add(CreationHelpers.createReturn(CreationHelpers.createTextExp("false")));
conditional.setElse(cond);
method.getStatements().add(CreationHelpers.createText("return " largeOr));


method.getStatements().add(conditional);

// JConditional conditional = JavagenFactory.eINSTANCE.createJConditional();
// for(EClass e : sorted) {
// JConditionalBlock cond = JavagenFactory.eINSTANCE.createJConditionalBlock();
//
// IMetaDriver driver = class2driver.get(e);
// String qName = driver.getClassQName(e);
//
// //cond.setExpr(createTextExp("o.getClass() == " qName ".class"));
// cond.setExpr(createTextExp("o instanceof " qName));
//
// cond.getStatements().add(CreationHelpers.createReturn(CreationHelpers.createTextExp("true")));
// conditional.getConditions().add(cond);
// }
//
// JConditionalBlock cond = JavagenFactory.eINSTANCE.createJConditionalBlock();
// cond.getStatements().add(CreationHelpers.createReturn(CreationHelpers.createTextExp("false")));
// conditional.setElse(cond);
// method.getStatements().add(conditional);
}


Expand Down
21 changes: 15 additions & 6 deletions plugins/lintra.emf/src/lintra/emf/EMFDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 23,7 @@
public class EMFDataSource implements IDataSource {

private Resource input;
private IFootprint footprint;
private IFootprint footprint = NullFootprint.INSTANCE;
private boolean alreadyFilled = false;

public EMFDataSource(Resource input) {
Expand All @@ -40,6 40,16 @@ public IdMapper getIdMapper() {
throw new UnsupportedOperationException("Not supported anymore");
}

private static class NullFootprint implements IFootprint {
private static NullFootprint INSTANCE = new NullFootprint();

@Override
public boolean inGlobal(Object o) {
return true;
}

}

public void fillList(Collection<Object> l, AllInstancesAdder adder) {
if ( alreadyFilled )
throw new IllegalArgumentException("Sanity check to make sure we don't fill several models with the same data source");
Expand All @@ -52,11 62,10 @@ public void fillList(Collection<Object> l, AllInstancesAdder adder) {
adder.accept(obj);
}

// If there is a footprint and the object won't appear, don't use it
if ( footprint != null && !footprint.inGlobal(obj) )
continue;

l.add(obj);
// Only add if it is in the footprint. By default we use NullFootprint which always add
if ( footprint.inGlobal(obj) ) {
l.add(obj);
}
}
}

Expand Down
92 changes: 74 additions & 18 deletions plugins/lintra2.lib/src-engine/a2l/runtime/DirectExecutor.java
Original file line number Diff line number Diff line change
@@ -1,10 1,12 @@
package a2l.runtime;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.LongStream;

import lintra2.blackboard.BlackboardException;
import lintra2.stats.IStatsRecorder;
Expand Down Expand Up @@ -35,32 37,33 @@ public DirectExecutor(int numThreads, ITransformationFactory factory, InputExten
}

public void execute() throws InterruptedException {
if (numThreads == 1) {
executeSingle();
return;
}


List<Worker> workers = new ArrayList<>();
List<Thread> threads = new ArrayList<>();

long lastId = extent.size();

int jobSize = LinTraParameters.JOB_SIZE;
jobSize = (int) ((lastId / (numThreads * 1.0)) / 100.0);
long increase = computeOptimalIncrease(numThreads, lastId, jobSize);
if ( increase == 0 ) {
increase = lastId;
}

increase = 1;
// int jobSize = LinTraParameters.JOB_SIZE;
// jobSize = (int) ((lastId / (numThreads * 1.0)) / 100.0);
// long increase = computeOptimalIncrease(numThreads, lastId, jobSize);
// if ( increase == 0 ) {
// increase = lastId;
// }
long increase = 1_000;

//System.out.println();
//System.out.println("Input extent is " lastId);
//System.out.println("Increase is " increase);
long numChunks = lastId / increase;

if (lastId % increase != 0)
numChunks ;
long numWorkers = Math.min(numThreads, numChunks);

Scheduler scheduler = new Scheduler(numWorkers, numChunks);
// System.out.println("With increase: " increase);
increase = 25;
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 @@ -100,6 103,20 @@ public void execute() throws InterruptedException {
}
}

private void executeSingle() {
ITransformation2 trafo = factory.create();
this.partialTrafos = new ArrayList<ITransformation2>();
partialTrafos.add(trafo);
for(long i = 0, len = extent.size(); i < len; i ) {
try {
trafo.transform(extent.get(i));
} catch (BlackboardException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

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

Expand Down Expand Up @@ -190,7 207,8 @@ private int computeOptimalIncrease(int numThreads, double numElems, int workExce

private static enum SchedulingKind {
DYNAMIC,
GUIDED
GUIDED,
NESTED
}

private final class Worker implements Runnable {
Expand Down Expand Up @@ -245,7 263,7 @@ public void run() {
currentChunk = nextChunk;
}
}
} else {
} else if (kind == SchedulingKind.GUIDED) {
int fragment = (int) currentChunk / numThreads;
int pos = (int) (currentChunk % numThreads);
long total = extent.size();
Expand Down Expand Up @@ -277,6 295,44 @@ public void run() {
long nextChunk = scheduler.getNextChunk2(currentChunk);
currentChunk = nextChunk;

} else if (kind == SchedulingKind.NESTED) {
long total = extent.size();
ForkJoinPool customThreadPool = new ForkJoinPool(2);

while ( true ) {
long min = currentChunk * chunkSize;
long max = Math.min(currentChunk * chunkSize chunkSize, total);

ForkJoinTask<?> future = customThreadPool.submit(() -> {
LongStream.rangeClosed(min, max).parallel().forEach(i -> {
try {
trafo.transform(extent.get(i));
} catch (BlackboardException e) {
e.printStackTrace();
return;
}

});
});

try {
future.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

long nextChunk = scheduler.getNextChunk(currentChunk);
if ( nextChunk == - 1 ) {
return;
} else {
currentChunk = nextChunk;
}
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 5,5 @@
public class AirQualityReportFootprint implements IFootprint{

public boolean inGlobal(java.lang.Object o){
if ( o instanceof airquality.Warning) {
return true;
}else if ( o instanceof airquality.Date) {
return true;
}else if ( o instanceof airquality.AirMeasurement) {
return true;
} else { return false;
}}}
return (o instanceof airquality.AirMeasurement);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 5,5 @@
public class AirQualityReportFootprint implements IFootprint{

public boolean inGlobal(java.lang.Object o){
if ( o instanceof airquality.Warning) {
return true;
}else if ( o instanceof airquality.AirMeasurement) {
return true;
}else if ( o instanceof airquality.Date) {
return true;
} else { return false;
}}}
return (o instanceof airquality.AirMeasurement);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 5,5 @@
public class DBLPv1Footprint implements IFootprint{

public boolean inGlobal(java.lang.Object o){
if ( o instanceof dblp.DBLP.InProceedings) {
return true;
}else if ( o instanceof dblp.DBLP.Author) {
return true;
} else { return false;
}}}
return (o instanceof dblp.DBLP.Author);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 5,5 @@
public class DBLPv1Footprint implements IFootprint{

public boolean inGlobal(java.lang.Object o){
if ( o instanceof dblp.DBLP.Author) {
return true;
}else if ( o instanceof dblp.DBLP.InProceedings) {
return true;
} else { return false;
}}}
return (o instanceof dblp.DBLP.Author);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 5,5 @@
public class DBLPv2Footprint implements IFootprint{

public boolean inGlobal(java.lang.Object o){
if ( o instanceof dblp.DBLP.InProceedings) {
return true;
}else if ( o instanceof dblp.DBLP.Author) {
return true;
} else { return false;
}}}
return (o instanceof dblp.DBLP.Author);
}}
Loading

0 comments on commit 6e74696

Please sign in to comment.