Search in sources :

Example 1 with PackageDescrBuilder

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);
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 2 with PackageDescrBuilder

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());
    }
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) ObjectOutput(java.io.ObjectOutput) ImportDescr(org.drools.drl.ast.descr.ImportDescr) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 3 with PackageDescrBuilder

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());
    }
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 4 with PackageDescrBuilder

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();
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLStatusHolder(org.kie.pmml.models.drools.executor.KiePMMLStatusHolder) Map(java.util.Map) SimplePredicate(org.dmg.pmml.SimplePredicate)

Example 5 with PackageDescrBuilder

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());
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) Before(org.junit.Before)

Aggregations

PackageDescrBuilder (org.drools.drl.ast.dsl.PackageDescrBuilder)11 Test (org.junit.Test)7 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 DrlDumper (org.drools.mvel.DrlDumper)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ObjectOutput (java.io.ObjectOutput)3 ObjectOutputStream (java.io.ObjectOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 ObjectInput (java.io.ObjectInput)2 ObjectInputStream (java.io.ObjectInputStream)2 RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)2 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)2 Before (org.junit.Before)2 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 SimplePredicate (org.dmg.pmml.SimplePredicate)1