Passed
Push — master ( c71db0...37262d )
by Alessandro
05:46 queued 14s
created

doRestore(FlawSolution)   C

Complexity

Conditions 10

Size

Total Lines 95
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 42
dl 0
loc 95
rs 5.9999
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.Resolver.doRestore(FlawSolution) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Set;
8
import java.util.concurrent.atomic.AtomicInteger;
9
10
import it.cnr.istc.pst.platinum.ai.framework.domain.component.Decision;
11
import it.cnr.istc.pst.platinum.ai.framework.domain.component.DomainComponent;
12
import it.cnr.istc.pst.platinum.ai.framework.domain.component.ex.DecisionPropagationException;
13
import it.cnr.istc.pst.platinum.ai.framework.domain.component.ex.FlawSolutionApplicationException;
14
import it.cnr.istc.pst.platinum.ai.framework.domain.component.ex.RelationPropagationException;
15
import it.cnr.istc.pst.platinum.ai.framework.microkernel.FrameworkObject;
16
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.inject.framework.DomainComponentPlaceholder;
17
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.inject.framework.ParameterFacadePlaceholder;
18
import it.cnr.istc.pst.platinum.ai.framework.microkernel.annotation.inject.framework.TemporalFacadePlaceholder;
19
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.ex.ConsistencyCheckException;
20
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.Flaw;
21
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.FlawSolution;
22
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.FlawType;
23
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.Relation;
24
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ex.UnsolvableFlawException;
25
import it.cnr.istc.pst.platinum.ai.framework.parameter.ParameterFacade;
26
import it.cnr.istc.pst.platinum.ai.framework.time.TemporalFacade;
27
28
/**
29
 * 
30
 * @author anacleto
31
 *
32
 * @param <T>
33
 */
34
public abstract class Resolver<T extends DomainComponent> extends FrameworkObject implements FlawManager 
35
{
36
	@TemporalFacadePlaceholder
37
	protected TemporalFacade tdb;
38
	
39
	@ParameterFacadePlaceholder
40
	protected ParameterFacade pdb;
41
	
42
	@DomainComponentPlaceholder
43
	protected T component;
44
	
45
	protected String label;
46
	protected FlawType[] flawTypes;
47
48
	// static information
49
	protected static final AtomicInteger FLAW_COUNTER = new AtomicInteger(0);
50
	
51
	
52
	/**
53
	 * 
54
	 * @param label
55
	 * @param flawTypes
56
	 */
57
	protected Resolver(String label, FlawType[] flawTypes) {
58
		super();
59
		this.label = label;
60
		this.flawTypes = flawTypes;
61
		
62
		// reset counter if necessary
63
		FLAW_COUNTER.set(0);
64
	}
65
	
66
	/**
67
	 * 
68
	 * @return
69
	 */
70
	public String getLabel() {
71
		return label;
72
	}
73
	
74
	/**
75
	 * 
76
	 * @return
77
	 */
78
	public FlawType[] getFlawTypes() {
79
		return this.flawTypes;
80
	}
81
	
82
	/**
83
	 * 
84
	 */
85
	@Override
86
	public synchronized List<Flaw> checkFlaws() {
87
		// get detected flaws
88
		return new ArrayList<>(this.doFindFlaws());
89
	}
90
	
91
	/**
92
	 * 
93
	 */
94
	@Override
95
	public synchronized List<Flaw> findFlaws() 
96
			throws UnsolvableFlawException
97
	{
98
		// list of flaws on the related component
99
		List<Flaw> flaws = new ArrayList<>();
100
		// find flaws on component
101
		for (Flaw flaw :  this.doFindFlaws()) 
102
		{
103
			// compute possible solutions
104
			this.doComputeFlawSolutions(flaw);
105
			// add flaw
106
			flaws.add(flaw);
107
		}
108
		
109
		// get detected flaws
110
		return flaws;
111
	}
112
	
113
	/**
114
	 * 
115
	 */
116
	@Override
117
	public final synchronized void apply(FlawSolution solution) 
118
			throws FlawSolutionApplicationException 
119
	{
120
		// check flaw type
121
		if (!Arrays.asList(this.flawTypes).contains(solution.getFlaw().getType())) {
122
			throw new FlawSolutionApplicationException("Impossible to apply solution for flaws of type type " + solution.getFlaw().getType());
123
		}
124
		
125
		// apply flaw solution
126
		this.doApply(solution);
127
	}
128
	
129
	/**
130
	 * 
131
	 */
132
	@Override
133
	public final synchronized void restore(FlawSolution solution) 
134
			throws Exception 
135
	{
136
		// check flaw type
137
		if (!Arrays.asList(this.flawTypes).contains(solution.getFlaw().getType())) {
138
			throw new FlawSolutionApplicationException("Impossible to restore solution for flaws of type type " + solution.getFlaw().getType());
139
		}
140
		
141
		// apply flaw solution
142
		this.doRestore(solution);
143
	}
144
	
145
	/**
146
	 * 
147
	 */
148
	@Override
149
	public final synchronized void retract(FlawSolution solution) 
150
	{
151
		// check flaw type
152
		if (!Arrays.asList(this.flawTypes).contains(solution.getFlaw().getType())) {
153
			throw new RuntimeException("Impossible to retract solution for flaws of type " + solution.getFlaw().getType());
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
154
		}
155
		
156
		// retract solution
157
		this.doRetract(solution);
158
	}
159
	
160
	/**
161
	 * 
162
	 * @param solution
163
	 */
164
	protected void doRetract(FlawSolution solution) 
165
	{
166
		// get the list of activated relations
167
		for (Relation relation : solution.getActivatedRelations()) {
168
			// get reference component
169
			DomainComponent refComp = relation.getReference().getComponent();
170
			// deactivate relation
171
			refComp.deactivate(relation);
172
		}
173
		
174
		// get the list of created relations
175
		for (Relation relation : solution.getCreatedRelations()) {
176
			// get reference component
177
			DomainComponent refComp = relation.getReference().getComponent();
178
			// remove relation from the data structure
179
			refComp.delete(relation);
180
		}
181
		
182
		// get the list of activated decisions
183
		for (Decision decision : solution.getActivatedDecisions()) {
184
			// get decision component
185
			DomainComponent dComp = decision.getComponent();
186
			// deactivate decision
187
			dComp.deactivate(decision);
188
		}
189
		
190
		// get the list of created decisions
191
		for (Decision decision : solution.getCreatedDecisions()) {
192
			// get decision component
193
			DomainComponent dComp = decision.getComponent();
194
			// delete decision from pending "list" 
195
			dComp.free(decision);
196
		}
197
	}
198
	
199
//	/**
200
//	 * 
201
//	 * @param constraint
202
//	 * @throws ConstraintPropagationException
203
//	 */
204
//	protected void doPropagetConstraint(Constraint constraint) 
205
//			throws ConstraintPropagationException 
206
//	{
207
//		// check constraint category
208
//		switch (constraint.getCategory()) 
209
//		{
210
//			// temporal constraint
211
//			case  TEMPORAL_CONSTRAINT : {
212
//				// cast constraint
213
//				TemporalConstraint cons = (TemporalConstraint) constraint;
214
//				// propagate temporal constraint
215
//				this.tdb.propagate(cons);
216
//			}
217
//			break;
218
//			
219
//			// parameter constraint
220
//			case PARAMETER_CONSTRAINT : {
221
//				// cast constraint
222
//				ParameterConstraint  cons = (ParameterConstraint) constraint;
223
//				// propagate parameter constraint
224
//				this.pdb.propagate(cons);
225
//			}
226
//			break;
227
//		}
228
//	}
229
	
230
//	/**
231
//	 * 
232
//	 * @param constraint
233
//	 */
234
//	protected void doRetractConstraint(Constraint constraint) {
235
//		// check constraint category
236
//		switch (constraint.getCategory()) {
237
//		
238
//			// temporal constraint
239
//			case  TEMPORAL_CONSTRAINT : {
240
//				
241
//				// cast constraint
242
//				TemporalConstraint cons = (TemporalConstraint) constraint;
243
//				// retract temporal constraint
244
//				this.tdb.retract(cons);
245
//			}
246
//			break;
247
//			
248
//			// parameter constraint
249
//			case PARAMETER_CONSTRAINT : {
250
//				
251
//				// cast constraint
252
//				ParameterConstraint cons = (ParameterConstraint) constraint;
253
//				// retract parameter constraint
254
//				this.pdb.retract(cons);
255
//			}
256
//			break;
257
//		}
258
//	}
259
	
260
	/**
261
	 * 
262
	 * @param solution
263
	 * @throws Exception
264
	 */
265
	protected abstract void doApply(FlawSolution solution) 
266
			throws FlawSolutionApplicationException;
267
	
268
	/**
269
	 * 
270
	 * @param solution
271
	 * @throws RelationPropagationException
272
	 * @throws DecisionPropagationException
273
	 */
274
	protected void doRestore(FlawSolution solution) 
275
			throws RelationPropagationException, DecisionPropagationException 
276
	{
277
		// list of restored decisions
278
		Set<Decision> dRestored = new HashSet<>();
279
		// list of activated decisions
280
		Set<Decision> dActivated = new HashSet<>();
281
		// list of restored relations
282
		Set<Relation> rRestored = new HashSet<>();
283
		// list of activated relations
284
		Set<Relation> rActivated = new HashSet<>();
285
		
286
		try
287
		{
288
			// get the list of created decisions
289
			for (Decision decision : solution.getCreatedDecisions()) {
290
				// get decision component
291
				DomainComponent dComp = decision.getComponent();
292
				// restore decision SILENT -> PENDING
293
				dComp.restore(decision);
294
				// add 
295
				dRestored.add(decision);
296
			}
297
			
298
			
299
			// check activated decisions
300
			for (Decision decision : solution.getActivatedDecisions()) {
301
				// get decision component
302
				DomainComponent dComp = decision.getComponent();
303
				// activate decision PENDING -> ACTIVE
304
				dComp.activate(decision);
305
				// add
306
				dActivated.add(decision);
307
			}
308
			
309
			// get the list of created relations
310
			for (Relation relation : solution.getCreatedRelations()) {
311
				// get reference component
312
				DomainComponent refComp = relation.getReference().getComponent();
313
				// restore relation
314
				refComp.restore(relation);
315
				// add
316
				rRestored.add(relation);
317
			}
318
319
			// check activated relations
320
			for (Relation relation : solution.getActivatedRelations()) {
321
				// get reference component
322
				DomainComponent refComp = relation.getReference().getComponent();
323
				// activate relation
324
				refComp.activate(relation);
325
				// add relation to committed list
326
				rActivated.add(relation);
327
			}
328
			
329
			// check consistency
330
			this.tdb.verify();
331
			this.pdb.verify();
332
		}
333
		catch (DecisionPropagationException | ConsistencyCheckException ex) 
334
		{
335
			// deactivate activated relations
336
			for (Relation rel : rActivated) {
337
				// get reference component
338
				DomainComponent refComp = rel.getReference().getComponent();
339
				// deactivate relation
340
				refComp.deactivate(rel);
341
			}
342
			
343
			// remove restored relations
344
			for (Relation rel : rRestored) {
345
				// get reference component
346
				DomainComponent refComp = rel.getReference().getComponent();
347
				// deactivate relation
348
				refComp.delete(rel);
349
			}
350
			
351
			// deactivate decisions
352
			for (Decision dec : dActivated) {
353
				// get decision component
354
				DomainComponent decComp = dec.getComponent();
355
				// deactivate decision
356
				decComp.deactivate(dec);
357
			}
358
			
359
			// remove restored decisions
360
			for (Decision dec: dRestored) {
361
				// get decision component
362
				DomainComponent decComp = dec.getComponent();
363
				// free decision
364
				decComp.free(dec);
365
			}
366
			
367
			// throw exception
368
			throw new DecisionPropagationException(ex.getMessage());
369
		}
370
	}
371
	
372
	/**
373
	 * 
374
	 * @return
375
	 */
376
	protected abstract List<Flaw> doFindFlaws();
377
	
378
	/**
379
	 * 
380
	 * @param flaw
381
	 * @throws UnsolvableFlawException
382
	 */
383
	protected abstract void doComputeFlawSolutions(Flaw flaw) 
384
			throws UnsolvableFlawException;
385
}
386