use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class MVELConsequenceBuilderTest method testImperativeCodeError.
@Test
public void testImperativeCodeError() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.setConsequence("if (cheese.price == 10) { cheese.price = 5; }");
Properties properties = new Properties();
properties.setProperty("drools.dialect.default", "mvel");
KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl(properties);
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg, cfg1);
PackageRegistry pkgRegistry = pkgBuilder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect(pkgRegistry.getDialect());
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjeectType);
final PatternExtractor extractor = new PatternExtractor(cheeseObjeectType);
final Declaration declaration = new Declaration("cheese", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("cheese", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
final AgendaItem item = new AgendaItemImpl(0, tuple, 10, null, null, null);
final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
kbHelper.setActivation(item);
try {
((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
context.getRule().getConsequence().evaluate(kbHelper, ksession);
fail("should throw an exception, as 'if' is not allowed");
} catch (Exception e) {
}
assertEquals(10, cheddar.getPrice());
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class MVELSalienceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
ruleDescr.addAttribute(new AttributeDescr("salience", "(p.age + 20)/2"));
ruleDescr.setConsequence("");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final ObjectType personObjeectType = new ClassObjectType(Person.class);
final Pattern pattern = new Pattern(0, personObjeectType);
final PatternExtractor extractor = new PatternExtractor(personObjeectType);
final Declaration declaration = new Declaration("p", extractor, pattern);
final Map<String, Declaration> map = new HashMap<String, Declaration>();
map.put("p", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
kBase = KnowledgeBaseFactory.newKnowledgeBase();
SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
salienceBuilder.build(context);
((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class AccumulateTemplateTest method testInvokerGenerationSinglePattern.
@Test
public void testInvokerGenerationSinglePattern() {
final String className = "accumulate0";
final String[] declarationTypes = new String[] { "String", "int" };
final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), null) };
final Declaration[] inner = new Declaration[] { new Declaration("cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("price", store.getReader(Cheese.class, "price"), null) };
final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
final List globalTypes = Arrays.asList(new String[] { "String", "String" });
final Map map = new HashMap();
map.put("className", StringUtils.ucFirst(className));
map.put("instanceName", className);
map.put("package", "org.drools");
map.put("ruleClassName", "Rule0");
map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
map.put("declarations", declarations);
map.put("declarationTypes", declarationTypes);
map.put("globals", globals);
map.put("globalTypes", globalTypes);
map.put("innerDeclarations", inner);
map.put("attributes", new Attribute[] { new Attribute("int", "x") });
map.put("initCode", "x = 0;");
map.put("actionCode", "x += 1;");
map.put("reverseCode", "");
map.put("resultCode", "x + 10");
map.put("supportsReverse", "false");
map.put("resultType", Integer.class);
map.put("hashCode", new Integer(10));
map.put("isMultiPattern", Boolean.FALSE);
TemplateRegistry registry = getInvokerTemplateRegistry();
Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class XpathBackReference method getDeclaration.
public Declaration getDeclaration(Pattern pattern, String id) {
if (!id.startsWith(BACK_REFERENCE_HEAD)) {
return null;
}
Declaration declaration = declarations.get(id);
if (declaration != null) {
return declaration;
}
int backRefPos = Integer.parseInt(id.substring(XpathBackReference.BACK_REFERENCE_HEAD.length()));
int relativeOffset = backReferenceClasses.size() - 1 - backRefPos;
declaration = new Declaration(id, new PatternExtractor(new ClassObjectType(backReferenceClasses.get(backRefPos))), pattern, true);
declaration.setxPathOffset(relativeOffset);
if (declarations.isEmpty()) {
declarations = new HashMap<>();
}
declarations.put(id, declaration);
return declaration;
}
use of org.drools.core.spi.PatternExtractor in project drools by kiegroup.
the class AccumulateTemplateTest method testInvokerGenerationMultiPattern.
@Test
public void testInvokerGenerationMultiPattern() {
final String className = "accumulate0";
final String[] declarationTypes = new String[] { "String", "int" };
final Declaration[] declarations = new Declaration[] { new Declaration("name", store.getReader(Person.class, "name"), null), new Declaration("age", store.getReader(Person.class, "age"), null) };
final Declaration[] inner = new Declaration[] { new Declaration("$cheese", new PatternExtractor(new ClassObjectType(Cheese.class)), null), new Declaration("$person", new PatternExtractor(new ClassObjectType(Person.class)), null) };
final String[] globals = new String[] { "aGlobal", "anotherGlobal" };
final List globalTypes = Arrays.asList(new String[] { "String", "String" });
final Map map = new HashMap();
map.put("className", StringUtils.ucFirst(className));
map.put("instanceName", className);
map.put("package", "org.drools");
map.put("ruleClassName", "Rule0");
map.put("invokerClassName", "Rule0" + StringUtils.ucFirst(className) + "Invoker");
map.put("declarations", declarations);
map.put("declarationTypes", declarationTypes);
map.put("globals", globals);
map.put("globalTypes", globalTypes);
map.put("innerDeclarations", inner);
map.put("attributes", new Attribute[] { new Attribute("int", "x") });
map.put("initCode", "x = 0;");
map.put("actionCode", "x += 1;");
map.put("reverseCode", "");
map.put("resultCode", "x + 10");
map.put("supportsReverse", "false");
map.put("resultType", Integer.class);
map.put("hashCode", new Integer(10));
map.put("isMultiPattern", Boolean.TRUE);
TemplateRegistry registry = getInvokerTemplateRegistry();
Object method = TemplateRuntime.execute(registry.getNamedTemplate("accumulateInvoker"), null, new MapVariableResolverFactory(map), registry);
// System.out.println( method );
}
Aggregations