use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method initPackage.
private void initPackage(PackageDescr packageDescr) {
// Gather all imports for all PackageDescrs for the current package and replicate into
// all PackageDescrs for the current package, thus maintaining a complete list of
// ImportDescrs for all PackageDescrs for the current package.
List<PackageDescr> packageDescrsForPackage = packages.computeIfAbsent(packageDescr.getName(), k -> new ArrayList<>());
packageDescrsForPackage.add(packageDescr);
Set<ImportDescr> imports = new HashSet<>();
for (PackageDescr pd : packageDescrsForPackage) {
imports.addAll(pd.getImports());
}
for (PackageDescr pd : packageDescrsForPackage) {
pd.getImports().clear();
pd.addAllImports(imports);
}
// Copy package level attributes for inclusion on individual rules
if (!packageDescr.getAttributes().isEmpty()) {
Map<String, AttributeDescr> pkgAttributes = packageAttributes.get(packageDescr.getNamespace());
if (pkgAttributes == null) {
pkgAttributes = new HashMap<>();
this.packageAttributes.put(packageDescr.getNamespace(), pkgAttributes);
}
for (AttributeDescr attr : packageDescr.getAttributes()) {
pkgAttributes.put(attr.getName(), attr);
}
}
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class KnowledgeBuilderImpl method processWindowDeclarations.
protected void processWindowDeclarations(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
for (WindowDeclarationDescr wd : packageDescr.getWindowDeclarations()) {
WindowDeclaration window = new WindowDeclaration(wd.getName(), packageDescr.getName());
// TODO: process annotations
// process pattern
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
DialectCompiletimeRegistry ctr = pkgRegistry.getDialectCompiletimeRegistry();
RuleDescr dummy = new RuleDescr(wd.getName() + " Window Declaration");
dummy.setResource(packageDescr.getResource());
dummy.addAttribute(new AttributeDescr("dialect", "java"));
RuleBuildContext context = new RuleBuildContext(this, dummy, ctr, pkg, ctr.getDialect(pkgRegistry.getDialect()));
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(wd.getPattern().getClass());
if (builder != null) {
final Pattern pattern = (Pattern) builder.build(context, wd.getPattern(), null);
if (pattern.getXpathConstraint() != null) {
context.addError(new DescrBuildError(wd, context.getParentDescr(), null, "OOpath expression " + pattern.getXpathConstraint() + " not allowed in window declaration\n"));
}
window.setPattern(pattern);
} else {
throw new RuntimeException("BUG: assembler not found for descriptor class " + wd.getPattern().getClass());
}
if (!context.getErrors().isEmpty()) {
for (DroolsError error : context.getErrors()) {
addBuilderResult(error);
}
} else {
pkgRegistry.getPackage().addWindowDeclaration(window);
}
}
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class MVELSalienceBuilderTest method setUp.
@Before
public void setUp() throws Exception {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("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();
buildContext = new BuildContext(kBase, Collections.emptyList());
SalienceBuilder salienceBuilder = new MVELSalienceBuilder();
salienceBuilder.build(context);
((MVELSalienceExpression) context.getRule().getSalience()).compile((MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel"));
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class PackageDescrResourceVisitor method visit.
protected void visit(final QueryDescr descr) {
checkResource(descr);
visit(descr.getLhs());
for (AttributeDescr attrDescr : descr.getAttributes().values()) {
visit(attrDescr);
}
}
use of org.drools.drl.ast.descr.AttributeDescr in project drools by kiegroup.
the class PackageDescrResourceVisitor method visit.
protected void visit(final RuleDescr descr) {
checkResource(descr);
for (AttributeDescr d : descr.getAttributes().values()) {
visit(d);
}
visit(descr.getLhs());
visitConsequence(descr.getConsequence());
for (Object o : descr.getNamedConsequences().values()) {
visitConsequence(o);
}
}
Aggregations