Passed
Push — master ( e49ba9...b59a28 )
by Alessandro
06:09
created

FailFirstFlawInspector()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package it.cnr.istc.pst.platinum.ai.deliberative.heuristic.pipeline;
2
3
import java.util.ArrayList;
4
import java.util.Collection;
5
import java.util.Collections;
6
import java.util.Comparator;
7
import java.util.HashSet;
8
import java.util.List;
9
import java.util.Set;
10
11
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.Flaw;
12
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ex.UnsolvableFlawException;
13
14
/**
15
 * 
16
 * @author alessandro
17
 *
18
 */
19
public class FailFirstFlawInspector extends FlawInspector implements Comparator<Flaw> {
20
	
21
	/**
22
	 * 
23
	 */
24
	protected FailFirstFlawInspector() {
25
		super("FailFirstFlawInspector");
26
	}
27
	
28
	/**
29
	 * 
30
	 */
31
	@Override
32
	public Set<Flaw> detectFlaws() 
33
			throws UnsolvableFlawException {
34
		
35
		// set of filtered list
36
		Set<Flaw> set = new HashSet<>();
37
		// filtered set
38
		List<Flaw> list = new ArrayList<>(this.pdb.detectFlaws());
39
		// check list 
40
		if (!list.isEmpty()) {
41
			
42
			// sort flaws according to their degree (i.e. number of available solutions
43
			Collections.sort(list, this);
44
			// get the first element of the list
45
			Flaw flaw = list.remove(0);
46
			// add flaw to the result set
47
			set.add(flaw);
48
		}
49
				
50
		// get flaws
51
		return set;
52
	}
53
	
54
	/**
55
	 * 
56
	 */
57
	@Override
58
	public Set<Flaw> check() {
59
		// set of filtered list
60
		return new HashSet<>(this.pdb.checkFlaws());
61
	}
62
	
63
	/**
64
	 * 
65
	 */
66
	@Override
67
	public Set<Flaw> filter(Collection<Flaw> flaws) {
68
		
69
		// filtered set
70
		Set<Flaw> set = new HashSet<>();
71
		// list of flaws
72
		List<Flaw> list = new ArrayList<>(flaws);
73
		// check flaws
74
		if (!list.isEmpty()) {
75
			
76
			// sort flaws according to their degree (i.e. number of available solutions
77
			Collections.sort(list, this);
78
			// get the first element of the list
79
			Flaw flaw = list.remove(0);
80
			// add the hardest flaw to solve i.e., the flaw with fewest solutions
81
			set.add(flaw);
82
		}
83
		
84
		// get filtered set
85
		return set;
86
	}
87
	
88
	/**
89
	 * 
90
	 */
91
	@Override
92
	public int compare(Flaw o1, Flaw o2) {
93
		// compare the number of available solutions
94
		return o1.getSolutions().size() < o2.getSolutions().size() ? -1 : 
95
			o1.getSolutions().size() > o2.getSolutions().size() ? 1 : 0;
96
	}
97
}
98