Skip to content

Commit

Permalink
Type protocols/"specs" as hints to compilation stage (#15)
Browse files Browse the repository at this point in the history
* 2 step fwd 1 step back
* Hard-coded, but working.
* Less hard-coding, working.
* Should no longer have hard-coding, and working.
* Draft of working version.
* Refactorings.
* Basic support for Maps.
* Refactoring to use FEEL Type for hierarchical type definition
* WIP hierarchical.
* Removing unnecessary changes.
* Removing scratchpad tests.
* Moving determineTypeFromClass from Type interface to ParserHelper class.
* Was static class in interface "Field", now "Property" interface+impl.
* Was "FEELAccessor", now "FEELProperty".
  • Loading branch information
tarilabs authored and etirelli committed Nov 24, 2016
1 parent bfce0a2 commit 879d8f1
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.kie.dmn.feel.lang;

import java.util.Map;

public interface CustomType extends Type {

Map<String, Property> getProperties();

}
15 changes: 15 additions & 0 deletions kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/FEELProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.kie.dmn.feel.lang;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(ElementType.METHOD)
public @interface FEELProperty {

String value();

}
10 changes: 10 additions & 0 deletions kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/Property.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.kie.dmn.feel.lang;


public interface Property {

String getName();

Type getType();

}
2 changes: 2 additions & 0 deletions kie-dmn-feel/src/main/java/org/kie/dmn/feel/lang/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,6 @@ public interface Scope {
*/
boolean followUp(String token, boolean isPredict);

Map<String, Symbol> getSymbols();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.kie.dmn.feel.lang.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.kie.dmn.feel.lang.CustomType;
import org.kie.dmn.feel.lang.FEELProperty;
import org.kie.dmn.feel.lang.Property;
import org.kie.dmn.feel.parser.feel11.ParserHelper;

public class JavaBackedType implements CustomType {
private Class<?> wrapped;
private Map<String, Property> properties = new HashMap<>();

public JavaBackedType(Class<?> class1) {
this.wrapped = class1;
Stream.of( class1.getMethods() )
.filter( m -> m.getAnnotation(FEELProperty.class) != null )
.forEach( m -> properties.put( m.getAnnotation(FEELProperty.class).value() , new PropertyImpl( m.getAnnotation(FEELProperty.class).value() , ParserHelper.determineTypeFromClass(m.getReturnType()) ) ) );
;
}

@Override
public String getName() {
return wrapped.getName();
}

@Override
public Object fromString(String value) {
return null;
}

@Override
public String toString(Object value) {
return null;
}

public Class<?> getWrapped() {
return wrapped;
}

@Override
public Map<String, Property> getProperties() {
return this.properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.kie.dmn.feel.lang.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kie.dmn.feel.lang.CustomType;
import org.kie.dmn.feel.lang.Property;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.parser.feel11.ParserHelper;

public class MapBackedType implements CustomType {

private Map<String, Property> properties = new HashMap<>();

public MapBackedType() {
}

/**
* Utility constructor by reflection over key-value pairs.
* @param fields
*/
public MapBackedType(Map<String, ?> map) {
map.entrySet().stream()
.map( kv -> new PropertyImpl( kv.getKey(), ParserHelper.determineTypeFromClass( kv.getValue().getClass()) ) )
.forEach( f -> properties.put( f.getName(), f ) );
}

public MapBackedType(List<Property> properties) {
properties.stream().forEach(p -> this.properties.put(p.getName(), p) );
}

@Override
public String getName() {
return null;
}

@Override
public Object fromString(String value) {
return null;
}

@Override
public String toString(Object value) {
return null;
}

public MapBackedType addField(String name, Type type) {
properties.put( name, new PropertyImpl(name, type) );
return this;
}

@Override
public Map<String, Property> getProperties() {
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.kie.dmn.feel.lang.impl;

import org.kie.dmn.feel.lang.Property;
import org.kie.dmn.feel.lang.Type;


public class PropertyImpl implements Property {
private final String name;
private final Type type;

public PropertyImpl(String name, Type type) {
super();
this.name = name;
this.type = type;
}

@Override
public String getName() {
return name;
}

@Override
public Type getType() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
import org.kie.dmn.feel.lang.Symbol;
import org.kie.dmn.feel.util.EvalHelper;
import org.kie.dmn.feel.util.TokenTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.Map.Entry;

public class ScopeImpl
implements Scope {
public static final Logger LOG = LoggerFactory.getLogger(ScopeImpl.class);

private String name;
private Scope parentScope;
Expand Down Expand Up @@ -110,8 +114,14 @@ public Map<String, Scope> getChildScopes() {
public void setChildScopes(Map<String, Scope> childScopes) {
this.childScopes = childScopes;
}

@Override
public Map<String, Symbol> getSymbols() {
return symbols;
}

public void start( String token ) {
LOG.trace("[{}]: start() {}", name, token);
if( tokenTree == null ) {
initializeTokenTree();
}
Expand All @@ -122,12 +132,14 @@ public void start( String token ) {
}

public boolean followUp( String token, boolean isPredict ) {
LOG.trace("[{}]: followUp() {}", name, token);
// must call followup on parent scope
boolean parent = this.parentScope != null ? this.parentScope.followUp( token, isPredict ) : false;
return this.tokenTree.followUp( token, !isPredict ) || parent;
}

private void initializeTokenTree() {
LOG.trace("[{}]: initializeTokenTree()");
tokenTree = new TokenTree();
for( String symbol : symbols.keySet() ) {
List<String> tokens = tokenize( symbol );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.antlr.v4.runtime.*;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.impl.JavaBackedType;
import org.kie.dmn.feel.lang.impl.FEELEventListenersManager;
import org.kie.dmn.feel.runtime.events.FEELEvent;
import org.kie.dmn.feel.runtime.events.SyntaxErrorEvent;
Expand All @@ -38,10 +39,22 @@ public static FEEL_1_1Parser parse(FEELEventListenersManager eventsManager, Stri

// pre-loads the parser with symbols
defineVariables( inputVariableTypes, inputVariables, parser );

return parser;
}

private static void defineVariables(Map<String, Type> inputVariableTypes, Map<String, Object> inputVariables, FEEL_1_1Parser parser) {

// switched order:

inputVariableTypes.forEach( (name, type) -> {
// if( ! inputVariables.containsKey( name ) ) {
// parser.getHelper().defineVariable( name );
// }
parser.getHelper().defineVariable( name, type );
} );

// TODO the following may be no longer necessary:
inputVariables.forEach( (name, value) -> {
parser.getHelper().defineVariable( name );
if( value instanceof Map ) {
Expand All @@ -55,11 +68,7 @@ private static void defineVariables(Map<String, Type> inputVariableTypes, Map<St
}
}
} );
inputVariableTypes.forEach( (name, type) -> {
if( ! inputVariables.containsKey( name ) ) {
parser.getHelper().defineVariable( name );
}
} );

}

public static class FEELErrorHandler extends DefaultErrorStrategy {
Expand Down
Loading

0 comments on commit 879d8f1

Please sign in to comment.