Skip to content

Commit

Permalink
added generators
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcG-TUE committed May 8, 2022
1 parent 24ce1bf commit cae323e
Show file tree
Hide file tree
Showing 17 changed files with 1,164 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ dependencies {
api group: 'commons-io', name: 'commons-io', version: '2.0.1'}

apply plugin: 'application'
mainClassName = "info.computationalmodeling.lang.codegen.compiler.ComputationalModellingCompiler"
mainClassName = "info.computationalmodeling.lang.codegen.compiler.ComputationalModelingCompiler"

startScripts {
applicationName = 'computational-modelling-codegen'
applicationName = 'computational-modeling-codegen'
}

task standaloneStartScript(type: CreateStartScripts) {
mainClassName = 'info.computationalmodeling.lang.codegen.compiler.ComputationalModellingCompiler'
applicationName = 'computational-modelling-codegen'
mainClassName = 'info.computationalmodeling.lang.codegen.compiler.ComputationalModelingCompiler'
applicationName = 'computational-modeling-codegen'
outputDir = new File(project.buildDir, 'scripts')
classpath = jar.outputs.files + project.configurations.runtimeClasspath
}
Expand All @@ -27,7 +27,7 @@ jar {
manifest {
attributes 'Implementation-Title': '5XIE0 Code Generator',
'Implementation-Version': '1.0',
'Main-Class': 'info.computationalmodeling.lang.codegen.compiler.ComputationalModellingCompiler'
'Main-Class': 'info.computationalmodeling.lang.codegen.compiler.ComputationalModelingCompiler'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import com.google.inject.Inject;
import com.google.inject.Provider;

public class ComputationalModellingCompiler {
public class ComputationalModelingCompiler {

@Inject
private Provider<ResourceSet> resourceSetProvider;
Expand All @@ -47,7 +47,7 @@ private static void exitError(String msg) {

private static void showUsage() {
System.out.println("Usage:");
System.out.println("computational-modelling-codegen <filename.sdf> <outputdir>");
System.out.println("computational-modeling-codegen <filename.sdf> <outputdir>");
}

public static void main(String[] args) {
Expand Down Expand Up @@ -109,8 +109,10 @@ public static void main(String[] args) {
}

if (injector != null) {
ComputationalModellingCompiler comp = injector.getInstance(ComputationalModellingCompiler.class);
ComputationalModelingCompiler comp = injector.getInstance(ComputationalModelingCompiler.class);
comp.runGenerator(filename, outputdir);
} else {
exitError("Failed to create injector.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import info.computationalmodeling.lang.DataflowRuntimeModule
import com.google.inject.Guice

/**
* Generates code from your model files on save.
Expand All @@ -15,11 +17,20 @@ import org.eclipse.xtext.generator.IGeneratorContext
*/
class DataflowGenerator extends AbstractGenerator {

com.google.inject.Injector injector = Guice.createInjector(new DataflowRuntimeModule());
DataflowGeneratorGraphviz genA = new DataflowGeneratorGraphviz();
DataflowGeneratorSDF3 genB = new DataflowGeneratorSDF3();

override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
// fsa.generateFile('greetings.txt', 'People to greet: ' +
// resource.allContents
// .filter(Greeting)
// .map[name]
// .join(', '))

// generate both Graphviz and SDF3 files at the same time

// first Graphviz
injector.injectMembers(genA);
genA.doGenerate(resource, fsa, context);

// then SDF3
injector.injectMembers(genB);
genB.doGenerate(resource, fsa, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* generated by Xtext 2.19.0
*/
package info.computationalmodeling.lang.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import com.google.inject.Inject
import org.eclipse.xtext.naming.IQualifiedNameProvider
import info.computationalmodeling.lang.dataflow.DataflowModel
import info.computationalmodeling.lang.dataflow.Edge
import info.computationalmodeling.lang.DataflowSupport

/**
* Generates code from your model files on save.
*
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
*/
class DataflowGeneratorGraphviz extends AbstractGenerator {


@Inject extension IQualifiedNameProvider

override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {


for (m : resource.allContents.toIterable.filter(DataflowModel)) {
var ds = new DataflowSupport()
ds.getChannelNames(m)
ds.extractActorProperties(m)
ds.extractInputOutputNames(m)
ds.extractChannelProperties(m)
ds.determinePortNames(m)
fsa.generateFile(
m.fullyQualifiedName.toString("/") + ".dot",
m.compile(ds)
)
}
}

def compile(DataflowModel m, DataflowSupport ds) '''
digraph «m.name» {
rankdir="LR";
graph [bgcolor=transparent,overlap=false]
node [fontsize=20 fontname="Calibri" fillcolor="#FDF498" width=0.6 penwidth=2 style=filled shape=circle]
edge [fontsize=16 fontname="Calibri"]
«this.compileActors(m, ds)»
«this.compileInputs(m, ds)»
«this.compileOutputs(m, ds)»
«this.compileGraph(m, ds)»
}
'''
def compileActors(DataflowModel m, DataflowSupport ds) '''
«FOR a: ds.setOfActors(m)»
«a» [label="«a»\n«ds.getExecutionTime(a)»"]
«ENDFOR»

'''
def compileInputs(DataflowModel m, DataflowSupport ds) '''
«FOR i:m.inputs»
«i.name» [shape=point, label="«i.name»", fillcolor="#000000" width=0.05 style=filled]
«ENDFOR»

'''
def compileOutputs(DataflowModel m, DataflowSupport ds) '''
«FOR o:m.outputs»
«o.name» [shape=point, label="«o.name»", fillcolor="#000000" width=0.05 style=filled]
«ENDFOR»

'''
def compileGraph(DataflowModel m, DataflowSupport ds) '''
«FOR e:m.edges»
«this.compileEdge(m, e, ds)»
«ENDFOR»

'''
def String prodLabel(Edge e) {
}
def compileInputEdge(Edge e, DataflowSupport ds) '''
«e.srcact.name» -> «e.dstact.name» [minlen=1 len=1 xlabel="" headlabel="" taillabel="«e.srcact.name»"]
'''
def compileOutputEdge(Edge e, DataflowSupport ds) '''
«e.srcact.name» -> «e.dstact.name» [minlen=1 len=1 xlabel="" headlabel="«e.dstact.name»" taillabel=""]
'''
def compileRegularEdge(Edge e, DataflowSupport ds) '''
«e.srcact.name» -> «e.dstact.name» [minlen=3 len=3 xlabel="«ds.getInitialTokens(e).toString»" headlabel="«ds.getConsRate(e).toString»" taillabel="«ds.getProdRate(e).toString»"]
'''
def compileEdge(DataflowModel m, Edge e, DataflowSupport ds) '''
«IF ds.inputNames.contains(e.srcact.name)»
«compileInputEdge(e, ds)»
«ELSE»
«IF ds.outputNames.contains(e.dstact.name)»
«compileOutputEdge(e, ds)»
«ELSE»
«compileRegularEdge(e, ds)»
«ENDIF»
«ENDIF»
'''
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* generated by Xtext 2.19.0
*/
package info.computationalmodeling.lang.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import com.google.inject.Inject
import org.eclipse.xtext.naming.IQualifiedNameProvider
import info.computationalmodeling.lang.dataflow.DataflowModel
import info.computationalmodeling.lang.DataflowSupport
import java.util.Map

/**
* Generates code from your model files on save.
*
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
*/
class DataflowGeneratorSDF3 extends AbstractGenerator {


@Inject extension IQualifiedNameProvider

override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {


for (m : resource.allContents.toIterable.filter(DataflowModel)) {
var ds = new DataflowSupport()
ds.getChannelNames(m)
ds.extractActorProperties(m)
ds.extractChannelProperties(m)
ds.determinePortNames(m)
fsa.generateFile(
m.fullyQualifiedName.toString("/") + ".sdfx",
m.compile(ds))
}
}



// Below the code generation methods

def compile(DataflowModel m, DataflowSupport ds) '''
<?xml version="1.0" encoding="ISO-8859-1"?>
<sdf3 xsi:noNamespaceSchemaLocation="http://www.es.ele.tue.nl/sdf3/xsd/sdf3-sdf.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" type="sdf">
<applicationGraph name="«m.name»">
<sdf name="«m.name»" type="«m.name»">
«this.compileActorlist(m, ds
«this.compileChannellist(m, ds
</sdf>
<sdfProperties>
«this.compileActorProperties(m, ds
«this.compileChannelProperties(m, ds
</sdfProperties>
</applicationGraph>
</sdf3>
'''


def compileActorlist(DataflowModel m, DataflowSupport ds) '''
«FOR a: ds.setOfActors(m
<actor name="«a»" type="«a»">
«this.compilePortsOfActor(a, ds
</actor>
«ENDFOR»
'''

def compilePortsOfActor(String a, DataflowSupport ds) '''
«FOR p: ds.getPortsOfActor(a).entrySet»
«this.compilePort(p
«ENDFOR»
'''

def compilePort(Map.Entry<String,Pair<Integer,String>> p) '''
<port type="«p.getValue().getValue()»" name="«p.getKey()»" rate="«p.getValue().getKey()»"/>
'''

def compileChannellist(DataflowModel m, DataflowSupport ds) '''
«FOR e: m.edges»
<channel name="«ds.channelNames.get(e)»" dstPort="«ds.getDstPortName(e)»" dstActor="«e.dstact.name»" srcPort="«ds.getSrcPortName(e)»" srcActor="«e.srcact.name»" initialTokens="«ds.channelProperties.get(ds.channelNames.get(e)).get("initialtokens")»"/>
«ENDFOR»
'''

def compileActorProperties(DataflowModel m, DataflowSupport ds)'''
«FOR a: ds.setOfActors(m
<actorProperties actor="«a»">
<processor type="p1" default="true">
<executionTime time="«ds.getExecutionTime(a).toString()»"/>
</processor>
</actorProperties>
«ENDFOR»
'''

def compileChannelProperties(DataflowModel m, DataflowSupport ds)'''
«FOR e: m.edges»
<channelProperties channel="«ds.channelNames.get(e)»"/>
«ENDFOR»
'''


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.AbstractGenerator
import org.eclipse.xtext.generator.IFileSystemAccess2
import org.eclipse.xtext.generator.IGeneratorContext
import info.computationalmodeling.lang.FiniteStateAutomataRuntimeModule
import com.google.inject.Guice

/**
* Generates code from your model files on save.
Expand All @@ -15,11 +17,19 @@ import org.eclipse.xtext.generator.IGeneratorContext
*/
class FiniteStateAutomataGenerator extends AbstractGenerator {

com.google.inject.Injector injector = Guice.createInjector(new FiniteStateAutomataRuntimeModule());
FiniteStateAutomataGeneratorGraphviz genA = new FiniteStateAutomataGeneratorGraphviz();
//FiniteStateAutomataGeneratorB genB = new FiniteStateAutomataGeneratorB();

override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
// fsa.generateFile('greetings.txt', 'People to greet: ' +
// resource.allContents
// .filter(Greeting)
// .map[name]
// .join(', '))

// generate graphviz image
injector.injectMembers(genA);
genA.doGenerate(resource, fsa, context);

// then some other generator perhaps
// injector.injectMembers(genB);
//genB.doGenerate(resource, fsa, context);
}

}
Loading

0 comments on commit cae323e

Please sign in to comment.