Skip to content

Commit 6ce2801

Browse files
committed
Add fromJson function and explicit conversion
1 parent 8474323 commit 6ce2801

File tree

49 files changed

+1251
-216
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1251
-216
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2016 Goldman Sachs.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
*
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.gs.dmn.el.synthesis.triple;
14+
15+
import com.gs.dmn.el.analysis.semantics.type.Type;
16+
17+
public class AsTriple extends Triple {
18+
private final Triple leftOperand;
19+
private final Type rightOperandType;
20+
private final String nativeType;
21+
22+
AsTriple(Triple leftOperand, Type rightOperandType, String nativeType) {
23+
this.leftOperand = leftOperand;
24+
this.rightOperandType = rightOperandType;
25+
this.nativeType = nativeType;
26+
}
27+
28+
Triple getLeftOperand() {
29+
return leftOperand;
30+
}
31+
32+
Type getRightOperandType() {
33+
return rightOperandType;
34+
}
35+
36+
public String getNativeType() {
37+
return nativeType;
38+
}
39+
40+
@Override
41+
public <C, R> R accept(Visitor<C, R> visitor, C context) {
42+
return visitor.visit(this, context);
43+
}
44+
45+
@Override
46+
public String toString() {
47+
return String.format("As(%s, %s)", leftOperand, rightOperandType);
48+
}
49+
}

dmn-core/src/main/java/com/gs/dmn/el/synthesis/triple/InstanceOfTriple.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
public class InstanceOfTriple extends Triple {
1818
private final Triple leftOperand;
1919
private final Type rightOperandType;
20+
private final String nativeType;
2021

21-
InstanceOfTriple(Triple leftOperand, Type rightOperandType) {
22+
InstanceOfTriple(Triple leftOperand, Type rightOperandType, String nativeType) {
2223
this.leftOperand = leftOperand;
2324
this.rightOperandType = rightOperandType;
25+
this.nativeType = nativeType;
2426
}
2527

2628
Triple getLeftOperand() {
@@ -31,6 +33,10 @@ Type getRightOperandType() {
3133
return rightOperandType;
3234
}
3335

36+
public String getNativeType() {
37+
return nativeType;
38+
}
39+
3440
@Override
3541
public <C, R> R accept(Visitor<C, R> visitor, C context) {
3642
return visitor.visit(this, context);

dmn-core/src/main/java/com/gs/dmn/el/synthesis/triple/TripleSerializerToString.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,13 @@ public String visit(SomeTriple triple, Triples context) {
296296
@Override
297297
public String visit(InstanceOfTriple triple, Triples context) {
298298
String operandText = triple.getLeftOperand().accept(this, context);
299-
return this.nativeFactory.makeInstanceOf(operandText, triple.getRightOperandType());
299+
return this.nativeFactory.makeInstanceOf(operandText, triple.getRightOperandType(), triple.getNativeType());
300+
}
301+
302+
@Override
303+
public String visit(AsTriple triple, Triples context) {
304+
String operandText = triple.getLeftOperand().accept(this, context);
305+
return this.nativeFactory.makeAsType(operandText, triple.getRightOperandType(), triple.getNativeType());
300306
}
301307

302308
@Override
@@ -314,4 +320,9 @@ public String visit(TripleReference triple, Triples context) {
314320
public String visit(TextTriple triple, Triples context) {
315321
return triple.getText();
316322
}
323+
324+
@Override
325+
public String visit(TypeExpressionTriple triple, Triples context) {
326+
return this.nativeFactory.makeTypeReference(triple.getType(), triple.getNativeTypeQName());
327+
}
317328
}

dmn-core/src/main/java/com/gs/dmn/el/synthesis/triple/Triples.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public Triple dateTimeLiteral(com.gs.dmn.feel.analysis.syntax.ast.expression.lit
7373
return addTriple(triple);
7474
}
7575

76+
public Triple typeExpression(Type type, String nativeTypeQName) {
77+
Triple triple = new TypeExpressionTriple(type, nativeTypeQName);
78+
return addTriple(triple);
79+
}
80+
7681
public Triple nullLiteral() {
7782
Triple triple = new NullLiteral();
7883
return addTriple(triple);
@@ -263,8 +268,13 @@ public Triple makeEveryExpression(Triple forList) {
263268
return addTriple(triple);
264269
}
265270

266-
public Triple makeInstanceOf(Triple leftOperand, Type rightOperandType) {
267-
Triple triple = new InstanceOfTriple(leftOperand, rightOperandType);
271+
public Triple makeInstanceOf(Triple leftOperand, Type rightOperandType, String nativeType) {
272+
Triple triple = new InstanceOfTriple(leftOperand, rightOperandType, nativeType);
273+
return addTriple(triple);
274+
}
275+
276+
public Triple makeAs(Triple leftOperand, Type rightOperandType, String nativeType) {
277+
Triple triple = new AsTriple(leftOperand, rightOperandType, nativeType);
268278
return addTriple(triple);
269279
}
270280

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016 Goldman Sachs.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
5+
*
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.gs.dmn.el.synthesis.triple;
14+
15+
import com.gs.dmn.el.analysis.semantics.type.Type;
16+
17+
public class TypeExpressionTriple extends Triple {
18+
private final Type type;
19+
private final String nativeTypeQName;
20+
21+
TypeExpressionTriple(Type type, String nativeTypeQName) {
22+
this.type = type;
23+
this.nativeTypeQName = nativeTypeQName;
24+
}
25+
26+
Type getType() {
27+
return type;
28+
}
29+
30+
public String getNativeTypeQName() {
31+
return nativeTypeQName;
32+
}
33+
34+
@Override
35+
public <C, R> R accept(Visitor<C, R> visitor, C context) {
36+
return visitor.visit(this, context);
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return String.format("TypeOf(%s)", type);
42+
}
43+
}

dmn-core/src/main/java/com/gs/dmn/el/synthesis/triple/Visitor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,13 @@ public interface Visitor<C, R> {
9797

9898
R visit(InstanceOfTriple triple, C context);
9999

100+
R visit(AsTriple triple, C context);
101+
100102
R visit(LazyEvaluationTriple triple, C context);
101103

102104
R visit(TripleReference triple, C context);
103105

104106
R visit(TextTriple triple, C context);
107+
108+
R visit(TypeExpressionTriple triple, C context);
105109
}

dmn-core/src/main/java/com/gs/dmn/feel/analysis/semantics/FEELSemanticVisitor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,18 @@ public Element<Type> visit(InstanceOfExpression<Type> element, DMNContext contex
518518
return element;
519519
}
520520

521+
@Override
522+
public Element<Type> visit(AsExpression<Type> element, DMNContext context) {
523+
// Visit children
524+
element.getLeftOperand().accept(this, context);
525+
element.getRightOperand().accept(this, context);
526+
527+
// Derive type
528+
element.setType(element.getRightOperand().getType());
529+
530+
return element;
531+
}
532+
521533
//
522534
// Expressions
523535
//

dmn-core/src/main/java/com/gs/dmn/feel/analysis/semantics/FunctionInvocationUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ private static Type refineFunctionType(FunctionInvocation<Type> element, Declara
142142
Type listType = parameters.getParameterType(0, "list");
143143
Type functionType = parameters.getParameterType(1, "function");
144144
return StandardEnvironmentFactory.makeSortBuiltinFunctionType(listType, functionType);
145+
} else if ("fromJson".equals(functionName)) {
146+
Expression<Type> secondParameter = parameters.getParameter(1, "type");
147+
return StandardEnvironmentFactory.makeFromJsonBuiltinFunctionType(secondParameter.getType());
145148
} else if ("range".equals(functionName)) {
146149
Expression<Type> from = parameters.getParameter(0, "from");
147150
Type returnType = ANY;

dmn-core/src/main/java/com/gs/dmn/feel/analysis/semantics/environment/StandardEnvironmentFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ public static BuiltinFunctionType makeRangeBuiltinFunctionType(Type returnType)
133133
return new BuiltinFunctionType(new RangeType(returnType), new FormalParameter<>("from", STRING));
134134
}
135135

136+
public static BuiltinFunctionType makeFromJsonBuiltinFunctionType(Type returnType) {
137+
return new BuiltinFunctionType(returnType, new FormalParameter<>("string", STRING), new FormalParameter<>("type", ANY));
138+
}
139+
136140
// Signavio
137141
private static boolean isLibraryFunction(Type originalFunctionType) {
138142
return originalFunctionType instanceof LibraryFunctionType;
@@ -272,6 +276,8 @@ private static void addBooleanFunctions(Environment environment) {
272276
}
273277

274278
private static void addStringFunctions(Environment environment) {
279+
addFunctionDeclaration(environment, "fromJson", makeFromJsonBuiltinFunctionType(ANY));
280+
addFunctionDeclaration(environment, "fromJson1", new BuiltinFunctionType(CONTEXT, new FormalParameter<>("string", STRING)));
275281
addFunctionDeclaration(environment, "substring", new BuiltinFunctionType(STRING, new FormalParameter<>("string", STRING), new FormalParameter<>("start position", NUMBER), new FormalParameter<>("length", NUMBER, true, false)));
276282
addFunctionDeclaration(environment, "string length", new BuiltinFunctionType(NUMBER, new FormalParameter<>("string", STRING)));
277283
addFunctionDeclaration(environment, "upper case", new BuiltinFunctionType(STRING, new FormalParameter<>("string", STRING)));

dmn-core/src/main/java/com/gs/dmn/feel/analysis/semantics/type/DataType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public boolean hasConversionFunction(String kind) {
3030

3131
@Override
3232
public String typeExpression() {
33-
return getName();
33+
String name = getName();
34+
if (name.indexOf(' ') != -1) {
35+
return String.format("'%s'", name);
36+
} else {
37+
return name;
38+
}
3439
}
3540
}

0 commit comments

Comments
 (0)