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());
}
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());
}
}
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());
}
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);
}
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());
}
Aggregations