Newer
Older
package org.etsi.mts.tdl.standalone;
import java.io.File;
import java.io.FilenameFilter;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
Philip Makedonski
committed
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.epsilon.evl.execute.UnsatisfiedConstraint;
Philip Makedonski
committed
import org.eclipse.ocl.pivot.ExpressionInOCL;
import org.eclipse.ocl.pivot.utilities.OCL;
import org.eclipse.ocl.pivot.utilities.ParserException;
import org.eclipse.ocl.pivot.utilities.Query;
import org.eclipse.xtext.EcoreUtil2;
import org.etsi.mts.tdl.GateType;
Philip Makedonski
committed
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.asn2tdl.ASN2TDLTranslator;
import org.etsi.mts.tdl.constraints.evl.Validator;
import org.etsi.mts.tdl.helper.TDLHelper;
import org.etsi.mts.tdl.openapi2tdl.next.OpenAPI2TDLTranslatorNext;
import org.etsi.mts.tdl.tools.to.docx.poi.Generator;
import org.etsi.mts.tdl.transform.AbstractTranslator;
public class Standalone {
static String sourceExtension = "tdltx";
static String targetExtension = "tdltx";
static String openapiExtension = "yaml";
static String asnExtension = "asn";
public static void main(String[] args) throws Exception {
Standalone app = new Standalone();
//folder
String path = "examples/basics";
Philip Makedonski
committed
path = "examples/basics";
app.processElements(path, app::listElements);
//TODO: may fail
app.processElements(path, app::validate);
//TODO: may fail
// app.processElements(path, app::translate);
Philip Makedonski
committed
TDLHelper.resetResourceSet();
path = "examples/validation/Example.tdltx";
//TODO: can only be used in isolation as it interferes with OCL delegates afterwards..
// app.processElements(path, app::validateOCL);
//TODO: may fail due to literal value use without data type
// app.processElements(path, app::validate);
app.processElements(path, app::exportDoc);
Philip Makedonski
committed
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
TDLHelper.resetResourceSet();
path = "examples/openapi";
app.processElements(path, openapiExtension, app::importOpenAPI);
//NOTE: keep in mind weird errors if legacy OpenAPI importer is imported as well in manifest (especially if it is first)
TDLHelper.resetResourceSet();
path = "examples/asn1";
app.processElements(path, asnExtension, app::importASN1);
//individual file without dependencies
TDLHelper.resetResourceSet();
path = "examples/basics/Sample.tdltx";
app.processElements(path, app::listElements);
//individual file with missing dependencies
TDLHelper.resetResourceSet();
path = "examples/basics/ImportSample.tdltx";
app.processElements(path, app::listElements);
//validation will fail due to unresolved items
//TODO: minimal example for TO translation
//TODO: minimal example for TTCN-3 generation
//TODO: clean up keywords automatically
}
private void processElements(String path, Consumer<String> operation) throws Exception {
processElements(path, sourceExtension, operation);
}
private void processElements(String path, String extension, Consumer<String> operation) throws Exception {
File target = new File(path);
//TODO: make recursive?
if (target.isDirectory()) {
FilenameFilter filter = (FilenameFilter) (dir, name) -> name.endsWith(extension);
File[] files = target.listFiles(filter);
Arrays.sort(files, (a, b) -> a.getName().compareTo(b.getName()));
//load all resources first
if (extension.contains("tdl")) {
for (File f : files) {
System.out.println("Loading: "+f.getAbsolutePath());
TDLHelper.load(f.getAbsolutePath());
}
TDLHelper.link();
TDLHelper.check();
}
for (File f : files) {
System.out.println("Processing: "+f.getAbsolutePath());
operation.accept(f.getAbsolutePath());
}
} else {
if (extension.contains("tdl")) {
System.out.println("Loading: "+target.getAbsolutePath());
TDLHelper.load(target.getAbsolutePath());
TDLHelper.link();
TDLHelper.check();
}
System.out.println("Processing: "+target.getAbsolutePath());
operation.accept(target.getAbsolutePath());
}
}
private void listElements(String path) {
Resource resource = TDLHelper.load(path);
Package p = (Package) resource.getContents().get(0);
System.out.println("Package: "+p.getName());
p.getPackagedElement().forEach(e -> {
System.out.println(" " + e.getName());
System.out.println(" " + e.eClass().getName() + " : " + e.getQualifiedName());
});
}
private void validate(String path) {
Resource resource = TDLHelper.load(path);
Validator validator = new Validator();
//TODO: make robust against unresolved imports
//TODO: report parsing / resolution errors
try {
List<UnsatisfiedConstraint> violations = validator.validate(resource);
validator.dumpViolations(violations, true);
} catch (Exception e) {
e.printStackTrace();
}
}
Philip Makedonski
committed
private void validateOCL(String path) {
Resource resource = TDLHelper.load(path);
OCL ocl = OCL.newInstance(TDLHelper.getResourceSet());
EClass contextEClass = tdlPackage.Literals.GATE_TYPE;
try {
ExpressionInOCL query = ocl.createQuery(contextEClass,
"self.allDataTypes()"
);
Query queryEval = ocl.createQuery(query);
for (var o : EcoreUtil2.getAllContentsOfType(resource.getContents().get(0), GateType.class)) {
System.out.println(o.allDataTypes());
System.out.println(queryEval.evaluateUnboxed(o));
}
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
private void translate(String path) {
Resource resource = TDLHelper.load(path);
try {
Resource target = TDLHelper.create(resource.getURI().path() + "." + targetExtension);
target.getContents().addAll(EcoreUtil.copyAll(resource.getContents()));
TDLHelper.store(target);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void importData(String path, AbstractTranslator translator) {
File source = new File(path);
try {
translator.setTargetResource(TDLHelper.create(source.getAbsolutePath()+"-generated."+targetExtension));
translator.initTargetResource(translator.cleanName(source.getName()));
translator.translate(source.getAbsolutePath());
TDLHelper.store(translator.getTargetResource());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void importOpenAPI(String path) {
importData(path, new OpenAPI2TDLTranslatorNext());
}
private void importASN1(String path) {
importData(path, new ASN2TDLTranslator());
}
private void exportDoc(String path) {
Resource resource = TDLHelper.load(path);
Generator generator = new Generator();
String target = path+".docx";
try {
generator.generate(
resource, target,
"Generated from "+resource.getURI().lastSegment(),
"TO_4_TABLE_TEMPLATE_EDITHELP"
);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// private void importOpenAPIAll(String path) {
// File target = new File(path);
// if (target.isDirectory()) {
// FilenameFilter filter = (FilenameFilter) (dir, name) -> name.endsWith(sourceExtension);
// File[] files = target.listFiles(filter);
// Arrays.sort(files, (a, b) -> a.getName().compareTo(b.getName()));
// for (File f : files) {
// importOpenAPISingle(f.getAbsolutePath());
// }
// } else {
// importOpenAPISingle(target.getAbsolutePath());
// }
// }
}