Search in sources :

Example 26 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class KieContainerImpl method update.

private Results update(final InternalKieModule currentKM, final ReleaseId newReleaseId) {
    final InternalKieModule newKM = (InternalKieModule) kr.getKieModule(newReleaseId);
    if (newKM == null) {
        return null;
    }
    final KieJarChangeSet cs = currentKM.getChanges(newKM);
    List<String> modifiedClassNames = getModifiedClasses(cs);
    final boolean modifyingUsedClass = isModifyingUsedClass(modifiedClassNames, getClassLoader());
    final List<Class<?>> modifiedClasses = reinitModifiedClasses(newKM, modifiedClassNames, getClassLoader());
    final List<String> unchangedResources = getUnchangedResources(newKM, cs);
    Map<String, KieBaseModel> currentKieBaseModels = ((KieModuleKieProject) kProject).updateToModule(newKM);
    final ResultsImpl results = new ResultsImpl();
    List<String> kbasesToRemove = new ArrayList<String>();
    for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) {
        String kbaseName = kBaseEntry.getKey();
        KieBaseModelImpl newKieBaseModel = (KieBaseModelImpl) kProject.getKieBaseModel(kbaseName);
        KieBaseModelImpl currentKieBaseModel = (KieBaseModelImpl) currentKieBaseModels.get(kbaseName);
        // if a kbase no longer exists, just remove it from the cache
        if (newKieBaseModel == null) {
            // have to save for later removal to avoid iteration errors
            kbasesToRemove.add(kbaseName);
        } else {
            final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue();
            KieBaseUpdateContext context = new KieBaseUpdateContext(kProject, kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, modifyingUsedClass, unchangedResources, results, newKieBaseModel, currentKieBaseModel);
            kBase.enqueueModification(currentKM.createKieBaseUpdater(context));
        }
    }
    for (String kbaseToRemove : kbasesToRemove) {
        kBases.remove(kbaseToRemove);
    }
    // remove sessions that no longer exist
    this.kSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    this.statelessKSessions.entrySet().removeIf(ksession -> kProject.getKieSessionModel(ksession.getKey()) == null);
    return results;
}
Also used : KieJarChangeSet(org.drools.compiler.kie.util.KieJarChangeSet) ArrayList(java.util.ArrayList) KieBaseModelImpl(org.drools.compiler.kproject.models.KieBaseModelImpl) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieBase(org.kie.api.KieBase) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 27 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class KieContainerImpl method createKieBase.

private KieBase createKieBase(KieBaseModelImpl kBaseModel, KieProject kieProject, ResultsImpl messages, KieBaseConfiguration conf) {
    InternalKieModule kModule = kieProject.getKieModuleForKBase(kBaseModel.getName());
    InternalKnowledgeBase kBase = kModule.createKieBase(kBaseModel, kieProject, messages, conf);
    if (kBase == null) {
        return null;
    }
    kBase.setResolvedReleaseId(containerReleaseId);
    kBase.setContainerId(containerId);
    kBase.setKieContainer(this);
    kBase.initMBeans();
    return kBase;
}
Also used : InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 28 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class BayesBeliefSystemTest method getSession.

protected KieSession getSession(String ruleFile) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(ResourceFactory.newClassPathResource(ruleFile), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
        fail();
    }
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    KieSession kSession = kBase.newKieSession(ksConf, null);
    return kSession;
}
Also used : 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 29 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class JavaDialectBinaryEqualityTest method getKnowledgePackage1.

public KiePackage getKnowledgePackage1() {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import " + Person.class.getName() + ";\n";
    str += "global java.util.List list\n";
    str += "rule rule1 dialect\"java\" \n";
    str += "when\n";
    str += "   $p : Person( age : age == ( 17 + 17 ), eval( age == 34 ))\n";
    str += "   eval( $p.getAge() == 34 )\n";
    str += "then\n";
    str += "   list.add( $p );\n";
    str += "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<Person> list = new ArrayList<Person>();
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("list", list);
    ksession.insert(new Person("darth", 34));
    ksession.fireAllRules();
    assertEquals(new Person("darth", 34), list.get(0));
    return kbase.getPackage("org.drools.compiler.test");
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 30 with InternalKnowledgeBase

use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.

the class JavaDialectTest method testEvalDetectionInBetaNode.

@Test
public void testEvalDetectionInBetaNode() {
    // Tests evals are generated and executed with Java dialect
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "global java.util.List list\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   $s  : String()\n";
    drl += "   $p1 : Person( eval( name \n != $s ), name == ( new String($s+\"xxx\") ) )\n";
    drl += "then\n";
    drl += "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    BetaNode betaanode = (BetaNode) node.getObjectSinkPropagator().getSinks()[0];
    BetaNodeFieldConstraint[] constraint = betaanode.getConstraints();
    PredicateConstraint c = (PredicateConstraint) constraint[0];
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
}
Also used : BetaNode(org.drools.core.reteoo.BetaNode) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) PredicateExpression(org.drools.core.spi.PredicateExpression) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)368 Test (org.junit.Test)307 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)203 KieSession (org.kie.api.runtime.KieSession)193 ArrayList (java.util.ArrayList)163 List (java.util.List)99 KiePackage (org.kie.api.definition.KiePackage)79 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)45 FactHandle (org.kie.api.runtime.rule.FactHandle)39 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)38 InternalFactHandle (org.drools.core.common.InternalFactHandle)37 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)37 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)37 Cheese (org.drools.compiler.Cheese)36 Person (org.drools.compiler.Person)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 FactType (org.kie.api.definition.type.FactType)26 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)24 HashMap (java.util.HashMap)23