Commit be23e0ae authored by Philip Makedonski's avatar Philip Makedonski
Browse files

* added model comparison to constraint exporter

parent 66b09e33
Loading
Loading
Loading
Loading
+107 −2
Changes for plugins/org.etsi.mts.tdl.constraints.helper/src/org/etsi/mts/tdl/constraints/helper/ConstraintExporter.java: 107 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ public class ConstraintExporter {

	private String generatedConstraints = "";
	private String generatedModel = "";
	private ModelIR documentModel = new ModelIR();
	private String generatedGrammar = "";

	private OutputFormat format;
@@ -91,6 +92,12 @@ public class ConstraintExporter {
			exporter.exportModel(doc, generatedPackage, extraImports, "resources/generated/tdl-generated-" + title + ".txt", true);
			constraints = exporter.getRecords();
			System.out.println("Extracted " + constraints.size() + " invariants from " + filename + " -> " + targetPath);
			//TODO: cleanup / extract
			String ecorePath = "../org.etsi.mts.tdl.model/model/tdl.ecore";
			//TODO: expose in options
			//TODO: separate better
			exporter.compareModel(doc, generatedPackage, extraImports, title, false, ecorePath);
			//TODO: extract end
			// part 8
			String grammarFile = "resources/es_20311908v010302e.docx";
			if (new File(grammarFile).exists()) {
@@ -254,7 +261,7 @@ public class ConstraintExporter {
		if (syntax.startsWith("Void") ) {
			syntax = "//"+syntax;
		}
		content += syntax;
		content += syntax.replaceAll(";\n", ";\n\n");
		content += "\n\n";
		return content;
	}
@@ -272,6 +279,19 @@ public class ConstraintExporter {
		}		
	}

	public void compareModel(Document doc, String generatedPackage, String[] extraImports, String generatedConstraintsPath, boolean skipSemantics, String ecorePath) {
		generatedModel = "";
		documentModel = new ModelIR();
		documentModel.packageName = doc.getFilename();
		documentModel.packageNsURI = "DOCX";

		exportModel(doc, skipSemantics);
		var roots = ModelComparison.loadModel(ecorePath);
		ModelIR ecoreModel = ModelComparison.buildModelIR(roots);
		ModelComparison.compareModels(documentModel, ecoreModel);
	}

	
	private void exportModel(Content c, boolean skipSemantics) {
		// DONE: Escape keywords
		// DONE: Export to file tdl-gen.evl (separate function)
@@ -288,7 +308,7 @@ public class ConstraintExporter {

		for (Content cx : c.getContent()) {
			if (cx.getText().equals("Semantics")) {
				String content = getModel(cx, skipSemantics);
				String content = getModelIR(cx, skipSemantics);
				generatedModel += content;
			}
			exportModel(cx, skipSemantics);
@@ -303,6 +323,59 @@ public class ConstraintExporter {
			superType = clause.get("Generalization").getContent().get(0).getText(); //generalisation
			if (!superType.startsWith("There is no")) {
				superType = " extends "+superType;
			} else {
				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;
	}

	private String getModelIR(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 {
				superType = "";
			}
		} else {
			System.out.println("No supertype for: "+cx.getParent().getText());
@@ -312,6 +385,12 @@ public class ConstraintExporter {
			content += "\t--"+semantics.replaceAll("\n", "\n\t--")+"\n";
		}
		content += "\tclass " + cx.getParent().getText().replaceAll(".+\\s", "") + superType+"";
		
		ClassIR classIR = new ClassIR();
		classIR.name = cx.getParent().getText().replaceAll(".+\\s", "");
		classIR.superType = superType.replaceAll(" extends ", "");
		documentModel.classes.add(classIR);
		
		boolean hasProperties = false;
		var propertiesText = "";
		if (clause.containsKey("Properties")) {
@@ -329,6 +408,32 @@ public class ConstraintExporter {
						}
					}
					propertiesText += "\t\tproperty "+propertyParts[0].replaceAll("/ ", "")+";\n";
					
					String pText = propertyParts[0].replaceAll("/ ", "");
					Pattern pat = Pattern.compile("(\\w+)\\s?:\\s?(\\w+)\\s?\\[(.+)\\].*");
					Matcher m = pat.matcher(pText);
					if (m.find()) {
						PropertyIR p = new PropertyIR();
						p.name = m.group(1);
						p.typeName = m.group(2);
						String cardinality = m.group(3).replaceAll("\\*", "-1");
						if (cardinality.contains("..")) {
							String[] c = cardinality.split("\\.\\.");
							p.lower = Integer.parseInt(c[0]);
							p.upper = Integer.parseInt(c[1]);
						} else {
							p.upper = Integer.parseInt(cardinality);
							if (p.upper == -1) {
								p.lower = 0;
							} else {
								p.lower = p.upper;
							}
						}
						classIR.properties.add(p);
					} else {
						System.out.println("Failed to parse: "+pText);
					}

					hasProperties = true;
				}
			}