@Rule(name = "weather rule", description = "if it rains then take an umbrella")
public class WeatherRule {
@Condition
public boolean itRains(@Fact("rain") boolean rain) {
return rain;
}
@Action
public void takeAnUmbrella() {
System.out.println("It rains, take an umbrella!");
}
}
1.2.链式编程
Rule weatherRule = new RuleBuilder()
.name("weather rule")
.description("if it rains then take an umbrella")
.when(facts -> facts.get("rain").equals(true))
.then(facts -> System.out.println("It rains, take an umbrella!"))
.build();
1.3.表达式方式
Rule weatherRule = new MVELRule()
.name("weather rule")
.description("if it rains then take an umbrella")
.when("rain == true")
.then("System.out.println(\"It rains, take an umbrella!\");");
1.4.文件脚本DSL方式
weather-rule.yml文件定义:
name: "weather rule"
description: "if it rains then take an umbrella"
condition: "rain == true"
actions:
- "System.out.println(\"It rains, take an umbrella!\");"
或者weather-rule.json文件定义:
[
{
"name": "weather rule",
"description": "if it rains then take an umbrella",
"priority": 1,
"condition": "rain == true",
"actions": [
"System.out.println(\"It rains, take an umbrella!\");"
]
}
]
/**
* A {@link Rule} implementation that uses
* SpEL
* to evaluate and execute the rule.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public class SpELRule extends BasicRule {
private Condition condition = Condition.FALSE;
private final List actions = new ArrayList<>();
private final ParserContext parserContext;
private BeanResolver beanResolver;
/**
* Create a new SpEL rule.
*/
public SpELRule() {
this(ParserContext.TEMPLATE_EXPRESSION);
}
/**
* Create a new SpEL rule.
*
* @param parserContext used when parsing expressions
*/
public SpELRule(ParserContext parserContext) {
super(Rule.DEFAULT_NAME, Rule.DEFAULT_DESCRIPTION, Rule.DEFAULT_PRIORITY);
this.parserContext = parserContext;
}
/**
* Create a new SpEL rule.
*
* @param beanResolver used to resolve bean references in expressions
*/
public SpELRule(BeanResolver beanResolver) {
super(Rule.DEFAULT_NAME, Rule.DEFAULT_DESCRIPTION, Rule.DEFAULT_PRIORITY);
this.parserContext = ParserContext.TEMPLATE_EXPRESSION;
this.beanResolver = beanResolver;
}
/**
* Create a new SpEL rule.
*
* @param parserContext used when parsing expressions
* @param beanResolver used to resolve bean references in expressions
*/
public SpELRule(ParserContext parserContext, BeanResolver beanResolver) {
super(Rule.DEFAULT_NAME, Rule.DEFAULT_DESCRIPTION, Rule.DEFAULT_PRIORITY);
this.parserContext = parserContext;
this.beanResolver = beanResolver;
}
/**
* Set rule name.
*
* @param name of the rule
* @return this rule
*/
public SpELRule name(String name) {
this.name = name;
return this;
}
/**
* Set rule description.
*
* @param description of the rule
* @return this rule
*/
public SpELRule description(String description) {
this.description = description;
return this;
}
/**
* Set rule priority.
*
* @param priority of the rule
* @return this rule
*/
public SpELRule priority(int priority) {
this.priority = priority;
return this;
}
/**
* Specify the rule's condition as SpEL expression.
* @param condition of the rule
* @return this rule
*/
public SpELRule when(String condition) {
this.condition = new SpELCondition(condition, parserContext, beanResolver);
return this;
}
/**
* Add an action specified as an SpEL expression to the rule.
* @param action to add to the rule
* @return this rule
*/
public SpELRule then(String action) {
this.actions.add(new SpELAction(action, parserContext, beanResolver));
return this;
}
@Override
public boolean evaluate(Facts facts) {
return condition.evaluate(facts);
}
@Override
public void execute(Facts facts) throws Exception {
for (Action action : actions) {
action.execute(facts);
}
}
}
/**
* Base class for rule factories.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public abstract class AbstractRuleFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRuleFactory.class);
private static final List ALLOWED_COMPOSITE_RULE_TYPES = Arrays.asList(
UnitRuleGroup.class.getSimpleName(),
ConditionalRuleGroup.class.getSimpleName(),
ActivationRuleGroup.class.getSimpleName()
);
protected Rule createRule(RuleDefinition ruleDefinition) {
if (ruleDefinition.isCompositeRule()) {
return createCompositeRule(ruleDefinition);
} else {
return createSimpleRule(ruleDefinition);
}
}
protected abstract Rule createSimpleRule(RuleDefinition ruleDefinition);
protected Rule createCompositeRule(RuleDefinition ruleDefinition) {
if (ruleDefinition.getCondition() != null) {
LOGGER.warn(
"Condition '{}' in composite rule '{}' of type {} will be ignored.",
ruleDefinition.getCondition(),
ruleDefinition.getName(),
ruleDefinition.getCompositeRuleType());
}
if (ruleDefinition.getActions() != null && !ruleDefinition.getActions().isEmpty()) {
LOGGER.warn(
"Actions '{}' in composite rule '{}' of type {} will be ignored.",
ruleDefinition.getActions(),
ruleDefinition.getName(),
ruleDefinition.getCompositeRuleType());
}
CompositeRule compositeRule;
String name = ruleDefinition.getName();
switch (ruleDefinition.getCompositeRuleType()) {
case "UnitRuleGroup":
compositeRule = new UnitRuleGroup(name);
break;
case "ActivationRuleGroup":
compositeRule = new ActivationRuleGroup(name);
break;
case "ConditionalRuleGroup":
compositeRule = new ConditionalRuleGroup(name);
break;
default:
throw new IllegalArgumentException("Invalid composite rule type, must be one of " + ALLOWED_COMPOSITE_RULE_TYPES);
}
compositeRule.setDescription(ruleDefinition.getDescription());
compositeRule.setPriority(ruleDefinition.getPriority());
for (RuleDefinition composingRuleDefinition : ruleDefinition.getComposingRules()) {
compositeRule.addRule(createRule(composingRuleDefinition));
}
return compositeRule;
}
}
/**
* Rule definition as defined in a rule descriptor.
* This class encapsulates the static definition of a {@link Rule}.
*
* Rule definitions are produced by a {@code RuleDefinitionReader}s
* and consumed by rule factories to create rules.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public class RuleDefinition {
private String name = Rule.DEFAULT_NAME;
private String description = Rule.DEFAULT_DESCRIPTION;
private int priority = Rule.DEFAULT_PRIORITY;
private String condition;
private List actions = new ArrayList<>();
private List composingRules = new ArrayList<>();
private String compositeRuleType;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public List getActions() {
return actions;
}
public void setActions(List actions) {
this.actions = actions;
}
public void setComposingRules(List composingRuleDefinitions) {
this.composingRules = composingRuleDefinitions;
}
public void setCompositeRuleType(String compositeRuleType) {
this.compositeRuleType = compositeRuleType;
}
public String getCompositeRuleType() {
return compositeRuleType;
}
public List getComposingRules() {
return composingRules;
}
public boolean isCompositeRule() {
return !composingRules.isEmpty();
}
}
/**
* Strategy interface for {@link RuleDefinition} readers.
*
* @see JsonRuleDefinitionReader
* @see YamlRuleDefinitionReader
*
* @author Mahmoud Ben Hassine ([email protected])
*/
@FunctionalInterface
public interface RuleDefinitionReader {
/**
* Read a list of rule definitions from a rule descriptor.
*
* The descriptor is expected to contain a collection of rule definitions
* even for a single rule.
*
* @param reader of the rules descriptor
* @return a list of rule definitions
* @throws Exception if a problem occurs during rule definition reading
*/
List read(Reader reader) throws Exception;
}
/**
* Base class for {@link RuleDefinitionReader}s.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public abstract class AbstractRuleDefinitionReader implements RuleDefinitionReader {
public List read(Reader reader) throws Exception {
List ruleDefinitions = new ArrayList<>();
Iterable
/**
* Rule definition reader based on Jackson.
*
* This reader expects an array of rule definitions as input even for a single rule. For example:
*
*
* [{rule1}, {rule2}]
*
*
* @author Mahmoud Ben Hassine ([email protected])
*/
@SuppressWarnings("unchecked")
public class JsonRuleDefinitionReader extends AbstractRuleDefinitionReader {
private final ObjectMapper objectMapper;
/**
* Create a new {@link JsonRuleDefinitionReader}.
*/
public JsonRuleDefinitionReader() {
this(new ObjectMapper());
}
/**
* Create a new {@link JsonRuleDefinitionReader}.
*
* @param objectMapper to use to read rule definitions
*/
public JsonRuleDefinitionReader(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
@Override
protected Iterable> loadRules(Reader reader) throws Exception {
List> rulesList = new ArrayList<>();
Object[] rules = objectMapper.readValue(reader, Object[].class);
for (Object rule : rules) {
rulesList.add((Map) rule);
}
return rulesList;
}
}
/**
* Rule definition reader based on Jackson Yaml.
*
* This reader expects a collection of rule definitions as input even for a single rule. For example:
*
*
* rule1
* ---
* rule2
*
*
* @author Mahmoud Ben Hassine ([email protected])
*/
@SuppressWarnings("unchecked")
public class YamlRuleDefinitionReader extends AbstractRuleDefinitionReader {
private final Yaml yaml;
/**
* Create a new {@link YamlRuleDefinitionReader}.
*/
public YamlRuleDefinitionReader() {
this(new Yaml());
}
/**
* Create a new {@link YamlRuleDefinitionReader}.
*
* @param yaml to use to read rule definitions
*/
public YamlRuleDefinitionReader(Yaml yaml) {
this.yaml = yaml;
}
@Override
protected Iterable> loadRules(Reader reader) {
List> rulesList = new ArrayList<>();
Iterable rules = yaml.loadAll(reader);
for (Object rule : rules) {
rulesList.add((Map) rule);
}
return rulesList;
}
}
至此,借助RuleDefinitionReader完成了RuleDefinition的解析;
3.2 组合规则创建
在方法createRule实现中,根据解析结果中是否包含组合规则会路由到不同的创建逻辑;
包含组合规则的情况下,会构造具体的组合规则,如下:
protected Rule createCompositeRule(RuleDefinition ruleDefinition) {
if (ruleDefinition.getCondition() != null) {
LOGGER.warn(
"Condition '{}' in composite rule '{}' of type {} will be ignored.",
ruleDefinition.getCondition(),
ruleDefinition.getName(),
ruleDefinition.getCompositeRuleType());
}
if (ruleDefinition.getActions() != null && !ruleDefinition.getActions().isEmpty()) {
LOGGER.warn(
"Actions '{}' in composite rule '{}' of type {} will be ignored.",
ruleDefinition.getActions(),
ruleDefinition.getName(),
ruleDefinition.getCompositeRuleType());
}
CompositeRule compositeRule;
String name = ruleDefinition.getName();
switch (ruleDefinition.getCompositeRuleType()) {
case "UnitRuleGroup":
compositeRule = new UnitRuleGroup(name);
break;
case "ActivationRuleGroup":
compositeRule = new ActivationRuleGroup(name);
break;
case "ConditionalRuleGroup":
compositeRule = new ConditionalRuleGroup(name);
break;
default:
throw new IllegalArgumentException("Invalid composite rule type, must be one of " + ALLOWED_COMPOSITE_RULE_TYPES);
}
compositeRule.setDescription(ruleDefinition.getDescription());
compositeRule.setPriority(ruleDefinition.getPriority());
for (RuleDefinition composingRuleDefinition : ruleDefinition.getComposingRules()) {
compositeRule.addRule(createRule(composingRuleDefinition));
}
return compositeRule;
}
/**
* Factory to create {@link SpELRule} instances.
*
* @author Mahmoud Ben Hassine ([email protected])
*/
public class SpELRuleFactory extends AbstractRuleFactory {
private final RuleDefinitionReader reader;
private BeanResolver beanResolver;
private ParserContext parserContext;
/**
* Create a new {@link SpELRuleFactory} with a given reader.
*
* @param reader used to read rule definitions
* @see YamlRuleDefinitionReader
* @see JsonRuleDefinitionReader
*/
public SpELRuleFactory(RuleDefinitionReader reader) {
this(reader, ParserContext.TEMPLATE_EXPRESSION);
}
/**
* Create a new {@link SpELRuleFactory} with a given reader.
*
* @param reader used to read rule definitions
* @param parserContext used to parse SpEL expressions
* @see YamlRuleDefinitionReader
* @see JsonRuleDefinitionReader
*/
public SpELRuleFactory(RuleDefinitionReader reader, ParserContext parserContext) {
this.reader = reader;
this.parserContext = parserContext;
}
/**
* Create a new {@link SpELRuleFactory} with a given reader.
*
* @param reader used to read rule definitions
* @param beanResolver used to resolve bean references in SpEL expressions
* @see YamlRuleDefinitionReader
* @see JsonRuleDefinitionReader
*/
public SpELRuleFactory(RuleDefinitionReader reader, BeanResolver beanResolver) {
this.reader = reader;
this.beanResolver = beanResolver;
}
/**
* Create a new {@link SpELRuleFactory} with a given reader.
*
* @param reader used to read rule definitions
* @param parserContext used to parse SpEL expressions
* @param beanResolver used to resolve bean references in SpEL expressions
* @see YamlRuleDefinitionReader
* @see JsonRuleDefinitionReader
*/
public SpELRuleFactory(RuleDefinitionReader reader, ParserContext parserContext, BeanResolver beanResolver) {
this.reader = reader;
this.parserContext = parserContext;
this.beanResolver = beanResolver;
}
/**
* Create a new {@link SpELRule} from a Reader.
*
* The rule descriptor should contain a single rule definition.
* If no rule definitions are found, a {@link IllegalArgumentException} will be thrown.
* If more than a rule is defined in the descriptor, the first rule will be returned.
*
* @param ruleDescriptor descriptor of rule definition
* @return a new rule
* @throws Exception if unable to create the rule from the descriptor
*/
public Rule createRule(Reader ruleDescriptor) throws Exception {
List ruleDefinitions = reader.read(ruleDescriptor);
if (ruleDefinitions.isEmpty()) {
throw new IllegalArgumentException("rule descriptor is empty");
}
return createRule(ruleDefinitions.get(0));
}
/**
* Create a set of {@link SpELRule} from a Reader.
*
* @param rulesDescriptor descriptor of rule definitions
* @return a set of rules
* @throws Exception if unable to create rules from the descriptor
*/
public Rules createRules(Reader rulesDescriptor) throws Exception {
Rules rules = new Rules();
List ruleDefinitions = reader.read(rulesDescriptor);
for (RuleDefinition ruleDefinition : ruleDefinitions) {
rules.register(createRule(ruleDefinition));
}
return rules;
}
protected Rule createSimpleRule(RuleDefinition ruleDefinition) {
SpELRule spELRule = new SpELRule(parserContext, beanResolver)
.name(ruleDefinition.getName())
.description(ruleDefinition.getDescription())
.priority(ruleDefinition.getPriority())
.when(ruleDefinition.getCondition());
for (String action : ruleDefinition.getActions()) {
spELRule.then(action);
}
return spELRule;
}
}
好久不来iteye,今天又来看看,哈哈,今天碰到在编码时,反射中会抛出
Illegal overloaded getter method with ambiguous type for propert这么个东东,从字面意思看,是反射在获取getter时迷惑了,然后回想起java在boolean值在生成getter时,分别有is和getter,也许我们的反射对象中就有is开头的方法迷惑了jdk,
泛型
在Java SE 1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,任意化的缺点就是要实行强制转换,这种强制转换可能会带来不安全的隐患
泛型的特点:消除强制转换 确保类型安全 向后兼容
简单泛型的定义:
泛型:就是在类中将其模糊化,在创建对象的时候再具体定义
class fan
安装lua_nginx_module 模块
lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty
Centos和debian的安装就简单了。。
这里说下freebsd的安装:
fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
ma
public class IsAccendListRecursive {
/*递归判断数组是否升序
* if a Integer array is ascending,return true
* use recursion
*/
public static void main(String[] args){
IsAccendListRecursiv
function init() {
var tt = document.getElementById("tt");
var childNodes = tt.childNodes[0].childNodes;
var level = 0;
for (var i = 0; i < childNodes.length; i++) {
在安装hadoop时,执行JPS出现下面错误
[slave16][email protected]:/tmp/hsperfdata_hdfs# jps
Error occurred during initialization of VM
java.lang.Error: Properties init: Could not determine current working