Search in sources :

Example 1 with IndexableConstraint

use of org.drools.core.rule.IndexableConstraint in project drools by kiegroup.

the class DefaultTemplateRuleBaseTest method testSimpleTemplate.

@SuppressWarnings("unchecked")
@Test
public void testSimpleTemplate() throws Exception {
    TemplateContainer tc = new TemplateContainer() {

        private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") };

        public Column[] getColumns() {
            return columns;
        }

        public String getHeader() {
            return null;
        }

        public Map<String, RuleTemplate> getTemplates() {
            Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>();
            RuleTemplate ruleTemplate = new RuleTemplate("template1", this);
            ruleTemplate.addColumn("column1 == 10");
            ruleTemplate.addColumn("column2 < 5 || > 20");
            ruleTemplate.addColumn("column3 == \"xyz\"");
            templates.put("template1", ruleTemplate);
            return templates;
        }

        public Column getColumn(String name) {
            return columns[Integer.parseInt(name.substring(6)) - 1];
        }
    };
    DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc);
    InternalKnowledgePackage[] packages = ((KnowledgeBaseImpl) ruleBase.newStatefulSession().getKieBase()).getPackages();
    assertEquals(1, packages.length);
    Map<String, String> globals = packages[0].getGlobals();
    assertEquals(DefaultGenerator.class.getName(), globals.get("generator"));
    Collection<org.kie.api.definition.rule.Rule> rules = packages[0].getRules();
    assertEquals(1, rules.size());
    assertEquals("template1", rules.iterator().next().getName());
    GroupElement lhs = ((RuleImpl) rules.iterator().next()).getLhs();
    // when
    // r : Row()
    // column1 : Column(name == "column1")
    // exists LongCell(row == r, column == column1, value == 10)
    // column2 : Column(name == "column2")
    // exists LongCell(row == r, column == column2, value < 5 | > 20)
    // column3 : Column(name == "column3")
    // exists StringCell(row == r, column == column3, value == "xyz")
    assertEquals(7, lhs.getChildren().size());
    org.drools.core.rule.Pattern pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(1);
    assertEquals(1, pattern.getConstraints().size());
    Constraint constraint = pattern.getConstraints().get(0);
    GroupElement exists = (GroupElement) lhs.getChildren().get(2);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier());
    pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(3);
    assertEquals(1, pattern.getConstraints().size());
    constraint = pattern.getConstraints().get(0);
    exists = (GroupElement) lhs.getChildren().get(4);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier());
    pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(5);
    assertEquals(1, pattern.getConstraints().size());
    constraint = pattern.getConstraints().get(0);
    exists = (GroupElement) lhs.getChildren().get(6);
    pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
    assertEquals(3, pattern.getConstraints().size());
    vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
    assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
    assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier());
}
Also used : HashMap(java.util.HashMap) Constraint(org.drools.core.spi.Constraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IndexableConstraint(org.drools.core.rule.IndexableConstraint) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 2 with IndexableConstraint

use of org.drools.core.rule.IndexableConstraint in project drools by kiegroup.

the class BaseBetaConstraintsTest method checkBetaConstraints.

protected void checkBetaConstraints(BetaNodeFieldConstraint[] constraints, Class cls, short betaNodeType) {
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    int depth = config.getCompositeKeyDepth();
    BetaConstraints betaConstraints;
    try {
        betaConstraints = (BetaConstraints) cls.getConstructor(new Class[] { BetaNodeFieldConstraint[].class, RuleBaseConfiguration.class }).newInstance(constraints, config);
    } catch (Exception e) {
        throw new RuntimeException("could not invoke constructor for " + cls.getName());
    }
    betaConstraints.initIndexes(depth, betaNodeType);
    // BetaConstraints betaConstraints = new DefaultBetaConstraints(constraints, config );
    constraints = betaConstraints.getConstraints();
    List<Integer> list = new ArrayList<Integer>();
    // get indexed positions
    for (int i = 0; i < constraints.length && list.size() < depth; i++) {
        if (((IndexableConstraint) constraints[i]).isIndexable(betaNodeType)) {
            list.add(i);
        }
    }
    // convert to array
    int[] indexedPositions = new int[list.size()];
    for (int i = 0; i < list.size(); i++) {
        indexedPositions[i] = i;
    }
    assertEquals((indexedPositions.length > 0), betaConstraints.isIndexed());
    assertEquals(indexedPositions.length, betaConstraints.getIndexCount());
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    if (indexedPositions.length > 0) {
        if (((IndexableConstraint) constraints[indexedPositions[0]]).getConstraintType() == ConstraintType.EQUAL) {
            TupleIndexHashTable tupleHashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
            assertTrue(tupleHashTable.isIndexed());
            Index index = tupleHashTable.getIndex();
            for (int i = 0; i < indexedPositions.length; i++) {
                checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
            }
            TupleIndexHashTable factHashTable = (TupleIndexHashTable) betaMemory.getRightTupleMemory();
            assertTrue(factHashTable.isIndexed());
            index = factHashTable.getIndex();
            for (int i = 0; i < indexedPositions.length; i++) {
                checkSameConstraintForIndex((IndexableConstraint) constraints[indexedPositions[i]], index.getFieldIndex(i));
            }
        } else {
        }
    } else {
        TupleList tupleHashTable = (TupleList) betaMemory.getLeftTupleMemory();
        assertFalse(tupleHashTable.isIndexed());
        TupleList factHashTable = (TupleList) betaMemory.getRightTupleMemory();
        assertFalse(factHashTable.isIndexed());
    }
}
Also used : ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Index(org.drools.core.util.AbstractHashTable.Index) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 3 with IndexableConstraint

use of org.drools.core.rule.IndexableConstraint in project drools by kiegroup.

the class IndexUtil method compositeAllowed.

public static boolean compositeAllowed(BetaNodeFieldConstraint[] constraints, short betaNodeType) {
    // 1) If there is 1 or more unification restrictions it cannot be composite
    // 2) Ensures any non unification restrictions are first
    int firstUnification = -1;
    int firstNonUnification = -1;
    for (int i = 0, length = constraints.length; i < length; i++) {
        if (isIndexable(constraints[i], betaNodeType)) {
            final boolean isUnification = ((IndexableConstraint) constraints[i]).isUnification();
            if (isUnification && firstUnification == -1) {
                firstUnification = i;
            } else if (!isUnification && firstNonUnification == -1) {
                firstNonUnification = i;
            }
        }
        if (firstUnification != -1 && firstNonUnification != -1) {
            break;
        }
    }
    if (firstNonUnification != -1 && firstNonUnification > 0) {
        // Make sure a nonunification indexable constraint is first
        swap(constraints, 0, firstNonUnification);
    }
    return (firstUnification == -1);
}
Also used : IndexableConstraint(org.drools.core.rule.IndexableConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 4 with IndexableConstraint

use of org.drools.core.rule.IndexableConstraint in project drools by kiegroup.

the class CompositeObjectSinkAdapter method removeObjectSink.

public ObjectSinkPropagator removeObjectSink(final ObjectSink sink) {
    // dirty it, so it'll rebuild on next get
    this.sinks = null;
    if (this.sinksMap != null) {
        this.sinksMap.remove(sink);
    }
    if (sink.getType() == NodeTypeEnums.AlphaNode) {
        final AlphaNode alphaNode = (AlphaNode) sink;
        final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
        if (fieldConstraint instanceof IndexableConstraint) {
            final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
            final FieldValue value = indexableConstraint.getField();
            if (isHashable(indexableConstraint)) {
                final InternalReadAccessor fieldAccessor = indexableConstraint.getFieldExtractor();
                final int index = fieldAccessor.getIndex();
                final FieldIndex fieldIndex = unregisterFieldIndex(index);
                if (fieldIndex.isHashed()) {
                    HashKey hashKey = new HashKey(index, value, fieldAccessor);
                    this.hashedSinkMap.remove(hashKey);
                    if (fieldIndex.getCount() <= this.alphaNodeHashingThreshold - 1) {
                        // we have less than three so unhash
                        unHashSinks(fieldIndex);
                    }
                } else {
                    this.hashableSinks.remove(alphaNode);
                }
                if (this.hashableSinks != null && this.hashableSinks.isEmpty()) {
                    this.hashableSinks = null;
                }
                return size() == 1 ? new SingleObjectSinkAdapter(getSinks()[0]) : this;
            }
        }
    }
    this.otherSinks.remove((ObjectSinkNode) sink);
    if (this.otherSinks.isEmpty()) {
        this.otherSinks = null;
    }
    return size() == 1 ? new SingleObjectSinkAdapter(getSinks()[0]) : this;
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 5 with IndexableConstraint

use of org.drools.core.rule.IndexableConstraint in project drools by kiegroup.

the class CompositeObjectSinkAdapter method hashSinks.

void hashSinks(final FieldIndex fieldIndex) {
    if (this.hashedSinkMap == null) {
        this.hashedSinkMap = new ObjectHashMap();
    }
    final int index = fieldIndex.getIndex();
    final InternalReadAccessor fieldReader = fieldIndex.getFieldExtractor();
    ObjectSinkNode currentSink = this.hashableSinks.getFirst();
    while (currentSink != null) {
        final AlphaNode alphaNode = (AlphaNode) currentSink;
        final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
        final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
        // position to the next sink now because alphaNode may be removed if the index is equal. If we were to do this
        // afterwards, currentSink.nextNode would be null
        currentSink = currentSink.getNextObjectSinkNode();
        // the right field index
        if (index == indexableConstraint.getFieldExtractor().getIndex()) {
            final FieldValue value = indexableConstraint.getField();
            this.hashedSinkMap.put(new HashKey(index, value, fieldReader), alphaNode);
            // remove the alpha from the possible candidates of hashable sinks since it is now hashed
            hashableSinks.remove(alphaNode);
        }
    }
    if (this.hashableSinks.isEmpty()) {
        this.hashableSinks = null;
    }
    fieldIndex.setHashed(true);
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectHashMap(org.drools.core.util.ObjectHashMap) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Aggregations

IndexableConstraint (org.drools.core.rule.IndexableConstraint)8 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4 FieldValue (org.drools.core.spi.FieldValue)4 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)4 ObjectHashMap (org.drools.core.util.ObjectHashMap)3 ArrayList (java.util.ArrayList)2 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)2 Constraint (org.drools.core.spi.Constraint)2 HashMap (java.util.HashMap)1 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)1 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 BetaMemory (org.drools.core.reteoo.BetaMemory)1 FieldIndex (org.drools.core.reteoo.CompositeObjectSinkAdapter.FieldIndex)1 GroupElement (org.drools.core.rule.GroupElement)1 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)1 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)1 Index (org.drools.core.util.AbstractHashTable.Index)1 Iterator (org.drools.core.util.Iterator)1