Search in sources :

Example 31 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class RuleBaseConfigurationTest method testProgrammaticPropertiesFile.

@Test
public void testProgrammaticPropertiesFile() {
    RuleBaseConfiguration cfg = new RuleBaseConfiguration();
    assertEquals(true, cfg.isIndexLeftBetaMemory());
    Properties properties = new Properties();
    properties.setProperty("drools.indexLeftBetaMemory", "false");
    cfg = new RuleBaseConfiguration(properties);
    assertEquals(false, cfg.isIndexLeftBetaMemory());
    System.getProperties().remove("drools.indexLeftBetaMemory");
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) Properties(java.util.Properties) Test(org.junit.Test)

Example 32 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class RuleBaseConfigurationTest method testSequential.

@Test
public void testSequential() {
    Properties properties = new Properties();
    properties.setProperty("drools.sequential", "false");
    RuleBaseConfiguration cfg = new RuleBaseConfiguration(properties);
    assertFalse(cfg.isSequential());
    assertTrue(cfg.getAgendaGroupFactory() instanceof PriorityQueueAgendaGroupFactory);
    properties = new Properties();
    properties.setProperty("drools.sequential.agenda", "sequential");
    properties.setProperty("drools.sequential", "true");
    cfg = new RuleBaseConfiguration(properties);
    assertTrue(cfg.isSequential());
    assertEquals(SequentialAgenda.SEQUENTIAL, cfg.getSequentialAgenda());
    properties = new Properties();
    properties.setProperty("drools.sequential.agenda", "dynamic");
    properties.setProperty("drools.sequential", "true");
    cfg = new RuleBaseConfiguration(properties);
    assertTrue(cfg.isSequential());
    assertEquals(SequentialAgenda.DYNAMIC, cfg.getSequentialAgenda());
    assertTrue(cfg.getAgendaGroupFactory() instanceof PriorityQueueAgendaGroupFactory);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) PriorityQueueAgendaGroupFactory(org.drools.core.common.PriorityQueueAgendaGroupFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 33 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class LeftLeftTupleIndexHashTableIteratorTest method test1.

@Test
public void test1() {
    BetaNodeFieldConstraint constraint0 = getConstraint("d", Operator.EQUAL, "this", Foo.class);
    BetaNodeFieldConstraint[] constraints = new BetaNodeFieldConstraint[] { constraint0 };
    RuleBaseConfiguration config = new RuleBaseConfiguration();
    BetaConstraints betaConstraints = null;
    betaConstraints = new SingleBetaConstraints(constraints, config);
    BetaMemory betaMemory = betaConstraints.createBetaMemory(config, NodeTypeEnums.JoinNode);
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ss = kBase.newKieSession();
    InternalFactHandle fh1 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh2 = (InternalFactHandle) ss.insert(new Foo("brie", 1));
    InternalFactHandle fh3 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh4 = (InternalFactHandle) ss.insert(new Foo("soda", 1));
    InternalFactHandle fh5 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh6 = (InternalFactHandle) ss.insert(new Foo("bread", 3));
    InternalFactHandle fh7 = (InternalFactHandle) ss.insert(new Foo("cream", 3));
    InternalFactHandle fh8 = (InternalFactHandle) ss.insert(new Foo("gorda", 15));
    InternalFactHandle fh9 = (InternalFactHandle) ss.insert(new Foo("beer", 16));
    InternalFactHandle fh10 = (InternalFactHandle) ss.insert(new Foo("mars", 0));
    InternalFactHandle fh11 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh12 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    InternalFactHandle fh13 = (InternalFactHandle) ss.insert(new Foo("snicker", 0));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh1, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh2, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh3, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh4, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh5, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh6, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh7, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh8, null, true));
    betaMemory.getLeftTupleMemory().add(new LeftTupleImpl(fh9, null, true));
    TupleIndexHashTable hashTable = (TupleIndexHashTable) betaMemory.getLeftTupleMemory();
    // can't create a 0 hashCode, so forcing
    TupleList leftTupleList = new TupleList();
    leftTupleList.add(new LeftTupleImpl(fh10, null, true));
    hashTable.getTable()[0] = leftTupleList;
    leftTupleList = new TupleList();
    leftTupleList.add(new LeftTupleImpl(fh11, null, true));
    leftTupleList.add(new LeftTupleImpl(fh12, null, true));
    leftTupleList.add(new LeftTupleImpl(fh13, null, true));
    ((TupleList) hashTable.getTable()[0]).setNext(leftTupleList);
    Entry[] table = hashTable.getTable();
    List list = new ArrayList();
    for (int i = 0; i < table.length; i++) {
        if (table[i] != null) {
            List entries = new ArrayList();
            entries.add(i);
            Entry entry = table[i];
            while (entry != null) {
                entries.add(entry);
                entry = entry.getNext();
            }
            list.add(entries.toArray());
        }
    }
    assertEquals(5, list.size());
    // This tests the hashcode index allocation. If the rehash function (or any other way hashcodes are computed) changes, these numbers will change.
    Object[] entries = (Object[]) list.get(0);
    assertEquals(0, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(1);
    assertEquals(102, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(2);
    assertEquals(103, entries[0]);
    assertEquals(2, entries.length);
    entries = (Object[]) list.get(3);
    assertEquals(115, entries[0]);
    assertEquals(3, entries.length);
    entries = (Object[]) list.get(4);
    assertEquals(117, entries[0]);
    assertEquals(3, entries.length);
    // System.out.println( entries );
    list = new ArrayList<LeftTupleImpl>();
    Iterator it = betaMemory.getLeftTupleMemory().iterator();
    for (LeftTupleImpl leftTuple = (LeftTupleImpl) it.next(); leftTuple != null; leftTuple = (LeftTupleImpl) it.next()) {
        list.add(leftTuple);
    }
    assertEquals(13, list.size());
}
Also used : SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) BetaConstraints(org.drools.core.common.BetaConstraints) SingleBetaConstraints(org.drools.core.common.SingleBetaConstraints) ArrayList(java.util.ArrayList) BetaMemory(org.drools.core.reteoo.BetaMemory) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) TupleList(org.drools.core.util.index.TupleList) FieldIndexHashTableFullIterator(org.drools.core.util.index.TupleIndexHashTable.FieldIndexHashTableFullIterator) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) TupleList(org.drools.core.util.index.TupleList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 34 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration 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 35 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class ScenarioTest method createContext.

public BuildContext createContext() {
    RuleBaseConfiguration conf = new RuleBaseConfiguration();
    KnowledgeBaseImpl rbase = new KnowledgeBaseImpl("ID", conf);
    BuildContext buildContext = new BuildContext(rbase);
    RuleImpl rule = new RuleImpl("rule1").setPackage("org.pkg1");
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
    pkg.getDialectRuntimeRegistry().setDialectData("mvel", new MVELDialectRuntimeData());
    pkg.addRule(rule);
    buildContext.setRule(rule);
    return buildContext;
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)43 Test (org.junit.Test)19 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11 BuildContext (org.drools.core.reteoo.builder.BuildContext)10 ArrayList (java.util.ArrayList)8 Properties (java.util.Properties)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)8 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)8 KieSession (org.kie.api.runtime.KieSession)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 BetaMemory (org.drools.core.reteoo.BetaMemory)6 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)6 TupleList (org.drools.core.util.index.TupleList)6 List (java.util.List)5 BetaConstraints (org.drools.core.common.BetaConstraints)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 SingleBetaConstraints (org.drools.core.common.SingleBetaConstraints)4