use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseFrom.
@Test
public void testParseFrom() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFrom.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
PatternDescr patterndescr = (PatternDescr) obj.getLhs().getDescrs().get(0);
FromDescr from = (FromDescr) patterndescr.getSource();
MVELExprDescr accessordescriptor = (MVELExprDescr) from.getDataSource();
assertEquals("cheesery.getCheeses(i+4)", accessordescriptor.getExpression());
assertEquals(patterndescr.getObjectType(), "Cheese");
assertEquals(patterndescr.getIdentifier(), "cheese");
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseExists.
@Test
public void testParseExists() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseExists.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
Object existdescr = obj.getLhs().getDescrs().get(0);
assertTrue(existdescr instanceof ExistsDescr);
Object patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
assertTrue(patternDescriptor instanceof PatternDescr);
assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Person");
Object notDescr = obj.getLhs().getDescrs().get(1);
assertEquals(notDescr.getClass().getName(), NotDescr.class.getName());
existdescr = ((NotDescr) notDescr).getDescrs().get(0);
patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
assertTrue(patternDescriptor instanceof PatternDescr);
assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Cheese");
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseForall.
@Test
public void testParseForall() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseForall.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
ForallDescr forall = (ForallDescr) obj.getLhs().getDescrs().get(0);
List forallPaterns = forall.getDescrs();
PatternDescr pattarnState = (PatternDescr) forallPaterns.get(0);
PatternDescr personState = (PatternDescr) forallPaterns.get(1);
PatternDescr cheeseState = (PatternDescr) forallPaterns.get(2);
assertEquals(pattarnState.getObjectType(), "State");
assertEquals(personState.getObjectType(), "Person");
assertEquals(cheeseState.getObjectType(), "Cheese");
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testAccumulate.
@Test
public void testAccumulate() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
Object patternobj = obj.getLhs().getDescrs().get(0);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patterncheese = (PatternDescr) patternobj;
assertEquals(patterncheese.getIdentifier(), "cheese");
assertEquals(patterncheese.getObjectType(), "Cheese");
AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese.getSource();
assertEquals("total += $cheese.getPrice();", accumulatedescr.getActionCode());
assertEquals("int total = 0;", accumulatedescr.getInitCode());
assertEquals("new Integer( total ) );", accumulatedescr.getResultCode());
patternobj = obj.getLhs().getDescrs().get(1);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patternmax = (PatternDescr) patternobj;
assertEquals(patternmax.getIdentifier(), "max");
assertEquals(patternmax.getObjectType(), "Number");
accumulatedescr = (AccumulateDescr) patternmax.getSource();
assertTrue(accumulatedescr.isExternalFunction());
assertEquals("max", accumulatedescr.getFunctions().get(0).getFunction());
assertNull(accumulatedescr.getInitCode());
assertNull(accumulatedescr.getActionCode());
assertNull(accumulatedescr.getResultCode());
assertNull(accumulatedescr.getReverseCode());
}
use of org.drools.compiler.lang.descr.RuleDescr in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method getRuleModel.
private RuleModel getRuleModel(final ExpandedDRLInfo expandedDRLInfo, final PackageDataModelOracle dmo, final Collection<RuleModelIActionPersistenceExtension> extensions) throws RuleModelDRLPersistenceException {
// De-serialize model
RuleDescr ruleDescr = parseDrl(expandedDRLInfo);
RuleModel model = new RuleModel();
model.name = ruleDescr.getName();
model.parentName = ruleDescr.getParentName();
for (AnnotationDescr annotation : ruleDescr.getAnnotations()) {
model.addMetadata(new RuleMetadata(annotation.getName(), annotation.getValuesAsString()));
}
// De-serialize Package name
final String packageName = PackageNameParser.parsePackageName(expandedDRLInfo.plainDrl);
model.setPackageName(packageName);
// De-serialize imports
final Imports imports = ImportsParser.parseImports(expandedDRLInfo.plainDrl);
for (Import item : imports.getImports()) {
model.getImports().addImport(item);
}
boolean isJavaDialect = parseAttributes(model, ruleDescr.getAttributes());
Map<String, String> boundParams = parseLhs(model, ruleDescr.getLhs(), isJavaDialect, expandedDRLInfo, dmo);
parseRhs(model, expandedDRLInfo.consequence != null ? expandedDRLInfo.consequence : (String) ruleDescr.getConsequence(), isJavaDialect, boundParams, expandedDRLInfo, dmo, extensions);
return model;
}
Aggregations