Skip to content

Commit cae323e

Browse files
committed
added generators
1 parent 24ce1bf commit cae323e

File tree

17 files changed

+1164
-38
lines changed

17 files changed

+1164
-38
lines changed

src/info.computationalmodeling.lang.codegen.parent/info.computationalmodeling.lang.codegen.compiler/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ dependencies {
55
api group: 'commons-io', name: 'commons-io', version: '2.0.1'}
66

77
apply plugin: 'application'
8-
mainClassName = "info.computationalmodeling.lang.codegen.compiler.ComputationalModellingCompiler"
8+
mainClassName = "info.computationalmodeling.lang.codegen.compiler.ComputationalModelingCompiler"
99

1010
startScripts {
11-
applicationName = 'computational-modelling-codegen'
11+
applicationName = 'computational-modeling-codegen'
1212
}
1313

1414
task standaloneStartScript(type: CreateStartScripts) {
15-
mainClassName = 'info.computationalmodeling.lang.codegen.compiler.ComputationalModellingCompiler'
16-
applicationName = 'computational-modelling-codegen'
15+
mainClassName = 'info.computationalmodeling.lang.codegen.compiler.ComputationalModelingCompiler'
16+
applicationName = 'computational-modeling-codegen'
1717
outputDir = new File(project.buildDir, 'scripts')
1818
classpath = jar.outputs.files + project.configurations.runtimeClasspath
1919
}
@@ -27,7 +27,7 @@ jar {
2727
manifest {
2828
attributes 'Implementation-Title': '5XIE0 Code Generator',
2929
'Implementation-Version': '1.0',
30-
'Main-Class': 'info.computationalmodeling.lang.codegen.compiler.ComputationalModellingCompiler'
30+
'Main-Class': 'info.computationalmodeling.lang.codegen.compiler.ComputationalModelingCompiler'
3131
}
3232
}
3333

src/info.computationalmodeling.lang.codegen.parent/info.computationalmodeling.lang.codegen.compiler/src/main/java/org/xtext/computationalmodelling/codegen/compiler/ComputationalModellingCompiler.java renamed to src/info.computationalmodeling.lang.codegen.parent/info.computationalmodeling.lang.codegen.compiler/src/main/java/info/computationalmodeling/codegen/compiler/ComputationalModelingCompiler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import com.google.inject.Inject;
3030
import com.google.inject.Provider;
3131

32-
public class ComputationalModellingCompiler {
32+
public class ComputationalModelingCompiler {
3333

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

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

5353
public static void main(String[] args) {
@@ -109,8 +109,10 @@ public static void main(String[] args) {
109109
}
110110

111111
if (injector != null) {
112-
ComputationalModellingCompiler comp = injector.getInstance(ComputationalModellingCompiler.class);
112+
ComputationalModelingCompiler comp = injector.getInstance(ComputationalModelingCompiler.class);
113113
comp.runGenerator(filename, outputdir);
114+
} else {
115+
exitError("Failed to create injector.");
114116
}
115117
}
116118

src/info.computationalmodeling.lang.codegen.parent/info.computationalmodeling.lang/src/main/java/info/computationalmodeling/lang/generator/DataflowGenerator.xtend

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import org.eclipse.emf.ecore.resource.Resource
77
import org.eclipse.xtext.generator.AbstractGenerator
88
import org.eclipse.xtext.generator.IFileSystemAccess2
99
import org.eclipse.xtext.generator.IGeneratorContext
10+
import info.computationalmodeling.lang.DataflowRuntimeModule
11+
import com.google.inject.Guice
1012

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

20+
com.google.inject.Injector injector = Guice.createInjector(new DataflowRuntimeModule());
21+
DataflowGeneratorGraphviz genA = new DataflowGeneratorGraphviz();
22+
DataflowGeneratorSDF3 genB = new DataflowGeneratorSDF3();
23+
1824
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
19-
// fsa.generateFile('greetings.txt', 'People to greet: ' +
20-
// resource.allContents
21-
// .filter(Greeting)
22-
// .map[name]
23-
// .join(', '))
25+
26+
// generate both Graphviz and SDF3 files at the same time
27+
28+
// first Graphviz
29+
injector.injectMembers(genA);
30+
genA.doGenerate(resource, fsa, context);
31+
32+
// then SDF3
33+
injector.injectMembers(genB);
34+
genB.doGenerate(resource, fsa, context);
2435
}
2536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* generated by Xtext 2.19.0
3+
*/
4+
package info.computationalmodeling.lang.generator
5+
6+
import org.eclipse.emf.ecore.resource.Resource
7+
import org.eclipse.xtext.generator.AbstractGenerator
8+
import org.eclipse.xtext.generator.IFileSystemAccess2
9+
import org.eclipse.xtext.generator.IGeneratorContext
10+
import com.google.inject.Inject
11+
import org.eclipse.xtext.naming.IQualifiedNameProvider
12+
import info.computationalmodeling.lang.dataflow.DataflowModel
13+
import info.computationalmodeling.lang.dataflow.Edge
14+
import info.computationalmodeling.lang.DataflowSupport
15+
16+
/**
17+
* Generates code from your model files on save.
18+
*
19+
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
20+
*/
21+
class DataflowGeneratorGraphviz extends AbstractGenerator {
22+
23+
24+
@Inject extension IQualifiedNameProvider
25+
26+
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
27+
28+
29+
for (m : resource.allContents.toIterable.filter(DataflowModel)) {
30+
var ds = new DataflowSupport()
31+
ds.getChannelNames(m)
32+
ds.extractActorProperties(m)
33+
ds.extractInputOutputNames(m)
34+
ds.extractChannelProperties(m)
35+
ds.determinePortNames(m)
36+
fsa.generateFile(
37+
m.fullyQualifiedName.toString("/") + ".dot",
38+
m.compile(ds)
39+
)
40+
}
41+
}
42+
43+
def compile(DataflowModel m, DataflowSupport ds) '''
44+
digraph «m.name» {
45+
rankdir="LR";
46+
graph [bgcolor=transparent,overlap=false]
47+
node [fontsize=20 fontname="Calibri" fillcolor="#FDF498" width=0.6 penwidth=2 style=filled shape=circle]
48+
edge [fontsize=16 fontname="Calibri"]
49+
«this.compileActors(m, ds)»
50+
«this.compileInputs(m, ds)»
51+
«this.compileOutputs(m, ds)»
52+
«this.compileGraph(m, ds)»
53+
}
54+
'''
55+
56+
def compileActors(DataflowModel m, DataflowSupport ds) '''
57+
«FOR a: ds.setOfActors(m)»
58+
«a» [label="«a»\n«ds.getExecutionTime(a)»"]
59+
«ENDFOR»
60+
61+
'''
62+
63+
def compileInputs(DataflowModel m, DataflowSupport ds) '''
64+
«FOR i:m.inputs»
65+
«i.name» [shape=point, label="«i.name»", fillcolor="#000000" width=0.05 style=filled]
66+
«ENDFOR»
67+
68+
'''
69+
70+
def compileOutputs(DataflowModel m, DataflowSupport ds) '''
71+
«FOR o:m.outputs»
72+
«o.name» [shape=point, label="«o.name»", fillcolor="#000000" width=0.05 style=filled]
73+
«ENDFOR»
74+
75+
'''
76+
77+
def compileGraph(DataflowModel m, DataflowSupport ds) '''
78+
«FOR e:m.edges»
79+
«this.compileEdge(m, e, ds)»
80+
«ENDFOR»
81+
82+
'''
83+
84+
def String prodLabel(Edge e) {
85+
}
86+
87+
def compileInputEdge(Edge e, DataflowSupport ds) '''
88+
«e.srcact.name» -> «e.dstact.name» [minlen=1 len=1 xlabel="" headlabel="" taillabel="«e.srcact.name»"]
89+
'''
90+
91+
def compileOutputEdge(Edge e, DataflowSupport ds) '''
92+
«e.srcact.name» -> «e.dstact.name» [minlen=1 len=1 xlabel="" headlabel="«e.dstact.name»" taillabel=""]
93+
'''
94+
95+
def compileRegularEdge(Edge e, DataflowSupport ds) '''
96+
«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»"]
97+
'''
98+
99+
100+
def compileEdge(DataflowModel m, Edge e, DataflowSupport ds) '''
101+
«IF ds.inputNames.contains(e.srcact.name)»
102+
«compileInputEdge(e, ds)»
103+
«ELSE»
104+
«IF ds.outputNames.contains(e.dstact.name)»
105+
«compileOutputEdge(e, ds)»
106+
«ELSE»
107+
«compileRegularEdge(e, ds)»
108+
«ENDIF»
109+
«ENDIF»
110+
'''
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* generated by Xtext 2.19.0
3+
*/
4+
package info.computationalmodeling.lang.generator
5+
6+
import org.eclipse.emf.ecore.resource.Resource
7+
import org.eclipse.xtext.generator.AbstractGenerator
8+
import org.eclipse.xtext.generator.IFileSystemAccess2
9+
import org.eclipse.xtext.generator.IGeneratorContext
10+
import com.google.inject.Inject
11+
import org.eclipse.xtext.naming.IQualifiedNameProvider
12+
import info.computationalmodeling.lang.dataflow.DataflowModel
13+
import info.computationalmodeling.lang.DataflowSupport
14+
import java.util.Map
15+
16+
/**
17+
* Generates code from your model files on save.
18+
*
19+
* See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#code-generation
20+
*/
21+
class DataflowGeneratorSDF3 extends AbstractGenerator {
22+
23+
24+
@Inject extension IQualifiedNameProvider
25+
26+
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
27+
28+
29+
for (m : resource.allContents.toIterable.filter(DataflowModel)) {
30+
var ds = new DataflowSupport()
31+
ds.getChannelNames(m)
32+
ds.extractActorProperties(m)
33+
ds.extractChannelProperties(m)
34+
ds.determinePortNames(m)
35+
fsa.generateFile(
36+
m.fullyQualifiedName.toString("/") + ".sdfx",
37+
m.compile(ds))
38+
}
39+
}
40+
41+
42+
43+
// Below the code generation methods
44+
45+
def compile(DataflowModel m, DataflowSupport ds) '''
46+
<?xml version="1.0" encoding="ISO-8859-1"?>
47+
<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">
48+
<applicationGraph name="«m.name»">
49+
<sdf name="«m.name»" type="«m.name»">
50+
«this.compileActorlist(m, ds
51+
«this.compileChannellist(m, ds
52+
</sdf>
53+
<sdfProperties>
54+
«this.compileActorProperties(m, ds
55+
«this.compileChannelProperties(m, ds
56+
</sdfProperties>
57+
</applicationGraph>
58+
</sdf3>
59+
'''
60+
61+
62+
def compileActorlist(DataflowModel m, DataflowSupport ds) '''
63+
«FOR a: ds.setOfActors(m
64+
<actor name="«a»" type="«a»">
65+
«this.compilePortsOfActor(a, ds
66+
</actor>
67+
«ENDFOR»
68+
'''
69+
70+
def compilePortsOfActor(String a, DataflowSupport ds) '''
71+
«FOR p: ds.getPortsOfActor(a).entrySet»
72+
«this.compilePort(p
73+
«ENDFOR»
74+
'''
75+
76+
def compilePort(Map.Entry<String,Pair<Integer,String>> p) '''
77+
<port type="«p.getValue().getValue()»" name="«p.getKey()»" rate="«p.getValue().getKey()»"/>
78+
'''
79+
80+
def compileChannellist(DataflowModel m, DataflowSupport ds) '''
81+
«FOR e: m.edges»
82+
<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")»"/>
83+
«ENDFOR»
84+
'''
85+
86+
def compileActorProperties(DataflowModel m, DataflowSupport ds)'''
87+
«FOR a: ds.setOfActors(m
88+
<actorProperties actor="«a»">
89+
<processor type="p1" default="true">
90+
<executionTime time="«ds.getExecutionTime(a).toString()»"/>
91+
</processor>
92+
</actorProperties>
93+
«ENDFOR»
94+
'''
95+
96+
def compileChannelProperties(DataflowModel m, DataflowSupport ds)'''
97+
«FOR e: m.edges»
98+
<channelProperties channel="«ds.channelNames.get(e)»"/>
99+
«ENDFOR»
100+
'''
101+
102+
103+
}

src/info.computationalmodeling.lang.codegen.parent/info.computationalmodeling.lang/src/main/java/info/computationalmodeling/lang/generator/FiniteStateAutomataGenerator.xtend

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import org.eclipse.emf.ecore.resource.Resource
77
import org.eclipse.xtext.generator.AbstractGenerator
88
import org.eclipse.xtext.generator.IFileSystemAccess2
99
import org.eclipse.xtext.generator.IGeneratorContext
10+
import info.computationalmodeling.lang.FiniteStateAutomataRuntimeModule
11+
import com.google.inject.Guice
1012

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

20+
com.google.inject.Injector injector = Guice.createInjector(new FiniteStateAutomataRuntimeModule());
21+
FiniteStateAutomataGeneratorGraphviz genA = new FiniteStateAutomataGeneratorGraphviz();
22+
//FiniteStateAutomataGeneratorB genB = new FiniteStateAutomataGeneratorB();
23+
1824
override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
19-
// fsa.generateFile('greetings.txt', 'People to greet: ' +
20-
// resource.allContents
21-
// .filter(Greeting)
22-
// .map[name]
23-
// .join(', '))
25+
26+
// generate graphviz image
27+
injector.injectMembers(genA);
28+
genA.doGenerate(resource, fsa, context);
29+
30+
// then some other generator perhaps
31+
// injector.injectMembers(genB);
32+
//genB.doGenerate(resource, fsa, context);
2433
}
34+
2535
}

0 commit comments

Comments
 (0)