Search in sources :

Example 1 with BitMask

use of org.drools.core.util.bitmask.BitMask in project drools by kiegroup.

the class AccumulateNode method addAccFunctionDeclarationsToLeftMask.

private void addAccFunctionDeclarationsToLeftMask(InternalKnowledgeBase kbase, LeftTupleSource leftInput, Accumulate accumulate) {
    BitMask leftMask = getLeftInferredMask();
    ObjectType leftObjectType = leftInput.getObjectType();
    if (leftObjectType instanceof ClassObjectType) {
        TypeDeclaration typeDeclaration = kbase.getExactTypeDeclaration(((ClassObjectType) leftObjectType).getClassType());
        if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
            List<String> accessibleProperties = typeDeclaration.getAccessibleProperties();
            for (Declaration decl : accumulate.getRequiredDeclarations()) {
                if (leftObjectType.equals(decl.getPattern().getObjectType())) {
                    leftMask = leftMask.setAll(calculatePositiveMask(typeDeclaration.getTypeClass(), decl.getPattern().getListenedProperties(), accessibleProperties));
                }
            }
        }
    }
    setLeftInferredMask(leftMask);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) BitMask(org.drools.core.util.bitmask.BitMask) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 2 with BitMask

use of org.drools.core.util.bitmask.BitMask in project drools by kiegroup.

the class AlphaNode method calculateDeclaredMask.

public BitMask calculateDeclaredMask(Class modifiedClass, List<String> settableProperties) {
    boolean typeBit = false;
    if (constraint instanceof EvaluatorConstraint && ((EvaluatorConstraint) constraint).isSelf()) {
        Operator op = ((EvaluatorConstraint) constraint).getEvaluator().getOperator();
        if (op == IsAEvaluatorDefinition.ISA || op == IsAEvaluatorDefinition.NOT_ISA) {
            typeBit = true;
        }
    }
    BitMask mask = constraint.getListenedPropertyMask(modifiedClass, settableProperties);
    return typeBit ? mask.set(PropertySpecificUtil.TRAITABLE_BIT) : mask;
}
Also used : Operator(org.kie.api.runtime.rule.Operator) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) BitMask(org.drools.core.util.bitmask.BitMask)

Example 3 with BitMask

use of org.drools.core.util.bitmask.BitMask in project drools by kiegroup.

the class NamedEntryPoint method update.

public void update(FactHandle handle, Object object, String... modifiedProperties) {
    Class modifiedClass = object.getClass();
    TypeDeclaration typeDeclaration = kBase.getOrCreateExactTypeDeclaration(modifiedClass);
    BitMask mask = typeDeclaration.isPropertyReactive() ? calculatePositiveMask(modifiedClass, asList(modifiedProperties), typeDeclaration.getAccessibleProperties()) : AllSetBitMask.get();
    update((InternalFactHandle) handle, object, mask, modifiedClass, null);
}
Also used : BitMask(org.drools.core.util.bitmask.BitMask) AllSetBitMask(org.drools.core.util.bitmask.AllSetBitMask) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 4 with BitMask

use of org.drools.core.util.bitmask.BitMask in project drools by kiegroup.

the class TraitHelper method updateCore.

private <T> void updateCore(TraitableBean inner, Object core, Class<T> trait, boolean logical, Activation activation) {
    FactHandle handle = lookupFactHandle(inner);
    InternalFactHandle h = (InternalFactHandle) handle;
    if (handle != null) {
        TraitFieldTMS fieldTMS = inner._getFieldTMS();
        BitMask mask = fieldTMS == null ? onlyTraitBitSetMask() : fieldTMS.getModificationMask();
        Object o = h.getObject();
        NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint();
        PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getInternalWorkingMemory().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation.getRule(), activation.getTuple().getTupleSink(), h, nep.getEntryPoint(), mask, core.getClass(), null);
        nep.update(h, o, o, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(nep.getEntryPoint(), o), propagationContext);
    } else {
        handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
    }
}
Also used : TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) BitMask(org.drools.core.util.bitmask.BitMask) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 5 with BitMask

use of org.drools.core.util.bitmask.BitMask in project drools by kiegroup.

the class JavaConsequenceBuilderPRAlwaysTest method testIfElseBlocks.

@Test
public void testIfElseBlocks() throws Exception {
    String consequence = " System.out.println(\"this is a test\");\n " + " Cheese c1 = $cheese;\n" + " if( c1 == $cheese )     { \r\n" + "     modify( c1 ) { setPrice( 10 ), \n" + "                    setOldPrice( age ) }\n " + "     Cheese c4 = $cheese;\n" + "     if ( true )     { \n" + "         modify( c4 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } else if (1==2) {\n" + "         modify( c1 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     } else {\n " + "         Cheese c3 = $cheese;\n" + "         modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + "    }\n" + " } else {\n " + "     Cheese c3 = $cheese;\n" + "     modify( c3 ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     if ( c4 ==  $cheese ) modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + "     else modify( $cheese ) { setPrice( 12 ) }\n " + "}\n" + " modify( $cheese ) { setPrice( 10 ), setOldPrice( age ) }\n " + " System.out.println(\"we are done\");\n ";
    setupTest("", new HashMap<String, Object>());
    ruleDescr.setConsequence(consequence);
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    Map<String, Class<?>> declrCls = new HashMap<String, Class<?>>();
    declrCls.put("$cheese", Cheese.class);
    JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock((String) ruleDescr.getConsequence(), new BoundIdentifiers(declrCls, null));
    BoundIdentifiers bindings = new BoundIdentifiers(new HashMap(), null);
    bindings.getDeclrClasses().put("$cheese", Cheese.class);
    bindings.getDeclrClasses().put("age", int.class);
    // Set the inputs for each container, this is needed for modifes when the target context is the result of an expression
    List<JavaBlockDescr> descrs = new ArrayList<JavaBlockDescr>();
    setContainerBlockInputs(context, descrs, analysis.getBlockDescrs(), consequence, bindings, new HashMap(), 0);
    String fixed = fixBlockDescr(context, analysis, context.getDeclarationResolver().getDeclarations(context.getRule()));
    List<String> cheeseAccessibleProperties = ClassUtils.getAccessibleProperties(Cheese.class);
    BitMask priceOldPrice = PropertySpecificUtil.calculatePositiveMask(Cheese.class, Arrays.asList("price", "oldPrice"), cheeseAccessibleProperties);
    BitMask price = PropertySpecificUtil.calculatePositiveMask(Cheese.class, Arrays.asList("price"), cheeseAccessibleProperties);
    String expected = "  System.out.println(\"this is a test\");\r\n" + "  Cheese c1 = $cheese;\r\n" + " if( c1 == $cheese )     { \r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); \r\n" + "__obj__.setOldPrice( age ); drools.update( __obj____Handle2__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + "      Cheese c4 = $cheese;\r\n" + "     if ( true )     { \r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c4 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + "      } else if (1==2) {\r\n" + "         { org.drools.compiler.Cheese __obj__ = ( c1 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + "      } else {\r\n" + "          Cheese c3 = $cheese;\r\n" + "         { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + "     }\r\n" + " } else {\r\n" + "      Cheese c3 = $cheese;\r\n" + "     { org.drools.compiler.Cheese __obj__ = ( c3 ); org.kie.api.runtime.rule.FactHandle __obj____Handle2__ = drools.getFactHandle(__obj__);__obj__.setPrice( 10 ); __obj__.setOldPrice( age ); drools.update( __obj____Handle2__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + "      if ( c4 ==  $cheese ) { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + // the following line please notice only price is actually modified.
    "      else { $cheese.setPrice( 12 ); drools.update( $cheese__Handle__, " + price.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + " }\r\n" + " { $cheese.setPrice( 10 ); $cheese.setOldPrice( age ); drools.update( $cheese__Handle__, " + priceOldPrice.getInstancingStatement() + ", org.drools.compiler.Cheese.class ); }\r\n" + "  System.out.println(\"we are done\");\r\n" + " \r\n";
    assertNotNull(context.getErrors().toString(), fixed);
    assertEqualsIgnoreSpaces(expected, fixed);
// System.out.println( "=============================" );
// System.out.println( ruleDescr.getConsequence() );
// System.out.println( "=============================" );
// System.out.println( fixed );
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) BitMask(org.drools.core.util.bitmask.BitMask) JavaBlockDescr(org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr) Test(org.junit.Test)

Aggregations

BitMask (org.drools.core.util.bitmask.BitMask)15 AllSetBitMask (org.drools.core.util.bitmask.AllSetBitMask)5 PropertySpecificUtil.allSetButTraitBitMask (org.drools.core.reteoo.PropertySpecificUtil.allSetButTraitBitMask)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)3 JavaBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaBlockDescr)3 Test (org.junit.Test)3 ClassObjectType (org.drools.core.base.ClassObjectType)2 ConsequenceMetaData (org.drools.core.rule.ConsequenceMetaData)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 JavaModifyBlockDescr (org.drools.compiler.rule.builder.dialect.java.parser.JavaModifyBlockDescr)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 TraitFieldTMS (org.drools.core.factmodel.traits.TraitFieldTMS)1 Id (org.drools.core.reteoo.ObjectTypeNode.Id)1 Declaration (org.drools.core.rule.Declaration)1 IndexableConstraint (org.drools.core.rule.IndexableConstraint)1 MutableTypeConstraint (org.drools.core.rule.MutableTypeConstraint)1