Code Duplication    Length = 125-139 lines in 2 locations

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

@@ 1094-1232 (lines=139) @@
1091
		}
1092
		
1093
		
1094
		try	
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"); 
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
	
@@ 336-460 (lines=125) @@
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
			
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"); 
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