Code Duplication    Length = 265-265 lines in 2 locations

src/main/java/it/cnr/istc/pst/platinum/ai/framework/microkernel/resolver/plan/TimelineAwarePlanRefinementResolver.java 1 location

@@ 197-461 (lines=265) @@
194
	 * @param solution
195
	 * @throws RelationPropagationException
196
	 */
197
	private void doRestoreUnification(GoalUnification solution) 
198
			throws RelationPropagationException {
199
		
200
		// restore relation translation
201
		for (Relation rel : solution.getTranslatedReferenceGoalRelations()) {
202
			
203
			// check relation category
204
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
205
				// replace reference
206
				rel.setReference(solution.getUnificationDecision());
207
			}
208
			
209
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
210
				
211
				// check relation type
212
				switch (rel.getType()) {
213
				
214
					// bind parameter
215
					case BIND_PARAMETER: 
216
					{
217
						// the goal can be only the reference of the relation
218
						ParameterRelation pRel = (ParameterRelation) rel;
219
						
220
						// get relation reference parameter label
221
						String refParamLabel = pRel.getReferenceParameterLabel();
222
						// get label index
223
						int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
224
						// get unification decision parameter label
225
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
226
227
						// update reference decision 
228
						pRel.setReference(solution.getUnificationDecision());
229
						// update reference label of the relation 
230
						pRel.setReferenceParameterLabel(label);
231
					}
232
					break;
233
					
234
					case EQUAL_PARAMETER : 
235
					{
236
						// get parameter relation
237
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
238
						// get relation reference parameter label
239
						String refParamLabel = eqRel.getReferenceParameterLabel();
240
						// get label index
241
						int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
242
						// get unification decision parameter label
243
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
244
245
						// update reference decision 
246
						eqRel.setReference(solution.getUnificationDecision());
247
						// update reference label of the relation 
248
						eqRel.setReferenceParameterLabel(label);
249
					}
250
					break;
251
					
252
					case NOT_EQUAL_PARAMETER : 
253
					{
254
						// get parameter relation
255
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
256
						// get relation reference parameter label
257
						String refParamLabel = neqRel.getReferenceParameterLabel();
258
						// get label index
259
						int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
260
						// get unification decision parameter label
261
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
262
263
						// update reference decision 
264
						neqRel.setReference(solution.getUnificationDecision());
265
						// update reference label of the relation 
266
						neqRel.setReferenceParameterLabel(label);
267
					}
268
					break;
269
					
270
					
271
					default:
272
						// unknown parameter relation
273
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
274
				}
275
			}
276
		}
277
		
278
		
279
		// restore relation translation
280
		for (Relation rel : solution.getTranslatedTargetGoalRelations()) {
281
			
282
			// check relation category
283
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
284
				// replace reference
285
				rel.setTarget(solution.getUnificationDecision());
286
			}
287
			
288
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
289
				
290
				// check relation type
291
				switch (rel.getType()) {
292
				
293
					case EQUAL_PARAMETER : 
294
					{
295
						// get parameter relation
296
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
297
						// get relation reference parameter label
298
						String refParamLabel = eqRel.getTargetParameterLabel();
299
						// get label index
300
						int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
301
						// get unification decision parameter label
302
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
303
304
						// update reference decision 
305
						eqRel.setTarget(solution.getUnificationDecision());
306
						// update reference label of the relation 
307
						eqRel.setTargetParameterLabel(label);
308
					}
309
					break;
310
					
311
					case NOT_EQUAL_PARAMETER : 
312
					{
313
						// get parameter relation
314
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
315
						// get relation reference parameter label
316
						String refParamLabel = neqRel.getTargetParameterLabel();
317
						// get label index
318
						int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
319
						// get unification decision parameter label
320
						String label = solution.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
321
322
						// update reference decision 
323
						neqRel.setTarget(solution.getUnificationDecision());
324
						// update reference label of the relation 
325
						neqRel.setTargetParameterLabel(label);
326
					}
327
					break;
328
					
329
					
330
					default:
331
						// unknown parameter relation
332
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
333
				}
334
			}
335
		}
336
337
		
338
		
339
		// list of committed parameter constraints
340
		Set<Relation> committed = new HashSet<>();
341
		try	
342
		{
343
			// get goal component
344
			DomainComponent gComp = solution.getGoalDecision().getComponent();
345
			// remove original goal: PENDING -> SILENT
346
			gComp.free(solution.getGoalDecision());
347
			
348
			// activate translated relations
349
			for (Relation rel : solution.getActivatedRelations()) 
350
			{
351
				// check if can be activated
352
				if (rel.getReference().getComponent().activate(rel)) {
353
					// add relation to the committed list
354
					committed.add(rel);
355
				}
356
			}
357
			
358
			// check consistency
359
			this.tdb.verify();
360
			this.pdb.verify();
361
			
362
		} catch (RelationPropagationException | ConsistencyCheckException ex) {
363
			
364
			// get goal component
365
			DomainComponent gComp = solution.getGoalDecision().getComponent();
366
			// restore goal: SILENT -> PENDING
367
			gComp.restore(solution.getGoalDecision());
368
			
369
			// deactivate committed relations
370
			for (Relation rel : committed) {
371
				// get reference component
372
				DomainComponent refComp = rel.getReference().getComponent();
373
				refComp.deactivate(rel);
374
			}
375
			
376
			// translated back relations
377
			for (Relation rel : solution.getTranslatedReferenceGoalRelations()) {
378
				// check category
379
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
380
					// get parameter relation
381
					ParameterRelation pRel = (ParameterRelation) rel;
382
					
383
					// get relation reference parameter label
384
					String refParamLabel = pRel.getReferenceParameterLabel();
385
					// get label index
386
					int pIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
387
					// get goal decision parameter label
388
					String label = solution.getGoalDecision().getParameterLabelByIndex(pIndex);
389
					
390
					// update relation
391
					pRel.setReference(solution.getGoalDecision());
392
					pRel.setReferenceParameterLabel(label);
393
				}
394
				
395
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
396
					// update relation
397
					rel.setReference(solution.getGoalDecision());
398
				}
399
			}
400
			
401
			
402
			// translated back parameter relations
403
			for (Relation rel : solution.getTranslatedTargetGoalRelations())
404
			{
405
				// check relation category 
406
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
407
				{
408
					// check relation
409
					switch (rel.getType())
410
					{
411
						case EQUAL_PARAMETER : 
412
						{
413
							// get equal relation
414
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
415
							// get relation reference parameter label
416
							String tarParamLabel = eqRel.getTargetParameterLabel();
417
							// get label index
418
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
419
							// get goal decision parameter label
420
							String label = solution.getGoalDecision().getParameterLabelByIndex(pIndex);
421
							
422
							// update relation
423
							eqRel.setTarget(solution.getGoalDecision());
424
							eqRel.setTargetParameterLabel(label);
425
						}
426
						break;
427
							
428
						case NOT_EQUAL_PARAMETER : 
429
						{
430
							// get equal relation
431
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
432
							// get relation reference parameter label
433
							String tarParamLabel = neqRel.getTargetParameterLabel();
434
							// get label index
435
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
436
							// get goal decision parameter label
437
							String label = solution.getGoalDecision().getParameterLabelByIndex(pIndex);
438
							
439
							// update relation
440
							neqRel.setTarget(solution.getGoalDecision());
441
							neqRel.setTargetParameterLabel(label);
442
						}
443
						break;
444
						
445
						default:
446
							// unknown parameter relation
447
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
448
							
449
					}
450
				}
451
				
452
				// check temporal relation
453
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT))
454
				{
455
					// update relation
456
					rel.setTarget(solution.getGoalDecision());
457
				}
458
			}
459
460
			// not feasible solution
461
			throw new RelationPropagationException(ex.getMessage());
462
		}
463
	}
464
	

src/main/java/it/cnr/istc/pst/platinum/ai/framework/microkernel/resolver/plan/PlanRefinementResolver.java 1 location

@@ 192-456 (lines=265) @@
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");
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");
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"); 
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