use of org.kie.dmn.feel.lang.Type in project drools by kiegroup.
the class ASTCompilerVisitor method visit.
@Override
public DirectCompilerResult visit(QualifiedNameNode n) {
List<NameRefNode> parts = n.getParts();
DirectCompilerResult nameRef0 = parts.get(0).accept(this);
Type typeCursor = nameRef0.resultType;
Expression currentContext = nameRef0.getExpression();
for (int i = 1; i < parts.size(); i++) {
NameRefNode acc = parts.get(i);
String key = acc.getText();
if (typeCursor instanceof CompositeType) {
CompositeType currentContextType = (CompositeType) typeCursor;
currentContext = Contexts.getKey(currentContext, currentContextType, key);
typeCursor = currentContextType.getFields().get(key);
} else {
// degraded mode, or accessing fields of DATE etc.
currentContext = Expressions.path(currentContext, new StringLiteralExpr(key));
typeCursor = BuiltInType.UNKNOWN;
}
}
// Otherwise in case of QualifiedName expression, for a structured type like this case, it need to be coerced on the last accessor:
return DirectCompilerResult.of(Expressions.coerceNumber(currentContext), typeCursor);
}
use of org.kie.dmn.feel.lang.Type in project drools by kiegroup.
the class ASTCompilerVisitor method visit.
@Override
public DirectCompilerResult visit(NameRefNode n) {
String nameRef = EvalHelper.normalizeVariableName(n.getText());
Type type = scopeHelper.resolve(nameRef).orElse(BuiltInType.UNKNOWN);
return DirectCompilerResult.of(FeelCtx.getValue(nameRef), type);
}
use of org.kie.dmn.feel.lang.Type in project drools by kiegroup.
the class FunctionTypeNode method evaluate.
@Override
public Type evaluate(EvaluationContext ctx) {
List<Type> args = argTypes.stream().map(t -> t.evaluate(ctx)).collect(Collectors.toList());
Type ret = retType.evaluate(ctx);
return new GenFnType(args, ret);
}
use of org.kie.dmn.feel.lang.Type in project drools by kiegroup.
the class ParserHelper method recoverScope.
public void recoverScope(String name) {
LOG.trace("[{}] recoverScope( name: {}) with currentScope: {}", this.currentScope.getName(), name, currentScope);
Scope s = this.currentScope.getChildScopes().get(name);
if (s != null) {
currentScope = s;
if (currentScope.getType() != null && currentScope.getType().equals(BuiltInType.UNKNOWN)) {
enableDynamicResolution();
}
} else {
Symbol resolved = this.currentScope.resolve(name);
Type scopeType = resolved != null ? resolved.getType() : null;
if (scopeType instanceof GenListType) {
scopeType = ((GenListType) scopeType).getGen();
}
if (resolved != null && scopeType instanceof CompositeType) {
pushScope(scopeType);
CompositeType type = (CompositeType) scopeType;
for (Map.Entry<String, Type> f : type.getFields().entrySet()) {
this.currentScope.define(new VariableSymbol(f.getKey(), f.getValue()));
}
LOG.trace(".. PUSHED, scope name {} with symbols {}", this.currentName.peek(), this.currentScope.getSymbols());
} else if (resolved != null && scopeType instanceof SimpleType) {
BuiltInType resolvedBIType = null;
if (scopeType instanceof BuiltInType) {
resolvedBIType = (BuiltInType) scopeType;
} else if (scopeType instanceof AliasFEELType) {
resolvedBIType = ((AliasFEELType) scopeType).getBuiltInType();
} else {
throw new UnsupportedOperationException("Unsupported BIType " + scopeType + "!");
}
pushScope(resolvedBIType);
switch(resolvedBIType) {
// FEEL spec table 53
case DATE:
this.currentScope.define(new VariableSymbol("year", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("month", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("day", BuiltInType.NUMBER));
if (isFeatDMN12weekday()) {
// Table 60 spec DMN v1.2
this.currentScope.define(new VariableSymbol("weekday", BuiltInType.NUMBER));
}
break;
case TIME:
this.currentScope.define(new VariableSymbol("hour", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("minute", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("second", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("time offset", BuiltInType.DURATION));
this.currentScope.define(new VariableSymbol("timezone", BuiltInType.NUMBER));
break;
case DATE_TIME:
this.currentScope.define(new VariableSymbol("year", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("month", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("day", BuiltInType.NUMBER));
if (isFeatDMN12weekday()) {
// Table 60 spec DMN v1.2
this.currentScope.define(new VariableSymbol("weekday", BuiltInType.NUMBER));
}
this.currentScope.define(new VariableSymbol("hour", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("minute", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("second", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("time offset", BuiltInType.DURATION));
this.currentScope.define(new VariableSymbol("timezone", BuiltInType.NUMBER));
break;
case DURATION:
// TODO might need to distinguish between `years and months duration` and `days and time duration`
this.currentScope.define(new VariableSymbol("years", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("months", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("days", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("hours", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("minutes", BuiltInType.NUMBER));
this.currentScope.define(new VariableSymbol("seconds", BuiltInType.NUMBER));
break;
case RANGE:
this.currentScope.define(new VariableSymbol("start included", BuiltInType.BOOLEAN));
this.currentScope.define(new VariableSymbol("start", BuiltInType.UNKNOWN));
this.currentScope.define(new VariableSymbol("end", BuiltInType.UNKNOWN));
this.currentScope.define(new VariableSymbol("end included", BuiltInType.BOOLEAN));
break;
// table 53 applies only to type(e) is a date/time/duration
case UNKNOWN:
enableDynamicResolution();
break;
default:
break;
}
} else {
pushScope();
}
}
}
use of org.kie.dmn.feel.lang.Type in project drools by kiegroup.
the class ASTBuilderVisitor method visitQualifiedName.
@Override
public BaseNode visitQualifiedName(FEEL_1_1Parser.QualifiedNameContext ctx) {
ArrayList<NameRefNode> parts = new ArrayList<>();
Type typeCursor = null;
for (FEEL_1_1Parser.NameRefContext t : ctx.nameRef()) {
String originalText = ParserHelper.getOriginalText(t);
if (typeCursor == null) {
typeCursor = scopeHelper.resolve(originalText).orElse(BuiltInType.UNKNOWN);
} else if (typeCursor instanceof CompositeType) {
typeCursor = ((CompositeType) typeCursor).getFields().get(originalText);
} else {
// TODO throw error here?
typeCursor = BuiltInType.UNKNOWN;
}
parts.add(ASTBuilderFactory.newNameRefNode(t, typeCursor));
}
return parts.size() > 1 ? ASTBuilderFactory.newQualifiedNameNode(ctx, parts, typeCursor) : parts.get(0);
}
Aggregations