AQL: dominance treeRelation
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java
index e52e018..6589659 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java
@@ -229,7 +229,7 @@
 			// TODO generalize operator
 			// TODO capture variableExprs
 			LinkedHashMap<String, Object> group = makeGroup("treeRelation");
-			group.put("treeRelation", getNodeCat(node.getChild(1).getChild(0)));
+			group.put("treeRelation", parseOperatorNode(node.getChild(1).getChild(0)));
 			List<Object> operands = (List<Object>) group.get("operands");
 			for (ParseTree refOrNode : getChildrenWithCat(node, "refOrNode")) {
 				String ref = refOrNode.getChild(0).toStringTree(parser).substring(1);
@@ -316,8 +316,42 @@
 
 
 
-	private Map<? extends String, ? extends Object> parseTextSpec(ParseTree node) {
-		HashMap<String, Object> term = new HashMap<String, Object>();
+	private LinkedHashMap<String, Object> parseOperatorNode(ParseTree operatorNode) {
+		LinkedHashMap<String, Object> treeRelation = new LinkedHashMap<String, Object>();
+		treeRelation.put("@type", "korap:treeRelation");
+		String operator = getNodeCat(operatorNode);
+		// TODO complete (check Antlr grammar)
+		if (operator.equals("dominance")) {
+			treeRelation.put("reltype", "dominance");
+			ParseTree leftChildSpec = getFirstChildWithCat(operatorNode, "@l");
+			ParseTree rightChildSpec = getFirstChildWithCat(operatorNode, "@r");
+			ParseTree edgeSpec = getFirstChildWithCat(operatorNode, "edgeSpec");
+			System.err.println(edgeSpec);
+			if (leftChildSpec != null) treeRelation.put("index", 0);
+			if (rightChildSpec != null) treeRelation.put("index", -1);
+			if (edgeSpec != null) {
+				for (ParseTree edgeAnno : getChildrenWithCat(edgeSpec, "edgeAnno")) {
+					treeRelation.putAll(parseEdgeAnno(edgeAnno));
+				}
+			}
+		}
+		return treeRelation;
+	}
+
+	private LinkedHashMap<String, Object> parseEdgeAnno(
+			ParseTree edgeAnnoSpec) {
+		LinkedHashMap<String, Object> edgeAnno = new LinkedHashMap<String, Object>();
+		ParseTree qNameNode = edgeAnnoSpec.getChild(0);
+		ParseTree matchOperatorNode = edgeAnnoSpec.getChild(1);
+		ParseTree textSpecNode = edgeAnnoSpec.getChild(2);
+		edgeAnno.putAll(parseQNameNode(qNameNode));
+		edgeAnno.putAll(parseTextSpec(textSpecNode));
+		edgeAnno.put("match", parseMatchOperator(matchOperatorNode));
+		return edgeAnno;
+	}
+
+	private LinkedHashMap<String, Object> parseTextSpec(ParseTree node) {
+		LinkedHashMap<String, Object> term = new LinkedHashMap<String, Object>();
 		if (hasChild(node, "regex")) {
 			term.put("type", "type:regex");
 			term.put("key", node.getChild(0).getChild(0).toStringTree(parser).replaceAll("/", ""));
@@ -376,12 +410,13 @@
 	
 	@SuppressWarnings({ "unchecked" })
 	private void putIntoSuperObject(LinkedHashMap<String, Object> object, int objStackPosition) {
-		if (distributedOperandsLists.size()>0) {
-			ArrayList<ArrayList<Object>> distributedOperands = distributedOperandsLists.pop();
-			for (ArrayList<Object> operands : distributedOperands) {
-				operands.add(object);
-			}
-		} else if (objectStack.size()>objStackPosition) {
+//		if (distributedOperandsLists.size()>0) {
+//			ArrayList<ArrayList<Object>> distributedOperands = distributedOperandsLists.pop();
+//			for (ArrayList<Object> operands : distributedOperands) {
+//				operands.add(object);
+//			}
+//		} else if (objectStack.size()>objStackPosition) {
+		if (objectStack.size()>objStackPosition) {
 			ArrayList<Object> topObjectOperands = (ArrayList<Object>) objectStack.get(objStackPosition).get("operands");
 			topObjectOperands.add(0, object);
 			
@@ -456,7 +491,9 @@
 			"tok!=/Frau/",
 			"node",
 			"treetagger/pos=\"NN\"",
-			"node & node & #2 > #1"
+			
+			"node & node & #2 >@r[foundry/layer=\"key\"] #1",
+			"node & node & #2 > #1",
 			};
 //		AqlTree.verbose=true;
 		for (String q : queries) {
diff --git a/src/test/java/AqlTreeTest.java b/src/test/java/AqlTreeTest.java
index 43b72ab..f5ae532 100644
--- a/src/test/java/AqlTreeTest.java
+++ b/src/test/java/AqlTreeTest.java
@@ -30,12 +30,12 @@
 	public void testSingleTokens() throws QueryException {
 		// "Mann"
 		query = "\"Mann\"";
-		String token1 = "{@type=korap:token, wrap={@type=korap:term, match=match:eq, key=Mann}}";
+		String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}";
 		assertTrue(equalsQueryContent(token1, query));
 		
 		// [orth!=Frau]
 		query = "tok!=\"Frau\"";
-		String token2 = "{@type=korap:token, wrap={@type=korap:term, match=match:ne, key=Frau}}";
+		String token2 = "{@type=korap:token, wrap={@type=korap:term, key=Frau, match=match:ne}}";
 		assertTrue(equalsQueryContent(token2, query));
 		
 		// Mann
@@ -57,7 +57,7 @@
 	@Test
 	public void testRegex() throws QueryException {
 		query = "/Mann/";
-		String regex1 = "{@type=korap:token, wrap={@type=korap:term, match=match:eq, type=type:regex, key=Mann}}";
+		String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}";
 		aqlt = new AqlTree(query);
 		map = aqlt.getRequestMap().get("query").toString();
 		assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
@@ -66,13 +66,13 @@
 	@Test
 	public void testLayers() throws QueryException {
 		query = "cnx/cat=\"NP\"";
-		String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, match=match:eq, key=NP}";
+		String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}";
 		aqlt = new AqlTree(query);
 		map = aqlt.getRequestMap().get("query").toString();
 		assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", ""));
 		
 		query = "treetagger/pos=\"NN\"";
-		String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, match=match:eq, key=NN}}";
+		String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}";
 		aqlt = new AqlTree(query);
 		map = aqlt.getRequestMap().get("query").toString();
 		assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", ""));
@@ -85,11 +85,46 @@
 				"{@type=korap:group, operation=operation:treeRelation, operands=[" +
 						"{@type=korap:span}," +
 						"{@type=korap:span}" +
-				"], treeRelation=dominance}";
+				"], treeRelation={@type=korap:treeRelation, reltype=dominance}" +
+				"}";
 		aqlt = new AqlTree(query);
 		map = aqlt.getRequestMap().get("query").toString();
 		assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
+		
+		query = "\"Mann\" & node & #2 > #1";
+		String dom2 = 
+				"{@type=korap:group, operation=operation:treeRelation, operands=[" +
+						"{@type=korap:span}," +
+						"{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
+				"], treeRelation={@type=korap:treeRelation, reltype=dominance}" +
+				"}";
+		aqlt = new AqlTree(query);
+		map = aqlt.getRequestMap().get("query").toString();
+		assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
+		
+		query = "\"Mann\" & node & #2 >[cnx/cat=\"NP\"] #1";
+		String dom3 = 
+				"{@type=korap:group, operation=operation:treeRelation, operands=[" +
+						"{@type=korap:span}," +
+						"{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
+				"], treeRelation={@type=korap:treeRelation, reltype=dominance, foundry=cnx, layer=cat, key=NP, match=match:eq}" +
+				"}";
+		aqlt = new AqlTree(query);
+		map = aqlt.getRequestMap().get("query").toString();
+		assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", ""));
+		
+		query = "\"Mann\" & node & #2 >@l[cnx/cat=\"NP\"] #1";
+		String dom4 = 
+				"{@type=korap:group, operation=operation:treeRelation, operands=[" +
+						"{@type=korap:span}," +
+						"{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
+				"], treeRelation={@type=korap:treeRelation, reltype=dominance, index=0, foundry=cnx, layer=cat, key=NP, match=match:eq}" +
+				"}";
+		aqlt = new AqlTree(query);
+		map = aqlt.getRequestMap().get("query").toString();
+		assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", ""));
 	}
 	
+	
 }