| Conditions | 15 |
| Total Lines | 198 |
| Code Lines | 113 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
Complex classes like it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.plan.SolutionPlan.generatePlanDescriptor() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package it.cnr.istc.pst.platinum.ai.framework.microkernel.lang.plan; |
||
| 336 | protected PlanProtocolDescriptor generatePlanDescriptor() |
||
| 337 | { |
||
| 338 | // get language factory |
||
| 339 | ProtocolLanguageFactory factory = new ProtocolLanguageFactory(this.horizion); |
||
| 340 | |||
| 341 | // create plan descriptor |
||
| 342 | PlanProtocolDescriptor plan = factory.createPlanDescriptor(this.name, 0, this.horizion); |
||
| 343 | // create an index |
||
| 344 | Map<Token, TokenProtocolDescriptor> index = new HashMap<>(); |
||
| 345 | // create timeline descriptors |
||
| 346 | for (Timeline tl : this.timelines) |
||
| 347 | { |
||
| 348 | // get the state variable related to the timeline |
||
| 349 | StateVariable comp = tl.getComponent(); |
||
| 350 | // initialize descriptor |
||
| 351 | TimelineProtocolDescriptor timelineDescriptor = factory.createTimelineDescriptor( |
||
| 352 | comp.getName(), |
||
| 353 | tl.getName(), |
||
| 354 | tl.isObservation()); |
||
| 355 | |||
| 356 | // get tokens of the timeline |
||
| 357 | for (Token token : tl.getTokens()) |
||
| 358 | { |
||
| 359 | // prepare the array of parameter names, values, and types |
||
| 360 | String[] paramNames = new String[token.getPredicate().getParameters().length]; |
||
| 361 | ParameterTypeDescriptor[] paramTypes = new ParameterTypeDescriptor[token.getPredicate().getParameters().length]; |
||
| 362 | long[][] paramBounds = new long[token.getPredicate().getParameters().length][]; |
||
| 363 | String[][] paramValues = new String[token.getPredicate().getParameters().length][]; |
||
| 364 | for (int i = 0; i < token.getPredicate().getParameters().length; i++) |
||
| 365 | { |
||
| 366 | // get parameter |
||
| 367 | Parameter<?> param = token.getPredicate().getParameterByIndex(i); |
||
| 368 | // set parameter name |
||
| 369 | paramNames[i] = param.getLabel(); |
||
| 370 | |||
| 371 | // check parameter type |
||
| 372 | if (param.getType().equals(ParameterType.NUMERIC_PARAMETER_TYPE)) |
||
| 373 | { |
||
| 374 | // get numeric parameter |
||
| 375 | NumericParameter numPar = (NumericParameter) param; |
||
| 376 | // set lower bound and upper bound |
||
| 377 | paramBounds[i] = new long[] { |
||
| 378 | numPar.getLowerBound(), |
||
| 379 | numPar.getUpperBound() |
||
| 380 | }; |
||
| 381 | // set default value to parameter values |
||
| 382 | paramValues[i] = new String[] {}; |
||
| 383 | // set parameter type |
||
| 384 | paramTypes[i] = ParameterTypeDescriptor.NUMERIC; |
||
| 385 | } |
||
| 386 | else if (param.getType().equals(ParameterType.ENUMERATION_PARAMETER_TYPE)) |
||
| 387 | { |
||
| 388 | // enumeration parameter |
||
| 389 | EnumerationParameter enuPar = (EnumerationParameter) param; |
||
| 390 | // one single value is expected |
||
| 391 | paramValues[i] = new String[] { |
||
| 392 | enuPar.getValues()[0] |
||
| 393 | }; |
||
| 394 | // set default value to parameter bounds |
||
| 395 | paramBounds[i] = new long[] {}; |
||
| 396 | // set parameter type |
||
| 397 | paramTypes[i] = ParameterTypeDescriptor.ENUMERATION; |
||
| 398 | } |
||
| 399 | else { |
||
| 400 | throw new RuntimeException("Unknown parameter type:\n- type: " + param.getType() + "\n"); |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | // create token descriptor |
||
| 405 | TokenProtocolDescriptor tokenDescriptor = factory.createTokenDescriptor( |
||
| 406 | timelineDescriptor, |
||
| 407 | token.getPredicate().getValue().getLabel(), |
||
| 408 | new long [] { |
||
| 409 | token.getInterval().getStartTime().getLowerBound(), |
||
| 410 | token.getInterval().getStartTime().getUpperBound() |
||
| 411 | }, |
||
| 412 | new long[] { |
||
| 413 | token.getInterval().getEndTime().getLowerBound(), |
||
| 414 | token.getInterval().getEndTime().getUpperBound() |
||
| 415 | }, |
||
| 416 | new long[] { |
||
| 417 | token.getInterval().getDurationLowerBound(), |
||
| 418 | token.getInterval().getDurationUpperBound() |
||
| 419 | }, |
||
| 420 | paramNames, paramTypes, paramBounds, paramValues, token.getStartExecutionState()); |
||
| 421 | |||
| 422 | // update index |
||
| 423 | index.put(token, tokenDescriptor); |
||
| 424 | } |
||
| 425 | |||
| 426 | // add timeline to plan |
||
| 427 | plan.addTimeline(timelineDescriptor); |
||
| 428 | } |
||
| 429 | |||
| 430 | // create timeline descriptors |
||
| 431 | for (Timeline tl : this.observations) |
||
| 432 | { |
||
| 433 | // get the state variable related to the timeline |
||
| 434 | StateVariable comp = tl.getComponent(); |
||
| 435 | // initialize descriptor |
||
| 436 | TimelineProtocolDescriptor timelineDescriptor = factory.createTimelineDescriptor( |
||
| 437 | comp.getName(), |
||
| 438 | tl.getName(), |
||
| 439 | tl.isObservation()); |
||
| 440 | |||
| 441 | // get tokens of the timeline |
||
| 442 | for (Token token : tl.getTokens()) |
||
| 443 | { |
||
| 444 | // prepare the array of parameter names, values, and types |
||
| 445 | String[] paramNames = new String[token.getPredicate().getParameters().length]; |
||
| 446 | ParameterTypeDescriptor[] paramTypes = new ParameterTypeDescriptor[token.getPredicate().getParameters().length]; |
||
| 447 | long[][] paramBounds = new long[token.getPredicate().getParameters().length][]; |
||
| 448 | String[][] paramValues = new String[token.getPredicate().getParameters().length][]; |
||
| 449 | for (int i = 0; i < token.getPredicate().getParameters().length; i++) |
||
| 450 | { |
||
| 451 | // get parameter |
||
| 452 | Parameter<?> param = token.getPredicate().getParameterByIndex(i); |
||
| 453 | // check parameter type |
||
| 454 | if (param.getType().equals(ParameterType.NUMERIC_PARAMETER_TYPE)) { |
||
| 455 | // get numeric parameter |
||
| 456 | NumericParameter numPar = (NumericParameter) param; |
||
| 457 | // set lower bound and upper bound |
||
| 458 | paramBounds[i] = new long[] { |
||
| 459 | numPar.getLowerBound(), |
||
| 460 | numPar.getUpperBound() |
||
| 461 | }; |
||
| 462 | // set default value to parameter values |
||
| 463 | paramValues[i] = new String[] {}; |
||
| 464 | } |
||
| 465 | else if (param.getType().equals(ParameterType.ENUMERATION_PARAMETER_TYPE)) { |
||
| 466 | // enumeration parameter |
||
| 467 | EnumerationParameter enuPar = (EnumerationParameter) param; |
||
| 468 | // one single value is expected |
||
| 469 | paramValues[i] = new String[] { |
||
| 470 | enuPar.getValues().toString() |
||
| 471 | }; |
||
| 472 | // set default value to parameter bounds |
||
| 473 | paramBounds[i] = new long[] {}; |
||
| 474 | } |
||
| 475 | else { |
||
| 476 | throw new RuntimeException("Unknown parameter type:\n- type: " + param.getType() + "\n"); |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | // create token descriptor |
||
| 481 | TokenProtocolDescriptor tokenDescriptor = factory.createTokenDescriptor( |
||
| 482 | timelineDescriptor, |
||
| 483 | token.getPredicate().getValue().getLabel(), |
||
| 484 | new long [] { |
||
| 485 | token.getInterval().getStartTime().getLowerBound(), |
||
| 486 | token.getInterval().getStartTime().getUpperBound() |
||
| 487 | }, |
||
| 488 | new long[] { |
||
| 489 | token.getInterval().getEndTime().getLowerBound(), |
||
| 490 | token.getInterval().getEndTime().getUpperBound() |
||
| 491 | }, |
||
| 492 | new long[] { |
||
| 493 | token.getInterval().getDurationLowerBound(), |
||
| 494 | token.getInterval().getDurationUpperBound() |
||
| 495 | }, |
||
| 496 | paramNames, paramTypes, paramBounds, paramValues, token.getStartExecutionState()); |
||
| 497 | |||
| 498 | // update index |
||
| 499 | index.put(token, tokenDescriptor); |
||
| 500 | } |
||
| 501 | |||
| 502 | // add timeline to plan |
||
| 503 | plan.addTimeline(timelineDescriptor); |
||
| 504 | } |
||
| 505 | |||
| 506 | // create relation descriptors |
||
| 507 | for (Relation relation : this.relations) |
||
| 508 | { |
||
| 509 | // export temporal relations only |
||
| 510 | if (relation.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) |
||
| 511 | { |
||
| 512 | // consider only relations between values of state variables |
||
| 513 | if (relation.getReference().getComponent() instanceof StateVariable && |
||
| 514 | relation.getTarget().getComponent() instanceof StateVariable) |
||
| 515 | { |
||
| 516 | // get temporal relation |
||
| 517 | TemporalRelation trel = (TemporalRelation) relation; |
||
| 518 | // create relation description |
||
| 519 | RelationProtocolDescriptor relDescriptor = factory.createRelationDescriptor( |
||
| 520 | relation.getType().name().toUpperCase(), |
||
| 521 | index.get(relation.getReference().getToken()), |
||
| 522 | index.get(relation.getTarget().getToken())); |
||
| 523 | |||
| 524 | // set bounds |
||
| 525 | relDescriptor.setBounds(trel.getBounds()); |
||
| 526 | // add relation descriptor to plan |
||
| 527 | plan.addRelation(relDescriptor); |
||
| 528 | } |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | // return plan descriptor |
||
| 533 | return plan; |
||
| 534 | } |
||
| 658 |