Commit 805b533b authored by Philip Makedonski's avatar Philip Makedonski
Browse files

* made trimming optional, refined constraint exporter to export grammar and model structure as well

parent 97a69f0d
Loading
Loading
Loading
Loading
Loading
+147 −2
Original line number Diff line number Diff line
@@ -8,8 +8,10 @@ import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.etsi.mts.tdl.document.model.Content;
import org.etsi.mts.tdl.document.model.Document;
@@ -17,6 +19,8 @@ import org.etsi.mts.tdl.document.model.Document;
public class ConstraintExporter {

	private String generatedConstraints = "";
	private String generatedModel = "";
	private String generatedGrammar = "";

	private OutputFormat format;

@@ -27,8 +31,8 @@ public class ConstraintExporter {
	public static void main(String[] args) {

		OutputFormat format = OutputFormat.EVL;
		String filename = "resources/es_20311901v010501p.docx";
		filename = "resources/es_20311904v010401p.docx";
		String filename = "resources/es_20311901v010802e.docx";
//		filename = "resources/es_20311904v010502e.docx";
		String[] extraImports = new String[0];
		String generatedPackage = "tdl";

@@ -60,8 +64,149 @@ public class ConstraintExporter {
		String targetPath = "resources/generated/tdl-generated-" + title
				+ (format == OutputFormat.EVL ? ".evl" : ".ocl");
		exporter.exportConstraints(doc, generatedPackage, extraImports, targetPath);
		exporter.exportModel(doc, generatedPackage, extraImports, "resources/generated/tdl-generated-" + title+".txt", true);
		//part 8
		filename = "resources/es_20311908v010302e.docx";
//		filename = "resources/es_20311904v010502e.docx";
		Document grammarDoc = DocumentHandler.loadDocument(new File(filename).getAbsolutePath(), false);
		exporter.exportGrammar(grammarDoc, generatedPackage, extraImports, "resources/generated/tdl-generated-" + title+"-grammar.txt", true);
	}

	public void exportGrammar(Document doc, String generatedPackage, String[] extraImports, String generatedConstraintsPath, boolean skipSemantics) {
		generatedGrammar = "";
		exportGrammar(doc, skipSemantics);
		Path gcp = Path.of(generatedConstraintsPath);
		try {
			Files.write(gcp, Arrays.asList(generatedGrammar.split("\n")), StandardOpenOption.WRITE,
					StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	private void exportGrammar(Content c, boolean skipSemantics) {
		// DONE: Escape keywords
		// DONE: Export to file tdl-gen.evl (separate function)
		// TODO: Rename old one to tdl-ref.evl
		// TODO: Compare / Merge latest -> tdl.evl
		// TODO: Backport changes to source word document,
		// based on diff to tdl-gen.evl
		// generate again and repeat
		// -> in progress...
		// TODO: Test with change marks
		// TODO: Clean up all the lets
		// DONE: Handle special case of CompatibleConfiguration or
		// TODO: Restructure so that no special handling is needed in the future

		for (Content cx : c.getContent()) {
			if (cx.getText().equals("Concrete Textual Notation")) {
				String content = getGrammar(cx, skipSemantics);
				generatedGrammar += content;
			}
			exportGrammar(cx, skipSemantics);
		}
	}

	private String getGrammar(Content cx, boolean skipSemantics) {
		String content = "";
		String element = cx.getParent().getText().replaceAll(".+\\s", "");
		content += "//"+element+"\n";
		Map<String, Content> clause = cx.getParent().getContent().stream().collect(Collectors.toMap(e->e.getText(), e->e));
		String syntax = String.join("\n", cx.getContent().stream().map(e->e.getText()).collect(Collectors.toList()));
		if (syntax.startsWith("Void") ) {
			syntax = "//"+syntax;
		}
		content += syntax;
		content += "\n\n";
		return content;
	}
	
	public void exportModel(Document doc, String generatedPackage, String[] extraImports, String generatedConstraintsPath, boolean skipSemantics) {
		generatedModel = "";
		exportModel(doc, skipSemantics);
		Path gcp = Path.of(generatedConstraintsPath);
		try {
			Files.write(gcp, Arrays.asList(generatedModel.split("\n")), StandardOpenOption.WRITE,
					StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	private void exportModel(Content c, boolean skipSemantics) {
		// DONE: Escape keywords
		// DONE: Export to file tdl-gen.evl (separate function)
		// TODO: Rename old one to tdl-ref.evl
		// TODO: Compare / Merge latest -> tdl.evl
		// TODO: Backport changes to source word document,
		// based on diff to tdl-gen.evl
		// generate again and repeat
		// -> in progress...
		// TODO: Test with change marks
		// TODO: Clean up all the lets
		// DONE: Handle special case of CompatibleConfiguration or
		// TODO: Restructure so that no special handling is needed in the future

		for (Content cx : c.getContent()) {
			if (cx.getText().equals("Semantics")) {
				String content = getModel(cx, skipSemantics);
				generatedModel += content;
			}
			exportModel(cx, skipSemantics);
		}
	}
	
	private String getModel(Content cx, boolean skipSemantics) {
		String content = "";
		Map<String, Content> clause = cx.getParent().getContent().stream().collect(Collectors.toMap(e->e.getText(), e->e));
		String superType = "";
		if (clause.containsKey("Generalization")) {
			superType = clause.get("Generalization").getContent().get(0).getText(); //generalisation
			if (!superType.startsWith("There is no")) {
				superType = " extends "+superType;
			}
		} else {
			System.out.println("No supertype for: "+cx.getParent().getText());
		}
		String semantics = String.join("\n", cx.getContent().stream().map(e->e.getText()).collect(Collectors.toList()));
		if (!skipSemantics) {
			content += "\t--"+semantics.replaceAll("\n", "\n\t--")+"\n";
		}
		content += "\tclass " + cx.getParent().getText().replaceAll(".+\\s", "") + superType+"";
		boolean hasProperties = false;
		var propertiesText = "";
		if (clause.containsKey("Properties")) {
			var properties = clause.get("Properties").getContent();
			for (Content cp : properties) {
				if (cp.getText().startsWith("There are no")) {
					//skip
				} else {
					String[] propertyParts = cp.getText().split("\n");
					if (!skipSemantics) {
						if (propertyParts.length > 1) {
							propertiesText += "\t\t--"+propertyParts[1]+"\n";
						} else {
							propertiesText += "\t\t--NO DESCRIPTION?\n";
						}
					}
					propertiesText += "\t\tproperty "+propertyParts[0].replaceAll("/ ", "")+";\n";
					hasProperties = true;
				}
			}
		} else {
			//?
		}
		if (!hasProperties) {
			content +=";\n\n";
		} else {
			content += "\n\t{\n" +propertiesText+ "\t}\n\n";
		}
		return content;
	}


	public void exportConstraints(Document doc, String generatedPackage, String[] extraImports, String generatedConstraintsPath) {
		generatedConstraints = "";
		String imports = "";
+8 −1
Original line number Diff line number Diff line
@@ -15,6 +15,10 @@ import org.etsi.mts.tdl.document.model.Document;

public class DocumentHandler {
	public static Document loadDocument(String filename) {
		return loadDocument(filename, true);
	}
	
	public static Document loadDocument(String filename, boolean trim) {
		Document doc = new Document();
		try {
			if (!filename.endsWith(".docx") && !filename.startsWith("~$")) {
@@ -35,7 +39,10 @@ public class DocumentHandler {
			Content toc = null;
			for (IBodyElement e : bodyElements) {
				if (e instanceof XWPFParagraph) {
					String text = ((XWPFParagraph) e).getText().trim();
					String text = ((XWPFParagraph) e).getText();
					if (trim) {
						text = text.trim();
					}
//					System.out.println(text);
//					if (text.startsWith("8.2.1")) {
//						System.out.println("hit");