Search in sources :

Example 16 with KnowledgeBaseImpl

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

the class CepEspTest method testEventExpiration.

@Test(timeout = 10000)
public void testEventExpiration() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_EventExpiration.drl");
    // read in the source
    TypeDeclaration factType = ((KnowledgeBaseImpl) kbase).getTypeDeclaration(StockTick.class);
    assertEquals(TimeIntervalParser.parse("1h30m")[0], factType.getExpirationOffset());
}
Also used : KieBase(org.kie.api.KieBase) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 17 with KnowledgeBaseImpl

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

the class TraitTest method testWrapperSize.

@Test(timeout = 10000)
public void testWrapperSize() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactory.setMode(mode, kb);
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        impClass.set(imp, "name", "john");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("school", "skol");
        assertEquals(3, wrapper.size());
        assertEquals(1, virtualFields.size());
        proxy.getFields().put("surname", "xxx");
        assertEquals(4, wrapper.size());
        assertEquals(2, virtualFields.size());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        // TraitableBean ind = (TraitableBean) indClass.newInstance();
        TraitableBean ind = new Entity();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
        Map virtualFields2 = ind._getDynamicProperties();
        Map wrapper2 = proxy2.getFields();
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        traitClass.set(proxy2, "name", "john");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("school", "skol");
        assertEquals(3, wrapper2.size());
        assertEquals(3, virtualFields2.size());
        proxy2.getFields().put("surname", "xxx");
        assertEquals(4, wrapper2.size());
        assertEquals(4, virtualFields2.size());
        FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = traitClass2.getFactClass();
        // TraitableBean ind2 = (TraitableBean) indClass.newInstance();
        TraitableBean ind2 = new Entity();
        TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
        proxy99.getFields().put("surname", "xxx");
        proxy99.getFields().put("name", "xyz");
        proxy99.getFields().put("school", "skol");
        assertEquals(3, proxy99.getFields().size());
        TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind2, trait);
        assertEquals(4, proxy100.getFields().size());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Entity(org.drools.core.factmodel.traits.Entity) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Map(java.util.Map) HashMap(java.util.HashMap) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 18 with KnowledgeBaseImpl

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

the class TraitTest method testTraitEncoding.

@Test(timeout = 10000)
public void testTraitEncoding() {
    String s1 = "package org.drools.core.factmodel.traits;\n" + "declare trait A end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "declare trait F extends C end\n" + "declare trait G extends D end\n" + "declare trait H extends D end\n" + "declare trait I extends E end\n" + "declare trait J extends F end\n" + "declare trait K extends G, H end\n" + "declare trait L extends G, H end\n" + "declare trait M extends I, J end\n" + "declare trait N extends K, L end\n" + "" + "rule \"donOneThing\"\n" + "when\n" + "    $x : Entity()\n" + "then\n" + "    don( $x, A.class );\n" + "end\n" + "" + "rule \"donManyThing\"\n" + "when\n" + "    String( this == \"y\" ) \n" + "    $x : Entity()\n" + "then\n" + "    don( $x, B.class );\n" + "    don( $x, D.class );\n" + "    don( $x, F.class );\n" + "    don( $x, E.class );\n" + "    don( $x, I.class );\n" + "    don( $x, K.class );\n" + "    don( $x, J.class );\n" + "    don( $x, C.class );\n" + "    don( $x, H.class );\n" + "    don( $x, G.class );\n" + "    don( $x, L.class );\n" + "    don( $x, M.class );\n" + "    don( $x, N.class );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    TraitRegistry tr = ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().getTraitRegistry();
    System.out.println(tr.getHierarchy());
    Entity ent = new Entity("x");
    KieSession ksession = kbase.newKieSession();
    ksession.insert(ent);
    ksession.fireAllRules();
    assertEquals(1, ent.getMostSpecificTraits().size());
    ksession.insert("y");
    ksession.fireAllRules();
    System.out.println(ent.getMostSpecificTraits());
    assertEquals(2, ent.getMostSpecificTraits().size());
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) TraitRegistry(org.drools.core.factmodel.traits.TraitRegistry) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 19 with KnowledgeBaseImpl

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

the class ReteooRuleBuilderTest method setUp.

/* (non-Javadoc)
     * @see junit.framework.TestCase#setUp()
     */
@Before
public void setUp() throws Exception {
    this.builder = new ReteooRuleBuilder();
    this.rulebase = new KnowledgeBaseImpl("default", null);
}
Also used : KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) Before(org.junit.Before)

Example 20 with KnowledgeBaseImpl

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

the class PackageCompilationDataTest method testCodeSourceUrl.

@Test
public void testCodeSourceUrl() throws Exception {
    final String className = TestEvalExpression.class.getName();
    KnowledgeBaseImpl kBase = new KnowledgeBaseImpl("xxx", null);
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    JavaDialectRuntimeData data = new JavaDialectRuntimeData();
    data.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pkg.getDialectRuntimeRegistry().setDialectData("java", data);
    kBase.addPackage(pkg);
    final JavaDialectRuntimeData pcData = (JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java");
    final EvalCondition invoker = new EvalCondition(null);
    pcData.putInvoker(className, invoker);
    final InputStream is = getClass().getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class");
    try {
        pcData.write(className.replace('.', '/') + ".class", read(is));
    } finally {
        is.close();
    }
    pcData.onAdd(pkg.getDialectRuntimeRegistry(), kBase.getRootClassLoader());
    pcData.onBeforeExecute();
    Class cls = kBase.getRootClassLoader().loadClass("org.drools.core.rule.PackageCompilationDataTest$TestEvalExpression");
    final CodeSource codeSource = invoker.getEvalExpression().getClass().getProtectionDomain().getCodeSource();
    assertNotNull(codeSource.getLocation());
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) InputStream(java.io.InputStream) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) CodeSource(java.security.CodeSource) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)45 Test (org.junit.Test)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 Resource (org.kie.api.io.Resource)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)9 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)9 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 KieBase (org.kie.api.KieBase)9 FactType (org.kie.api.definition.type.FactType)9 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)8 KieSession (org.kie.api.runtime.KieSession)8 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)6 WorkingMemory (org.drools.core.WorkingMemory)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 KieHelper (org.kie.internal.utils.KieHelper)6