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

check()   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
package it.cnr.istc.pst.platinum.ai.deliberative.heuristic.pipeline;
2
3
import java.util.Collection;
4
import java.util.HashSet;
5
import java.util.Iterator;
6
import java.util.Set;
7
8
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.lifecycle.PostConstruct;
9
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.Flaw;
10
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.FlawType;
11
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ex.UnsolvableFlawException;
12
import it.cnr.istc.pst.platinum.ai.framework.utils.properties.FilePropertyReader;
13
14
/**
15
 * 
16
 * @author alessandro
17
 *
18
 */
19
public class PreferenceFlawInspector extends FlawInspector {
20
	
21
	private FlawType[] preferences;
22
23
	/**
24
	 * 
25
	 */
26
	protected PreferenceFlawInspector() {
27
		super("PreferenceFlawInspector");
28
	}
29
	
30
	/**
31
	 * 
32
	 */
33 View Code Duplication
	@PostConstruct 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
34
	protected void init() {
35
		
36
		// get deliberative property file
37
		FilePropertyReader properties = new FilePropertyReader(
38
				FRAMEWORK_HOME + FilePropertyReader.DEFAULT_DELIBERATIVE_PROPERTY);
39
		// get preference property
40
		String[] prefs = properties.getProperty("preferences").trim().split(",");
41
		
42
		// set preferences
43
		this.preferences = new FlawType[prefs.length];
44
		for (int i = 0; i < prefs.length; i++) {
45
			// set preference
46
			this.preferences[i] = FlawType.getFlawTypeFromLabel(prefs[i]);
47
		}
48
	}
49
	
50
	/**
51
	 * 
52
	 */
53
	@Override
54
	public Set<Flaw> detectFlaws() 
55
			throws UnsolvableFlawException {
56
		
57
		// filtered set
58
		Set<Flaw> set = new HashSet<>();
59
		// look for flaws of a given type
60
		for (int index = 0; index < this.preferences.length && set.isEmpty(); index++) {
61
			
62
			// get type of flaw
63
			FlawType type = this.preferences[index];
64
			// detect flaws
65
			set = new HashSet<>(this.pdb.detectFlaws(type));
66
		}
67
		
68
		// get flaws
69
		return set;
70
	}
71
	
72
	/**
73
	 * 
74
	 */
75
	@Override
76
	public Set<Flaw> check() {
77
		
78
		// filtered set
79
		Set<Flaw> set = new HashSet<>();
80
		// look for flaws of a given type
81
		for (int index = 0; index < this.preferences.length && set.isEmpty(); index++) {
82
			// get type of flaw
83
			FlawType type = this.preferences[index];
84
			// detect flaws
85
			set = new HashSet<>(this.pdb.checkFlaws(new FlawType[] {
86
					type
87
					}));
88
		}
89
		
90
		// get flaws
91
		return set;
92
	}
93
	
94
	/**
95
	 * 
96
	 */
97
	@Override
98
	public Set<Flaw> filter(Collection<Flaw> flaws) {
99
		
100
		// filtered set
101
		Set<Flaw> set = new HashSet<>();
102
		// look for flaw of a given type
103
		for (int index = 0; index < this.preferences.length && set.isEmpty(); index++) 
104
		{
105
			// get current type
106
			FlawType type = this.preferences[index];
107
			// check flaws
108
			Iterator<Flaw> it = flaws.iterator();
109
			while(it.hasNext()) 
110
			{
111
				// get next flaw
112
				Flaw flaw = it.next();
113
				// check 
114
				if (flaw.getType().equals(type)) {
115
					set.add(flaw);
116
				}
117
			}
118
		}
119
		
120
		// get filtered set
121
		return set;
122
	}
123
}
124