use of org.kie.api.definition.type.FactType 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.kie.api.definition.type.FactType in project drools by kiegroup.
the class TypeDeclarationTest method testMultipleAnnotationDeclarations.
@Test
public void testMultipleAnnotationDeclarations() {
String str1 = "";
str1 += "package org.kie1 " + "" + "declare Foo \n" + " name : String " + " age : int " + "end ";
String str2 = "";
str2 += "package org.kie2 " + "" + "declare org.kie1.Foo " + " @role(event) " + "end ";
String str3 = "";
str3 += "package org.kie3 " + "" + "declare org.kie1.Foo " + " @propertyReactive " + "end ";
String str4 = "" + "package org.kie4; " + "import org.kie1.Foo; " + "" + "rule Check " + "when " + " $f : Foo( name == 'bar' ) " + "then " + " modify( $f ) { setAge( 99 ); } " + "end ";
KieHelper helper = new KieHelper();
helper.addContent(str1, ResourceType.DRL);
helper.addContent(str2, ResourceType.DRL);
helper.addContent(str3, ResourceType.DRL);
helper.addContent(str4, ResourceType.DRL);
List<Message> msg = helper.verify().getMessages(Message.Level.ERROR);
System.out.println(msg);
assertEquals(0, msg.size());
KieBase kieBase = helper.build();
FactType type = kieBase.getFactType("org.kie1", "Foo");
assertEquals(2, type.getFields().size());
Object foo = null;
try {
foo = type.newInstance();
type.set(foo, "name", "bar");
assertEquals("bar", type.get(foo, "name"));
} catch (InstantiationException e) {
fail(e.getMessage());
} catch (IllegalAccessException e) {
fail(e.getMessage());
}
KieSession session = kieBase.newKieSession();
FactHandle handle = session.insert(foo);
int n = session.fireAllRules(5);
assertTrue(handle instanceof EventFactHandle);
assertEquals(1, n);
assertEquals(99, type.get(foo, "age"));
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class TypeDeclarationTest method testPreventReflectionAPIsOnJavaClasses.
@Test(expected = UnsupportedOperationException.class)
public void testPreventReflectionAPIsOnJavaClasses() {
String drl = "package org.test; " + // existing java class
"declare org.drools.compiler.Person " + " @role(event) " + "end \n" + "";
KieBuilder kieBuilder = build(drl);
assertFalse(kieBuilder.getResults().hasMessages(Message.Level.ERROR));
KieBase kieBase = KieServices.Factory.get().newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();
FactType type = kieBase.getFactType("org.drools.compiler", "Person");
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class AdapterTest method testCustomInputAdapter.
@Test
public void testCustomInputAdapter() {
String source = PMML4Helper.pmmlDefaultPackageName().replace(".", "/") + "/" + "mock_cold_adapter.xml";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource(source).setResourceType(ResourceType.PMML));
Results res = ks.newKieBuilder(kfs).buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages(Message.Level.ERROR));
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieBase kieBase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
FactType ft = kieBase.getFactType("test", "MyAdapter");
assertTrue(ft != null);
assertTrue(ft.getFactClass().isInterface());
FactType fto = kieBase.getFactType("test", "MyOutAdapter");
assertTrue(fto != null);
assertTrue(fto.getFactClass().isInterface());
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class ConstrainedDataDictionaryTest method testContinuousDomainIntervals.
@Test
public void testContinuousDomainIntervals() throws Exception {
FactType ivals = getKbase().getFactType(packageName, "Intervalled");
assertNotNull(ivals);
Object data1 = ivals.newInstance();
ivals.set(data1, "value", -0.4);
Object data2 = ivals.newInstance();
ivals.set(data2, "value", 0.3);
Object data3 = ivals.newInstance();
ivals.set(data3, "value", 1.6);
Object data4 = ivals.newInstance();
ivals.set(data4, "value", 2.0);
Object data5 = ivals.newInstance();
ivals.set(data5, "value", 3.0);
Object data6 = ivals.newInstance();
ivals.set(data6, "value", 8.2);
Object data7 = ivals.newInstance();
ivals.set(data7, "value", 12.4);
Object data8 = ivals.newInstance();
ivals.set(data8, "value", 999.9);
getKSession().insert(data1);
getKSession().insert(data2);
getKSession().insert(data3);
getKSession().insert(data4);
getKSession().insert(data5);
getKSession().insert(data6);
getKSession().insert(data7);
getKSession().insert(data8);
getKSession().fireAllRules();
assertEquals(false, ivals.get(data1, "valid"));
assertEquals(true, ivals.get(data2, "valid"));
assertEquals(false, ivals.get(data3, "valid"));
assertEquals(true, ivals.get(data4, "valid"));
assertEquals(false, ivals.get(data5, "valid"));
assertEquals(false, ivals.get(data6, "valid"));
assertEquals(true, ivals.get(data7, "valid"));
assertEquals(false, ivals.get(data8, "valid"));
checkGeneratedRules();
}
Aggregations