Passed
Push — master ( ff18e0...a286a0 )
by Alessandro
05:17
created

clear()   A

Complexity

Conditions 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 1
c 0
b 0
f 0
cc 1
rs 10
1
package it.cnr.istc.pst.platinum.ai.deliberative.heuristic;
2
3
import java.lang.reflect.Constructor;
4
import java.util.Set;
5
6
import it.cnr.istc.pst.platinum.ai.deliberative.heuristic.pipeline.FlawInspector;
7
import it.cnr.istc.pst.platinum.ai.framework.domain.component.PlanDataBase;
8
import it.cnr.istc.pst.platinum.ai.framework.microkernel.FrameworkObject;
9
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.inject.framework.PlanDataBasePlaceholder;
10
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.lifecycle.PostConstruct;
11
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.ex.NoFlawFoundException;
12
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.Flaw;
13
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ex.UnsolvableFlawException;
14
import it.cnr.istc.pst.platinum.ai.framework.utils.reflection.FrameworkReflectionUtils;
15
16
/**
17
 * 
18
 * @author anacleto
19
 *
20
 */
21
public abstract class FlawSelectionHeuristic extends FrameworkObject 
22
{
23
	@PlanDataBasePlaceholder
24
	protected PlanDataBase pdb;
25
	
26
	private String label;
27
	
28
	/**
29
	 * 
30
	 * @param label
31
	 */
32
	protected FlawSelectionHeuristic(String label) {
33
		super();
34
		this.label = label;
35
	}
36
	
37
	/**
38
	 * 
39
	 * @return
40
	 */
41
	public String getLabel() {
42
		return label;
43
	}
44
45
	/**
46
	 * Return a set of equivalent flaws to solve for plan refinement. Each solution of a 
47
	 * flaw determines a branch in the resulting search space of the planner.
48
	 * 
49
	 * @return
50
	 * @throws UnsolvableFlawException
51
	 * @throws NoFlawFoundException
52
	 */
53
	public abstract Set<Flaw> choose() 
54
			throws UnsolvableFlawException, NoFlawFoundException; 
55
	
56
	/**
57
	 * Check existing flaws without computing possible solutions
58
	 * 
59
	 * @return
60
	 */
61
	public abstract Set<Flaw> check();
62
	
63
	/**
64
	 * Filter a set of already computed flaws according to a certain criterion
65
	 * 
66
	 * @param flaws
67
	 * @return
68
	 * @throws NoFlawFoundException
69
	 */
70
	public abstract Set<Flaw> filter(Set<Flaw> flaws) 
71
			throws NoFlawFoundException;
72
	
73
	/**
74
	 * 
75
	 */
76
	@Override
77
	public String toString() {
78
		return "{ \"label\": \"" + this.label + "\" }";
79
	}
80
	
81
	/**
82
	 * 
83
	 * @param className
84
	 * @return
85
	 */
86
	protected <T extends FlawInspector> T doCreateFlawFilter(String className) 
87
	{
88
		// create filter instance
89
		T filter = this.createFilter(className);
90
		try
91
		{
92
			// inject plan database reference
93
			FrameworkReflectionUtils.doInjectReferenceThroughAnnotation(filter, PlanDataBasePlaceholder.class, this.pdb);
94
		}
95
		catch (Exception ex) { 
96
			throw new RuntimeException("Error while injecting plan database reference into flaw filter:\n- filter class: " + className +"\n- message: " + ex.getMessage() + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
97
		}
98
		
99
		try
100
		{
101
			// finalize filter construction
102
			FrameworkReflectionUtils.doInvokeMethodTaggedWithAnnotation(filter, PostConstruct.class);
103
		}
104
		catch (Exception ex) {
105
			throw new RuntimeException("Error while calling post construct method on filter from class: " + className + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
106
		}
107
		
108
		// get created filter class
109
		return filter;
110
	}
111
	
112
	
113
	/**
114
	 * 
115
	 * @param className
116
	 * @return
117
	 */
118
	private <T extends FlawInspector> T createFilter(String className) {
119
		// flaw filter
120
		T filter = null;
121
		try
122
		{
123
			// get class
124
			Class<?> clazz = Class.forName(className);
125
			// get constructor
126
			@SuppressWarnings("unchecked")
127
			Constructor<T> c = (Constructor<T>) clazz.getDeclaredConstructor();
128
			// set accessibility
129
			c.setAccessible(true);
130
			// create instance
131
			filter = c.newInstance();
132
		}
133
		catch (Exception ex) {
134
			throw new RuntimeException("Error while creating flaw filter from class: " + className + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
135
		}
136
		
137
		// get created filter
138
		return filter;
139
	}
140
141
	/**
142
	 * 
143
	 */
144
	public void clear() {
145
		// nothing to do
146
	}
147
}
148
149
150
151