Project

General

Profile

1
package eu.dnetlib.validator.service.impls.rules;
2

    
3
import java.util.List;
4
import java.util.Properties;
5

    
6
import eu.dnetlib.validator.engine.data.Rule;
7
import eu.dnetlib.validator.engine.data.RuleException;
8
import eu.dnetlib.validator.engine.execution.ValidationObject;
9

    
10
/**
11
 * Applies a chain of rules on a single Validation Object.
12
 * The behavior may be changedusing the {@link ChainRule#TYPE} property.
13
 * @author manos
14
 *
15
 * @param <T>
16
 */
17
public class ChainRule<T extends Rule> extends Rule {
18

    
19
	private static final long serialVersionUID = -6422953546341833949L;
20
	protected final List<T> rules;
21
	
22
	/**
23
	 * Permitted values:
24
	 * 'or': true if any of the rules is successful
25
	 * 'and': true if all the rules are successful
26
	 * 'horn': rule[0] => rule[1] /\ rule[2] /\ ... /\ rule[n-1]
27
	 */
28
	public static final String TYPE = "type";
29
	
30
	public ChainRule(Properties pros, int id, List<T> rules) {
31
		super(pros, id);
32
		this.rules = rules;
33
		log.debug("Chain Rule. Rules: "+rules);
34
	}
35

    
36
	@Override
37
	public boolean apply(ValidationObject obj) throws RuleException {
38
		if(rules.size() == 0)
39
			return true;
40
		String condition = pros.getProperty(TYPE).trim();
41
		if(condition.equalsIgnoreCase("AND")) {
42
			for(T rule : rules) {
43
				if(!rule.apply(obj))
44
					return false;
45
			}
46
			return true;
47
		} else if(condition.equalsIgnoreCase("OR")) {
48
			for(T rule : rules) {
49
				if(rule.apply(obj))
50
					return true;
51
			}
52
			return false;
53
		} else if(condition.equalsIgnoreCase("HORN")) {
54
			if(!rules.get(0).apply(obj))
55
				return true;
56
			for(int i=1; i<rules.size(); i++) {
57
				T rule = rules.get(i);
58
				if(!rule.apply(obj))
59
					return false;
60
			}
61
			return true;
62
		} else {
63
			return false;
64
		}
65
	}
66

    
67
}
(2-2/6)