Reputation: 962
I have the following Rule
@Rule
@Slf4j
public class ModuleRule{
private Content content;
private String baseDir;
@Condition
public boolean when(Facts facts) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
content = facts.get("content");
baseDir = facts.get("base_dir");
Method getModule = content.getClass().getMethod("get"+name);
return (boolean) getModule.invoke(content);
}
@Action
public void then(@Fact("toInclude") List<Template> selectedTemplates) throws IOException, TemplateGenUtilsException {
log.info("Adding module:" + name);
final String moduleTemplatesPath = String.format("%s/%s", baseDir, name);
selectedTemplates.addAll(FileUtils.replacePath(moduleTemplatesPath, FileUtils.loadTemplates(name), content.getDOMAIN(), content.getAPP_NAME()));
}
}
Which is instantiated in a the following way:
@Bean
public Rules rules() {
Rules rules = new Rules();
templatesConfig.getModules().stream() . //Modules is a list of String
.map(ModuleRule::new)
.forEach(rules::register);
return rules;
}
The code won't work as after the first registration, the other rules have the same name and therefore won't be registered. Therefore, here is my question: is there a way to create a new rule and set its name at runtime?
I have also tried to extend BasicRules, the problem here is that when the engine fires, the rule is not evaluated. Here is the code:
@Slf4j
public class ModuleRule extends BasicRule{
private Content content;
private String templatesBaseDir;
public ModuleRule(String name) {
super(name);
}
@Condition
public boolean when(Facts facts) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
content = facts.get("content");
templatesBaseDir = facts.get("base_dir");
Method getModule = content.getClass().getMethod("get"+name);
return (boolean) getModule.invoke(content);
}
@Action
public void then(@Fact("toInclude") List<Template> selectedTemplates) throws IOException, TemplateGenUtilsException {
log.info("Adding module:" + name);
final String moduleTemplatesPath = String.format("%s/%s", templatesBaseDir, name);
selectedTemplates.addAll(FileUtils.replacePath(moduleTemplatesPath, FileUtils.loadTemplates(name), content.getDOMAIN(), content.getAPP_NAME()));
}
Upvotes: 0
Views: 630
Reputation: 962
Silly error: the BasicRule extension works BUT you need to override the execute and evaluate methods and not use the annotation.
Everything works now!
Upvotes: 1