it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.plan.PlanRefinementResolver   F
last analyzed

Complexity

Total Complexity 149

Size/Duplication

Total Lines 1557
Duplicated Lines 82.59 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 647
c 3
b 1
f 0
dl 1286
loc 1557
rs 1.953
wmc 149

15 Methods

Rating   Name   Duplication   Size   Complexity  
A doRestore(FlawSolution) 0 14 2
A doApply(FlawSolution) 0 22 3
A load() 0 10 1
A PlanRefinementResolver() 0 6 1
A doRetract(FlawSolution) 0 13 2
A doComputeFlawSolutions(Flaw) 24 24 4
A doFindFlaws() 27 27 4
A doComputeUnificationSolutions(Goal) 35 35 5
F doApplyExpansion(GoalExpansion) 185 244 22
F doRestoreUnification(GoalUnification) 265 265 27
C doRetractUnification(GoalUnification) 97 97 11
F doApplyUnification(GoalUnification) 292 292 27
F isPredicateUnificationFeasible(Decision,Decision) 253 253 22
D isTemporalUnificationFeasible(Decision,Decision) 85 85 12
B doComputeExpansionSolutions(Goal) 23 64 6

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.plan.PlanRefinementResolver 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.plan;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10
11
import it.cnr.istc.pst.platinum.ai.framework.domain.component.Decision;
12
import it.cnr.istc.pst.platinum.ai.framework.domain.component.DomainComponent;
13
import it.cnr.istc.pst.platinum.ai.framework.domain.component.ex.DecisionPropagationException;
14
import it.cnr.istc.pst.platinum.ai.framework.domain.component.ex.FlawSolutionApplicationException;
15
import it.cnr.istc.pst.platinum.ai.framework.domain.component.ex.RelationPropagationException;
16
import it.cnr.istc.pst.platinum.ai.framework.domain.component.pdb.ParameterSynchronizationConstraint;
17
import it.cnr.istc.pst.platinum.ai.framework.domain.component.pdb.SynchronizationConstraint;
18
import it.cnr.istc.pst.platinum.ai.framework.domain.component.pdb.SynchronizationRule;
19
import it.cnr.istc.pst.platinum.ai.framework.domain.component.pdb.TemporalSynchronizationConstraint;
20
import it.cnr.istc.pst.platinum.ai.framework.domain.component.pdb.TokenVariable;
21
import it.cnr.istc.pst.platinum.ai.framework.microkernel.ConstraintCategory;
22
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.ex.ConsistencyCheckException;
23
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.Flaw;
24
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.flaw.FlawSolution;
25
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.Relation;
26
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.parameter.BindParameterRelation;
27
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.parameter.EqualParameterRelation;
28
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.parameter.NotEqualParameterRelation;
29
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.parameter.ParameterRelation;
30
import it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.relations.temporal.TemporalRelation;
31
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.Resolver;
32
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ResolverType;
33
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.ex.UnsolvableFlawException;
34
import it.cnr.istc.pst.platinum.ai.framework.microkernel.resolver.plan.GoalJustification.JustificationType;
35
import it.cnr.istc.pst.platinum.ai.framework.utils.properties.FilePropertyReader;
36
37
/**
38
 * 
39
 * @author alessandro
40
 *
41
 */
42
public class PlanRefinementResolver extends Resolver<DomainComponent> {
43
	
44
	private boolean load;
45
	private double expansionCost;
46
	private double unificationCost;
47
	
48
	/**
49
	 * 
50
	 */
51
	protected PlanRefinementResolver() {
52
		super(ResolverType.PLAN_REFINEMENT.getLabel(), 
53
				ResolverType.PLAN_REFINEMENT.getFlawTypes());
54
		
55
		// flag to load parameters when it is necessary
56
		this.load = false;
57
	}
58
	
59
	/**
60
	 * 
61
	 */
62
	private void load() {
63
		// get deliberative property file
64
		FilePropertyReader properties = new FilePropertyReader(
65
				FRAMEWORK_HOME + FilePropertyReader.DEFAULT_DELIBERATIVE_PROPERTY);
66
		
67
		// read weights
68
		this.expansionCost = Double.parseDouble(properties.getProperty("expansion-cost"));
69
		this.unificationCost = Double.parseDouble(properties.getProperty("unification-cost"));
70
		// set load flag
71
		this.load = true;
72
	}
73
	
74
	/**
75
	 * 
76
	 */
77 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
	protected List<Flaw> doFindFlaws() {
79
		
80
		// check load parameters
81
		if (!this.load) {
82
			this.load();
83
		}
84
85
		// list of goals
86
		List<Flaw> flaws = new ArrayList<>();
87
		// check pending decisions
88
		for (Decision decision : this.component.getPendingDecisions()) {
89
			
90
			// add sub-goal
91
			Goal goal = new Goal(FLAW_COUNTER.getAndIncrement(), this.component, decision);
92
			// check if external component
93
			if (decision.getComponent().isExternal()) {
94
				// set mandatory unification
95
				goal.setMandatoryUnification();
96
			}
97
			
98
			// add goal to flaws
99
			flaws.add(goal);
100
		}
101
		
102
		// get flaws
103
		return flaws;
104
	}
105
	
106
	/**
107
	 * 
108
	 */
109 View Code Duplication
	@Override
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
110
	protected void doComputeFlawSolutions(Flaw flaw) 
111
			throws UnsolvableFlawException {
112
		
113
		// get goal
114
		Goal goal = (Goal) flaw;
115
		
116
		// check solving information
117
		if (!goal.isMandatoryExpansion()) {
118
			// compute unification solutions
119
			this.doComputeUnificationSolutions(goal);
120
		}
121
		
122
		// check solving information
123
		if (!goal.isMandatoryUnification()) {
124
			// compute expansion solutions
125
			this.doComputeExpansionSolutions(goal);
126
		}
127
		 
128
		// check if solvable
129
		if (!goal.isSolvable()) {
130
			// simply throw exception
131
			throw new UnsolvableFlawException("Unsolvable flaw found on component " + this.component.getName() + ":"
132
					+ "\n" + flaw + "\n");
133
		}
134
	}
135
	
136
	
137
	/**
138
	 * 
139
	 * @param solution
140
	 * @throws Exception
141
	 */
142
	@Override
143
	protected void doApply(FlawSolution solution) 
144
			throws FlawSolutionApplicationException 
145
	{
146
		// get goal justification
147
		GoalJustification just = (GoalJustification) solution;
148
		// check type 
149
		switch (just.getJustificationType()) 
150
		{
151
			// expansion step
152
			case EXPANSION : {
153
				// apply solution
154
				this.doApplyExpansion((GoalExpansion) just);
155
			}
156
			break;
157
			
158
			// unification step
159
			case UNIFICATION : {
160
				// apply solution
161
				this.doApplyUnification((GoalUnification) just);
162
			}
163
			break;
164
		}
165
	}
166
	
167
	/**
168
	 * 
169
	 */
170
	@Override
171
	protected void doRestore(FlawSolution solution) 
172
			throws DecisionPropagationException, RelationPropagationException 
173
	{
174
		// get goal justification
175
		GoalJustification just = (GoalJustification) solution;
176
		// check if unification 
177
		if (just.getJustificationType().equals(JustificationType.UNIFICATION)) {
178
			// restore unification
179
			this.doRestoreUnification((GoalUnification) just);
180
		}
181
		else {
182
			// "standard" way of restoring a flaw solution
183
			super.doRestore(solution);
184
		}
185
	}
186
	
187
	/**
188
	 * 
189
	 * @param solution
190
	 * @throws RelationPropagationException
191
	 */
192 View Code Duplication
	private void doRestoreUnification(GoalUnification solution) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
193
			throws RelationPropagationException {
194
		
195
		// restore relation translation
196
		for (Relation rel : solution.getTranslatedReferenceGoalRelations()) {
197
			
198
			// check relation category
199
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
200
				// replace reference
201
				rel.setReference(solution.getUnificationDecision());
202
			}
203
			
204
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
205
				
206
				// check relation type
207
				switch (rel.getType()) {
208
				
209
					// bind parameter
210
					case BIND_PARAMETER: 
211
					{
212
						// the goal can be only the reference of the relation
213
						ParameterRelation pRel = (ParameterRelation) rel;
214
						
215
						// get relation reference parameter label
216
						String refParamLabel = pRel.getReferenceParameterLabel();
217
						// get label index
218
						int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
219
						// get unification decision parameter label
220
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
221
222
						// update reference decision 
223
						pRel.setReference(solution.getUnificationDecision());
224
						// update reference label of the relation 
225
						pRel.setReferenceParameterLabel(label);
226
					}
227
					break;
228
					
229
					case EQUAL_PARAMETER : 
230
					{
231
						// get parameter relation
232
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
233
						// get relation reference parameter label
234
						String refParamLabel = eqRel.getReferenceParameterLabel();
235
						// get label index
236
						int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
237
						// get unification decision parameter label
238
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
239
240
						// update reference decision 
241
						eqRel.setReference(solution.getUnificationDecision());
242
						// update reference label of the relation 
243
						eqRel.setReferenceParameterLabel(label);
244
					}
245
					break;
246
					
247
					case NOT_EQUAL_PARAMETER : 
248
					{
249
						// get parameter relation
250
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
251
						// get relation reference parameter label
252
						String refParamLabel = neqRel.getReferenceParameterLabel();
253
						// get label index
254
						int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
255
						// get unification decision parameter label
256
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
257
258
						// update reference decision 
259
						neqRel.setReference(solution.getUnificationDecision());
260
						// update reference label of the relation 
261
						neqRel.setReferenceParameterLabel(label);
262
					}
263
					break;
264
					
265
					
266
					default:
267
						// unknown parameter relation
268
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
269
				}
270
			}
271
		}
272
		
273
		
274
		// restore relation translation
275
		for (Relation rel : solution.getTranslatedTargetGoalRelations()) {
276
			
277
			// check relation category
278
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
279
				// replace reference
280
				rel.setTarget(solution.getUnificationDecision());
281
			}
282
			
283
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
284
				
285
				// check relation type
286
				switch (rel.getType()) {
287
				
288
					case EQUAL_PARAMETER : 
289
					{
290
						// get parameter relation
291
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
292
						// get relation reference parameter label
293
						String refParamLabel = eqRel.getTargetParameterLabel();
294
						// get label index
295
						int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
296
						// get unification decision parameter label
297
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
298
299
						// update reference decision 
300
						eqRel.setTarget(solution.getUnificationDecision());
301
						// update reference label of the relation 
302
						eqRel.setTargetParameterLabel(label);
303
					}
304
					break;
305
					
306
					case NOT_EQUAL_PARAMETER : 
307
					{
308
						// get parameter relation
309
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
310
						// get relation reference parameter label
311
						String refParamLabel = neqRel.getTargetParameterLabel();
312
						// get label index
313
						int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
314
						// get unification decision parameter label
315
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
316
317
						// update reference decision 
318
						neqRel.setTarget(solution.getUnificationDecision());
319
						// update reference label of the relation 
320
						neqRel.setTargetParameterLabel(label);
321
					}
322
					break;
323
					
324
					
325
					default:
326
						// unknown parameter relation
327
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
328
				}
329
			}
330
		}
331
332
		
333
		
334
		// list of committed parameter constraints
335
		Set<Relation> committed = new HashSet<>();
336
		try	
337
		{
338
			// get goal component
339
			DomainComponent gComp = solution.getGoalDecision().getComponent();
340
			// remove original goal: PENDING -> SILENT
341
			gComp.free(solution.getGoalDecision());
342
			
343
			// activate translated relations
344
			for (Relation rel : solution.getActivatedRelations()) 
345
			{
346
				// check if can be activated
347
				if (rel.getReference().getComponent().activate(rel)) {
348
					// add relation to the committed list
349
					committed.add(rel);
350
				}
351
			}
352
			
353
			// check consistency
354
//			this.tdb.verify();
355
			this.pdb.verify();
356
			
357
		} catch (RelationPropagationException | ConsistencyCheckException ex) {
358
			
359
			// get goal component
360
			DomainComponent gComp = solution.getGoalDecision().getComponent();
361
			// restore goal: SILENT -> PENDING
362
			gComp.restore(solution.getGoalDecision());
363
			
364
			// deactivate committed relations
365
			for (Relation rel : committed) {
366
				// get reference component
367
				DomainComponent refComp = rel.getReference().getComponent();
368
				refComp.deactivate(rel);
369
			}
370
			
371
			// translated back relations
372
			for (Relation rel : solution.getTranslatedReferenceGoalRelations()) {
373
				// check category
374
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
375
					// get parameter relation
376
					ParameterRelation pRel = (ParameterRelation) rel;
377
					
378
					// get relation reference parameter label
379
					String refParamLabel = pRel.getReferenceParameterLabel();
380
					// get label index
381
					int pIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
382
					// get goal decision parameter label
383
					String label = solution.getGoalDecision().getParameterLabelByIndex(pIndex);
384
					
385
					// update relation
386
					pRel.setReference(solution.getGoalDecision());
387
					pRel.setReferenceParameterLabel(label);
388
				}
389
				
390
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
391
					// update relation
392
					rel.setReference(solution.getGoalDecision());
393
				}
394
			}
395
			
396
			
397
			// translated back parameter relations
398
			for (Relation rel : solution.getTranslatedTargetGoalRelations())
399
			{
400
				// check relation category 
401
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
402
				{
403
					// check relation
404
					switch (rel.getType())
405
					{
406
						case EQUAL_PARAMETER : 
407
						{
408
							// get equal relation
409
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
410
							// get relation reference parameter label
411
							String tarParamLabel = eqRel.getTargetParameterLabel();
412
							// get label index
413
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
414
							// get goal decision parameter label
415
							String label = solution.getGoalDecision().getParameterLabelByIndex(pIndex);
416
							
417
							// update relation
418
							eqRel.setTarget(solution.getGoalDecision());
419
							eqRel.setTargetParameterLabel(label);
420
						}
421
						break;
422
							
423
						case NOT_EQUAL_PARAMETER : 
424
						{
425
							// get equal relation
426
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
427
							// get relation reference parameter label
428
							String tarParamLabel = neqRel.getTargetParameterLabel();
429
							// get label index
430
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
431
							// get goal decision parameter label
432
							String label = solution.getGoalDecision().getParameterLabelByIndex(pIndex);
433
							
434
							// update relation
435
							neqRel.setTarget(solution.getGoalDecision());
436
							neqRel.setTargetParameterLabel(label);
437
						}
438
						break;
439
						
440
						default:
441
							// unknown parameter relation
442
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
443
							
444
					}
445
				}
446
				
447
				// check temporal relation
448
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT))
449
				{
450
					// update relation
451
					rel.setTarget(solution.getGoalDecision());
452
				}
453
			}
454
455
			// not feasible solution
456
			throw new RelationPropagationException(ex.getMessage());
457
		}
458
	}
459
	
460
	/**
461
	 * 
462
	 */
463
	@Override
464
	protected void doRetract(FlawSolution solution) 
465
	{
466
		// check solution type
467
		GoalJustification justif = (GoalJustification) solution;
468
		// check if unification solution
469
		if (justif.getJustificationType().equals(JustificationType.UNIFICATION)) {
470
			// special management of unification
471
			this.doRetractUnification((GoalUnification) solution);
472
		}
473
		else {
474
			// "standard" management of flaw solution
475
			super.doRetract(solution);
476
		}
477
	}
478
	
479
	/**
480
	 * 
481
	 * @param goal
482
	 */
483 View Code Duplication
	private void doComputeUnificationSolutions(Goal goal) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
484
		
485
		// get goal component
486
		DomainComponent gComp = goal.getComponent();
487
		// list of goal unifications found
488
		List<GoalUnification> unifications = new ArrayList<>();
489
		
490
		// search active decisions that can be unified with the goal
491
		for (Decision unif : gComp.getActiveDecisions()) {
492
			
493
			// check predicate and temporal unification
494
			if (this.isPredicateUnificationFeasible(goal.getDecision(), unif) && 
495
					this.isTemporalUnificationFeasible(goal.getDecision(), unif)) {
496
				
497
				// possible unification found
498
				GoalUnification unification = new GoalUnification(goal, unif, this.unificationCost);
499
				// add unifications
500
				unifications.add(unification);
501
				info("Feasible unification found:\n"
502
						+ "- planning goal: " + goal + "\n"
503
						+ "- unification decision: " + unification + "\n");
504
			}
505
			else {
506
				
507
				// unification not feasible
508
				debug("No feasible unification:\n"
509
						+ "- planning goal: " + goal + "\n"
510
						+ "- decision : \"" + unif + "\"\n");
511
			}
512
		}
513
		
514
		// add unifications as goal solutions
515
		Collections.shuffle(unifications);
516
		for (GoalUnification unif : unifications) {
517
			goal.addSolution(unif);
518
		}
519
	}
520
521
	/**
522
	 * 
523
	 * @param goal
524
	 * @param decision
525
	 * @return
526
	 */
527 View Code Duplication
	private boolean isPredicateUnificationFeasible(Decision goal, Decision decision) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
528
	{
529
		// feasibility flag
530
		boolean feasible = true;
531
		// first check if the decisions refer to the same values
532
		if (!decision.getValue().equals(goal.getValue()) && 
533
				decision.getComponent().equals(goal.getComponent())) {
534
			
535
			// not feasible unification
536
			feasible = false;
537
			debug("Not feasible predicate unification:\n"
538
					+ "- planning goal: " + goal + "\n"
539
					+ "- unification decision: " + decision + "\n"); 
540
		}
541
		else 
542
		{
543
			// list of committed parameter constraints
544
			Set<Relation> committed = new HashSet<>();
545
			// list of translated parameter relations - reference
546
			Set<ParameterRelation> translatedReferenceGoalRelations = new HashSet<>();
547
			// list of translated parameter relations - target
548
			Set<ParameterRelation> translatedTargetGoalRelations = new HashSet<>();
549
			
550
			// get goal component
551
			DomainComponent goalComp = goal.getComponent();
552
			// get all (pending) relation concerning the goal decision
553
			Set<Relation> pending = goalComp.getRelations(goal);
554
			// check relations
555
			for (Relation rel : pending)
556
			{
557
				// check parameter constraint type
558
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
559
				{
560
					// check relation type
561
					switch (rel.getType())
562
					{
563
						// bind parameter
564
						case BIND_PARAMETER: 
565
						{
566
							// the goal can be only the reference of the relation
567
							ParameterRelation pRel = (ParameterRelation) rel;
568
							
569
							// get relation reference parameter label
570
							String refParamLabel = pRel.getReferenceParameterLabel();
571
							// get label index
572
							int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
573
							// get unification decision parameter label
574
							String label = decision.getParameterLabelByIndex(refParameterIndex);
575
576
							// update reference decision 
577
							pRel.setReference(decision);
578
							
579
							// update reference label of the relation 
580
							pRel.setReferenceParameterLabel(label);
581
							// add relation to the list of translated ones
582
							translatedReferenceGoalRelations.add(pRel);
583
						}
584
						break;
585
						
586
						case EQUAL_PARAMETER : 
587
						{
588
							// get parameter relation
589
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
590
							// check if the goal is the reference or the parameter constraint 
591
							if (eqRel.getReference().equals(goal))
592
							{
593
								// get relation reference parameter label
594
								String refParamLabel = eqRel.getReferenceParameterLabel();
595
								// get label index
596
								int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
597
								// get unification decision parameter label
598
								String label = decision.getParameterLabelByIndex(refParameterIndex);
599
600
								// update reference decision 
601
								eqRel.setReference(decision);
602
								// update reference label of the relation 
603
								eqRel.setReferenceParameterLabel(label);
604
								// add relation to the list of translated ones
605
								translatedReferenceGoalRelations.add(eqRel);
606
							}
607
							else // the goal is the target of the relation 
608
							{
609
								// get relation reference parameter label
610
								String refParamLabel = eqRel.getTargetParameterLabel();
611
								// get label index
612
								int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
613
								// get unification decision parameter label
614
								String label = decision.getParameterLabelByIndex(refParameterIndex);
615
616
								// update reference decision 
617
								eqRel.setTarget(decision);
618
								// update reference label of the relation 
619
								eqRel.setTargetParameterLabel(label);
620
								// add relation to the list of translated ones
621
								translatedTargetGoalRelations.add(eqRel);
622
							}
623
						}
624
						break;
625
						
626
						case NOT_EQUAL_PARAMETER : 
627
						{
628
							// get parameter relation
629
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
630
							// check if the goal is the reference or the parameter constraint 
631
							if (neqRel.getReference().equals(goal))
632
							{
633
								// get relation reference parameter label
634
								String refParamLabel = neqRel.getReferenceParameterLabel();
635
								// get label index
636
								int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
637
								// get unification decision parameter label
638
								String label = decision.getParameterLabelByIndex(refParameterIndex);
639
640
								// update reference decision 
641
								neqRel.setReference(decision);
642
								// update reference label of the relation 
643
								neqRel.setReferenceParameterLabel(label);
644
								// add relation to the list of translated ones
645
								translatedReferenceGoalRelations.add(neqRel);
646
							}
647
							else // the goal is the target of the relation 
648
							{
649
								// get relation reference parameter label
650
								String refParamLabel = neqRel.getTargetParameterLabel();
651
								// get label index
652
								int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
653
								// get unification decision parameter label
654
								String label = decision.getParameterLabelByIndex(refParameterIndex);
655
656
								// update reference decision 
657
								neqRel.setTarget(decision);
658
								// update reference label of the relation 
659
								neqRel.setTargetParameterLabel(label);
660
								// add relation to the list of translated ones
661
								translatedTargetGoalRelations.add(neqRel);
662
							}
663
						}
664
						break;
665
						
666
						
667
						default:
668
							// unknown parameter relation
669
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
670
					}
671
				}
672
			}
673
				
674
			try
675
			{
676
				// activate translated relations
677
				for (Relation rel : translatedReferenceGoalRelations) {
678
					// check if can be activated
679
					if (rel.getReference().getComponent().activate(rel)) {
680
						// add relation to the committed list
681
						committed.add(rel);
682
					}
683
				}
684
				
685
				// activate translated relations
686
				for (Relation rel : translatedTargetGoalRelations) {
687
					// check if can be activated
688
					if (rel.getReference().getComponent().activate(rel)) {
689
						// add relation to the committed list
690
						committed.add(rel);
691
					}
692
				}
693
				
694
				// check parameter of the plan
695
				this.pdb.verify();
696
			}
697
			catch (ConsistencyCheckException | RelationPropagationException ex) {
698
				// not feasible 
699
				feasible = false;
700
				// not feasible unification
701
				debug("Not feasible predicate unification:\n"
702
						+ "- planning goal: " + goal + "\n"
703
						+ "- unification decision: " + decision + "\n");
704
			}
705
			finally 
706
			{
707
				// check committed relations
708
				for (Relation rel : committed) {
709
					// deactivate relation
710
					rel.getReference().getComponent().deactivate(rel);
711
				}
712
713
				
714
				// translated back parameter relations
715
				for (ParameterRelation rel : translatedReferenceGoalRelations)
716
				{
717
					// get relation reference parameter label
718
					String refParamLabel = rel.getReferenceParameterLabel();
719
					// get label index
720
					int pIndex = rel.getReference().getParameterIndexByLabel(refParamLabel);
721
					// get goal decision parameter label
722
					String label = goal.getParameterLabelByIndex(pIndex);
723
					
724
					// update relation
725
					rel.setReference(goal);
726
					rel.setReferenceParameterLabel(label);
727
				}
728
				
729
				// translated back parameter relations
730
				for (ParameterRelation rel : translatedTargetGoalRelations)
731
				{
732
					// check relation
733
					switch (rel.getType())
734
					{
735
						case EQUAL_PARAMETER : 
736
						{
737
							// get equal relation
738
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
739
							// get relation reference parameter label
740
							String tarParamLabel = eqRel.getTargetParameterLabel();
741
							// get label index
742
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
743
							// get goal decision parameter label
744
							String label = goal.getParameterLabelByIndex(pIndex);
745
							
746
							// update relation
747
							eqRel.setTarget(goal);
748
							eqRel.setTargetParameterLabel(label);
749
						}
750
						break;
751
							
752
						case NOT_EQUAL_PARAMETER : 
753
						{
754
							// get equal relation
755
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
756
							// get relation reference parameter label
757
							String tarParamLabel = neqRel.getTargetParameterLabel();
758
							// get label index
759
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
760
							// get goal decision parameter label
761
							String label = goal.getParameterLabelByIndex(pIndex);
762
							
763
							// update relation
764
							neqRel.setTarget(goal);
765
							neqRel.setTargetParameterLabel(label);
766
						}
767
						break;
768
						
769
						default:
770
							// unknown parameter relation
771
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
Bug introduced by
Remove this throw statement from this finally block.
Loading history...
Best Practice introduced by
If you throw an exception in a finally block, the original exception will be hidden. Additionally, the finally block is no longer guaranteed to run to completion. Consider refactoring your code to avoid throwing the exception here.
Loading history...
772
							
773
					}
774
				}
775
			}
776
		}
777
		
778
		// get feasibility flag
779
		return feasible;
780
	}
781
	
782
	/**
783
	 * 
784
	 * @param goal
785
	 * @param decision
786
	 * @return
787
	 */
788 View Code Duplication
	private boolean isTemporalUnificationFeasible(Decision goal,  Decision decision) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
789
	{
790
		// feasibility flag
791
		boolean feasible = true;
792
		// list of translated relations
793
		Set<Relation> translated = new HashSet<>();
794
		// list of committed relations
795
		Set<Relation> committed = new HashSet<>();
796
		
797
		// get goal component 
798
		DomainComponent gComp = goal.getComponent();
799
		// get all (pending) relations associated to the goal 
800
		Set<Relation> pRels = gComp.getRelations(goal);
801
		// translate relations
802
		for (Relation pRel : pRels)
803
		{
804
			// focus on temporal relations only 
805
			if (pRel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT))
806
			{
807
				// check relation reference
808
				if (pRel.getReference().equals(goal)) {
809
					// replace reference
810
					pRel.setReference(decision);
811
					// add translated relation
812
					translated.add(pRel);
813
				}
814
				
815
				// check relation target
816
				if (pRel.getTarget().equals(goal)) {
817
					// replace target
818
					pRel.setTarget(decision);
819
					// add translated relation
820
					translated.add(pRel);
821
				}
822
			}
823
		}
824
	
825
		try
826
		{
827
			// check translated relation and activate them if possible
828
			for (Relation tRel : translated) {
829
				// activate relation
830
				if (tRel.getReference().getComponent().activate(tRel)) {
831
					// add relation to committed list
832
					committed.add(tRel);
833
				}
834
			}
835
			
836
			// check temporal consistency after activated relations
837
//			this.tdb.verify();
838
		}
839
		catch (RelationPropagationException ex) {
840
			// not feasible unification 
841
			feasible = false;
842
			// not feasible unification
843
			debug("Not feasible temporal unification:\n"
844
					+ "- planning goal: " + goal + "\n"
845
					+ "- unification decision: " + decision + "\n");
846
		}
847
		finally 
848
		{
849
			// deactivate relations
850
			for (Relation rel : committed) {
851
				// deactivate relation
852
				rel.getReference().getComponent().deactivate(rel);
853
			}
854
			
855
			// translate back relations
856
			for (Relation rel : translated) {
857
				// check reference 
858
				if (rel.getReference().equals(decision)) {
859
					// replace reference
860
					rel.setReference(goal);
861
				}
862
				
863
				// check target
864
				if (rel.getTarget().equals(decision)) {
865
					// replace target
866
					rel.setTarget(goal);
867
				}
868
			}
869
		}
870
		
871
		// get feasibility flag
872
		return feasible;
873
	}
874
	
875
	/**
876
	 * 
877
	 * @param goal
878
	 */
879
	private void doComputeExpansionSolutions(Goal goal) {
880
		
881
		// get component
882
		DomainComponent gComp = goal.getComponent();
883
		// set of activated relations
884
		List<Relation> rels = new ArrayList<>();
885
		// list of goal expansions
886
		List<GoalExpansion> expansions = new ArrayList<>();
887
		
888
		try {
889
			
890
			// activate the goal and get activated relations
891
			rels.addAll(gComp.activate(goal.getDecision()));
892
			// check temporal and parameter consistency
893
//			this.tdb.verify();
894
			this.pdb.verify();
895
		
896
			// check rules
897
			List<SynchronizationRule> rules = this.component.getSynchronizationRules(goal.getDecision().getValue());
898 View Code Duplication
			if (rules.isEmpty()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
899
				
900
				// the goal can be justified without applying rules
901
				GoalExpansion expansion = new GoalExpansion(goal, this.expansionCost);
902
				// add expansion 
903
				expansions.add(expansion);
904
				// print debug message
905
				debug("Simple goal found no synchronization is triggered after expansion:\n"
906
						+ "- planning goal: " + goal.getDecision() + "\n");
907
				
908
			} else {
909
				
910
				// can do expansion
911
				for (SynchronizationRule rule : rules) {
912
					
913
					// expansion solution
914
					GoalExpansion expansion = new GoalExpansion(goal, rule, this.expansionCost);
915
					// add expansion
916
					expansions.add(expansion);
917
					// print debug message
918
					debug("Complex goal found:\n"
919
							+ "- planning goal: " + goal.getDecision() + "\n"
920
							+ "- synchronization rule: " + rule + "\n");
921
				}
922
			}
923
			
924
			// add all expansions found as solutions
925
			Collections.shuffle(expansions);
926
			for (GoalExpansion exp : expansions) {
927
				goal.addSolution(exp);
928
			}
929
			
930
		} catch (DecisionPropagationException | ConsistencyCheckException  ex) {
931
			// not feasible goal expansion
932
			warning(ex.getMessage());
933
			
934
		} finally {
935
			
936
			// deactivate relations if any
937
			for (Relation rel : rels) {
938
				gComp.deactivate(rel);
939
			}
940
			
941
			// deactivate goal decision
942
			gComp.deactivate(goal.getDecision());
943
		}
944
	}
945
	
946
	/**
947
	 * 
948
	 * @param unification
949
	 * @throws Exception
950
	 */
951 View Code Duplication
	private void doApplyUnification(GoalUnification unification) 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
952
			throws FlawSolutionApplicationException {
953
		
954
		// get original goal
955
		Decision goal = unification.getGoalDecision();
956
		// get all (pending) relations concerning the planning goal
957
		DomainComponent goalComp = goal.getComponent();
958
959
		// list of committed parameter constraints
960
		Set<Relation> committed = new HashSet<>();
961
		// list of translated parameter relations - reference
962
		Set<Relation> translatedReferenceGoalRelations = new HashSet<>();
963
		// list of translated parameter relations - target
964
		Set<Relation> translatedTargetGoalRelations = new HashSet<>();
965
		
966
		
967
		// get pending relations associated to the goal
968
		Set<Relation> gRels = goalComp.getRelations(goal);
969
		// translate pending relations by replacing goal's information with unification decision's information
970
		for (Relation rel : gRels) {
971
			
972
			// check relation category
973
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
974
				
975
				// check relation reference
976
				if (rel.getReference().equals(goal)) {
977
					// replace reference 
978
					rel.setReference(unification.getUnificationDecision());
979
					// add relation to the list
980
					translatedReferenceGoalRelations.add(rel);
981
				}
982
983
				// check relation target
984
				if (rel.getTarget().equals(goal)) {
985
					// replace target
986
					rel.setTarget(unification.getUnificationDecision());
987
					// add relation to the list
988
					translatedTargetGoalRelations.add(rel);
989
				}
990
				
991
			}
992
			
993
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
994
			{
995
				// check relation type
996
				switch (rel.getType())
997
				{
998
					// bind parameter
999
					case BIND_PARAMETER: 
1000
					{
1001
						// the goal can be only the reference of the relation
1002
						ParameterRelation pRel = (ParameterRelation) rel;
1003
						
1004
						// get relation reference parameter label
1005
						String refParamLabel = pRel.getReferenceParameterLabel();
1006
						// get label index
1007
						int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
1008
						// get unification decision parameter label
1009
						String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1010
1011
						// update reference decision 
1012
						pRel.setReference(unification.getUnificationDecision());
1013
						// update reference label of the relation 
1014
						pRel.setReferenceParameterLabel(label);
1015
						// add relation to the list of translated ones
1016
						translatedReferenceGoalRelations.add(pRel);
1017
					}
1018
					break;
1019
					
1020
					case EQUAL_PARAMETER : 
1021
					{
1022
						// get parameter relation
1023
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
1024
						// check if the goal is the reference or the parameter constraint 
1025
						if (eqRel.getReference().equals(goal))
1026
						{
1027
							// get relation reference parameter label
1028
							String refParamLabel = eqRel.getReferenceParameterLabel();
1029
							// get label index
1030
							int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
1031
							// get unification decision parameter label
1032
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1033
1034
							// update reference decision 
1035
							eqRel.setReference(unification.getUnificationDecision());
1036
							// update reference label of the relation 
1037
							eqRel.setReferenceParameterLabel(label);
1038
							// add relation to the list of translated ones
1039
							translatedReferenceGoalRelations.add(eqRel);
1040
						}
1041
						else // the goal is the target of the relation 
1042
						{
1043
							// get relation reference parameter label
1044
							String refParamLabel = eqRel.getTargetParameterLabel();
1045
							// get label index
1046
							int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
1047
							// get unification decision parameter label
1048
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1049
1050
							// update reference decision 
1051
							eqRel.setTarget(unification.getUnificationDecision());
1052
							// update reference label of the relation 
1053
							eqRel.setTargetParameterLabel(label);
1054
							// add relation to the list of translated ones
1055
							translatedTargetGoalRelations.add(eqRel);
1056
						}
1057
					}
1058
					break;
1059
					
1060
					case NOT_EQUAL_PARAMETER : 
1061
					{
1062
						// get parameter relation
1063
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
1064
						// check if the goal is the reference or the parameter constraint 
1065
						if (neqRel.getReference().equals(goal))
1066
						{
1067
							// get relation reference parameter label
1068
							String refParamLabel = neqRel.getReferenceParameterLabel();
1069
							// get label index
1070
							int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
1071
							// get unification decision parameter label
1072
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1073
1074
							// update reference decision 
1075
							neqRel.setReference(unification.getUnificationDecision());
1076
							// update reference label of the relation 
1077
							neqRel.setReferenceParameterLabel(label);
1078
							// add relation to the list of translated ones
1079
							translatedReferenceGoalRelations.add(neqRel);
1080
						}
1081
						else // the goal is the target of the relation 
1082
						{
1083
							// get relation reference parameter label
1084
							String refParamLabel = neqRel.getTargetParameterLabel();
1085
							// get label index
1086
							int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
1087
							// get unification decision parameter label
1088
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1089
1090
							// update reference decision 
1091
							neqRel.setTarget(unification.getUnificationDecision());
1092
							// update reference label of the relation 
1093
							neqRel.setTargetParameterLabel(label);
1094
							// add relation to the list of translated ones
1095
							translatedTargetGoalRelations.add(neqRel);
1096
						}
1097
					}
1098
					break;
1099
					
1100
					
1101
					default:
1102
						// unknown parameter relation
1103
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
1104
				}
1105
			}
1106
		}
1107
		
1108
		
1109
		try	
1110
		{
1111
			// remove original goal: PENDING -> SILENT
1112
			goalComp.free(goal);
1113
			
1114
			// activate translated relations
1115
			for (Relation rel : translatedReferenceGoalRelations) 
1116
			{
1117
				// check if can be activated
1118
				if (rel.getReference().getComponent().activate(rel)) {
1119
					
1120
					// add activated relations
1121
					unification.addActivatedRelation(rel);
1122
					// add relation to the committed list
1123
					committed.add(rel);
1124
				}
1125
			}
1126
			
1127
			// activate translated relations
1128
			for (Relation rel : translatedTargetGoalRelations) 
1129
			{
1130
				// check if can be activated
1131
				if (rel.getReference().getComponent().activate(rel)) {
1132
					
1133
					// add activated relations
1134
					unification.addActivatedRelation(rel);
1135
					// add relation to the committed list
1136
					committed.add(rel);
1137
				}
1138
			}
1139
1140
			// set translated relations
1141
			unification.setTranslatedReferenceGoalRelation(translatedReferenceGoalRelations);
1142
			unification.setTranslatedTargetGoalRealtion(translatedTargetGoalRelations);
1143
			
1144
			// check consistency
1145
//			this.tdb.verify();
1146
			this.pdb.verify();
1147
			
1148
		} catch (RelationPropagationException | ConsistencyCheckException ex) {
1149
1150
			// restore goal: SILENT -> PENDING
1151
			goalComp.restore(goal);
1152
			// deactivate committed relations
1153
			for (Relation rel : committed) {
1154
				// get reference component
1155
				DomainComponent refComp = rel.getReference().getComponent();
1156
				refComp.deactivate(rel);
1157
			}
1158
			
1159
			
1160
			// translated back relations
1161
			for (Relation rel : translatedReferenceGoalRelations) {
1162
				// check category
1163
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
1164
					
1165
					// get parameter relation
1166
					ParameterRelation pRel = (ParameterRelation) rel;
1167
					// get relation reference parameter label
1168
					String refParamLabel = pRel.getReferenceParameterLabel();
1169
					// get label index
1170
					int pIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
1171
					// get goal decision parameter label
1172
					String label = goal.getParameterLabelByIndex(pIndex);
1173
					
1174
					// update relation
1175
					pRel.setReference(goal);
1176
					pRel.setReferenceParameterLabel(label);
1177
				}
1178
				
1179
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1180
					// update relation
1181
					rel.setReference(goal);
1182
				}
1183
			}
1184
			
1185
			// translated back parameter relations
1186
			for (Relation rel : translatedTargetGoalRelations) {
1187
				
1188
				// check relation category 
1189
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
1190
					// check relation
1191
					switch (rel.getType()) {
1192
					
1193
						case EQUAL_PARAMETER : {
1194
							
1195
							// get equal relation
1196
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
1197
							// get relation reference parameter label
1198
							String tarParamLabel = eqRel.getTargetParameterLabel();
1199
							// get label index
1200
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
1201
							// get goal decision parameter label
1202
							String label = goal.getParameterLabelByIndex(pIndex);
1203
							
1204
							// update relation
1205
							eqRel.setTarget(goal);
1206
							eqRel.setTargetParameterLabel(label);
1207
						}
1208
						break;
1209
							
1210
						case NOT_EQUAL_PARAMETER : {
1211
							
1212
							// get equal relation
1213
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
1214
							// get relation reference parameter label
1215
							String tarParamLabel = neqRel.getTargetParameterLabel();
1216
							// get label index
1217
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
1218
							// get goal decision parameter label
1219
							String label = goal.getParameterLabelByIndex(pIndex);
1220
							
1221
							// update relation
1222
							neqRel.setTarget(goal);
1223
							neqRel.setTargetParameterLabel(label);
1224
						}
1225
						break;
1226
						
1227
						default:
1228
							// unknown parameter relation
1229
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
1230
							
1231
					}
1232
				}
1233
				
1234
				// check temporal relation
1235
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1236
					// update relation
1237
					rel.setTarget(goal);
1238
				}
1239
			}
1240
1241
			// not feasible solution
1242
			throw new FlawSolutionApplicationException(ex.getMessage());
1243
		}
1244
	}
1245
	
1246
	/**
1247
	 * 
1248
	 * @param expansion
1249
	 * @throws Exception
1250
	 */
1251
	private void doApplyExpansion(GoalExpansion expansion) 
1252
			throws FlawSolutionApplicationException {
1253
		
1254
		// get goal
1255
		Decision goal = expansion.getGoalDecision();
1256
		// check subgoals from selected synchronization rule if any
1257 View Code Duplication
		if (expansion.hasSubGoals()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1258
			
1259
			// get the rule to apply
1260
			SynchronizationRule rule = expansion.getSynchronizationRule();
1261
			// create an index of token variable
1262
			Map<TokenVariable, Decision> var2dec = new HashMap<>();
1263
			var2dec.put(rule.getTriggerer(), goal);
1264
			// add pending decisions
1265
			for (TokenVariable var : rule.getTokenVariables()) {
1266
				
1267
				// get target component
1268
				DomainComponent target = var.getValue().getComponent(); 
1269
				// create a pending decision
1270
				Decision pending = target.create(
1271
						var.getValue(),
1272
						var.getParameterLabels());
1273
				
1274
				// set causal link
1275
				pending.setCausalLink(goal);
1276
				// check solving knowledge
1277
				if (var.isMandatoryExpansion()) {
1278
					pending.setMandatoryExpansion();
1279
				}
1280
1281
				// check solving knowledge
1282
				if (var.isMandatoryUnificaiton()) {
1283
					pending.setMandatoryUnification();
1284
				}
1285
				
1286
				// add entry to cache
1287
				var2dec.put(var, pending);
1288
				// add created decision
1289
				expansion.addCreatedDecision(pending);
1290
			}
1291
			
1292
			// add pending relations
1293
			for (SynchronizationConstraint c : rule.getConstraints()) {
1294
				// check category
1295
				switch (c.getCategory()) {
1296
1297
					// temporal category
1298
					case TEMPORAL_CONSTRAINT : {
1299
						
1300
						// temporal constraint
1301
						TemporalSynchronizationConstraint tc = (TemporalSynchronizationConstraint) c;
1302
						// get decisions
1303
						Decision reference = var2dec.get(tc.getReference());
1304
						Decision target = var2dec.get(tc.getTarget());
1305
						// get reference component
1306
						DomainComponent refComp = reference.getComponent();
1307
						// create pending relation
1308
						TemporalRelation rel = refComp.create(tc.getType(), reference, target);
1309
						// set bounds
1310
						rel.setBounds(tc.getBounds());
1311
						// add created relation
1312
						expansion.addCreatedRelation(rel);
1313
					}
1314
					break;
1315
					
1316
					// parameter category
1317
					case PARAMETER_CONSTRAINT: {
1318
						
1319
						// parameter constraint
1320
						ParameterSynchronizationConstraint pc = (ParameterSynchronizationConstraint) c;
1321
						// get decisions
1322
						Decision reference = var2dec.get(pc.getReference());
1323
						Decision target = var2dec.get(pc.getTarget());
1324
						// get reference component
1325
						DomainComponent refComp = reference.getComponent();
1326
						// create pending relation
1327
						ParameterRelation rel = (ParameterRelation) refComp.create(pc.getType(), reference, target);
1328
						
1329
						// check parameter relation type
1330
						switch (rel.getType()) {
1331
						
1332
							// bind parameter relation
1333
							case BIND_PARAMETER : {
1334
								
1335
								// bind constraint
1336
								BindParameterRelation bind = (BindParameterRelation) rel;
1337
								// set binding value
1338
								bind.setValue(pc.getTargetLabel());
1339
								// set reference label
1340
								if (pc.getReference().equals(rule.getTriggerer())) {
1341
									
1342
									// get trigger label index
1343
									int index = rule.getTriggerer().getParameterIndexByLabel(pc.getReferenceLabel());
1344
									// set decision's label
1345
									String label = goal.getParameterLabelByIndex(index);
1346
									// set label
1347
									bind.setReferenceParameterLabel(label);
1348
									
1349
								} else {
1350
									
1351
									bind.setReferenceParameterLabel(pc.getReferenceLabel());
1352
								}
1353
							}
1354
							break;
1355
							
1356
							// equal parameter relation
1357
							case EQUAL_PARAMETER : {
1358
								
1359
								// get relation
1360
								EqualParameterRelation eq = (EqualParameterRelation) rel;
1361
								
1362
								// check if source is the trigger
1363
								if (pc.getReference().equals(rule.getTriggerer())) {
1364
									
1365
									// get trigger label index
1366
									int index = rule.getTriggerer().getParameterIndexByLabel(pc.getReferenceLabel());
1367
									// get decions's label
1368
									String label = goal.getParameterLabelByIndex(index);
1369
									// set label
1370
									eq.setReferenceParameterLabel(label);
1371
									
1372
								} else {
1373
									
1374
									// directly set the label
1375
									eq.setReferenceParameterLabel(pc.getReferenceLabel());
1376
								}
1377
								
1378
								// check if target is the trigger
1379
								if (pc.getTarget().equals(rule.getTriggerer())) {
1380
									
1381
									// get trigger label index
1382
									int index = rule.getTriggerer().getParameterIndexByLabel(pc.getTargetLabel());
1383
									// get decision's label
1384
									String label = goal.getParameterLabelByIndex(index);
1385
									// set label
1386
									eq.setTargetParameterLabel(label);
1387
									
1388
								} else {
1389
									
1390
									// directly set the label
1391
									eq.setTargetParameterLabel(pc.getTargetLabel());
1392
								}
1393
							}
1394
							break;
1395
							
1396
							// not-equal parameter relation
1397
							case NOT_EQUAL_PARAMETER : {
1398
								
1399
								// get relation
1400
								NotEqualParameterRelation neq = (NotEqualParameterRelation) rel;
1401
								
1402
								// check if source is the trigger
1403
								if (pc.getReference().equals(rule.getTriggerer())) {
1404
									
1405
									// get trigger label index
1406
									int index = rule.getTriggerer().getParameterIndexByLabel(pc.getReferenceLabel());
1407
									// get decions's label
1408
									String label = goal.getParameterLabelByIndex(index);
1409
									// set label
1410
									neq.setReferenceParameterLabel(label);
1411
									
1412
								} else {
1413
									
1414
									// directly set the label
1415
									neq.setReferenceParameterLabel(pc.getReferenceLabel());
1416
								}
1417
								
1418
								// check if target is the trigger
1419
								if (pc.getTarget().equals(rule.getTriggerer())) {
1420
									
1421
									// get trigger label index
1422
									int index = rule.getTriggerer().getParameterIndexByLabel(pc.getTargetLabel());
1423
									// get decision's label
1424
									String label = goal.getParameterLabelByIndex(index);
1425
									// set label
1426
									neq.setTargetParameterLabel(label);
1427
									
1428
								} else {
1429
									// directly set the label
1430
									neq.setTargetParameterLabel(pc.getTargetLabel());
1431
								}
1432
							}
1433
							break;
1434
							
1435
							default : {
1436
								throw new RuntimeException("Unknown parameter constraint type - " + rel.getType());
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
1437
							}
1438
						}
1439
						
1440
						// add created relation
1441
						expansion.addCreatedRelation(rel);
1442
					}
1443
				}
1444
			}
1445
		}
1446
		
1447
		try {
1448
			
1449
			// activate goal decision 
1450
			DomainComponent goalComp = goal.getComponent();
1451
			// get goal-related activated relations
1452
			Set<Relation> list = goalComp.activate(goal);
1453
			// add goal to activated decisions
1454
			expansion.addActivatedDecision(goal);
1455
			// add to activated relations
1456
			expansion.addActivatedRelations(list);
1457
			
1458
			// check consistency 
1459
			this.tdb.verify();
1460
			this.pdb.verify();
1461
			
1462
		} catch (DecisionPropagationException | ConsistencyCheckException ex) {
1463
			
1464
			// deactivate activated relations
1465
			for (Relation rel : expansion.getActivatedRelations()) {
1466
				// get reference component
1467
				DomainComponent refComp = rel.getReference().getComponent();
1468
				refComp.deactivate(rel);
1469
			}
1470
			
1471
			// delete created relations
1472
			for (Relation rel : expansion.getCreatedRelations()) {
1473
				// get reference component
1474
				DomainComponent refComp = rel.getReference().getComponent();
1475
				refComp.delete(rel);
1476
			}
1477
1478
			// deactivate activated decisions
1479
			for (Decision dec : expansion.getActivatedDecisions()) {
1480
				// get component
1481
				DomainComponent refComp = dec.getComponent();
1482
				refComp.deactivate(dec);
1483
			}
1484
			
1485
			
1486
			// delete created decisions 
1487
			for (Decision dec : expansion.getCreatedDecisions()) {
1488
				// get component
1489
				DomainComponent comp = dec.getComponent();
1490
				comp.free(dec);
1491
			}
1492
			
1493
			// throw exception
1494
			throw new FlawSolutionApplicationException(ex.getMessage());
1495
		}
1496
	}
1497
1498
	/**
1499
	 * 
1500
	 * @param unification
1501
	 */
1502 View Code Duplication
	private void doRetractUnification(GoalUnification unification) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1503
		
1504
		// original goal 
1505
		Decision goal = unification.getGoalDecision();
1506
		
1507
		// get goal component
1508
		DomainComponent goalComp = goal.getComponent();
1509
		// restore original planning goal SILENT -> PENDING
1510
		goalComp.restore(goal);
1511
1512
		// deactivate activated relations
1513
		for (Relation rel : unification.getActivatedRelations()) {
1514
			// get reference component
1515
			DomainComponent refComp = rel.getReference().getComponent();
1516
			refComp.deactivate(rel);
1517
		}
1518
		
1519
		// translated back relations
1520
		for (Relation rel : unification.getTranslatedReferenceGoalRelations()){
1521
			// check category
1522
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
1523
				
1524
				// get parameter relation
1525
				ParameterRelation pRel = (ParameterRelation) rel;
1526
				
1527
				// get relation reference parameter label
1528
				String refParamLabel = pRel.getReferenceParameterLabel();
1529
				// get label index
1530
				int pIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
1531
				// get goal decision parameter label
1532
				String label = goal.getParameterLabelByIndex(pIndex);
1533
				
1534
				// update relation
1535
				pRel.setReference(goal);
1536
				pRel.setReferenceParameterLabel(label);
1537
			}
1538
			
1539
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1540
				// update relation
1541
				rel.setReference(goal);
1542
			}
1543
		}
1544
		
1545
		// translated back parameter relations
1546
		for (Relation rel : unification.getTranslatedTargetGoalRelations()) {
1547
			
1548
			// check relation category 
1549
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
1550
				// check relation
1551
				switch (rel.getType()) {
1552
				
1553
					case EQUAL_PARAMETER : {
1554
						
1555
						// get equal relation
1556
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
1557
						// get relation reference parameter label
1558
						String tarParamLabel = eqRel.getTargetParameterLabel();
1559
						// get label index
1560
						int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
1561
						// get goal decision parameter label
1562
						String label = goal.getParameterLabelByIndex(pIndex);
1563
						
1564
						// update relation
1565
						eqRel.setTarget(goal);
1566
						eqRel.setTargetParameterLabel(label);
1567
					}
1568
					break;
1569
						
1570
					case NOT_EQUAL_PARAMETER : {
1571
						
1572
						// get equal relation
1573
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
1574
						// get relation reference parameter label
1575
						String tarParamLabel = neqRel.getTargetParameterLabel();
1576
						// get label index
1577
						int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
1578
						// get goal decision parameter label
1579
						String label = goal.getParameterLabelByIndex(pIndex);
1580
						
1581
						// update relation
1582
						neqRel.setTarget(goal);
1583
						neqRel.setTargetParameterLabel(label);
1584
					}
1585
					break;
1586
					
1587
					default:
1588
						// unknown parameter relation
1589
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
1590
						
1591
				}
1592
			}
1593
			
1594
			// check temporal relation
1595
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1596
				
1597
				// update relation
1598
				rel.setTarget(goal);
1599
			}
1600
		}
1601
	}
1602
	
1603
}
1604