use of org.drools.drl.ast.dsl.PackageDescrBuilder in project drools by kiegroup.
the class DescrBuilderTest method testRule.
@Test
public void testRule() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs(" System.out.println(\"foo\");\n").end();
PackageDescr pkg = packBuilder.getDescr();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
KiePackage kpkg = compilePkgDescr(pkg);
assertEquals("org.drools.mvel.compiler", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = kbase.newKieSession();
ksession.insert(new StockTick(1, "RHT", 80, 1));
int rules = ksession.fireAllRules();
assertEquals(0, rules);
ksession = kbase.newKieSession();
ksession.insert(new StockTick(2, "RHT", 150, 1));
rules = ksession.fireAllRules();
assertEquals(1, rules);
}
use of org.drools.drl.ast.dsl.PackageDescrBuilder in project drools by kiegroup.
the class PackageDescrTest method testSerializationImportDescr.
@Test
public void testSerializationImportDescr() {
PackageDescrBuilder builder = DescrFactory.newPackage().name("foo");
String className = Person.class.getName();
builder.newImport().target(className).end();
PackageDescr descr = builder.getDescr();
ImportDescr importDescr = new ImportDescr(className);
ImportDescr badImportDescr = new ImportDescr(null);
assertTrue(descr.getImports().contains(importDescr));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(baos);
descr.writeExternal(out);
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
PackageDescr newDescr = new PackageDescr();
newDescr.readExternal(in);
assertFalse(newDescr.getImports().contains(badImportDescr));
assertTrue(newDescr.getImports().contains(importDescr));
assertFalse(newDescr.getPreferredPkgUUID().isPresent());
} catch (IOException | ClassNotFoundException e) {
fail(e.getMessage());
}
}
use of org.drools.drl.ast.dsl.PackageDescrBuilder in project drools by kiegroup.
the class PackageDescrTest method testSerializationPkgUUID.
@Test
public void testSerializationPkgUUID() {
PackageDescrBuilder builder = DescrFactory.newPackage().name("foo");
String className = Person.class.getName();
builder.newImport().target(className).end();
PackageDescr descr = builder.getDescr();
String pkgUUID = generateUUID();
descr.setPreferredPkgUUID(pkgUUID);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(baos);
descr.writeExternal(out);
ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
PackageDescr newDescr = new PackageDescr();
newDescr.readExternal(in);
assertTrue(newDescr.getPreferredPkgUUID().isPresent());
assertEquals(pkgUUID, newDescr.getPreferredPkgUUID().get());
} catch (IOException | ClassNotFoundException e) {
fail(e.getMessage());
}
}
use of org.drools.drl.ast.dsl.PackageDescrBuilder in project drools by kiegroup.
the class KiePMMLDescrFactory method getBaseDescr.
/**
* Returns the <code>PackageDescr</code> built out of the given <code>KiePMMLDroolsAST</code>.
* @param kiePMMLDroolsAST
* @param packageName
* @return
*/
public static PackageDescr getBaseDescr(final KiePMMLDroolsAST kiePMMLDroolsAST, String packageName) {
logger.trace("getBaseDescr {} {}", kiePMMLDroolsAST, packageName);
PackageDescrBuilder builder = DescrFactory.newPackage().name(packageName);
builder.newImport().target(KiePMMLStatusHolder.class.getName());
builder.newImport().target(SimplePredicate.class.getName());
builder.newImport().target(PMML4Result.class.getName());
builder.newImport().target(Map.class.getName());
builder.newGlobal().identifier(PMML4_RESULT_IDENTIFIER).type(PMML4_RESULT);
builder.newGlobal().identifier(OUTPUTFIELDS_MAP_IDENTIFIER).type(OUTPUTFIELDS_MAP);
KiePMMLDescrTypesFactory.factory(builder).declareTypes(kiePMMLDroolsAST.getTypes());
KiePMMLDescrRulesFactory.factory(builder).declareRules(kiePMMLDroolsAST.getRules());
return builder.getDescr();
}
use of org.drools.drl.ast.dsl.PackageDescrBuilder in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method setUp.
@Before
public void setUp() throws Exception {
PackageDescrBuilder builder = DescrFactory.newPackage().name(PACKAGE_NAME);
ruleBuilder = builder.newRule().name(CURRENT_RULE);
assertEquals(CURRENT_RULE, ruleBuilder.getDescr().getName());
}
Aggregations