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

doComputeExpansionSolutions(Goal)   B

Complexity

Conditions 5

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 30
dl 0
loc 55
rs 8.6933
c 0
b 0
f 0

How to fix   Long Method   

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:

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