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

HierarchicalPlanFlawInspector()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 2
rs 10
c 1
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.List;
6
import java.util.Set;
7
8
import it.cnr.istc.pst.platinum.ai.framework.domain.component.DomainComponent;
9
import it.cnr.istc.pst.platinum.ai.framework.domain.knowledge.DomainKnowledge;
10
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.lifecycle.PostConstruct;
11
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.Flaw;
12
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.FlawType;
13
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ex.UnsolvableFlawException;
14
import it.cnr.istc.pst.platinum.ai.framework.utils.properties.FilePropertyReader;
15
16
/**
17
 * 
18
 * @author alessandro
19
 *
20
 */
21
public class HierarchicalPlanFlawInspector extends FlawInspector {
22
	
23
	private FlawType[] preferences;
24
25
	/**
26
	 * 
27
	 */
28
	protected HierarchicalPlanFlawInspector() {
29
		super("HierarchicalPlanFlawInspector");
30
	}
31
	
32
	/**
33
	 * 
34
	 */
35 View Code Duplication
	@PostConstruct 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
	protected void init() {
37
		
38
		// get deliberative property file
39
		FilePropertyReader properties = new FilePropertyReader(
40
				FRAMEWORK_HOME + FilePropertyReader.DEFAULT_DELIBERATIVE_PROPERTY);
41
		// get preference property
42
		String[] prefs = properties.getProperty("preferences").trim().split(",");
43
		
44
		// set preferences
45
		this.preferences = new FlawType[prefs.length];
46
		for (int i = 0; i < prefs.length; i++) {
47
			// set preference
48
			this.preferences[i] = FlawType.getFlawTypeFromLabel(prefs[i]);
49
		}
50
	}
51
	
52
	/**
53
	 * 
54
	 */
55 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
	public Set<Flaw> detectFlaws() 
57
			throws UnsolvableFlawException {
58
		
59
		// filtered set
60
		Set<Flaw> set = new HashSet<>();
61
		// get domain knowledge
62
		DomainKnowledge knowledge = this.pdb.getDomainKnowledge();
63
		// get the hierarchy
64
		List<DomainComponent>[] hierarchy = knowledge.getDomainHierarchy();
65
		// check domain hierarchy first
66
		for (int index = 0; index < hierarchy.length && set.isEmpty(); index++) {
67
			// check specified flaw preferences
68
			for (int pndex = 0; pndex < this.preferences.length && set.isEmpty(); pndex++) {
69
				// get current flaw type 
70
				FlawType type = this.preferences[pndex];
71
				
72
				// get components at the current level of the hierarchy
73
				for (DomainComponent component : hierarchy[index]) {
74
					// detect flaws on the current component
75
					set.addAll(component.detectFlaws(type));
76
				}
77
			}
78
		}
79
		
80
		
81
		// get detected flaws
82
		return set;
83
	}
84
	
85
	/**
86
	 * 
87
	 */
88 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
89
	public Set<Flaw> check() {
90
		
91
		// filtered set
92
		Set<Flaw> set = new HashSet<>();
93
		// get domain knowledge
94
		DomainKnowledge knowledge = this.pdb.getDomainKnowledge();
95
		// get the hierarchy
96
		List<DomainComponent>[] hierarchy = knowledge.getDomainHierarchy();
97
		// check domain hierarchy first
98
		for (int index = 0; index < hierarchy.length && set.isEmpty(); index++) {
99
			// check specified flaw preferences
100
			for (int pndex = 0; pndex < this.preferences.length && set.isEmpty(); pndex++) {
101
				// get current flaw type 
102
				FlawType type = this.preferences[pndex];
103
				
104
				// get components at the current level of the hierarchy
105
				for (DomainComponent component : hierarchy[index]) {
106
					// check flaws on the current component
107
					set.addAll(component.checkFlaws(new FlawType[] {
108
							type
109
					}));
110
				}
111
			}
112
		}
113
		
114
		// get flaws
115
		return set;
116
	}
117
	
118
	/**
119
	 * 
120
	 */
121
	@Override
122
	public Set<Flaw> filter(Collection<Flaw> flaws) {
123
		
124
		// filtered set
125
		Set<Flaw> set = new HashSet<>();
126
		// get domain knowledge
127
		DomainKnowledge knowledge = this.pdb.getDomainKnowledge();
128
		// get the hierarchy
129
		List<DomainComponent>[] hierarchy = knowledge.getDomainHierarchy();
130
		// get component flaws first
131
		Set<Flaw> componentFlaws = new HashSet<>();
132
		// check domain hierarchy first
133
		for (int index = 0; index < hierarchy.length && componentFlaws.isEmpty(); index++) {
134
			
135
			// extract flaws of equivalent components
136
			for (DomainComponent component : hierarchy[index]) {
137
				// check flaws
138
				for (Flaw flaw : flaws ) {
139
					if (flaw.getComponent().equals(component)) {
140
						// add flaw
141
						componentFlaws.add(flaw);
142
					}
143
				}
144
				
145
			}
146
		}
147
		
148
		
149
		// look for flaw of a given type
150
		for (int index = 0; index < this.preferences.length && set.isEmpty(); index++) {
151
			
152
			// get current type
153
			FlawType type = this.preferences[index];
154
			// check component flaws
155
			for (Flaw flaw : componentFlaws) {
156
				// check type
157
				if (flaw.getType().equals(type)) {
158
					// add flaw
159
					set.add(flaw);
160
				}
161
			}
162
		}
163
		
164
		// get filtered set
165
		return set;
166
	}
167
}
168