Commit 097cdf40 authored by Georg Fette's avatar Georg Fette
Browse files

further work

parent db353b94
package de.uniwue.query.CQL;
import de.uniwue.query.OperatorMap;
import de.uniwue.query.OperatorType;
public class CQLOperatorMapper {
public static void initializeOpMappings(OperatorMap aMap) {
aMap.addOp("is", OperatorType.IS);
aMap.addOp("sort by", OperatorType.SORT);
aMap.addOp("exists", OperatorType.EXISTS);
aMap.addOp("", OperatorType.FIELD);
aMap.addOp("", OperatorType.ITERATE);
aMap.addOp("", OperatorType.ALIAS_REF);
aMap.addOp("", OperatorType.FILTER);
aMap.addOp("", OperatorType.QUANTITY);
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);
aMap.addOp("not", OperatorType.NOT);
aMap.addOp("alltrue", OperatorType.ALLTRUE);
aMap.addOp("anytrue", OperatorType.ANYTRUE);
aMap.addOp("First", OperatorType.FIRST);
aMap.addOp("Last", OperatorType.LAST);
aMap.addOp("In", OperatorType.IN);
aMap.addOp("contains", OperatorType.CONTAINS);
aMap.addOp("in", OperatorType.IN_VALUE_SET);
aMap.addOp("Now", OperatorType.NOW);
aMap.addOp("Today", OperatorType.TODAY);
aMap.addOp("TimeOfDay", OperatorType.TIMEOFDAY);
aMap.addOp("Time", OperatorType.TIME);
aMap.addOp("Datetime", OperatorType.DATETIME);
aMap.addOp("Interval", OperatorType.INTERVAL);
aMap.addOp("during", OperatorType.DURING);
aMap.addOp("after", OperatorType.AFTER);
aMap.addOp("ends", OperatorType.END);
aMap.addOp("=", OperatorType.EQUALS);
aMap.addOp("<", OperatorType.LESS);
aMap.addOp("<=", OperatorType.LESS_OR_EQUAL);
aMap.addOp(">", OperatorType.MORE);
aMap.addOp(">=", OperatorType.MORE_OR_EQUAL);
aMap.addOp("Avg", OperatorType.AVG);
aMap.addOp("Count", OperatorType.COUNT);
aMap.addOp("Min", OperatorType.MIN);
aMap.addOp("Max", OperatorType.MAX);
aMap.addOp("Median", OperatorType.MEDIAN);
aMap.addOp("Sum", OperatorType.SUM);
aMap.addOp("StdDev", OperatorType.STDDEV);
aMap.addOp("Variance", OperatorType.VARIANCE);
aMap.addOp("-", OperatorType.NEGATE);
aMap.addOp("^", OperatorType.POWER);
aMap.addOp("Round", OperatorType.ROUND);
aMap.addOp("Abs", OperatorType.ABS);
aMap.addOp("Exp", OperatorType.EXP);
aMap.addOp("Log", OperatorType.LOG);
aMap.addOp("Ln", OperatorType.LN);
aMap.addOp("Truncate", OperatorType.TRUNCATE);
aMap.addOp("Floor", OperatorType.FLOOR);
aMap.addOp("Ceiling", OperatorType.CEILING);
aMap.addOp("+", OperatorType.ADD);
aMap.addOp("-", OperatorType.SUBTRACT);
aMap.addOp("*", OperatorType.MULTIPLY);
aMap.addOp("/", OperatorType.DIVIDE);
aMap.addOp("div", OperatorType.TRUNCATED_DIVIDE);
aMap.addOp("mod", OperatorType.MODULO);
}
}
......@@ -9,6 +9,7 @@ import java.util.Set;
import de.uniwue.query.Expression;
import de.uniwue.query.Graph_2_System_Mapper;
import de.uniwue.query.OperatorMap;
import de.uniwue.query.OperatorType;
import de.uniwue.query.Primitive;
import de.uniwue.query.Primitive.PrimitiveDataType;
......@@ -46,7 +47,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
public Graph_2_CQL_Mapper() {
super(system);
CQLOperatorMapper.initializeOpMappings(opMap);
initializeOpMappings(opMap);
}
protected void initialize(QueryOperator aQuery) {
......@@ -146,13 +147,13 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
List<QueryOperator> result = new ArrayList<QueryOperator>();
List<QueryOperator> allOps = root.getOpsRecursive();
for (QueryOperator anOp : allOps) {
Set<QueryOperator> aliasRoots = aliasRoots(anOp.getConceptsRecursive());
Set<QueryOperator> aliasRoots = aliasRoots(anOp.getIteratorsRecursive());
if (alreadyMentionedConcepts.containsAll(aliasRoots)) {
result.add(anOp);
}
}
for (QueryOperator anOp : allOps) {
Set<QueryOperator> aliasRoots = aliasRoots(anOp.getConceptsRecursive());
Set<QueryOperator> aliasRoots = aliasRoots(anOp.getIteratorsRecursive());
if (alreadyMentionedConcepts.containsAll(aliasRoots)) {
result.removeAll(anOp.getOpsRecursive());
}
......@@ -211,7 +212,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
String subPath = getPath(aConc.getParameters().get(0).asOperator());
return subPath + "." + aConc.getParameters().get(1).asPrimitive().value;
} else if (aConc.type == OperatorType.ITERATE) {
return "[" + aConc.getCode() + "]";
return "[" + aConc.getIteratorSelector() + "]";
} else {
return writeOp(aConc);
}
......@@ -393,7 +394,7 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
}
} else if (anOp.type == OperatorType.FIELD) {
String frstExp = writeExpr(anOp.getParameters().get(0));
String sndExp = anOp.getParameters().get(1).asConcept().getCode();
String sndExp = anOp.getParameters().get(1).asConcept().getIteratorSelector();
result += frstExp + "." + sndExp;
} else if (anOp.type == OperatorType.RETURN) {
writeReturn(anOp);
......@@ -433,4 +434,74 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
return Graph_2_CQL_Mapper.system;
}
public static void initializeOpMappings(OperatorMap aMap) {
initializeStdOpMappings(aMap);
aMap.addOp("is", OperatorType.IS);
aMap.addOp("sort by", OperatorType.SORT);
aMap.addOp("exists", OperatorType.EXISTS);
aMap.addOp("", OperatorType.QUANTITY);
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);
aMap.addOp("not", OperatorType.NOT);
aMap.addOp("alltrue", OperatorType.ALLTRUE);
aMap.addOp("anytrue", OperatorType.ANYTRUE);
aMap.addOp("First", OperatorType.FIRST);
aMap.addOp("Last", OperatorType.LAST);
aMap.addOp("In", OperatorType.IN);
aMap.addOp("contains", OperatorType.CONTAINS);
aMap.addOp("in", OperatorType.IN_VALUE_SET);
aMap.addOp("Now", OperatorType.NOW);
aMap.addOp("Today", OperatorType.TODAY);
aMap.addOp("TimeOfDay", OperatorType.TIMEOFDAY);
aMap.addOp("Time", OperatorType.TIME);
aMap.addOp("Datetime", OperatorType.DATETIME);
aMap.addOp("Interval", OperatorType.INTERVAL);
aMap.addOp("during", OperatorType.DURING);
aMap.addOp("after", OperatorType.AFTER);
aMap.addOp("ends", OperatorType.END);
aMap.addOp("=", OperatorType.EQUALS);
aMap.addOp("<", OperatorType.LESS);
aMap.addOp("<=", OperatorType.LESS_OR_EQUAL);
aMap.addOp(">", OperatorType.MORE);
aMap.addOp(">=", OperatorType.MORE_OR_EQUAL);
aMap.addOp("Avg", OperatorType.AVG);
aMap.addOp("Count", OperatorType.COUNT);
aMap.addOp("Min", OperatorType.MIN);
aMap.addOp("Max", OperatorType.MAX);
aMap.addOp("Median", OperatorType.MEDIAN);
aMap.addOp("Sum", OperatorType.SUM);
aMap.addOp("StdDev", OperatorType.STDDEV);
aMap.addOp("Variance", OperatorType.VARIANCE);
aMap.addOp("-", OperatorType.NEGATE);
aMap.addOp("^", OperatorType.POWER);
aMap.addOp("Round", OperatorType.ROUND);
aMap.addOp("Abs", OperatorType.ABS);
aMap.addOp("Exp", OperatorType.EXP);
aMap.addOp("Log", OperatorType.LOG);
aMap.addOp("Ln", OperatorType.LN);
aMap.addOp("Truncate", OperatorType.TRUNCATE);
aMap.addOp("Floor", OperatorType.FLOOR);
aMap.addOp("Ceiling", OperatorType.CEILING);
aMap.addOp("+", OperatorType.ADD);
aMap.addOp("-", OperatorType.SUBTRACT);
aMap.addOp("*", OperatorType.MULTIPLY);
aMap.addOp("/", OperatorType.DIVIDE);
aMap.addOp("div", OperatorType.TRUNCATED_DIVIDE);
aMap.addOp("mod", OperatorType.MODULO);
}
}
......@@ -8,7 +8,7 @@ public abstract class Expression {
public boolean isInResult;
public boolean optional;
public String alias;
public QueryOperator parent;
......@@ -26,7 +26,7 @@ public abstract class Expression {
if (isOperator()) {
QueryOperator thisAsOp = asOperator();
if ((thisAsOp.type == OperatorType.ITERATE) || (thisAsOp.type == OperatorType.FIELD)
|| (thisAsOp.type == OperatorType.REVERSE_FIELD)) {
|| (thisAsOp.type == OperatorType.FIELD_WITH_TYPE)) {
return true;
}
}
......
......@@ -49,4 +49,14 @@ public class Mapper {
root = new QueryOperator(OperatorType.QUERY);
}
public static void initializeStdOpMappings(OperatorMap opMap) {
opMap.addOp("", OperatorType.FILTER);
opMap.addOp("", OperatorType.ITERATE);
opMap.addOp("", OperatorType.EXISTS);
opMap.addOp("", OperatorType.FIELD);
opMap.addOp("", OperatorType.ALIAS_REF);
opMap.addOp("", OperatorType.RETURN);
opMap.addOp("", OperatorType.QUERY);
}
}
......@@ -88,7 +88,6 @@ public enum OperatorType {
ITERATE,
FIELD_WITH_TYPE,
FIELD,
REVERSE_FIELD,
RETURN,
QUERY,
ALIAS_REF,
......
......@@ -30,13 +30,12 @@ public class QueryOperator extends Expression {
this(aContainerOp, aParentConc, fieldName, OperatorType.FIELD);
}
public QueryOperator(QueryOperator aContainerOp, QueryOperator aParentConc, String fieldName, OperatorType opType) {
public QueryOperator(QueryOperator aContainerOp, QueryOperator aParentConc, String fieldName,
OperatorType opType) {
this(aContainerOp, opType);
parameters.add(aParentConc);
Primitive primitive = new Primitive(fieldName);
parameters.add(primitive);
// the new chain-member has to be put at the head of the chain and thus replace the former head
aContainerOp.removeParameter(aParentConc);
addParameter(aParentConc);
addParameter(fieldName);
}
public QueryOperator(String conceptCode) {
......@@ -108,6 +107,14 @@ public class QueryOperator extends Expression {
return aPrim;
}
public QueryOperator getRoot() {
if (parent != null) {
return parent.getRoot();
} else {
return this;
}
}
public boolean isBooleanOp() {
return (type == OperatorType.AND) || (type == OperatorType.OR) || (type == OperatorType.NOT)
|| (type == OperatorType.XOR) || (type == OperatorType.FILTER);
......@@ -202,47 +209,62 @@ public class QueryOperator extends Expression {
return result;
}
public List<QueryOperator> getConceptsRecursive() {
return getOpsRecursiveWithType(OperatorType.ITERATE);
}
public List<QueryOperator> getConceptsRecursiveWithCode(String aCode) {
List<QueryOperator> result = new ArrayList<QueryOperator>();
List<QueryOperator> opsRecursiveWithType = getConceptsRecursive();
for (QueryOperator aConc : opsRecursiveWithType) {
if (aConc.getCode().equals(aCode)) {
result.add(aConc);
public QueryOperator getParentConcept() {
if (!isIterator()) {
return null;
}
if (type == OperatorType.ITERATE) {
// I have no parent because I am a root concept
return null;
}
String aliasRefValue = getParameters().get(0).asOperator().getParameters().get(0)
.asPrimitive().value;
for (QueryOperator anOp : getRoot().getOpsRecursive()) {
if ((anOp.alias != null) && anOp.alias.equals(aliasRefValue)) {
return anOp;
}
}
return null;
}
public List<QueryOperator> getIteratorsRecursive() {
List<QueryOperator> result = getOpsRecursiveWithType(OperatorType.ITERATE);
result.addAll(getOpsRecursiveWithType(OperatorType.FIELD));
result.addAll(getOpsRecursiveWithType(OperatorType.FIELD_WITH_TYPE));
return result;
}
public List<QueryOperator> getChildConceptsWithCode(String aCode) {
public List<QueryOperator> getIteratorsWithSelector(String aSelector) {
List<QueryOperator> result = new ArrayList<QueryOperator>();
List<QueryOperator> childConcepts = getChildConcepts();
for (QueryOperator aConc : childConcepts) {
if (aConc.getCode().equals(aCode)) {
List<QueryOperator> opsRecursiveWithType = getIteratorsRecursive();
for (QueryOperator aConc : opsRecursiveWithType) {
if (aConc.getIteratorSelector().equals(aSelector)) {
result.add(aConc);
}
}
return result;
}
public String getCode() {
public boolean isIterator() {
return (type == OperatorType.ITERATE) || (type == OperatorType.FIELD)
|| (type == OperatorType.FIELD_WITH_TYPE);
}
public String getIteratorSelector() {
if (type == OperatorType.ITERATE) {
return getParameters().get(0).asPrimitive().value;
} else if (type == OperatorType.FIELD) {
} else if ((type == OperatorType.FIELD) || (type == OperatorType.FIELD_WITH_TYPE)) {
return getParameters().get(1).asPrimitive().value;
} else {
return null;
}
}
public String setCode(String aCode) {
public String setIteratorSelector(String aSelector) {
if (type == OperatorType.ITERATE) {
return getParameters().get(0).asPrimitive().value = aCode;
} else if (type == OperatorType.FIELD) {
return getParameters().get(1).asPrimitive().value = aCode;
return getParameters().get(0).asPrimitive().value = aSelector;
} else if ((type == OperatorType.FIELD) || (type == OperatorType.FIELD_WITH_TYPE)) {
return getParameters().get(1).asPrimitive().value = aSelector;
} else {
return null;
}
......
package de.uniwue.query.cypher;
import de.uniwue.query.OperatorMap;
import de.uniwue.query.OperatorType;
public class CypherOperatorMapper {
public static void initializeOpMappings(OperatorMap aMap) {
aMap.addOp("sort", OperatorType.SORT);
aMap.addOp("exists", OperatorType.EXISTS);
aMap.addOp("", OperatorType.FIELD);
aMap.addOp("", OperatorType.FILTER);
aMap.addOp("", OperatorType.QUANTITY);
aMap.addOp("", OperatorType.INTERVAL);
aMap.addOp("", OperatorType.LIST);
aMap.addOp("", OperatorType.TO_LIST);
aMap.addOp("AND", OperatorType.AND);
aMap.addOp("OR", OperatorType.OR);
aMap.addOp("count", OperatorType.COUNT);
aMap.addOp("avg", OperatorType.AVG);
aMap.addOp("First", OperatorType.FIRST);
aMap.addOp("Last", OperatorType.LAST);
aMap.addOp("In", OperatorType.IN);
aMap.addOp("contains", OperatorType.CONTAINS);
aMap.addOp("In", OperatorType.IN_VALUE_SET);
aMap.addOp("Now()", OperatorType.NOW);
aMap.addOp("Today()", OperatorType.TODAY);
aMap.addOp("TimeOfDay()", OperatorType.TIMEOFDAY);
aMap.addOp("Interval", OperatorType.INTERVAL);
aMap.addOp("during", OperatorType.DURING);
aMap.addOp(">", OperatorType.AFTER);
aMap.addOp("ends", OperatorType.END);
aMap.addOp("=", OperatorType.EQUALS);
aMap.addOp("<", OperatorType.LESS);
aMap.addOp("<=", OperatorType.LESS_OR_EQUAL);
aMap.addOp(">", OperatorType.MORE);
aMap.addOp(">=", OperatorType.MORE_OR_EQUAL);
aMap.addOp("^", OperatorType.POWER);
aMap.addOp("Round", OperatorType.ROUND);
aMap.addOp("Abs", OperatorType.ABS);
aMap.addOp("Exp", OperatorType.EXP);
aMap.addOp("Log", OperatorType.LOG);
aMap.addOp("Log", OperatorType.LN);
aMap.addOp("Truncate", OperatorType.TRUNCATE);
aMap.addOp("Floor", OperatorType.FLOOR);
aMap.addOp("Ceiling", OperatorType.CEILING);
aMap.addOp("+", OperatorType.ADD);
aMap.addOp("-", OperatorType.SUBTRACT);
aMap.addOp("*", OperatorType.MULTIPLY);
aMap.addOp("/", OperatorType.DIVIDE);
aMap.addOp("div", OperatorType.TRUNCATED_DIVIDE);
aMap.addOp("mod", OperatorType.MODULO);
}
}
......@@ -24,6 +24,7 @@ import de.uniwue.misc.util.ResourceUtil;
import de.uniwue.misc.util.StringUtilsUniWue;
import de.uniwue.query.Expression;
import de.uniwue.query.Graph_2_System_Mapper;
import de.uniwue.query.OperatorMap;
import de.uniwue.query.OperatorType;
import de.uniwue.query.Primitive;
import de.uniwue.query.Primitive.PrimitiveDataType;
......@@ -70,7 +71,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
public Graph_2_Cypher_Mapper() {
super(system);
CypherOperatorMapper.initializeOpMappings(opMap);
initializeOpMappings(opMap);
}
protected void initialize() {
......@@ -160,7 +161,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
result = aliases.get(currentConc) + result;
break;
} else {
result = aConc.getCode() + result;
result = aConc.getIteratorSelector() + result;
}
currentConc = currentConc.parent;
} while (currentConc != null);
......@@ -294,7 +295,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
result += ".";
Expression anExp2 = anOp.getParameters().get(1);
if (anExp2.isConcept()) {
result += anExp2.asConcept().getCode();
result += anExp2.asConcept().getIteratorSelector();
} else {
throw new RuntimeException("unexpected");
}
......@@ -309,9 +310,11 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
}
private boolean isRelation(QueryOperator aConc, QueryOperator aPossibleRel) {
String propertyType = getPropertyType(aConc.getCode(), aPossibleRel.getCode());
String propertyType = getPropertyType(aConc.getIteratorSelector(),
aPossibleRel.getIteratorSelector());
if (propertyType == null) {
throw new RuntimeException("unknown property of concept '" + aConc.getCode() + "'");
throw new RuntimeException(
"unknown property of concept '" + aConc.getIteratorSelector() + "'");
}
if (propertyType.matches("^[a-z].*")) {
return false;
......@@ -322,8 +325,8 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
private String writeRelation(QueryOperator aConc, QueryOperator aRel) {
String result;
String targetType = getPropertyType(aConc.getCode(), aRel.getCode());
String relName = aRel.getCode().toUpperCase();
String targetType = getPropertyType(aConc.getIteratorSelector(), aRel.getIteratorSelector());
String relName = aRel.getIteratorSelector().toUpperCase();
result = "-[" + ":" + relName + "]->";
String targetAlias = aliasNames[aliasIndex++];
if (targetType.equals("Reference")) {
......@@ -418,7 +421,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
}
String alias = aliasNames[aliasIndex++];
aliases.put(aConc, alias);
result = result + "(" + alias + ":" + aConc.getCode() + ")";
result = result + "(" + alias + ":" + aConc.getIteratorSelector() + ")";
String relsString = writeRelations(aConc);
result = result + relsString;
}
......@@ -437,7 +440,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
if (!path.isEmpty()) {
path = "." + path;
}
path = currentConc.getCode() + path;
path = currentConc.getIteratorSelector() + path;
currentConc = currentConc.parent;
notToAggregateConcets.add(currentConc);
}
......@@ -446,7 +449,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
String opAlias = aliasNames[aliasIndex++];
aliases.put(anOp, opAlias);
result += " AS " + opAlias;
for (QueryOperator aConc : root.getConceptsRecursive()) {
for (QueryOperator aConc : root.getIteratorsRecursive()) {
// if (aConc.includeInResult) {
if (aliases.containsKey(aConc) && !notToAggregateConcets.contains(aConc)) {
String returnAlias = aliases.get(aConc);
......@@ -474,7 +477,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
if (!path.isEmpty()) {
path = "." + path;
}
path = currentConc.getCode() + path;
path = currentConc.getIteratorSelector() + path;
currentConc = currentConc.parent;
}
String alias = aliases.get(currentConc);
......@@ -488,7 +491,7 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
aliases.put(anOp, opAlias);
result += " AS " + opAlias;
for (QueryOperator aConc : root.getConceptsRecursive()) {
for (QueryOperator aConc : root.getIteratorsRecursive()) {
if (aliases.containsKey(aConc)) {
String returnAlias = aliases.get(aConc);
result += ", " + returnAlias;
......@@ -532,4 +535,58 @@ public class Graph_2_Cypher_Mapper extends Graph_2_System_Mapper {
return Graph_2_Cypher_Mapper.system;
}
public static void initializeOpMappings(OperatorMap aMap) {
initializeStdOpMappings(aMap);
aMap.addOp("sort", OperatorType.SORT);
aMap.addOp("exists", OperatorType.EXISTS);
aMap.addOp("", OperatorType.QUANTITY);
aMap.addOp("", OperatorType.INTERVAL);
aMap.addOp("", OperatorType.LIST);
aMap.addOp("", OperatorType.TO_LIST);
aMap.addOp("AND", OperatorType.AND);
aMap.addOp("OR", OperatorType.OR);
aMap.addOp("count", OperatorType.COUNT);
aMap.addOp("avg", OperatorType.AVG);
aMap.addOp("First", OperatorType.FIRST);
aMap.addOp("Last", OperatorType.LAST);
aMap.addOp("In", OperatorType.IN);
aMap.addOp("contains", OperatorType.CONTAINS);
aMap.addOp("In", OperatorType.IN_VALUE_SET);
aMap.addOp("Now()", OperatorType.NOW);
aMap.addOp("Today()", OperatorType.TODAY);
aMap.addOp("TimeOfDay()", OperatorType.TIMEOFDAY);
aMap.addOp("Interval", OperatorType.INTERVAL);
aMap.addOp("during", OperatorType.DURING);