Search in sources :

Example 56 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class SessionInspectorTest method testGetSessionInfoWithCustomTemplate.

@Test
public void testGetSessionInfoWithCustomTemplate() {
    if (System.getProperty("java.vendor").toUpperCase().contains("IBM")) {
        // Does not work in the IBM JDK due to a bug in MVEL
        return;
    }
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_SubNetworks.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_AccumulateWithFromChaining.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_CollectResultsBetaConstraint.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_QueryMemoryLeak.drl"), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.setGlobal("results", new ArrayList<Object>());
    ksession.insert(new Dimension(100, 50));
    ksession.insert(new Dimension(130, 80));
    ksession.insert(new Dimension(50, 40));
    ksession.insert(new Dimension(50, 40));
    Cheesery cheesery = new Cheesery();
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    ksession.insert(cheesery);
    ksession.insert(new Person("Bob", "muzzarella"));
    ksession.insert(new Person("Mark", "brie"));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Double(10));
    ksession.insert(new Double(11));
    ksession.insert(new Double(12));
    ksession.insert(new Double(13));
    ksession.insert(new Double(14));
    ksession.insert(new Integer(15));
    ksession.insert(new Integer(16));
    ksession.insert(new Integer(17));
    ksession.insert(new Integer(18));
    FactHandle handle = ksession.insert(new Worker());
    ksession.retract(handle);
    SessionInspector inspector = new SessionInspector(ksession);
    StatefulKnowledgeSessionInfo info = inspector.getSessionInfo();
    SessionReporter.addNamedTemplate("topten", getClass().getResourceAsStream("customreports.mvel"));
    String report = SessionReporter.generateReport("topten", info, null);
    assertNotNull(report);
}
Also used : StatefulKnowledgeSessionInfo(org.drools.core.util.debug.StatefulKnowledgeSessionInfo) FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.compiler.Cheese) Dimension(java.awt.Dimension) Cheesery(org.drools.compiler.Cheesery) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Worker(org.drools.compiler.Worker) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) SessionInspector(org.drools.core.util.debug.SessionInspector) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 57 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class SessionInspectorTest method testGetSessionInfo.

@Test
public void testGetSessionInfo() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_SubNetworks.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_AccumulateWithFromChaining.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_CollectResultsBetaConstraint.drl"), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/test_QueryMemoryLeak.drl"), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.setGlobal("results", new ArrayList<Object>());
    ksession.insert(new Dimension(100, 50));
    ksession.insert(new Dimension(130, 80));
    ksession.insert(new Dimension(50, 40));
    ksession.insert(new Dimension(50, 40));
    Cheesery cheesery = new Cheesery();
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("brie", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("muzzarella", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("stilton", 10));
    ksession.insert(cheesery);
    ksession.insert(new Person("Bob", "muzzarella"));
    ksession.insert(new Person("Mark", "brie"));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("brie", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("muzzarella", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Cheese("Stilton", 10));
    ksession.insert(new Double(10));
    ksession.insert(new Double(11));
    ksession.insert(new Double(12));
    ksession.insert(new Double(13));
    ksession.insert(new Double(14));
    ksession.insert(new Integer(15));
    ksession.insert(new Integer(16));
    ksession.insert(new Integer(17));
    ksession.insert(new Integer(18));
    FactHandle handle = ksession.insert(new Worker());
    ksession.retract(handle);
    SessionInspector inspector = new SessionInspector(ksession);
    StatefulKnowledgeSessionInfo info = inspector.getSessionInfo();
    String report = SessionReporter.generateReport("simple", info, null);
    assertNotNull(report);
}
Also used : StatefulKnowledgeSessionInfo(org.drools.core.util.debug.StatefulKnowledgeSessionInfo) FactHandle(org.kie.api.runtime.rule.FactHandle) Cheese(org.drools.compiler.Cheese) Dimension(java.awt.Dimension) Cheesery(org.drools.compiler.Cheesery) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Worker(org.drools.compiler.Worker) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) SessionInspector(org.drools.core.util.debug.SessionInspector) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 58 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class TraitHelper method getFactHandle.

public FactHandle getFactHandle(Object object) {
    FactHandle handle = null;
    if (handle != null) {
        return handle;
    }
    handle = getFactHandleFromWM(object);
    if (handle == null) {
        if (object instanceof CoreWrapper) {
            handle = getFactHandleFromWM(((CoreWrapper) object).getCore());
        }
        if (handle == null) {
            throw new RuntimeException("Update error: handle not found for object: " + object + ". Is it in the working memory?");
        }
    }
    return handle;
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle)

Example 59 with FactHandle

use of org.kie.api.runtime.rule.FactHandle 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 60 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class SetVariableCommandFromLastReturn method execute.

public Object execute(Context context) {
    Context targetCtx;
    if (this.contextName == null) {
        targetCtx = context;
    } else {
        targetCtx = ((RegistryContext) context).getContextManager().getContext(this.contextName);
    }
    GetDefaultValue sim = (GetDefaultValue) context.get("simulator");
    Object o = sim.getObject();
    // for FactHandle's we store the handle on a map and the actual object as
    if (o instanceof FactHandle) {
        Map<String, FactHandle> handles = (Map<String, FactHandle>) targetCtx.get("h");
        if (handles == null) {
            handles = new HashMap<String, FactHandle>();
            targetCtx.set("h", handles);
        }
        handles.put(identifier, (FactHandle) o);
        o = ((InternalFactHandle) o).getObject();
    }
    targetCtx.set(identifier, o);
    return o;
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) Context(org.kie.api.runtime.Context) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) RegistryContext(org.drools.core.command.impl.RegistryContext) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

FactHandle (org.kie.api.runtime.rule.FactHandle)650 Test (org.junit.Test)547 KieSession (org.kie.api.runtime.KieSession)506 KieBase (org.kie.api.KieBase)304 ArrayList (java.util.ArrayList)298 InternalFactHandle (org.drools.core.common.InternalFactHandle)192 List (java.util.List)165 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)119 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)66 KieHelper (org.kie.internal.utils.KieHelper)50 Person (org.drools.modelcompiler.domain.Person)46 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 HashMap (java.util.HashMap)41 Person (org.drools.compiler.Person)41 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)39 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 Person (org.drools.mvel.compiler.Person)38 Ignore (org.junit.Ignore)36 Match (org.kie.api.runtime.rule.Match)36 Collection (java.util.Collection)34