Skip to content

Commit

Permalink
KOGITO-1853: Bump to KIE 7.36.0.Final + Backport KOGITO-1575 (0.9.x) (#…
Browse files Browse the repository at this point in the history
…444)


* [DROOLS-5227] improve FactType API (#2)

* [DROOLS-5227] improve FactType API

* [DROOLS-5207] fix oopath binding for chunks after the first (#4)

* [DROOLS-5227] improve FactType API

* [DROOLS-5207] fix oopath binding for chunks after the first

* [DROOLS-5227] improve FactType API

* fix rule unit tests

Co-authored-by: Matteo Mortari <[email protected]>
Co-authored-by: Mario Fusco <[email protected]>
  • Loading branch information
3 people authored Apr 16, 2020
1 parent 0c3de4a commit 86cdae4
Show file tree
Hide file tree
Showing 27 changed files with 187 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ private void lookupObjectType(RuleBuildContext context, PatternDescr patternDesc
firstXpathChunk.getConstraints()
.forEach(s -> patternDescr.addConstraint(new ExprConstraintDescr(s)));
if (!xpathAnalysis.isSinglePart()) {
patternDescr.addConstraint(new ExprConstraintDescr(patternDescr.getIdentifier() + " : " + expr.substring(xpathAnalysis.getPart(1).getStart())));
String xpathExpr = (patternDescr.getIdentifier() == null ? "" : patternDescr.getIdentifier() + " : ") + expr.substring(xpathAnalysis.getPart(1).getStart());
patternDescr.addConstraint(new ExprConstraintDescr(xpathExpr));
patternDescr.setIdentifier("$void$");
}
} else {
Expand Down Expand Up @@ -885,6 +886,7 @@ private Constraint buildXPathDescr(RuleBuildContext context,
currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
}

context.increaseXpathChuckNr();
pattern.setObjectType(currentObjectType);
backReferenceClasses.add(0, patternClass);
backRef.reset();
Expand All @@ -904,6 +906,7 @@ private Constraint buildXPathDescr(RuleBuildContext context,
mvelCtx.setInXpath(false);
pattern.setBackRefDeclarations(null);
pattern.setObjectType(originalType);
context.resetXpathChuckNr();
}

xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
Expand Down Expand Up @@ -1455,6 +1458,9 @@ protected void buildRuleBindings(RuleBuildContext context,
}

Declaration declr = pattern.addDeclaration(fieldBindingDescr.getVariable());
if (context.isInXpath()) {
declr.setxPathOffset( context.getXpathChuckNr() );
}

final InternalReadAccessor extractor = getFieldReadAccessor(context,
fieldBindingDescr,
Expand All @@ -1471,7 +1477,7 @@ protected void buildRuleBindings(RuleBuildContext context,

declr.setReadAccessor(extractor);

if (typeDeclaration != null && extractor instanceof ClassFieldReader) {
if (!declr.isFromXpathChunk() && typeDeclaration != null && extractor instanceof ClassFieldReader) {
addFieldToPatternWatchlist(pattern, typeDeclaration, ((ClassFieldReader) extractor).getFieldName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class RuleBuildContext extends PackageBuildContext {

private boolean inXpath;

private int xpathChuckNr = 0;

/**
* Default constructor
*/
Expand Down Expand Up @@ -236,4 +238,16 @@ private String extractClassNameFromSourcePath() {

return rule.getPackage() + "." + classNameBuilder.reverse().toString();
}

public void increaseXpathChuckNr() {
xpathChuckNr++;
}

public void resetXpathChuckNr() {
xpathChuckNr = 0;
}

public int getXpathChuckNr() {
return xpathChuckNr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ public static String setter2property(String methodName) {
return Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
}

public static boolean isGetter(String methodName) {
return (methodName.startsWith("get") && methodName.length() > 3) || (methodName.startsWith("is") && methodName.length() > 2);
}

public static boolean isSetter(String methodName) {
return (methodName.startsWith("set") && methodName.length() > 3);
}

public static Class<?> convertFromPrimitiveType(Class<?> type) {
if (!type.isPrimitive()) {
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public FactHandle getFactHandle(String identifier) {
}

public FactHandle getFactHandle(Declaration declr) {
return this.row.getHandles()[ declr.getPattern().getOffset() ];
return this.row.getHandles()[ declr.getOffset() ];
}

public FactHandle getFactHandle(int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private void updateFactory( Object knowledgeHelper,
}

for (Declaration decl : prevDecl) {
int offset = decl.getPattern().getOffset();
int offset = decl.getOffset();
Object o = decl.getValue(workingMemory, objs != null ? objs[offset] : handles[offset].getObject());
factory.getIndexedVariableResolver(i++).setValue(o);
}
Expand All @@ -401,7 +401,7 @@ private void updateFactory( Object knowledgeHelper,
for ( Declaration decl : this.localDeclarations ) {
Object value;
if( readLocalsFromTuple && tuple != null ) {
int offset = decl.getPattern().getOffset();
int offset = decl.getOffset();
value = decl.getValue( workingMemory,
objs != null ? objs[offset] : handles[offset].getObject() );
} else {
Expand Down Expand Up @@ -439,7 +439,7 @@ private void updateFactory( Object knowledgeHelper,

public static InternalFactHandle getFactHandle( Declaration declaration,
InternalFactHandle[] handles ) {
return handles != null && handles.length > declaration.getPattern().getOffset() ? handles[declaration.getPattern().getOffset()] : null;
return handles != null && handles.length > declaration.getOffset() ? handles[declaration.getOffset()] : null;
}

private static Serializable compile( final String text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public List<Object> getObjects() {

public Object getDeclarationValue(String variableName) {
Declaration decl = ((RuleImpl)this.rule).getDeclaration( variableName );
return decl.getValue( null, ((InternalFactHandle)factHandles.get(decl.getPattern().getOffset())).getObject() );
return decl.getValue( null, ((InternalFactHandle)factHandles.get(decl.getOffset())).getObject() );
}

public List<String> getDeclarationIds() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2020. Red Hat, Inc. and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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.core.factmodel;

public interface AccessibleFact {
Object getValue(String fieldName);
void setValue(String fieldName, Object value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Map;

import org.drools.core.phreak.Reactive;
import org.drools.core.util.ClassUtils;
import org.kie.api.definition.type.Annotation;
import org.kie.api.definition.type.FactField;
import org.kie.api.definition.type.FactType;
Expand Down Expand Up @@ -167,7 +166,7 @@ public final Collection<FieldDefinition> getFieldsDefinitions() {
* @param fieldName
* @return
*/
public final FieldDefinition getField(final String fieldName) {
public FieldDefinition getField(final String fieldName) {
return this.fields.get( fieldName );
}

Expand Down Expand Up @@ -268,15 +267,6 @@ public Object get(Object bean,
if (fieldDefinition != null) {
return fieldDefinition.getFieldAccessor().getValue( bean );
}
java.lang.reflect.Field f = ClassUtils.getField( definedClass, field );
if (f != null) {
f.setAccessible( true );
try {
return f.get( bean );
} catch (IllegalAccessException e) {
throw new RuntimeException( e );
}
}
return null;
}

Expand All @@ -286,16 +276,6 @@ public void set(Object bean,
FieldDefinition fieldDefinition = getField( field );
if (fieldDefinition != null) {
fieldDefinition.getFieldAccessor().setValue( bean, value );
} else {
java.lang.reflect.Field f = ClassUtils.getField( definedClass, field );
if (f != null) {
f.setAccessible( true );
try {
f.set( bean, value );
} catch (IllegalAccessException e) {
throw new RuntimeException( e );
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract class BaseTuple implements Tuple {
private boolean expired;

public Object getObject(Declaration declaration) {
return getObject(declaration.getPattern().getOffset());
return getObject(declaration.getOffset());
}

public Object getContextObject() {
Expand Down Expand Up @@ -128,7 +128,7 @@ public void clear() {

@Override
public InternalFactHandle get( Declaration declaration ) {
return get(declaration.getPattern().getOffset());
return get(declaration.getOffset());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class Declaration

private transient Class<?> declarationClass;

private int xPathOffset = 0;

// ------------------------------------------------------------
// Constructors
// ------------------------------------------------------------
Expand Down Expand Up @@ -188,6 +190,18 @@ public void setPattern(final Pattern pattern) {
this.pattern = pattern;
}

public int getOffset() {
return pattern.getOffset() + xPathOffset;
}

public void setxPathOffset( int xPathOffset ) {
this.xPathOffset = xPathOffset;
}

public boolean isFromXpathChunk() {
return xPathOffset >= 1;
}

/**
* Returns true if this declaration is a pattern declaration
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public boolean equals(final Object object) {
}

for ( int i = 0, length = this.requiredDeclarations.length; i < length; i++ ) {
if ( this.requiredDeclarations[i].getPattern().getOffset() != other.requiredDeclarations[i].getPattern().getOffset() ) {
if ( this.requiredDeclarations[i].getOffset() != other.requiredDeclarations[i].getOffset() ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public boolean equals(final Object object) {
}

for ( int i = 0, length = this.previousDeclarations.length; i < length; i++ ) {
if ( this.previousDeclarations[i].getPattern().getOffset() != other.previousDeclarations[i].getPattern().getOffset() ) {
if ( this.previousDeclarations[i].getOffset() != other.previousDeclarations[i].getOffset() ) {
return false;
}

Expand All @@ -220,7 +220,7 @@ public boolean equals(final Object object) {
}

for ( int i = 0, length = this.localDeclarations.length; i < length; i++ ) {
if ( this.localDeclarations[i].getPattern().getOffset() != other.localDeclarations[i].getPattern().getOffset() ) {
if ( this.localDeclarations[i].getOffset() != other.localDeclarations[i].getOffset() ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static class DeclarationMatcher implements Comparable {
public DeclarationMatcher(int originalIndex, Declaration declaration) {
this.declaration = declaration;
this.originalIndex = originalIndex;
this.rootDistance = declaration.getPattern().getOffset();
this.rootDistance = declaration.getOffset();
}

public int getOriginalIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public ContextEntry createContextEntry() {

public FieldIndex getFieldIndex() {
// declaration's offset can be modified by the reteoo's PatternBuilder so modify the indexingDeclaration accordingly
indexingDeclaration.getPattern().setOffset(declarations[0].getPattern().getOffset());
indexingDeclaration.getPattern().setOffset(declarations[0].getOffset());
return new FieldIndex(extractor, indexingDeclaration, INDEX_EVALUATOR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public Iterator getResults(Tuple leftTuple, InternalWorkingMemory wm, Propagatio
Object obj = fh.getObject();

if (obj instanceof DroolsQuery) {
obj = ((DroolsQuery)obj).getElements()[declaration.getPattern().getOffset()];
obj = ((DroolsQuery)obj).getElements()[declaration.getOffset()];
}

return xpathEvaluator.evaluate(wm, leftTuple, obj).iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public RowAdapter(final RuleImpl rule,
}

private InternalFactHandle getFactHandle(Declaration declr) {
return this.factHandles[ declr.getPattern().getOffset() ];
return this.factHandles[ declr.getOffset() ];
}

public Object get(String identifier) {
Expand Down
Loading

0 comments on commit 86cdae4

Please sign in to comment.