Code Duplication    Length = 292-292 lines in 2 locations

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

@@ 1135-1426 (lines=292) @@
1132
	 * @param unification
1133
	 * @throws Exception
1134
	 */
1135
	private void doApplyUnification(GoalUnification unification) 
1136
			throws FlawSolutionApplicationException {
1137
		
1138
		// get original goal
1139
		Decision goal = unification.getGoalDecision();
1140
		// get all (pending) relations concerning the planning goal
1141
		DomainComponent goalComp = goal.getComponent();
1142
1143
		// list of committed parameter constraints
1144
		Set<Relation> committed = new HashSet<>();
1145
		// list of translated parameter relations - reference
1146
		Set<Relation> translatedReferenceGoalRelations = new HashSet<>();
1147
		// list of translated parameter relations - target
1148
		Set<Relation> translatedTargetGoalRelations = new HashSet<>();
1149
		
1150
		
1151
		// get pending relations associated to the goal
1152
		Set<Relation> gRels = goalComp.getRelations(goal);
1153
		// translate pending relations by replacing goal's information with unification decision's information
1154
		for (Relation rel : gRels) {
1155
			
1156
			// check relation category
1157
			if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1158
				
1159
				// check relation reference
1160
				if (rel.getReference().equals(goal)) {
1161
					// replace reference 
1162
					rel.setReference(unification.getUnificationDecision());
1163
					// add relation to the list
1164
					translatedReferenceGoalRelations.add(rel);
1165
				}
1166
1167
				// check relation target
1168
				if (rel.getTarget().equals(goal)) {
1169
					// replace target
1170
					rel.setTarget(unification.getUnificationDecision());
1171
					// add relation to the list
1172
					translatedTargetGoalRelations.add(rel);
1173
				}
1174
				
1175
			}
1176
			
1177
			if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT))
1178
			{
1179
				// check relation type
1180
				switch (rel.getType())
1181
				{
1182
					// bind parameter
1183
					case BIND_PARAMETER: 
1184
					{
1185
						// the goal can be only the reference of the relation
1186
						ParameterRelation pRel = (ParameterRelation) rel;
1187
						
1188
						// get relation reference parameter label
1189
						String refParamLabel = pRel.getReferenceParameterLabel();
1190
						// get label index
1191
						int refParameterIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
1192
						// get unification decision parameter label
1193
						String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1194
1195
						// update reference decision 
1196
						pRel.setReference(unification.getUnificationDecision());
1197
						// update reference label of the relation 
1198
						pRel.setReferenceParameterLabel(label);
1199
						// add relation to the list of translated ones
1200
						translatedReferenceGoalRelations.add(pRel);
1201
					}
1202
					break;
1203
					
1204
					case EQUAL_PARAMETER : 
1205
					{
1206
						// get parameter relation
1207
						EqualParameterRelation eqRel = (EqualParameterRelation) rel;
1208
						// check if the goal is the reference or the parameter constraint 
1209
						if (eqRel.getReference().equals(goal))
1210
						{
1211
							// get relation reference parameter label
1212
							String refParamLabel = eqRel.getReferenceParameterLabel();
1213
							// get label index
1214
							int refParameterIndex = eqRel.getReference().getParameterIndexByLabel(refParamLabel);
1215
							// get unification decision parameter label
1216
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1217
1218
							// update reference decision 
1219
							eqRel.setReference(unification.getUnificationDecision());
1220
							// update reference label of the relation 
1221
							eqRel.setReferenceParameterLabel(label);
1222
							// add relation to the list of translated ones
1223
							translatedReferenceGoalRelations.add(eqRel);
1224
						}
1225
						else // the goal is the target of the relation 
1226
						{
1227
							// get relation reference parameter label
1228
							String refParamLabel = eqRel.getTargetParameterLabel();
1229
							// get label index
1230
							int refParameterIndex = eqRel.getTarget().getParameterIndexByLabel(refParamLabel);
1231
							// get unification decision parameter label
1232
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1233
1234
							// update reference decision 
1235
							eqRel.setTarget(unification.getUnificationDecision());
1236
							// update reference label of the relation 
1237
							eqRel.setTargetParameterLabel(label);
1238
							// add relation to the list of translated ones
1239
							translatedTargetGoalRelations.add(eqRel);
1240
						}
1241
					}
1242
					break;
1243
					
1244
					case NOT_EQUAL_PARAMETER : 
1245
					{
1246
						// get parameter relation
1247
						NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
1248
						// check if the goal is the reference or the parameter constraint 
1249
						if (neqRel.getReference().equals(goal))
1250
						{
1251
							// get relation reference parameter label
1252
							String refParamLabel = neqRel.getReferenceParameterLabel();
1253
							// get label index
1254
							int refParameterIndex = neqRel.getReference().getParameterIndexByLabel(refParamLabel);
1255
							// get unification decision parameter label
1256
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1257
1258
							// update reference decision 
1259
							neqRel.setReference(unification.getUnificationDecision());
1260
							// update reference label of the relation 
1261
							neqRel.setReferenceParameterLabel(label);
1262
							// add relation to the list of translated ones
1263
							translatedReferenceGoalRelations.add(neqRel);
1264
						}
1265
						else // the goal is the target of the relation 
1266
						{
1267
							// get relation reference parameter label
1268
							String refParamLabel = neqRel.getTargetParameterLabel();
1269
							// get label index
1270
							int refParameterIndex = neqRel.getTarget().getParameterIndexByLabel(refParamLabel);
1271
							// get unification decision parameter label
1272
							String label = unification.getUnificationDecision().getParameterLabelByIndex(refParameterIndex);
1273
1274
							// update reference decision 
1275
							neqRel.setTarget(unification.getUnificationDecision());
1276
							// update reference label of the relation 
1277
							neqRel.setTargetParameterLabel(label);
1278
							// add relation to the list of translated ones
1279
							translatedTargetGoalRelations.add(neqRel);
1280
						}
1281
					}
1282
					break;
1283
					
1284
					
1285
					default:
1286
						// unknown parameter relation
1287
						throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n");
1288
				}
1289
			}
1290
		}
1291
		
1292
		
1293
		try	
1294
		{
1295
			// remove original goal: PENDING -> SILENT
1296
			goalComp.free(goal);
1297
			
1298
			// activate translated relations
1299
			for (Relation rel : translatedReferenceGoalRelations) 
1300
			{
1301
				// check if can be activated
1302
				if (rel.getReference().getComponent().activate(rel)) {
1303
					
1304
					// add activated relations
1305
					unification.addActivatedRelation(rel);
1306
					// add relation to the committed list
1307
					committed.add(rel);
1308
				}
1309
			}
1310
			
1311
			// activate translated relations
1312
			for (Relation rel : translatedTargetGoalRelations) 
1313
			{
1314
				// check if can be activated
1315
				if (rel.getReference().getComponent().activate(rel)) {
1316
					
1317
					// add activated relations
1318
					unification.addActivatedRelation(rel);
1319
					// add relation to the committed list
1320
					committed.add(rel);
1321
				}
1322
			}
1323
1324
			// set translated relations
1325
			unification.setTranslatedReferenceGoalRelation(translatedReferenceGoalRelations);
1326
			unification.setTranslatedTargetGoalRealtion(translatedTargetGoalRelations);
1327
			
1328
			// check consistency
1329
			this.tdb.verify();
1330
			this.pdb.verify();
1331
			
1332
		} catch (RelationPropagationException | ConsistencyCheckException ex) {
1333
1334
			// restore goal: SILENT -> PENDING
1335
			goalComp.restore(goal);
1336
			// deactivate committed relations
1337
			for (Relation rel : committed) {
1338
				// get reference component
1339
				DomainComponent refComp = rel.getReference().getComponent();
1340
				refComp.deactivate(rel);
1341
			}
1342
			
1343
			
1344
			// translated back relations
1345
			for (Relation rel : translatedReferenceGoalRelations) {
1346
				// check category
1347
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
1348
					
1349
					// get parameter relation
1350
					ParameterRelation pRel = (ParameterRelation) rel;
1351
					// get relation reference parameter label
1352
					String refParamLabel = pRel.getReferenceParameterLabel();
1353
					// get label index
1354
					int pIndex = pRel.getReference().getParameterIndexByLabel(refParamLabel);
1355
					// get goal decision parameter label
1356
					String label = goal.getParameterLabelByIndex(pIndex);
1357
					
1358
					// update relation
1359
					pRel.setReference(goal);
1360
					pRel.setReferenceParameterLabel(label);
1361
				}
1362
				
1363
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1364
					// update relation
1365
					rel.setReference(goal);
1366
				}
1367
			}
1368
			
1369
			// translated back parameter relations
1370
			for (Relation rel : translatedTargetGoalRelations) {
1371
				
1372
				// check relation category 
1373
				if (rel.getCategory().equals(ConstraintCategory.PARAMETER_CONSTRAINT)) {
1374
					// check relation
1375
					switch (rel.getType()) {
1376
					
1377
						case EQUAL_PARAMETER : {
1378
							
1379
							// get equal relation
1380
							EqualParameterRelation eqRel = (EqualParameterRelation) rel;
1381
							// get relation reference parameter label
1382
							String tarParamLabel = eqRel.getTargetParameterLabel();
1383
							// get label index
1384
							int pIndex = eqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
1385
							// get goal decision parameter label
1386
							String label = goal.getParameterLabelByIndex(pIndex);
1387
							
1388
							// update relation
1389
							eqRel.setTarget(goal);
1390
							eqRel.setTargetParameterLabel(label);
1391
						}
1392
						break;
1393
							
1394
						case NOT_EQUAL_PARAMETER : {
1395
							
1396
							// get equal relation
1397
							NotEqualParameterRelation neqRel = (NotEqualParameterRelation) rel;
1398
							// get relation reference parameter label
1399
							String tarParamLabel = neqRel.getTargetParameterLabel();
1400
							// get label index
1401
							int pIndex = neqRel.getTarget().getParameterIndexByLabel(tarParamLabel);
1402
							// get goal decision parameter label
1403
							String label = goal.getParameterLabelByIndex(pIndex);
1404
							
1405
							// update relation
1406
							neqRel.setTarget(goal);
1407
							neqRel.setTargetParameterLabel(label);
1408
						}
1409
						break;
1410
						
1411
						default:
1412
							// unknown parameter relation
1413
							throw new RuntimeException("Unknown Parameter relation type : " + rel.getType() + "\n"); 
1414
							
1415
					}
1416
				}
1417
				
1418
				// check temporal relation
1419
				if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) {
1420
					// update relation
1421
					rel.setTarget(goal);
1422
				}
1423
			}
1424
1425
			// not feasible solution
1426
			throw new FlawSolutionApplicationException(ex.getMessage());
1427
		}
1428
	}
1429
	

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

@@ 951-1242 (lines=292) @@
948
	 * @param unification
949
	 * @throws Exception
950
	 */
951
	private void doApplyUnification(GoalUnification unification) 
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");
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"); 
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