use of org.smartdata.rule.objects.Property in project SSM by Intel-bigdata.
the class SmartRuleVisitTranslator method doGenerateSql.
public NodeTransResult doGenerateSql(TreeNode root, String tableName) throws IOException {
if (root == null) {
return new NodeTransResult(tableName, "");
}
if (root.isOperNode()) {
OperatorType optype = ((OperNode) root).getOperatorType();
String op = optype.getOpInSql();
NodeTransResult lop = doGenerateSql(root.getLeft(), tableName);
NodeTransResult rop = null;
if (optype != OperatorType.NOT) {
rop = doGenerateSql(root.getRight(), tableName);
}
boolean bEflag = false;
if (lop.getTableName() == null && rop.getTableName() != null) {
NodeTransResult temp = lop;
lop = rop;
rop = temp;
bEflag = true;
}
if (optype == OperatorType.AND || optype == OperatorType.OR || optype == OperatorType.NONE) {
String lopTable = lop.getTableName();
if (lopTable != null && !lopTable.equals(tableName)) {
lop = new NodeTransResult(tableName, connectTables(tableName, lop));
}
String ropTable = rop.getTableName();
if (ropTable != null && !ropTable.equals(tableName)) {
rop = new NodeTransResult(tableName, connectTables(tableName, rop));
}
}
if (optype == OperatorType.NOT) {
return new NodeTransResult(tableName, op + " " + connectTables(tableName, lop));
}
boolean procAccLt = false;
String res;
if (op.length() > 0) {
String ropStr = rop.getRet();
if (optype == OperatorType.MATCHES) {
ropStr = ropStr.replace("*", "%");
ropStr = ropStr.replace("?", "_");
}
if (bEflag && !procAcc) {
switch(optype) {
case LT:
op = ">";
break;
case LE:
op = ">=";
break;
case GT:
op = "<";
break;
case GE:
op = "<=";
break;
}
}
if (procAcc) {
switch(optype) {
case LT:
op = ">=";
procAccLt = true;
break;
case LE:
op = ">";
procAccLt = true;
break;
case EQ:
if (ropStr.equals("0")) {
ropStr = "1";
op = ">=";
procAccLt = true;
}
break;
}
procAcc = false;
}
res = "(" + lop.getRet() + " " + op + " " + ropStr + ")";
} else {
res = "(" + lop.getRet() + ")";
}
return new NodeTransResult(lop.getTableName() != null ? lop.getTableName() : rop.getTableName(), res, procAccLt);
} else {
ValueNode vNode = (ValueNode) root;
VisitResult vr = vNode.eval();
if (vr.isConst()) {
switch(vr.getValueType()) {
case TIMEINTVAL:
case TIMEPOINT:
case LONG:
return new NodeTransResult(null, "" + ((Long) vr.getValue()));
case STRING:
return new NodeTransResult(null, "'" + ((String) vr.getValue()) + "'");
case BOOLEAN:
if ((Boolean) vr.getValue()) {
return new NodeTransResult(null, "1");
} else {
return new NodeTransResult(null, "0");
}
default:
throw new IOException("Type = " + vr.getValueType().toString());
}
} else {
PropertyRealParas realParas = vr.getRealParas();
Property p = realParas.getProperty();
// TODO: Handle not
if (p.getPropertyName().equals("path")) {
ValueNode pathStrNode = (ValueNode) (vNode.getPeer());
VisitResult pathVr = pathStrNode.eval();
String pathStr = (String) pathVr.getValue();
pathCheckGlob.add(pathStr);
}
// TODO: hard code now, abstract later
if (p.getPropertyName().equals("accessCount") || p.getPropertyName().equals("ac")) {
String virTab = genAccessCountTable(transCtx == null ? 0 : transCtx.getRuleId(), (Long) realParas.getValues().get(0));
procAcc = true;
return new NodeTransResult(virTab, realParas.formatParameters());
}
if (p.getPropertyName().equals("accessCountTop") || p.getPropertyName().equals("accessCountBottom") || p.getPropertyName().equals("acTop") || p.getPropertyName().equals("acBot")) {
boolean topFlag = p.getPropertyName().equals("accessCountTop") || p.getPropertyName().equals("acTop");
String virTab = genAccessCountTable(transCtx == null ? 0 : transCtx.getRuleId(), (Long) realParas.getValues().get(0));
String func = "$@genVirtualAccessCountTable" + (topFlag ? "Top" : "Bottom") + "Value";
String mStr = virTab + (topFlag ? "_top_" : "_bottom_") + realParas.getValues().get(1).toString();
String mStrValue = mStr + "_value";
if (!sqlStatements.contains(func + "(" + mStr + ")")) {
sqlStatements.add(func + "(" + mStr + ")");
dynamicParameters.put(mStr, Arrays.asList(realParas.getValues(), virTab, mStrValue));
}
procAcc = true;
return new NodeTransResult(null, "$" + mStrValue);
}
if (p.getPropertyName().equals("accessCountTopOnStoragePolicy") || p.getPropertyName().equals("accessCountBottomOnStoragePolicy") || p.getPropertyName().equals("acTopSp") || p.getPropertyName().equals("acBotSp")) {
boolean topFlag = p.getPropertyName().equals("accessCountTopOnStoragePolicy") || p.getPropertyName().equals("acTopSp");
String virTab = genAccessCountTable(transCtx == null ? 0 : transCtx.getRuleId(), (Long) realParas.getValues().get(0));
String func = "$@genVirtualAccessCountTable" + (topFlag ? "Top" : "Bottom") + "ValueOnStoragePolicy";
String mStr = virTab + (topFlag ? "_top_" : "_bottom_") + realParas.getValues().get(1).toString() + "_on_storage_policy_" + realParas.getValues().get(2).toString();
String mStrValue = mStr + "_value";
if (!sqlStatements.contains(func + "(" + mStr + ")")) {
sqlStatements.add(func + "(" + mStr + ")");
dynamicParameters.put(mStr, Arrays.asList(realParas.getValues(), virTab, mStrValue));
}
procAcc = true;
return new NodeTransResult(null, "$" + mStrValue);
}
return new NodeTransResult(p.getTableName(), realParas.formatParameters());
}
}
// return new NodeTransResult(tableName, "");
}
use of org.smartdata.rule.objects.Property in project SSM by Intel-bigdata.
the class SmartRuleVisitTranslator method visitIdAtt.
// ID
@Override
public TreeNode visitIdAtt(SmartRuleParser.IdAttContext ctx) {
// System.out.println("Bare ID: " + ctx.getText());
Property p = objects.get("Default").getProperty(ctx.getText());
if (p == null) {
throw new RuleParserException("Object " + objects.get("Default").toString() + " does not have a attribute named '" + "'" + ctx.getText());
}
if (p.getParamsTypes() != null) {
throw new RuleParserException("Should have no parameter(s) for " + ctx.getText());
}
PropertyRealParas realParas = new PropertyRealParas(p, null);
realParases.add(realParas);
return new ValueNode(new VisitResult(p.getValueType(), null, realParas));
}
use of org.smartdata.rule.objects.Property in project SSM by Intel-bigdata.
the class SmartRuleVisitTranslator method visitIdAttPara.
@Override
public TreeNode visitIdAttPara(SmartRuleParser.IdAttParaContext ctx) {
SmartObject obj = createIfNotExist("Default");
Property p = obj.getProperty(ctx.ID().getText());
if (p == null) {
throw new RuleParserException("Object " + obj.toString() + " does not have a attribute named '" + ctx.ID().getText() + "'");
}
if (p.getParamsTypes() == null) {
throw new RuleParserException(obj.toString() + "." + ctx.ID().getText() + " does not need parameter(s)");
}
int numParameters = ctx.getChildCount() / 2 - 1;
if (p.getParamsTypes().size() != numParameters) {
throw new RuleParserException(obj.toString() + "." + ctx.ID().getText() + " needs " + p.getParamsTypes().size() + " instead of " + numParameters);
}
return parseIdParams(ctx, p, 2);
}
use of org.smartdata.rule.objects.Property in project SSM by Intel-bigdata.
the class SmartRuleVisitTranslator method visitIdObjAtt.
@Override
public TreeNode visitIdObjAtt(SmartRuleParser.IdObjAttContext ctx) {
SmartObject obj = createIfNotExist(ctx.OBJECTTYPE().toString());
Property p = obj.getProperty(ctx.ID().getText());
if (p == null) {
throw new RuleParserException("Object " + obj.toString() + " does not have a attribute named '" + "'" + ctx.ID().getText());
}
if (p.getParamsTypes() != null) {
throw new RuleParserException("Should have no parameter(s) for " + ctx.getText());
}
PropertyRealParas realParas = new PropertyRealParas(p, null);
realParases.add(realParas);
return new ValueNode(new VisitResult(p.getValueType(), null, realParas));
}
use of org.smartdata.rule.objects.Property in project SSM by Intel-bigdata.
the class SmartRuleVisitTranslator method visitIdObjAttPara.
@Override
public TreeNode visitIdObjAttPara(SmartRuleParser.IdObjAttParaContext ctx) {
String objName = ctx.OBJECTTYPE().getText();
String propertyName = ctx.ID().getText();
Property p = createIfNotExist(objName).getProperty(propertyName);
if (p == null) {
throw new RuleParserException("Object " + ctx.OBJECTTYPE().toString() + " does not have a attribute named '" + "'" + ctx.ID().getText());
}
if (p.getParamsTypes() == null) {
throw new RuleParserException(ctx.OBJECTTYPE().toString() + "." + ctx.ID().getText() + " does not need parameter(s)");
}
int numParameters = ctx.getChildCount() / 2 - 2;
if (p.getParamsTypes().size() != numParameters) {
throw new RuleParserException(ctx.OBJECTTYPE().toString() + "." + ctx.ID().getText() + " needs " + p.getParamsTypes().size() + " instead of " + numParameters);
}
return parseIdParams(ctx, p, 4);
}
Aggregations