Search in sources :

Example 21 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleBuilderTest method testInvalidDialect.

@Test
public void testInvalidDialect() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mvl"));
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertFalse(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 22 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class PackageDescrTest method testGetPreferredPkgUUID.

@Test
public void testGetPreferredPkgUUID() {
    PackageDescr descr = new PackageDescr();
    assertFalse(descr.getPreferredPkgUUID().isPresent());
    String pkgUUID = generateUUID();
    descr.setPreferredPkgUUID(pkgUUID);
    assertTrue(descr.getPreferredPkgUUID().isPresent());
    assertEquals(pkgUUID, descr.getPreferredPkgUUID().get());
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 23 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class PackageDescrTest method testAttributeOverriding.

@Test
public void testAttributeOverriding() {
    PackageDescr desc = new PackageDescr("foo");
    AttributeDescr at1 = new AttributeDescr("foo", "bar");
    AttributeDescr at2 = new AttributeDescr("foo2", "default");
    desc.addAttribute(at1);
    desc.addAttribute(at2);
    RuleDescr rule = new RuleDescr("abc");
    rule.addAttribute(new AttributeDescr("foo", "overridden"));
    desc.addRule(rule);
    List pkgAts = desc.getAttributes();
    assertEquals("bar", ((AttributeDescr) pkgAts.get(0)).getValue());
    assertEquals("default", ((AttributeDescr) pkgAts.get(1)).getValue());
    desc.afterRuleAdded(rule);
    Map<String, AttributeDescr> ruleAts = rule.getAttributes();
    assertEquals("overridden", ((AttributeDescr) ruleAts.get("foo")).getValue());
    assertEquals("default", ((AttributeDescr) ruleAts.get("foo2")).getValue());
}
Also used : RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) Test(org.junit.Test)

Example 24 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr 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 25 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr 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)

Aggregations

PackageDescr (org.drools.drl.ast.descr.PackageDescr)262 Test (org.junit.Test)210 RuleDescr (org.drools.drl.ast.descr.RuleDescr)104 PatternDescr (org.drools.drl.ast.descr.PatternDescr)85 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)52 AndDescr (org.drools.drl.ast.descr.AndDescr)39 DrlParser (org.drools.drl.parser.DrlParser)39 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 InputStreamReader (java.io.InputStreamReader)23 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)23 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)23 KieSession (org.kie.api.runtime.KieSession)19 List (java.util.List)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)17 KiePackage (org.kie.api.definition.KiePackage)17 DrlDumper (org.drools.mvel.DrlDumper)16 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15