Search in sources :

Example 91 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class TruthMaintenanceTest method testJustificationStateOverridingBySuperClass.

@Test(timeout = 10000)
public void testJustificationStateOverridingBySuperClass() {
    // DROOLS-352
    String droolsSource = "package org.drools.tms.test; \n" + "" + "declare Foo end \n" + "declare Bar extends Foo end \n" + "" + "rule Justify_Sub \n" + "when \n" + "then \n" + " insertLogical( new Bar() ); \n" + " insertLogical( 42.0 ); \n" + "end \n" + "rule Justify_Sup \n" + "when \n" + " $d : Double() \n" + "then \n" + " insertLogical( new Foo() ); \n" + "end \n" + "" + "" + "rule Fuu when Foo() then end \n" + "rule Bor when Bar() then end \n";
    // ///////////////////////////////////
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.EQUALITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    session.fireAllRules();
    for (FactHandle fh : session.getFactHandles()) {
        InternalFactHandle ifh = (InternalFactHandle) fh;
        assertEquals(EqualityKey.JUSTIFIED, ifh.getEqualityKey().getStatus());
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 92 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class TruthMaintenanceTest method testLogicalThenUpdateAsStatedShadowSingleOccurance.

@Test(timeout = 10000)
public void testLogicalThenUpdateAsStatedShadowSingleOccurance() {
    String droolsSource = "package org.drools.tms.test; \n" + "global java.util.List list; \n" + "rule Justify \n" + "when \n" + "    String( this == 'go1' ) " + "then \n" + "    insertLogical( 'f1' ); \n" + "end \n" + "rule StillHere \n" + "when \n" + "    String( this == 'go2' ) " + "    s : String( this == 'f1' ) " + "then \n" + "    list.add( s ); \n" + "end \n" + "";
    KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieConf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
    KieSession session = kbase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    session.insert("go1");
    session.fireAllRules();
    TruthMaintenanceSystem tms = ((StatefulKnowledgeSessionImpl) session).getTruthMaintenanceSystem();
    InternalFactHandle jfh1 = tms.get("f1").getLogicalFactHandle();
    assertEquals(EqualityKey.JUSTIFIED, jfh1.getEqualityKey().getStatus());
    InternalFactHandle fh1 = (InternalFactHandle) session.insert("f1");
    InternalFactHandle fh2 = (InternalFactHandle) session.insert("f2");
    session.insert("go2");
    session.fireAllRules();
    assertEquals(EqualityKey.STATED, fh1.getEqualityKey().getStatus());
    assertSame(fh1.getEqualityKey(), jfh1.getEqualityKey());
    assertNotSame(fh1, jfh1);
    // Make sure f1 only occurs once
    assertEquals(1, list.size());
    assertEquals("f1", list.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 93 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class AbductionTest method testGenesExplanationBackTracking.

@Test
@Ignore("Not implemented yet")
public void testGenesExplanationBackTracking() {
    // from wikipedia, abductive reasoning example
    /*
            feed(lactose):-make(permease),make(galactosidase).
            make(Enzyme):-code(Gene,Enzyme),express(Gene).
            express(lac(X)):-amount(glucose,low),amount(lactose,hi).
            express(lac(X)):-amount(glucose,medium),amount(lactose,medium).
            code(lac(y),permease).
            code(lac(z),galactosidase).

            temperature(low):-amount(glucose,low).
         */
    String droolsSource = "package org.drools.abductive.test; \n" + "import org.kie.api.runtime.rule.Match;\n" + "" + "declare Amount " + "   enz : String @key " + "   level : String @key " + "end " + "query feed( String $enz ) " + "   $enz := String() from 'lactose' " + "   ?make( 'permease' ; ) " + "   ?make( 'galactosidase' ; ) " + "end " + "query make( String $enz ) " + "   ?code( $gen, $enz ; ) and ?express( $gen ; ) " + "end " + "query code( String $gen, String $enz ) " + "   ( $gen := String() from 'lacY' and $enz := String() from 'permease' ) " + "   or " + "   ( $gen := String() from 'lacZ' and $enz := String() from 'galactosidase' ) " + "end " + "query express( String $gen ) " + "  ( ?amount( 'glucose', 'low' ; ) and ?amount( 'lactose', 'hi' ; ) ) " + "  or " + "  ( ?amount( 'glucose', 'medium' ; ) and ?amount( 'lactose', 'medium' ; ) ) " + "end " + "query amount( String $enz, String $lev ) @Abductive( target=Amount.class ) end " + "rule Check " + "when " + "   ?feed( 'lactose' ; ) " + "then " + "   System.out.println( 'YES' ); " + "end " + "rule Match " + "when " + "   $m : Match( rule.name != 'Match' ) " + "then" + "   System.out.println( $m ); " + "end  " + "";
    // ///////////////////////////////////
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieSession session = getSessionFromString(droolsSource, kconf);
    session.fireAllRules();
    for (Object o : session.getObjects()) {
        System.out.println(">>> " + o);
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieSession(org.kie.api.runtime.KieSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 94 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class DefeasibilityTest method getSessionFromString.

protected KieSession getSessionFromString(String drlString) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    try {
        System.setProperty("drools.negatable", "on");
        kBuilder.add(ResourceFactory.newByteArrayResource(drlString.getBytes()), ResourceType.DRL);
        if (kBuilder.hasErrors()) {
            System.err.println(kBuilder.getErrors());
            fail();
        }
    } finally {
        System.setProperty("drools.negatable", "off");
    }
    KieBaseConfiguration kieBaseConfiguration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kieBaseConfiguration.setOption(EqualityBehaviorOption.EQUALITY);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase(kieBaseConfiguration);
    kBase.addPackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    KieSession kSession = kBase.newKieSession(ksConf, null);
    return kSession;
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 95 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class ObjectHashMapTest method testIdentityWithResize.

@Test
public void testIdentityWithResize() {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EqualityBehaviorOption.IDENTITY);
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    KieSession ksession = kbase.newKieSession();
    int length = 1 * 300 * 1000;
    List<FactHandle> handles = new ArrayList<FactHandle>(1000);
    List<String> objects = new ArrayList<String>(1000);
    for (int i = 0; i < length; i++) {
        String s = getPropertyName(i);
        FactHandle handle = ksession.insert(s);
        objects.add(s);
        handles.add(handle);
    }
    for (int i = 0; i < length; i++) {
        String s = objects.get(i);
        FactHandle handle = handles.get(i);
        assertEquals(s, ksession.getObject(handle));
        assertSame(handle, ksession.getFactHandle(s));
        // now check with disconnected facthandle
        handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
        assertEquals(s, ksession.getObject(handle));
    }
    for (int i = 0; i < length; i++) {
        FactHandle handle = handles.get(i);
        // now retract with disconnected facthandle
        handle = DefaultFactHandle.createFromExternalFormat(((DefaultFactHandle) handle).toExternalForm());
        ksession.retract(handle);
        assertEquals(length - i - 1, ksession.getObjects().size());
        assertEquals(length - i - 1, ksession.getFactHandles().size());
    }
    assertEquals(0, ksession.getObjects().size());
    assertEquals(0, ksession.getFactHandles().size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7