Search in sources :

Example 1 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class PMMLRuntimeFactoryInternalTest method populateNestedKiePackageList.

@Test
public void populateNestedKiePackageList() {
    // Setup kiebase
    KiePMMLModel kiePMMLModel = getKiePMMLModelWithNested("FAKE");
    InternalKnowledgePackage pmmlKnowledgePackage = getKnowledgePackageWithPMMLResourceType(kiePMMLModel);
    List<KiePackage> kiePackages = ((HasNestedModels) kiePMMLModel).getNestedModels().stream().map(this::getKnowledgePackageWithPMMLResourceType).collect(Collectors.toList());
    InternalKnowledgeBase kieBase = (InternalKnowledgeBase) new KieHelper().build(ExecutableModelProject.class);
    kieBase.addPackage(pmmlKnowledgePackage);
    kieBase.addPackages(kiePackages);
    // Actual test
    final List<KiePackage> toPopulate = new ArrayList<>();
    PMMLRuntimeFactoryInternal.populateNestedKiePackageList(Collections.singleton(kiePMMLModel), toPopulate, kieBase);
    assertFalse(toPopulate.isEmpty());
    assertEquals(kiePackages.size(), toPopulate.size());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) ExecutableModelProject(org.drools.modelcompiler.ExecutableModelProject) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 2 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class PMMLRuntimeFactoryInternalTest method getKiePackageByFullClassNamePresent.

@Test
public void getKiePackageByFullClassNamePresent() {
    // Setup kiebase
    KiePMMLModel kiePMMLModel = new KiePMMLModelA("FAKE");
    InternalKnowledgePackage pmmlKnowledgePackage = getKnowledgePackageWithPMMLResourceType(kiePMMLModel);
    InternalKnowledgeBase kieBase = (InternalKnowledgeBase) new KieHelper().build(ExecutableModelProject.class);
    kieBase.addPackage(pmmlKnowledgePackage);
    // Actual test
    assertNotNull(PMMLRuntimeFactoryInternal.getKiePackageByFullClassName(kiePMMLModel.getClass().getName(), kieBase));
}
Also used : KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KieHelper(org.kie.internal.utils.KieHelper) ExecutableModelProject(org.drools.modelcompiler.ExecutableModelProject) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 3 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class PMMLRuntimeFactoryInternal method createKieBase.

protected static KieBase createKieBase(KnowledgeBuilderImpl kbuilderImpl) {
    KieHelper kieHelper = new KieHelper();
    kbuilderImpl.getPackageNames().stream().flatMap(name -> kbuilderImpl.getPackageDescrs(name).stream()).forEach(pDescr -> kieHelper.addResource(createDescrResource(pDescr)));
    KieBase kieBase = kieHelper.build(ExecutableModelProject.class);
    // Temporarily because all this path has to be removed
    // Needed for non-drools models
    kbuilderImpl.getKnowledgePackages().forEach(kBuilderPackage -> {
        if (kieBase.getKiePackage(kBuilderPackage.getName()) == null) {
            ((InternalKnowledgeBase) kieBase).addPackage(kBuilderPackage);
        }
    });
    // Temporarily because all this path has to be removed
    // Basically, get PMMLPackage from kbuilderImpl' InternalKnowledgePackages and put inside kieBase' ones
    kieBase.getKiePackages().forEach(kiePackage -> {
        PMMLPackage pmmlPackage = (PMMLPackage) ((InternalKnowledgePackage) kiePackage).getResourceTypePackages().get(ResourceType.PMML);
        if (pmmlPackage == null) {
            InternalKnowledgePackage kBuilderPackage = kbuilderImpl.getPackage(kiePackage.getName());
            if (kBuilderPackage.getResourceTypePackages().get(ResourceType.PMML) != null) {
                ((InternalKnowledgePackage) kiePackage).getResourceTypePackages().put(ResourceType.PMML, kBuilderPackage.getResourceTypePackages().get(ResourceType.PMML));
            }
        }
    });
    return kieBase;
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) DescrResource(org.drools.core.io.impl.DescrResource) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) PMMLRuntime(org.kie.pmml.api.runtime.PMMLRuntime) LoggerFactory(org.slf4j.LoggerFactory) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) FileSystemResource(org.drools.core.io.impl.FileSystemResource) ResourceType(org.kie.api.io.ResourceType) ExternalException(org.kie.pmml.api.exceptions.ExternalException) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) Map(java.util.Map) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KieServices(org.kie.api.KieServices) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KieBase(org.kie.api.KieBase) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieHelper(org.kie.internal.utils.KieHelper) Logger(org.slf4j.Logger) InternalResource(org.drools.core.io.internal.InternalResource) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExecutableModelProject(org.drools.modelcompiler.ExecutableModelProject) HasNestedModels(org.kie.pmml.commons.model.HasNestedModels) Collection(java.util.Collection) FileOutputStream(java.io.FileOutputStream) UUID(java.util.UUID) File(java.io.File) KnowledgeBaseFactory(org.drools.kiesession.rulebase.KnowledgeBaseFactory) List(java.util.List) FileUtils.getFile(org.apache.commons.io.FileUtils.getFile) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PMMLAssemblerService(org.kie.pmml.evaluator.assembler.service.PMMLAssemblerService) InputStream(java.io.InputStream) PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 4 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class PMMLRuntimeFactoryInternal method getPMMLRuntime.

public static PMMLRuntime getPMMLRuntime(String pmmlFileName, String pmmlModelName, KieBase kieBase) {
    RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration(((InternalKnowledgeBase) kieBase).getRootClassLoader());
    InternalKnowledgeBase kieBaseNew = KnowledgeBaseFactory.newKnowledgeBase(ruleBaseConfiguration);
    KiePackage kiePackage = getKiePackageByModelName(pmmlModelName, kieBase);
    kieBaseNew.addPackage(kiePackage);
    List<KiePackage> nestedKiePackages = getNestedKiePackages((InternalKnowledgePackage) kiePackage, kieBase);
    if (!nestedKiePackages.isEmpty()) {
        kieBaseNew.addPackages(nestedKiePackages);
    }
    ((InternalKnowledgePackage) kiePackage).getResourceTypePackages().get(ResourceType.PMML);
    return getPMMLRuntime(kieBaseNew);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Example 5 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class SerializationSecurityPolicyTest method testSerialization.

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
    // kpackage serialization is not supported. But leave it for standard-drl.
    final String rule = " rule R " + " when " + " then " + "     System.out.println(\"consequence!\"); " + " end";
    final KieServices kieServices = KieServices.get();
    final Resource drlResource = kieServices.getResources().newByteArrayResource(rule.getBytes(StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name());
    drlResource.setResourceType(ResourceType.DRL);
    drlResource.setTargetPath("test.drl");
    final KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write(drlResource);
    final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    final KieBase kieBase = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();
    final Collection<KiePackage> kpkgs = kieBase.getKiePackages();
    final Collection<KiePackage> newCollection = new ArrayList<>();
    for (KiePackage kpkg : kpkgs) {
        kpkg = SerializationHelper.serializeObject(kpkg);
        newCollection.add(kpkg);
    }
    ((InternalKnowledgeBase) kieBase).addPackages(newCollection);
    final KieSession kieSession = kieBase.newKieSession();
    assertThat(kieSession.fireAllRules()).isEqualTo(1);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17