Skip to content

Commit

Permalink
[DROOLS-2625] fix annotations usage and array access in executable mo…
Browse files Browse the repository at this point in the history
…del (#1944)

[DROOLS-2625] fix annotations usage and array access in executable model
  • Loading branch information
mariofusco authored Jun 13, 2018
1 parent 40b2d10 commit d41c3c1
Show file tree
Hide file tree
Showing 40 changed files with 999 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.drools.model.functions.Function0;
import org.drools.model.functions.Function1;
import org.drools.model.functions.Function2;
import org.drools.model.functions.Function3;
import org.drools.model.functions.Operator;
import org.drools.model.functions.Predicate1;
import org.drools.model.functions.Predicate2;
Expand Down Expand Up @@ -56,6 +57,7 @@
import org.drools.model.impl.From0Impl;
import org.drools.model.impl.From1Impl;
import org.drools.model.impl.From2Impl;
import org.drools.model.impl.From3Impl;
import org.drools.model.impl.GlobalImpl;
import org.drools.model.impl.PrototypeImpl;
import org.drools.model.impl.PrototypeVariableImpl;
Expand Down Expand Up @@ -201,6 +203,10 @@ public static <T> UnitData<T> unitData( Class<T> type, String name ) {
return new UnitDataImpl( type, name );
}

public static <T> From<T> from( T value ) {
return from( () -> value );
}

public static <T> From<T> from( Variable<T> variable ) {
return new From1Impl<>( variable );
}
Expand All @@ -213,10 +219,14 @@ public static <T> From<T> from( Variable<T> variable, Function1<T, ?> provider )
return new From1Impl<>( variable, new Function1.Impl<>(provider) );
}

public static <T,U> From<T> from( Variable<T> var1, Variable<U> var2, Function2<T, U, ?> provider ) {
public static <A,B> From<A> from( Variable<A> var1, Variable<B> var2, Function2<A, B, ?> provider ) {
return new From2Impl<>( var1, var2, new Function2.Impl<>(provider) );
}

public static <A,B,C> From<A> from( Variable<A> var1, Variable<B> var2, Variable<C> var3, Function3<A, B, C, ?> provider ) {
return new From3Impl<>( var1, var2, var3, new Function3.Impl<>(provider) );
}

public static <T> From<T> reactiveFrom( Variable<T> variable, Function1<T, ?> provider ) {
return new From1Impl<>( variable, provider, true );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.drools.model.functions.Predicate1;
import org.drools.model.functions.Predicate2;
import org.drools.model.functions.temporal.TemporalPredicate;
import org.drools.model.impl.DeclarationImpl;
import org.drools.model.impl.Query0DefImpl;
import org.drools.model.impl.Query1DefImpl;
import org.drools.model.impl.Query2DefImpl;
Expand Down Expand Up @@ -39,6 +40,11 @@ public static <T> InputViewItem<T> input( Variable<T> var ) {
return new InputViewItemImpl<>( var );
}

public static <T> InputViewItem<T> input(Variable<T> var, DeclarationSource source) {
(( DeclarationImpl<T> ) var).setSource( source );
return new InputViewItemImpl<>( var );
}

public static <T> Expr1ViewItem<T> expr( Variable<T> var) {
return new Expr1ViewItemImpl<>( var, new Predicate1.Impl<>(t -> true) );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model;

import org.drools.model.functions.Function3;

public interface From3<A, B, C> extends From<A> {
Variable<B> getVariable2();
Variable<C> getVariable3();
Function3<A,B,C,?> getProvider();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.drools.model.functions.Predicate4;
import org.drools.model.functions.Predicate5;
import org.drools.model.functions.temporal.TemporalPredicate;
import org.drools.model.impl.DeclarationImpl;
import org.drools.model.impl.Query0DefImpl;
import org.drools.model.impl.Query1DefImpl;
import org.drools.model.impl.Query2DefImpl;
Expand All @@ -58,6 +59,11 @@ public static <T> PatternDef<T> pattern(Variable<T> var) {
return new PatternDefImpl<>( var );
}

public static <T> PatternDef<T> pattern(Variable<T> var, DeclarationSource source) {
(( DeclarationImpl<T> ) var).setSource( source );
return new PatternDefImpl<>( var );
}

public static <T, U> AlphaIndex<T, U> alphaIndexedBy( Class<U> indexedClass, Index.ConstraintType constraintType, int indexId, Function1<T, U> leftOperandExtractor, U rightValue ) {
return new AlphaIndexImpl<>( indexedClass, constraintType, indexId, leftOperandExtractor, rightValue );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2005 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.model.impl;

import org.drools.model.From3;
import org.drools.model.Variable;
import org.drools.model.functions.Function3;

public class From3Impl<A, B, C> implements From3<A, B, C>, ModelComponent {

private final Variable<A> var1;
private final Variable<B> var2;
private final Variable<C> var3;
private final Function3<A, B, C, ?> provider;
private final boolean reactive;

public From3Impl( Variable<A> var1, Variable<B> var2, Variable<C> var3, Function3<A, B, C, ?> provider ) {
this(var1, var2, var3, provider, false);
}

public From3Impl( Variable<A> var1, Variable<B> var2, Variable<C> var3, Function3<A, B, C, ?> provider, boolean reactive ) {
this.var1 = var1;
this.var2 = var2;
this.var3 = var3;
this.provider = provider;
this.reactive = reactive;
}

@Override
public Variable<A> getVariable() {
return var1;
}

@Override
public Variable<B> getVariable2() {
return var2;
}

@Override
public Variable<C> getVariable3() {
return var3;
}

@Override
public Function3<A, B, C, ?> getProvider() {
return provider;
}

@Override
public boolean isReactive() {
return reactive;
}

@Override
public boolean isEqualTo( ModelComponent o ) {
if ( this == o ) return true;
if ( !(o instanceof From3Impl) ) return false;

From3Impl<?,?,?> from = ( From3Impl<?,?,?> ) o;

if ( reactive != from.reactive ) return false;
if ( !ModelComponent.areEqualInModel( var1, from.var1 ) ) return false;
if ( !ModelComponent.areEqualInModel( var2, from.var2 ) ) return false;
if ( !ModelComponent.areEqualInModel( var3, from.var3 ) ) return false;
return provider != null ? provider.equals( from.provider ) : from.provider == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,76 @@ public boolean eval( Object a, Object b ) {
if ( a instanceof Collection ) {
return (( Collection ) a).contains( b );
}
if ( a.getClass().isArray() && evalArray( a, b ) ) {
return true;
}
return false;
}

private boolean evalArray( Object a, Object b ) {
if (a instanceof Object[]) {
for (Object o : (( Object[] ) a)) {
if (o.equals( b )) {
return true;
}
}
}
if (a instanceof int[]) {
for (int o : (( int[] ) a)) {
if (o == (int)b) {
return true;
}
}
}
if (a instanceof long[]) {
for (long o : (( long[] ) a)) {
if (o == (long)b) {
return true;
}
}
}
if (a instanceof double[]) {
for (double o : (( double[] ) a)) {
if (o == (double)b) {
return true;
}
}
}
if (a instanceof float[]) {
for (float o : (( float[] ) a)) {
if (o == (float)b) {
return true;
}
}
}
if (a instanceof boolean[]) {
for (boolean o : (( boolean[] ) a)) {
if (o == (boolean)b) {
return true;
}
}
}
if (a instanceof char[]) {
for (char o : (( char[] ) a)) {
if (o == (char)b) {
return true;
}
}
}
if (a instanceof byte[]) {
for (byte o : (( byte[] ) a)) {
if (o == (byte)b) {
return true;
}
}
}
if (a instanceof short[]) {
for (short o : (( short[] ) a)) {
if (o == (short)b) {
return true;
}
}
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ private Pattern findPatternImplSource() {
return ( Pattern ) condition;
}

final Variable source = getAccumulateFunctions()[0].getSource();
if (accumulateFunctions.length == 0) {
return null;
}

final Variable source = accumulateFunctions[0].getSource();

for (Condition subCondition : condition.getSubConditions()) {
if (subCondition instanceof PatternImpl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.drools.model.From0;
import org.drools.model.From1;
import org.drools.model.From2;
import org.drools.model.From3;
import org.drools.model.Global;
import org.drools.model.Index;
import org.drools.model.Model;
Expand Down Expand Up @@ -706,6 +707,9 @@ private DataProvider createFromDataProvider( RuleContext ctx, From<?> from ) {
if (from instanceof From2 ) {
return new LambdaDataProvider( toFunctionN( (( From2 ) from).getProvider() ), from.isReactive(), ctx.getDeclaration( from.getVariable() ), ctx.getDeclaration( (( From2 ) from).getVariable2() ) );
}
if (from instanceof From3 ) {
return new LambdaDataProvider( toFunctionN( (( From3 ) from).getProvider() ), from.isReactive(), ctx.getDeclaration( from.getVariable() ), ctx.getDeclaration( (( From3 ) from).getVariable2() ), ctx.getDeclaration( (( From3 ) from).getVariable3() ) );
}
throw new UnsupportedOperationException( "Unknown from type " + from );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected void generatePOJOs(PackageDescr packageDescr, PackageRegistry pkgRegis
return new PackageModel(pkgName, this.getBuilderConfiguration(), isPattern, dialectCompiletimeRegistry, exprIdGenerator);
});
model.addImports(pkg.getTypeResolver().getImports());
generatePOJO(pkg, packageDescr, model);
generatePOJO(this, pkg, packageDescr, model);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static TypedExpression nameExprToMethodCallExpr(String name, java.lang.re
return new TypedExpression( body, accessor.getReturnType() );
}
if (clazz.isArray() && name.equals( "length" )) {
FieldAccessExpr expr = new FieldAccessExpr( scope, name );
FieldAccessExpr expr = new FieldAccessExpr( scope != null ? scope : new NameExpr( "_this" ), name );
return new TypedExpression( expr, int.class );
}
try {
Expand Down Expand Up @@ -474,7 +474,7 @@ public static Type classToReferenceType(Class<?> declClass) {
Type parsedType = JavaParser.parseType(declClass.getCanonicalName());
return parsedType instanceof PrimitiveType ?
((PrimitiveType) parsedType).toBoxedType() :
parsedType.getElementType();
parsedType;
}

public static Type toType(Class<?> declClass) {
Expand Down
Loading

0 comments on commit d41c3c1

Please sign in to comment.