Search in sources :

Example 11 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class BayesRuntimeImpl method createInstance.

// @Override
// public BayesInstance createBayesFact(Class cls) {
// // using the two-tone pattern, to ensure only one is created
// BayesInstance instance = instances.get( cls.getName() );
// if ( instance == null ) {
// instance = createInstance(cls);
// }
// 
// return instance;
// }
public BayesInstance createInstance(Class cls) {
    // synchronised using the two-tone pattern, to ensure only one is created
    // BayesInstance instance = instances.get( cls.getName() );
    // if ( instance != null ) {
    // return instance;
    // }
    InternalKnowledgePackage kpkg = (InternalKnowledgePackage) runtime.getKieBase().getKiePackage(cls.getPackage().getName());
    Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
    BayesPackage bayesPkg = (BayesPackage) map.get(ResourceType.BAYES);
    JunctionTree jtree = bayesPkg.getJunctionTree(cls.getSimpleName());
    BayesInstance instance = new BayesInstance(jtree, cls);
    return instance;
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) BayesInstance(org.drools.beliefs.bayes.BayesInstance) JunctionTree(org.drools.beliefs.bayes.JunctionTree) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 12 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class BayesWeaverService method merge.

@Override
public void merge(KieBase kieBase, KiePackage kiePkg, BayesPackage bayesPkg) {
    Map<ResourceType, ResourceTypePackage> map = ((InternalKnowledgePackage) kiePkg).getResourceTypePackages();
    BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
    if (existing == null) {
        existing = new BayesPackage();
        map.put(ResourceType.BAYES, existing);
    }
    for (String name : bayesPkg.listJunctionTrees()) {
        existing.addJunctionTree(name, bayesPkg.getJunctionTree(name));
    }
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 13 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class AssemblerTest method testBayesPackageAssembly.

@Test
public void testBayesPackageAssembly() throws Exception {
    KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
    kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
    InternalKnowledgePackage kpkg = kbuilder.getPackageRegistry("org.drools.beliefs.bayes.integration").getPackage();
    BayesPackage bkpg = (BayesPackage) kpkg.getResourceTypePackages().get(ResourceType.BAYES);
    assertNotNull(bkpg.getJunctionTree("Garden"));
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 14 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class TypeDeclarationConfigurator method wireTimestampAccessor.

private static void wireTimestampAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
    Timestamp timestamp = annotated.getTypedAnnotation(Timestamp.class);
    if (timestamp != null) {
        BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
        String timestampField;
        try {
            timestampField = timestamp.value();
        } catch (Exception e) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
            return;
        }
        type.setTimestampAttribute(timestampField);
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, timestampField, pkg);
        if (results != null) {
            type.setTimestampExtractor(getFieldExtractor(type, timestampField, pkg, results));
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error creating field accessors for timestamp field '" + timestamp + "' for type '" + type.getTypeName() + "'"));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Timestamp(org.kie.api.definition.type.Timestamp) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 15 with InternalKnowledgePackage

use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.

the class RuleBuilderTest method testBuild.

@Test
public void testBuild() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
    final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    pkg.addGlobal("results", List.class);
    final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
    final String ruleClassName = "RuleClassName.java";
    ruleDescr.setClassName(ruleClassName);
    ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage("org.drools.compiler").getRule("test nested CEs");
    assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
    // second GE should be a not
    final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
    assertTrue(not.isNot());
    // not has no outer declarations
    assertTrue(not.getOuterDeclarations().isEmpty());
    assertEquals(1, not.getInnerDeclarations().size());
    assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
    // second not
    final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
    assertTrue(not2.isNot());
    // not has no outer declarations
    assertTrue(not2.getOuterDeclarations().isEmpty());
    assertEquals(1, not2.getInnerDeclarations().size());
    assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)97 Test (org.junit.Test)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)28 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)26 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)24 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)21 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)20 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)16 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)14 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 TypeDeclaration (org.drools.core.rule.TypeDeclaration)13 HashMap (java.util.HashMap)12 Pattern (org.drools.core.rule.Pattern)12 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)11 ClassObjectType (org.drools.core.base.ClassObjectType)10 ArrayList (java.util.ArrayList)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)9 ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)8