-
Notifications
You must be signed in to change notification settings - Fork 32
/
OpaqueThingMLCompiler.java
158 lines (139 loc) · 6.38 KB
/
OpaqueThingMLCompiler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*/
package org.thingml.compilers.utils;
import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Date;
import org.eclipse.xtext.validation.Issue;
import org.thingml.compilers.ThingMLCompiler;
import org.thingml.compilers.configuration.CfgBuildCompiler;
import org.thingml.compilers.configuration.CfgMainGenerator;
import org.thingml.compilers.thing.NewThingActionCompiler;
import org.thingml.compilers.thing.ThingActionCompiler;
import org.thingml.compilers.thing.ThingApiCompiler;
import org.thingml.compilers.thing.ThingImplCompiler;
import org.thingml.utilities.logging.Logger;
import org.thingml.xtext.constraints.ThingMLHelpers;
import org.thingml.xtext.helpers.AnnotatedElementHelper;
import org.thingml.xtext.thingML.Configuration;
import org.thingml.xtext.thingML.ThingMLModel;
/**
* Created by ffl on 24.11.14.
*/
public abstract class OpaqueThingMLCompiler extends ThingMLCompiler {
PrintStream m, e;
private void initializeStream() {
final OutputStream stream = getMessageStream();
if (stream != null) {
m = new PrintStream(stream);
e = new PrintStream(stream);
}
}
public OpaqueThingMLCompiler(ThingActionCompiler thingActionCompiler, ThingApiCompiler thingApiCompiler, CfgMainGenerator mainCompiler, CfgBuildCompiler cfgBuildCompiler, ThingImplCompiler thingImplCompiler) {
super(thingActionCompiler, thingApiCompiler, mainCompiler, cfgBuildCompiler, thingImplCompiler);
initializeStream();
}
public OpaqueThingMLCompiler(NewThingActionCompiler thingActionCompiler, ThingApiCompiler thingApiCompiler, CfgMainGenerator mainCompiler, CfgBuildCompiler cfgBuildCompiler, ThingImplCompiler thingImplCompiler) {
super(thingActionCompiler, thingApiCompiler, mainCompiler, cfgBuildCompiler, thingImplCompiler);
initializeStream();
}
public void println(String msg) {
if (m != null)
m.println(msg);
else
System.out.println(msg);
}
public void erroln(String msg) {
if (e != null)
e.println(msg);
else
System.err.println(msg);
}
public void printStack(String msg, Throwable t) {
erroln(msg);
erroln(t.toString());
for(StackTraceElement ste : t.getStackTrace()) {
erroln("\t" + ste.toString());
}
}
@Override
public boolean compile(Configuration cfg, Logger log, String... options) {
// Abort if the configuration contains annotation @compiler for other compilers (i.e. it is not compatible with this compiler)
if (AnnotatedElementHelper.hasAnnotation(cfg, "compiler")) {
if (!AnnotatedElementHelper.isDefined(cfg, "compiler", this.getID())) {
log.info("Skipping " + getName() + " compiler on configuration " + cfg.getName() + " because of @compiler annotation present and does not include " +this.getID()+ " [" + new Date() + "]");
return false;
}
}
log.info("Running " + getName() + " compiler on configuration " + cfg.getName() + " [" + new Date() + "]");
//Saving the complete model, e.g. to get all required inputs if there is a problem in the compiler
ThingMLModel flatModel = ThingMLHelpers.flattenModel(ThingMLHelpers.findContainingModel(cfg));
try {
saveAsThingML(flatModel, new File(ctx.getOutputDirectory(), cfg.getName() + "_merged.thingml").getAbsolutePath());
} catch (Exception e) {
System.err.println("Could not save merged ThingML file!");
}
//Run validation
log.info("Checking configuration...");
final long startChecker = System.currentTimeMillis();
final boolean isValid = checker.validateConfiguration(cfg);
log.info("Checking configuration... Done! Took " + (System.currentTimeMillis() - startChecker) + " ms.");
if (isValid) {
String location = "";
for (Issue error : checker.getWarnings()) {
if (error.getUriToProblem()!=null && error.getUriToProblem().toFileString()!=null && !location.equals(error.getUriToProblem().toFileString())) {
log.info("Warnings(s) in " + error.getUriToProblem().toFileString());
location = error.getUriToProblem().toFileString();
}
log.info("\t[line " + error.getLineNumber() + "]: " + error.getMessage());
}
location = "";
for (Issue error : checker.getInfos()) {
if (error.getUriToProblem()!=null && error.getUriToProblem().toFileString()!=null && !location.equals(error.getUriToProblem().toFileString())) {
log.debug("Infos(s) in " + error.getUriToProblem().toFileString());
location = error.getUriToProblem().toFileString();
}
log.debug("\t[line " + error.getLineNumber() + "]: " + error.getMessage());
}
//Compile
final long start = System.currentTimeMillis();
if (do_call_compiler(cfg, log, options)) {
log.info("Compilation complete [" + new Date() + "]. Took " + (System.currentTimeMillis() - start) + " ms.");
return true;
}
log.error("Compilation failed [" + new Date() + "]. Took " + (System.currentTimeMillis() - start) + " ms.");
} else {
String location = "";
for (Issue error : checker.getErrors()) {
if (error.getUriToProblem()!=null && error.getUriToProblem().toFileString()!=null && !location.equals(error.getUriToProblem().toFileString())) {
log.error("Error(s) in " + error.getUriToProblem().toFileString());
location = error.getUriToProblem().toFileString();
}
log.error("\t[line " + error.getLineNumber() + "]: " + error.getMessage());
}
}
// Failed
return false;
}
@Override
public void compileConnector(String connector, Configuration cfg, String... options) {
println("Running connector compiler " + connector + " on configuration " + cfg.getName());
super.compileConnector(connector, cfg, options);
}
public abstract boolean do_call_compiler(Configuration cfg, Logger log, String... options);
}