Commit 6d4e2f6d authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ added filter for comments (//!) in exported word files. #60

parent c7debd5c
Loading
Loading
Loading
Loading
+78 −35
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.TerminalRule;
import org.eclipse.xtext.XtextPackage;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.etsi.mts.tdl.Behaviour;
import org.etsi.mts.tdl.Block;
@@ -459,16 +462,22 @@ public class Generator {
		if (behaviour instanceof CompoundBehaviour) {
			Block block = ((CompoundBehaviour) behaviour).getBlock();
			for (Behaviour b : block.getBehaviour()) {
				if (!b.getAnnotation().isEmpty()) { //TODO: skip altogether if no annotations found?
					if (b.getAnnotation().get(0).getKey().getName().startsWith("Initial")) {
						//TODO: filter extra new line
					String initialConditions = NodeModelUtils.getNode(b).getText();
						ICompositeNode node = NodeModelUtils.getNode(b);
						String initialConditions = node.getText();
						initialConditions = initialConditions.replaceAll("\\s*Initial conditions", "");
						initialConditions = filterSource(initialConditions, "\n", "\\w").trim();
						initialConditions = filterComments(node, initialConditions);

						map.put(Placeholders.INITIAL, initialConditions);
					} else if  (b.getAnnotation().get(0).getKey().getName().startsWith("Expected")) {
					String expected = NodeModelUtils.getNode(b).getText();
						ICompositeNode node = NodeModelUtils.getNode(b);
						String expected = node.getText();
						expected = expected.replaceAll("\\s*Expected behaviour", "");
						expected = filterSource(expected, "\n", "ensure").trim();
						expected = filterComments(node, expected);
						map.put(Placeholders.EXPECTED, expected);
						
						EList<Behaviour> expectedBehaviours = ((CompoundBehaviour) b).getBlock().getBehaviour();
@@ -477,17 +486,22 @@ public class Generator {
							String then = NodeModelUtils.getNode(expectedBehaviours.get(1)).getText();
							//TODO: a bit of a hack
							when = filterSource(when, "\n", "\\s\\s\\s\\s\\w");
							when = filterComments(node, when);
							//then = filterSource(then, "\n", "\\s+\\w");
							//then = filterSource(then, "\n", "\\s+\\w");
							then = filterSource(then, "\n", "\\s\\s\\s\\s\\w");
							then = filterComments(node, then);

							map.put(Placeholders.WHEN, "when {"+when+"\n}");
							map.put(Placeholders.THEN, "then {"+then+"\n}");
						}
					} else if  (b.getAnnotation().get(0).getKey().getName().startsWith("Final")) {
					String finalConditions = NodeModelUtils.getNode(b).getText();
						ICompositeNode node = NodeModelUtils.getNode(b);
						String finalConditions = node.getText();
						finalConditions = finalConditions.replaceAll("\\s*Final conditions", "");
						finalConditions = filterSource(finalConditions, "\n", "\\w").trim();
						finalConditions = filterComments(node, finalConditions);

						map.put(Placeholders.FINAL, finalConditions);
						//TODO: filter extra new line
					} else {
@@ -495,10 +509,32 @@ public class Generator {
					}
				}
			}
		}

		return map;
	}

	private String filterComments(ICompositeNode node, String expected) {
		//DONE: filter empty lines?
		//DONE: export to method 
		//TODO: update, expose
		String filterPrefix = "//!";
		ArrayList<String> filteredComments = new ArrayList<>();
		for (var n : node.getLeafNodes()) {
			if (n.getGrammarElement() instanceof TerminalRule && ((TerminalRule) n.getGrammarElement()).getName().equals("SL_COMMENT")) {
				if (n.getText().trim().startsWith(filterPrefix)) {
					filteredComments.add(n.getText().trim());
				}
			}
		}
		for (var c : filteredComments) {
			expected = expected.replace(c, ""); 
		}
		expected = expected.replaceAll("\n[\\s\\t]+\n", "\n");
		System.out.println(expected);
		return expected;
	}

	
	private LinkedHashMap<String, String> getSTOReplacementMap(StructuredTestObjective sto) {
		//map
@@ -527,16 +563,21 @@ public class Generator {
		map.put(Placeholders.INITIAL, "");
		if (sto.getInitialConditions()!=null) {
			//TODO: filter extra new line
			String initialConditions = NodeModelUtils.getNode(sto.getInitialConditions()).getText();
			ICompositeNode node = NodeModelUtils.getNode(sto.getInitialConditions());
			String initialConditions = node.getText();
			initialConditions = initialConditions.replaceAll("\\s*Initial conditions", "");
			initialConditions = filterSource(initialConditions, "\n", "\\w").trim();
			initialConditions = filterComments(node, initialConditions);

			map.put(Placeholders.INITIAL, initialConditions);
		}
		
		if (sto.getExpectedBehaviour()!=null) {
			String expected = NodeModelUtils.getNode(sto.getExpectedBehaviour()).getText();
			ICompositeNode node = NodeModelUtils.getNode(sto.getExpectedBehaviour());
			String expected = node.getText();
			expected = expected.replaceAll("\\s*Expected behaviour", "");
			expected = filterSource(expected, "\n", "ensure").trim();
			expected = filterComments(node, expected);
			map.put(Placeholders.EXPECTED, expected);
		} else {
			map.put(Placeholders.EXPECTED, "");
@@ -545,9 +586,11 @@ public class Generator {
		map.put(Placeholders.FINAL, "");
		if (sto.getFinalConditions()!=null) {
			//TODO: filter extra new line
			String finalConditions = NodeModelUtils.getNode(sto.getFinalConditions()).getText();
			ICompositeNode node = NodeModelUtils.getNode(sto.getFinalConditions());
			String finalConditions = node.getText();
			finalConditions = finalConditions.replaceAll("\\s*Final conditions", "");
			finalConditions = filterSource(finalConditions, "\n", "\\w").trim();
			finalConditions = filterComments(node, finalConditions);
			map.put(Placeholders.FINAL, finalConditions);
		}