Commit 76bb37e1 authored by Georg Fette's avatar Georg Fette
Browse files

refactored a lot

parent 8b47a198
......@@ -19,6 +19,7 @@ public class CQLOperatorMapper {
aMap.addOp("", OperatorType.INTERVAL);
aMap.addOp("", OperatorType.LIST);
aMap.addOp("", OperatorType.TO_LIST);
aMap.addOp("return", OperatorType.RETURN);
aMap.addOp("and", OperatorType.AND);
aMap.addOp("or", OperatorType.OR);
......
......@@ -83,82 +83,40 @@ import org.cqframework.cql.gen.cqlParser.WithClauseContext;
import de.uniwue.misc.util.RegexUtil;
import de.uniwue.query.Expression;
import de.uniwue.query.GraphPath;
import de.uniwue.query.Mapper;
import de.uniwue.query.OperatorType;
import de.uniwue.query.Primitive;
import de.uniwue.query.Primitive.PrimitiveDataType;
import de.uniwue.query.PrimitiveDateTime;
import de.uniwue.query.QueryOperator;
import de.uniwue.query.System_2_Graph_Mapper;
public class CQL_2_Graph_Mapper extends Mapper {
private GraphPath currentPath;
private QueryOperator currentOp;
public class CQL_2_Graph_Mapper extends System_2_Graph_Mapper {
private boolean parsingReturnRoot;
private boolean parsingRetrieveClause;
private QueryOperator retrieveRoot;
private String[] aliasNames = { "A", "B", "C", "D", "E", "F" };
private int aliasIndex;
private List<String> availableAliasNames;
public CQL_2_Graph_Mapper() {
super(cqlParser.ruleNames);
}
public static String getSystem() {
return Graph_2_CQL_Mapper.system;
}
private void clear() {
root.clear();
currentOp = null;
protected void clear() {
super.clear();
parsingReturnRoot = false;
parsingRetrieveClause = false;
retrieveRoot = null;
aliasIndex = 0;
availableAliasNames = new ArrayList<String>(
Arrays.asList(new String[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }));
}
public void dispose() {
}
private String prettyPrint(ParseTree expression) {
String prettyTree = TreeUtils.toPrettyTree(expression, Arrays.asList(cqlParser.ruleNames));
return prettyTree;
}
private QueryOperator addOp(OperatorType aType) {
if (currentOp == null) {
currentOp = new QueryOperator(aType);
root.addParameter(currentOp);
} else {
currentOp = new QueryOperator(currentOp, aType);
}
return currentOp;
}
private QueryOperator insertOp(OperatorType aType) {
if (currentOp == null) {
addOp(aType);
} else {
QueryOperator lastOp = currentOp;
currentOp = currentOp.parents.get(0);
currentOp = new QueryOperator(currentOp, aType);
for (QueryOperator aParent : lastOp.parents) {
aParent.removeParameter(lastOp);
if (!aParent.getParameters().contains(currentOp)) {
aParent.addParameter(currentOp);
}
}
currentOp.addParameter(lastOp);
}
return currentOp;
}
private ExpressionContext parseWithAntlr(String aQueryString) {
ANTLRInputStream stream = new ANTLRInputStream(aQueryString);
cqlLexer lexer = new cqlLexer(stream);
......@@ -175,9 +133,22 @@ public class CQL_2_Graph_Mapper extends Mapper {
ExpressionContext e = parseWithAntlr(aQueryString);
currentOp = root;
parseExpression(e);
addLeafConceptOpsToReturn();
return root;
}
private void addLeafConceptOpsToReturn() {
if (root.getParameters().size() == 1) {
QueryOperator returnOp = addOp(OperatorType.RETURN);
Expression firstOp = root.getParameters().get(0);
String alias = availableAliasNames.get(0);
firstOp.alias = alias;
availableAliasNames.remove(alias);
QueryOperator aliasRef = addOp(OperatorType.ALIAS_REF);
aliasRef.addParameter(alias);
}
}
private QueryOperator parseQueryExpression(QueryExpressionContext qe) {
String prettyPrint = prettyPrint(qe);
QueryOperator lastOp = currentOp;
......@@ -272,14 +243,13 @@ public class CQL_2_Graph_Mapper extends Mapper {
QueryOperator lastCreatedOp = parseExpression(expression);
if (lastCreatedOp != null) {
lastCreatedOp.outputName = text;
} else if (getCurrentPath() != null) {
QueryOperator lastElement = getCurrentPath().getLastElement();
root.addParameter(lastElement);
lastElement.outputName = text;
} else if (lastPathElem != null) {
root.addParameter(lastPathElem);
lastPathElem.outputName = text;
} else {
throw new RuntimeException("unexpected");
}
currentPath = null;
lastPathElem = null;
}
private QueryOperator parseSortClauseContext(SortClauseContext sortClause) {
......@@ -313,7 +283,7 @@ public class CQL_2_Graph_Mapper extends Mapper {
} else if (qualifiedIdentifier != null) {
thisOp = parseQualifiedIdentifier(qualifiedIdentifier);
// the path has been added to the query, but it has vanished from the currentPath
setCurrentPath(new GraphPath(currentOp.getParameters().get(0).asOperator()));
lastPathElem = currentOp.getParameters().get(0).asOperator();
} else if (expression != null) {
thisOp = parseExpression(expression);
} else {
......@@ -321,13 +291,14 @@ public class CQL_2_Graph_Mapper extends Mapper {
}
String alias = aliasedQuerySource.alias().identifier().getText();
thisOp.alias = alias;
setCurrentPath(null);
availableAliasNames.remove(alias);
lastPathElem = null;
}
private QueryOperator startPathWithAliasRef(String text) {
QueryOperator alias = new QueryOperator(OperatorType.ALIAS_REF);
alias.addParameter(text);
setCurrentPath(new GraphPath(alias));
lastPathElem = alias;
return alias;
}
......@@ -336,17 +307,16 @@ public class CQL_2_Graph_Mapper extends Mapper {
List<QualifierContext> qualifiers = qualifiedIdentifier.qualifier();
for (QualifierContext aQualifier : qualifiers) {
String text = aQualifier.identifier().getText();
if (getCurrentPath() == null) {
if (lastPathElem == null) {
startPathWithAliasRef(text);
} else {
QueryOperator newElem = new QueryOperator(currentOp, getCurrentPath().getLastElement(),
text);
getCurrentPath().addElement(newElem);
QueryOperator newElem = new QueryOperator(currentOp, lastPathElem, text);
lastPathElem = newElem;
}
}
String text = qualifiedIdentifier.identifier().getText();
QueryOperator newElem = new QueryOperator(currentOp, getCurrentPath().getLastElement(), text);
getCurrentPath().addElement(newElem);
QueryOperator newElem = new QueryOperator(currentOp, lastPathElem, text);
lastPathElem = newElem;
return newElem;
}
......@@ -360,8 +330,9 @@ public class CQL_2_Graph_Mapper extends Mapper {
thisOp = root;
TerminologyContext terminology = retrieve.terminology();
if (terminology != null) {
String alias = aliasNames[aliasIndex++];
String alias = availableAliasNames.get(0);
root.alias = alias;
availableAliasNames.remove(alias);
currentOp.removeParameter(root);
thisOp = addOp(OperatorType.FILTER);
currentOp.addParameter(root);
......@@ -379,13 +350,12 @@ public class CQL_2_Graph_Mapper extends Mapper {
if (expression != null) {
// set the path because the following constraints are all related to the just created
// profile
retrieveRoot = root;
parsingRetrieveClause = true;
thisOp = parseExpression(expression);
parsingRetrieveClause = false;
}
}
setCurrentPath(new GraphPath(root));
lastPathElem = root;
currentOp = lastOp;
return thisOp;
}
......@@ -501,7 +471,7 @@ public class CQL_2_Graph_Mapper extends Mapper {
} else {
throw new RuntimeException("unexpected");
}
setCurrentPath(null);
lastPathElem = null;
parseExpression(expression.get(0));
currentOp = tempOp;
parseExpression(expression.get(1));
......@@ -521,7 +491,7 @@ public class CQL_2_Graph_Mapper extends Mapper {
} else {
throw new RuntimeException("unexpected");
}
setCurrentPath(null);
lastPathElem = null;
parseExpression(expression.get(0));
currentOp = tempOp;
parseExpression(expression.get(1));
......@@ -766,17 +736,19 @@ public class CQL_2_Graph_Mapper extends Mapper {
for (ExpressionContext anOp : parameters) {
String prettyPrint = prettyPrint(anOp);
if (parsingRetrieveClause) {
// all parts of the returns always start with the first root concept
QueryOperator aliasRef = new QueryOperator(OperatorType.ALIAS_REF);
aliasRef.addParameter(retrieveRoot.alias);
setCurrentPath(new GraphPath(aliasRef));
QueryOperator firstRootConcept = root.getFirstRootConcept();
aliasRef.addParameter(firstRootConcept.alias);
lastPathElem = aliasRef;
} else {
setCurrentPath(null);
lastPathElem = null;
}
parseExpression(anOp);
}
}
// after anOp with params, erase the path, so that further field calls begin from the start
setCurrentPath(null);
lastPathElem = null;
currentOp = lastOp;
return newOp;
}
......@@ -863,7 +835,7 @@ public class CQL_2_Graph_Mapper extends Mapper {
private QueryOperator parseMemberInvocationContext(MemberInvocationContext mi) {
String prettyPrint = prettyPrint(mi);
String text = mi.identifier().getText();
if (getCurrentPath() == null) {
if (lastPathElem == null) {
if (currentOp.type == OperatorType.FIELD) {
currentOp.addParameter(text);
return currentOp;
......@@ -877,18 +849,16 @@ public class CQL_2_Graph_Mapper extends Mapper {
QueryOperator anOp = currentOp.getParameters().get(0).asOperator();
if ((anOp.type == OperatorType.LAST) || (anOp.type == OperatorType.FIRST)) {
QueryOperator demotionConcept = getDemotionConcept(anOp);
setCurrentPath(new GraphPath(demotionConcept));
lastPathElem = demotionConcept;
} else if (anOp.type == OperatorType.FIELD) {
setCurrentPath(new GraphPath(
anOp.getParameters().get(anOp.getParameters().size() - 1).asConcept()));
lastPathElem = anOp.getParameters().get(anOp.getParameters().size() - 1).asConcept();
} else {
throw new RuntimeException("unexpected");
}
}
QueryOperator currentPathEnd = getCurrentPath().getLastElement();
QueryOperator aChildConcept = new QueryOperator(currentOp, currentPathEnd, text);
currentOp.removeParameter(currentPathEnd);
getCurrentPath().addElement(aChildConcept);
QueryOperator aChildConcept = new QueryOperator(currentOp, lastPathElem, text);
currentOp.removeParameter(lastPathElem);
lastPathElem = aChildConcept;
return aChildConcept;
}
}
......@@ -964,12 +934,4 @@ public class CQL_2_Graph_Mapper extends Mapper {
currentOp = lastOp;
}
private GraphPath getCurrentPath() {
return currentPath;
}
private void setCurrentPath(GraphPath currentPath) {
this.currentPath = currentPath;
}
}
......@@ -60,21 +60,11 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
public String visit(QueryOperator aQuery) {
initialize(aQuery);
String result = "";
for (Expression aReturnElem : aQuery.getParameters()) {
result = writeSelector(aReturnElem);
boolean first = true;
for (Expression anExp : root.getParameters()) {
String rootOp = writeExpr(anExp);
if (!rootOp.isEmpty()) {
if (first) {
first = false;
} else {
result += " and ";
}
result += rootOp;
}
}
}
Expression expression = aQuery.getParameters().get(0);
result = writeExpr(expression);
QueryOperator returnOp = aQuery.getParameters().get(1).asOperator();
String writeReturn = writeReturn(returnOp);
result += writeReturn;
return result.trim();
}
......@@ -104,18 +94,26 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
private String writeReturn(QueryOperator returnOp) {
String result = "";
if (returnOp.getParameters().size() > 1) {
result += "return Tuple { ";
boolean first = true;
for (Expression anExp : returnOp.getParameters()) {
if (!first) {
result += ", ";
Expression firstReturnParam = returnOp.getParameters().get(0);
if (firstReturnParam.isOperator()) {
QueryOperator firstReturnOp = firstReturnParam.asOperator();
if (firstReturnOp.type == OperatorType.ALIAS_REF) {
String alias = firstReturnOp.getParameters().get(0).asPrimitive().value;
if (alias.equals(root.getParameters().get(0).alias)) {
return "";
}
first = false;
result += anExp.outputName + ": " + writeExpr(anExp);
}
result += " }";
}
result += "return Tuple { ";
boolean first = true;
for (Expression anExp : returnOp.getParameters()) {
if (!first) {
result += ", ";
}
first = false;
result += anExp.outputName + ": " + writeExpr(anExp);
}
result += " }";
return result;
}
......@@ -135,7 +133,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
Set<QueryOperator> result = new HashSet<QueryOperator>();
for (QueryOperator aConc : concepts) {
while (!aliases.containsKey(aConc)) {
aConc = aConc.parents.get(0);
aConc = aConc.parent;
}
result.add(aConc);
}
......@@ -168,73 +166,43 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
return alias;
}
// private boolean isConnectedToAnotherConceptViaEquals(QueryOperator aConc) {
// boolean includedInEquals = false;
// for (QueryOperator aChildConc : aConc.getConceptsRecursive()) {
// for (QueryOperator anOp : aChildConc.parents) {
// if (anOp.type == OperatorType.EQUALS) {
// for (Expression aParam : anOp.getParameters()) {
// if ((aParam != aChildConc) && aParam.isConcept()) {
// return true;
// }
// }
// }
// }
// }
// return false;
// }
private Set<QueryOperator> getConceptsToBeAliased(Expression aQuery) {
Set<QueryOperator> result = new HashSet<QueryOperator>();
// for (QueryOperator aConc : root.getConceptsRecursive()) {
// if ((aConc.getChildConcepts().size() > 1) || isConnectedToAnotherConceptViaEquals(aConc)) {
// result.add(aConc);
// }
// }
// if (root.getRootConcepts().size() > 0) {
// result.add(root.getRootConcepts().get(0));
// }
return result;
}
private String writeSelector(Expression aQuery) {
String result = "";
Set<QueryOperator> conceptsToBeAliased = getConceptsToBeAliased(aQuery);
Set<QueryOperator> alreadyMentionedConcepts = new HashSet<QueryOperator>();
boolean first = true;
for (QueryOperator aConc : conceptsToBeAliased) {
if (!first) {
result += " with ";
}
result += "[" + aConc.getCode() + "] ";
String alias = getNextAlias(aConc);
result += alias + " ";
String suchThat = "";
List<QueryOperator> hullOfSuchThatOperators = getHullOfSuchThatOperators(aConc,
new ArrayList<QueryOperator>(alreadyMentionedConcepts));
boolean firstWithOps = true;
for (QueryOperator anOp : hullOfSuchThatOperators) {
if (firstWithOps) {
firstWithOps = false;
} else {
suchThat += " and ";
}
suchThat += writeOp(anOp);
alreadyHandledInWith.add(anOp);
// TODO: what with multiple parents ?
if ((anOp.parents.size() > 0) && (anOp.parents.get(0).type == OperatorType.EXISTS)) {
alreadyHandledInWith.add(anOp.parents.get(0));
}
}
if (!first && !suchThat.isEmpty()) {
result += "such that " + suchThat;
}
alreadyMentionedConcepts.add(aConc);
first = false;
}
return result;
}
// private String writeSelector(Expression aQuery) {
// String result = "";
// Set<QueryOperator> conceptsToBeAliased = getConceptsToBeAliased(aQuery);
// Set<QueryOperator> alreadyMentionedConcepts = new HashSet<QueryOperator>();
// boolean first = true;
// for (QueryOperator aConc : conceptsToBeAliased) {
// if (!first) {
// result += " with ";
// }
// result += "[" + aConc.getCode() + "] ";
// String alias = getNextAlias(aConc);
// result += alias + " ";
// String suchThat = "";
// List<QueryOperator> hullOfSuchThatOperators = getHullOfSuchThatOperators(aConc,
// new ArrayList<QueryOperator>(alreadyMentionedConcepts));
// boolean firstWithOps = true;
// for (QueryOperator anOp : hullOfSuchThatOperators) {
// if (firstWithOps) {
// firstWithOps = false;
// } else {
// suchThat += " and ";
// }
// suchThat += writeOp(anOp);
// alreadyHandledInWith.add(anOp);
// // TODO: what with multiple parents ?
// if ((anOp.parents.size() > 0) && (anOp.parents.get(0).type == OperatorType.EXISTS)) {
// alreadyHandledInWith.add(anOp.parents.get(0));
// }
// }
// if (!first && !suchThat.isEmpty()) {
// result += "such that " + suchThat;
// }
// alreadyMentionedConcepts.add(aConc);
// first = false;
// }
// return result;
// }
private String getPath(QueryOperator aConc) {
if (aConc.type == OperatorType.ALIAS_REF) {
......@@ -327,7 +295,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
private String writePrefixOp(QueryOperator anOp) {
String result = "";
String opString = opMap.graph_2_system_operatorMap.get(anOp.type);
String opString = opMap.getOpString(anOp.type);
result += opString + "(";
boolean first = true;
for (Expression anExp : anOp.getParameters()) {
......@@ -345,7 +313,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
private String writeInfixOp(QueryOperator anOp) {
String result = "";
String opString = opMap.graph_2_system_operatorMap.get(anOp.type);
String opString = opMap.getOpString(anOp.type);
boolean first = true;
for (Expression anExp : anOp.getParameters()) {
if (first) {
......@@ -360,7 +328,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
}
private String writeExists(QueryOperator anOp) {
String opString = opMap.graph_2_system_operatorMap.get(anOp.type);
String opString = opMap.getOpString(anOp.type);
String value = writeFilter(anOp);
String result = opString + "(" + value + ")";
return result;
......@@ -375,7 +343,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
if (alreadyHandledInWith.contains(anOp)) {
return "";
}
String opString = opMap.graph_2_system_operatorMap.get(anOp.type);
String opString = opMap.getOpString(anOp.type);
if (prefixOps.contains(anOp.type)) {
String string = writePrefixOp(anOp);
result += string;
......@@ -427,6 +395,8 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
String frstExp = writeExpr(anOp.getParameters().get(0));
String sndExp = anOp.getParameters().get(1).asConcept().getCode();
result += frstExp + "." + sndExp;
} else if (anOp.type == OperatorType.RETURN) {
writeReturn(anOp);
} else if (anOp.getParameters().size() == 1) {
String exp = writeExpr(anOp.getParameters().get(0));
if ((anOp.type == OperatorType.END) || (anOp.type == OperatorType.START)) {
......
package de.uniwue.query;
import java.util.ArrayList;
import java.util.List;
public abstract class Expression {
// an identifier with which the expression can be identified inside the returned results
......@@ -14,7 +11,7 @@ public abstract class Expression {
public String alias;
public List<QueryOperator> parents = new ArrayList<QueryOperator>();
public QueryOperator parent;
public boolean isPrimitive() {
return this instanceof Primitive;
......
......@@ -9,6 +9,7 @@ public abstract class Graph_2_System_Mapper extends Mapper {
}
protected void initialize(QueryOperator aQuery) {
initialize();
this.root = aQuery;
checkErrors();
}
......@@ -19,10 +20,7 @@ public abstract class Graph_2_System_Mapper extends Mapper {
protected void checkOperators() {
for (QueryOperator anOp : root.getOpsRecursive()) {
String opString = opMap.graph_2_system_operatorMap.get(anOp.type);
if (opString == null) {
throw new RuntimeException("unsupported operation '" + anOp.type + "'");
}
opMap.getOpString(anOp.type);
}
}
......
......@@ -2,10 +2,51 @@ package de.uniwue.query;
public class Mapper {
protected QueryOperator root = new QueryOperator(OperatorType.RETURN);
protected QueryOperator root;
protected OperatorMap opMap = new OperatorMap();
public static String graphSystemName = "Graph";
protected QueryOperator currentOp;