use of org.kie.dmn.feel.lang.impl.MapBackedType in project drools by kiegroup.
the class ASTCompilerVisitor method visit.
@Override
public DirectCompilerResult visit(ContextNode n) {
if (n.getEntries().isEmpty()) {
return DirectCompilerResult.of(FeelCtx.emptyContext(), BuiltInType.CONTEXT);
}
scopeHelper.pushScope();
// openContext(feelCtx)
MapBackedType resultType = new MapBackedType();
DirectCompilerResult openContext = DirectCompilerResult.of(FeelCtx.openContext(), resultType);
// .setEntry( k,v )
// .setEntry( k,v )
// ...
DirectCompilerResult entries = n.getEntries().stream().map(e -> {
DirectCompilerResult r = e.accept(this);
scopeHelper.addInScope(e.getName().getText(), r.resultType);
return r;
}).reduce(openContext, (l, r) -> DirectCompilerResult.of(r.getExpression().asMethodCallExpr().setScope(l.getExpression()), r.resultType, DirectCompilerResult.mergeFDs(l, r)));
scopeHelper.popScope();
// .closeContext()
return DirectCompilerResult.of(FeelCtx.closeContext(entries), resultType, entries.getFieldDeclarations());
}
use of org.kie.dmn.feel.lang.impl.MapBackedType in project drools by kiegroup.
the class FEELParserTest method testQualifiedName.
@Test
public void testQualifiedName() {
String inputExpression = "My Person.Full Name";
MapBackedType personType = new MapBackedType("Person", mapOf(entry("Full Name", BuiltInType.STRING), entry("Age", BuiltInType.NUMBER)));
BaseNode qualRef = parse(inputExpression, mapOf(entry("My Person", personType)));
assertThat(qualRef, is(instanceOf(QualifiedNameNode.class)));
assertThat(qualRef.getResultType(), is(BuiltInType.STRING));
List<NameRefNode> parts = ((QualifiedNameNode) qualRef).getParts();
// `My Person` ...
assertThat(parts.get(0), is(instanceOf(NameRefNode.class)));
assertThat(parts.get(0).getResultType(), is(personType));
// ... `.Full Name`
assertThat(parts.get(1), is(instanceOf(NameRefNode.class)));
assertThat(parts.get(1).getResultType(), is(BuiltInType.STRING));
assertLocation(inputExpression, qualRef);
}
use of org.kie.dmn.feel.lang.impl.MapBackedType in project drools by kiegroup.
the class CompileEvaluateTest method test2.
@Test
public void test2() {
CompilerContext ctx = feel.newCompilerContext();
ctx.addInputVariableType("MyPerson", new MapBackedType().addField("FullName", BuiltInType.STRING));
CompiledExpression compiledExpression = feel.compile("MyPerson.fullName", ctx);
assertThat(errors.toString(), errors.size(), is(1));
Map<String, Object> inputs = new HashMap<>();
inputs.put("MyPerson", prototype(entry("FullName", "John Doe")));
Object result = feel.evaluate(compiledExpression, inputs);
assertThat(result, nullValue());
}
use of org.kie.dmn.feel.lang.impl.MapBackedType in project drools by kiegroup.
the class CompileEvaluateTest method testHyphenInPropertyOfCollectionForAccessor.
@Test
public void testHyphenInPropertyOfCollectionForAccessor() {
MapBackedType compositeType = new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING);
CompilerContext ctx = feel.newCompilerContext();
ctx.addInputVariableType("my input", new GenListType(compositeType));
CompiledExpression compiledExpression = feel.compile("my input[1].Primary-Key", ctx);
assertTrue(errors.isEmpty());
Map<String, Object> inputs = new HashMap<>();
inputs.put("my input", Arrays.asList(prototype(entry("Primary-Key", "k987"))));
Object result = feel.evaluate(compiledExpression, inputs);
assertThat(result, is("k987"));
assertTrue(errors.isEmpty());
}
use of org.kie.dmn.feel.lang.impl.MapBackedType in project drools by kiegroup.
the class CompileEvaluateTest method testHyphenInPropertyOfCollectionForProjection.
@Test
public void testHyphenInPropertyOfCollectionForProjection() {
MapBackedType compositeType = new MapBackedType().addField("Primary-Key", BuiltInType.STRING).addField("Value", BuiltInType.STRING);
CompilerContext ctx = feel.newCompilerContext();
ctx.addInputVariableType("input", new GenListType(compositeType));
CompiledExpression compiledExpression = feel.compile("input.Primary-Key", ctx);
assertTrue(errors.isEmpty());
Map<String, Object> inputs = new HashMap<>();
inputs.put("input", Arrays.asList(prototype(entry("Primary-Key", "k987"))));
Object result = feel.evaluate(compiledExpression, inputs);
assertThat(result, is(Arrays.asList("k987")));
assertTrue(errors.isEmpty());
}
Aggregations