Commit c88a1676 authored by Georg Fette's avatar Georg Fette
Browse files

intermediate commit, does not compile !

parent 40e53397
......@@ -3,6 +3,7 @@ package de.uniwue.query.CQL;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
......@@ -119,6 +120,10 @@ public class CQL_2_Graph_Mapper {
return prettyTree;
}
private void addOp(OperatorType aType) {
currentOp = new QueryOperator(currentOp, aType);
}
private cqlParser.QueryExpressionContext parseWithAntlr(String aQueryString) {
ANTLRInputStream stream = new ANTLRInputStream(aQueryString);
cqlLexer lexer = new cqlLexer(stream);
......@@ -198,6 +203,17 @@ public class CQL_2_Graph_Mapper {
for (TupleElementSelectorContext anElem : tupleElementSelector) {
parseTupleElementSelectorContext(anElem);
}
} else if (term instanceof InvocationTermContext) {
InvocationTermContext itc = (InvocationTermContext) term;
InvocationContext invocation = itc.invocation();
currentOp = query.rootClause;
List<QueryOperator> formerOps = new ArrayList<QueryOperator>(query.rootClause.getOperators());
parseInvocationContext(invocation);
for (QueryOperator anOp : query.rootClause.getOperators()) {
if (!formerOps.contains(anOp)) {
anOp.includeInResult = true;
}
}
} else {
throw new RuntimeException("unexpected");
}
......@@ -222,8 +238,7 @@ public class CQL_2_Graph_Mapper {
private void parseSortClauseContext(SortClauseContext sortClause) {
String prettyPrint = prettyPrint(sortClause);
QueryOperator lastOp = currentOp;
QueryOperator sort = new QueryOperator(currentOp, OperatorType.SORT);
currentOp = sort;
addOp(OperatorType.SORT);
List<SortByItemContext> sortByItem = sortClause.sortByItem();
for (SortByItemContext anItem : sortByItem) {
ExpressionTermContext expressionTerm = anItem.expressionTerm();
......@@ -296,9 +311,6 @@ public class CQL_2_Graph_Mapper {
NamedTypeSpecifierContext namedTypeSpecifier = retrieve.namedTypeSpecifier();
String text = namedTypeSpecifier.identifier().getText();
QueryConcept root = new QueryConcept(text);
if (!(retrieve.parent.parent.parent instanceof WithClauseContext)) {
root.includeInResult = true;
}
query.rootConcepts.add(root);
setCurrentPath(new GraphPath(root));
}
......@@ -357,7 +369,7 @@ public class CQL_2_Graph_Mapper {
// this is very sloppy
String prettyPrint = prettyPrint(te);
QueryOperator lastOp = currentOp;
currentOp = new QueryOperator(currentOp, OperatorType.IS);
addOp(OperatorType.IS);
ExpressionContext e = te.expression();
parseExpression(e);
addCurrentPathToCurrentOp();
......@@ -371,7 +383,7 @@ public class CQL_2_Graph_Mapper {
private void parseExistenceExpression(ExistenceExpressionContext ee) {
String prettyPrint = prettyPrint(ee);
QueryOperator lastOp = currentOp;
currentOp = new QueryOperator(currentOp, OperatorType.EXISTS);
addOp(OperatorType.EXISTS);
TermExpressionContext te = (TermExpressionContext) ee.expression();
TermExpressionTermContext tet = (TermExpressionTermContext) te.expressionTerm();
ParenthesizedTermContext pt = (ParenthesizedTermContext) tet.term();
......@@ -400,13 +412,13 @@ public class CQL_2_Graph_Mapper {
String text2 = temporalRelationship.getText();
QueryOperator tempOp, intervalOp;
if (text2.equals("after")) {
currentOp = new QueryOperator(currentOp, OperatorType.AFTER);
addOp(OperatorType.AFTER);
tempOp = currentOp;
} else {
throw new RuntimeException("unexpected");
}
if (text.equals("ends")) {
currentOp = new QueryOperator(currentOp, OperatorType.END);
addOp(OperatorType.END);
intervalOp = currentOp;
} else {
throw new RuntimeException("unexpected");
......@@ -426,7 +438,7 @@ public class CQL_2_Graph_Mapper {
String prettyPrint = prettyPrint(ae);
QueryOperator lastOp = currentOp;
if ((currentOp.type != OperatorType.QUERY) && (currentOp.type != OperatorType.AND)) {
currentOp = new QueryOperator(currentOp, OperatorType.AND);
addOp(OperatorType.AND);
}
List<ExpressionContext> expression = ae.expression();
for (ExpressionContext ec : expression) {
......@@ -443,26 +455,26 @@ public class CQL_2_Graph_Mapper {
isDotInvocation = true;
}
if (te instanceof PowerExpressionTermContext) {
currentOp = new QueryOperator(currentOp, OperatorType.POWER);
addOp(OperatorType.POWER);
}
if (te instanceof AdditionExpressionTermContext) {
if (te.children.get(1).getText().equals("+")) {
currentOp = new QueryOperator(currentOp, OperatorType.ADD);
addOp(OperatorType.ADD);
} else if (te.children.get(1).getText().equals("-")) {
currentOp = new QueryOperator(currentOp, OperatorType.SUBTRACT);
addOp(OperatorType.SUBTRACT);
} else {
throw new RuntimeException("unexpected");
}
}
if (te instanceof MultiplicationExpressionTermContext) {
if (te.children.get(1).getText().equals("*")) {
currentOp = new QueryOperator(currentOp, OperatorType.MULTIPLY);
addOp(OperatorType.MULTIPLY);
} else if (te.children.get(1).getText().equals("/")) {
currentOp = new QueryOperator(currentOp, OperatorType.DIVIDE);
addOp(OperatorType.DIVIDE);
} else if (te.children.get(1).getText().equals("div")) {
currentOp = new QueryOperator(currentOp, OperatorType.TRUNCATED_DIVIDE);
addOp(OperatorType.TRUNCATED_DIVIDE);
} else if (te.children.get(1).getText().equals("mod")) {
currentOp = new QueryOperator(currentOp, OperatorType.MODULO);
addOp(OperatorType.MODULO);
} else {
throw new RuntimeException("unexpected");
}
......@@ -534,7 +546,7 @@ public class CQL_2_Graph_Mapper {
String prettyPrint = prettyPrint(iet);
if (containsFunctionCall(iet)) {
QueryOperator lastOp = currentOp;
currentOp = new QueryOperator(currentOp, OperatorType.OPERATOR_RESULT_PROPERTY);
addOp(OperatorType.OPERATOR_RESULT_PROPERTY);
parseExpressionTerm(iet);
addCurrentPathToCurrentOp();
currentOp = lastOp;
......@@ -545,8 +557,8 @@ public class CQL_2_Graph_Mapper {
private QueryOperator parseOpWithParams(List<ExpressionContext> parameters, OperatorType aType) {
QueryOperator lastOp = currentOp;
QueryOperator newOp = new QueryOperator(currentOp, aType);
currentOp = newOp;
addOp(aType);
QueryOperator newOp = currentOp;
for (ExpressionContext anOp : parameters) {
String prettyPrint = prettyPrint(anOp);
parseExpression(anOp);
......@@ -584,6 +596,8 @@ public class CQL_2_Graph_Mapper {
opType = OperatorType.CEILING;
} else if (text.equals("Last")) {
opType = OperatorType.LAST;
} else if (text.equals("Count")) {
opType = OperatorType.COUNT;
} else {
throw new RuntimeException("unexpected");
}
......@@ -803,7 +817,7 @@ public class CQL_2_Graph_Mapper {
private void parseQuantityLiteralContext(QuantityLiteralContext ql) {
String prettyPrint = prettyPrint(ql);
QueryOperator lastOp = currentOp;
currentOp = new QueryOperator(currentOp, OperatorType.QUANTITY);
addOp(OperatorType.QUANTITY);
QuantityContext quantity = ql.quantity();
UnitContext unit = quantity.unit();
String text = quantity.NUMBER().getText();
......
package de.uniwue.query;
public class Expression {
import java.util.List;
public abstract class Expression {
public boolean isConcept() {
return this instanceof QueryConcept;
......@@ -26,4 +28,8 @@ public class Expression {
return (QueryOperator) this;
}
public abstract void getReturnedExpressions(List<Expression> results);
public boolean includeInResult;
}
package de.uniwue.query.FHIRRest;
import java.util.List;
import de.uniwue.query.Expression;
import de.uniwue.query.GraphQuery;
import de.uniwue.query.Graph_2_System_Mapper;
import de.uniwue.query.OperatorMap;
import de.uniwue.query.OperatorType;
import de.uniwue.query.QueryConcept;
import de.uniwue.query.QueryOperator;
public class Graph_2_FHIRRest_Mapper extends Graph_2_System_Mapper {
......@@ -16,13 +8,13 @@ public class Graph_2_FHIRRest_Mapper extends Graph_2_System_Mapper {
public Graph_2_FHIRRest_Mapper() {
super(system);
initializeOpMappings(opMap);
// initializeOpMappings(opMap);
}
public String getSystem() {
return system;
}
/*
@Override
protected void initialize(GraphQuery aQuery) {
super.initialize(aQuery);
......@@ -114,5 +106,5 @@ public class Graph_2_FHIRRest_Mapper extends Graph_2_System_Mapper {
opMap.addOp("=lt", OperatorType.LESS);
opMap.addOp("=le", OperatorType.LESS_OR_EQUAL);
}
*/
}
......@@ -10,7 +10,7 @@ public class GraphQuery {
public QueryOperator rootClause = new QueryOperator(OperatorType.QUERY);
public GraphLibrary library = new GraphLibrary();
public List<QueryConcept> getConceptsRecursive() {
List<QueryConcept> result = new ArrayList<QueryConcept>();
for (QueryConcept aConc : rootConcepts) {
......@@ -31,14 +31,13 @@ public class GraphQuery {
return result;
}
public List<QueryConcept> getReturnedConcepts() {
List<QueryConcept> returnConcs = new ArrayList<QueryConcept>();
public List<Expression> getReturnedExpressions() {
List<Expression> returns = new ArrayList<Expression>();
for (QueryConcept aConc : getConceptsRecursive()) {
if (aConc.includeInResult) {
returnConcs.add(aConc);
}
aConc.getReturnedExpressions(returns);
}
return returnConcs;
rootClause.getReturnedExpressions(returns);
return returns;
}
public QueryConcept getFirstRoot() {
......@@ -53,5 +52,5 @@ public class GraphQuery {
result += "\n" + "RootClause:\n" + rootClause.toString();
return result;
}
}
......@@ -3,13 +3,14 @@ package de.uniwue.query;
public enum OperatorType {
//@formatter:off
// boolean
AND,
OR,
XOR,
NOT,
SORT,
SORT_ITEM,
// comparators
EQUALS,
END,
START,
......@@ -26,11 +27,21 @@ public enum OperatorType {
PER_MONTH,
PER_INTERVALS,
EXISTS,
IS,
// string
MATCHES,
// list
IN_VALUE_SET,
TO_LIST,
FIRST,
LAST,
MATCHES,
IS,
// aggregation
COUNT,
// arithmetics
POWER,
ROUND,
ABS,
......@@ -47,19 +58,21 @@ public enum OperatorType {
TRUNCATED_DIVIDE,
MODULO,
// datetime
NOW,
TODAY,
TIME_OF_DAY,
TIME,
AFTER,
// system
PARAMETER,
OPERATOR_RESULT_PROPERTY,
QUERY,
IN_VALUE_SET,
TO_LIST,
SORT,
SORT_ITEM,
// constructors
QUANTITY,
LIST,
INTERVAL
......
package de.uniwue.query;
import java.util.List;
public class Primitive extends Expression {
public String value;
......@@ -7,24 +9,24 @@ public class Primitive extends Expression {
public enum PrimitiveDataType {
String, Integer, Double, DateTime
}
public PrimitiveDataType dataType;
public Primitive(int aValue) {
this.value = Integer.toString(aValue);
dataType = PrimitiveDataType.Integer;
}
public Primitive(double aValue) {
this.value = Double.toString(aValue);
dataType = PrimitiveDataType.Double;
}
public Primitive(String aValue) {
this.value = aValue;
dataType = PrimitiveDataType.String;
}
@Override
public String toString() {
String result = value;
......@@ -34,4 +36,9 @@ public class Primitive extends Expression {
return result;
}
public void getReturnedExpressions(List<Expression> results) {
if (includeInResult) {
results.add(this);
}
}
}
......@@ -12,8 +12,6 @@ public class QueryConcept extends Expression {
public String code;
public boolean includeInResult;
public boolean optional;
// an identifier with which the concept can be identified inside the returned results
......@@ -142,4 +140,13 @@ public class QueryConcept extends Expression {
return result;
}
public void getReturnedExpressions(List<Expression> results) {
if (includeInResult) {
results.add(this);
}
for (QueryConcept aConc : children) {
aConc.getReturnedExpressions(results);
}
}
}
......@@ -8,8 +8,6 @@ import de.uniwue.query.Primitive.PrimitiveDataType;
public class QueryOperator extends Expression {
public boolean includeInResult;
// an identifier with which the operator result can be identified inside the returned results
public String outputName;
......@@ -181,4 +179,12 @@ public class QueryOperator extends Expression {
return result;
}
public void getReturnedExpressions(List<Expression> results) {
if (includeInResult) {
results.add(this);
}
for (Expression anExp : parameters) {
anExp.getReturnedExpressions(results);
}
}
}
......@@ -20,6 +20,7 @@ public class CypherOperatorMapper {
aMap.addOp("AND", OperatorType.AND);
aMap.addOp("OR", OperatorType.OR);
aMap.addOp("count", OperatorType.COUNT);
aMap.addOp("First", OperatorType.FIRST);
aMap.addOp("Last", OperatorType.LAST);
aMap.addOp("In", OperatorType.IN);
......
......@@ -346,15 +346,23 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
private String writeReturn(GraphQuery aQuery) {
String result = " RETURN ";
boolean first = true;
for (QueryConcept aConc : aQuery.rootConcepts) {
String alias = aliasesConcs.get(aConc);
if (aConc.includeInResult) {
if (!first) {
result += ", ";
} else {
first = false;
}
List<Expression> returnedExpressions = aQuery.getReturnedExpressions();
for (Expression anExp : returnedExpressions) {
if (!first) {
result += ", ";
} else {
first = false;
}
if (anExp.isConcept()) {
String alias = aliasesConcs.get(anExp.asConcept());
result += alias;
} else if (anExp.isOperator()) {
String alias = aliasesOps.get(anExp.asOperator());
result += alias;
} else if (anExp.isPrimitive()) {
result += anExp.asPrimitive().toString();
} else {
throw new RuntimeException("unexpected");
}
}
return result;
......
package de.uniwue.query;
import java.io.IOException;
import java.text.ParseException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import org.jdom.JDOMException;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
import de.uniwue.query.CQL.CQL_2_Graph_Mapper;
import de.uniwue.query.CQL.Graph_2_CQL_Mapper;
import de.uniwue.query.FHIRRest.FHIRRest_2_Graph_Mapper;
import de.uniwue.query.FHIRRest.Graph_2_FHIRRest_Mapper;
public class Test_CQL_2_FHIRRest {
/*
CQL_2_Graph_Mapper cql_2_graph_mapper;
Graph_2_FHIRRest_Mapper graph_2_fhir_mapper;
......@@ -71,5 +56,5 @@ public class Test_CQL_2_FHIRRest {
processQuery(aMultiQuery);
}
}
*/
}
*CQL
[Patient] A return count(A)
[Patient] A return Count(A)
*Cypher
MATCH (A:Patient) RETURN count(A)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment