• No results found

Matching Algorithms

A Case-Based Approach for Reuse in Software Design

4.1 Retrieval Module

4.1.3 Matching Algorithms

This subsection presents the algorithms used in REBUILDER to match lists of: pack-ages, objects, relations, attributes, methods, and parameters. These algorithms are used by the similarity metrics dealing with lists of elements. For instance, the sub package list similarity metric (see subsection 4.1.2) receives two lists of packages and has to establish a mapping between the elements of these lists. The next subsections describe the matching algorithms.

Package Matching Algorithm

The package matching algorithm is used in the sub package list similarity metric (see subsection 4.1.2) to match two lists of packages. The input are two lists: LP1 and LP2, it returns: a list of package mappings, the respective mapping similarities, and

1. M appings ←?

2. FORALL Package (P k1) in LP1DO

3. List1← Get all packages with equivalent classification as P k1 in LP2

4. Ranked1← Rank List1 by similarity to P k1

5. IF Ranked16= ∅ THEN

6. P k2← Best package in Ranked1

7. Add (P k1, P k2, Sim(P k1, P k2)) to M appings 8. Remove P k1from LP1and P k2 from LP2

9. ENDIF

10. ENFOR

11. RETURN M appings, #LP1

Figure 4.5: The package matching algorithm used in REBUILDER.

the number of unmatched packages in LP1. The algorithm is presented in figure 4.5.

Note that two objects have equivalent classification if they have the same name and synsets.

The algorithm starts by initializing the mappings list (Mappings) as empty, and then goes into a cycle, where for each package (P k1) in LP1 it performs the following operations: get all the packages in LP2 with the same classification as P k1, that is, packages with the same synset; rank the found packages by similarity with P k1; if the resulting list is not empty, then the best package is chosen, mapped to P k1, and the mappings added to Mappings; the mapped packages are removed from the respective lists. At the end of the cycle the Mappings and the number of unmapped packages in LP1 are returned.

Package Objects Matching Algorithm

During the assessment of the diagram similarity (see subsection 4.1.2) it is necessary to match the UML objects from two lists. Object matching uses two algorithms, both based on structural matching. One is guided by relation mapping (see figure 4.6), and the other is guided by object matching (see figure 4.7). These algorithms are the core of the mapping phase for analogical reasoning (see section 4.2).

Considering a problem (or query) diagram that has to be matched with a case diagram, the relation guided algorithm starts by choosing the best relation from the problem diagram based on an independence measure, which is described in the next section. This relation is added to the current relation list (Relations). Then the list of mappings (MappingList) is initialized as empty. The algorithm then enters a cycle while there are relations in Relations to be processed, comprising: get the best relation from Relations based on the independence measure; select the best

CHAPTER 4. CBR Engine

1. Relations ← Get best relation from the query diagram based on independence measure 2. M appingList ←?

3. WHILE Relations 6=? DO

4. P Relation ← Get best relation from Relations based on the independence measure 5. CRelation ← Get best matching relation from the base case relations that are

candidates (structural constraints must be met)

6. M apping ← Get the mapping between objects, based on the mapping between P Relation and CRelation

7. Add M apping to M appingList 8. Remove P Relation from Relations

9. Add to Relations all the same type relations adjacent to P Relations, which are not already mapped (if P Relation connects A and B then the adjacent relations of P Relations are the relations in which A or B are part of, excluding P Relation) 10. ENDWHILE

11. Return M appingList

Figure 4.6: The object matching algorithm guided by relation mapping.

1. Objects ←Get object from query diagram which has the highest independence measure 2. M appingList ←?

3. WHILE Objects 6=? DO

4. P Object ← Get best object from Objects, based on the object’s independence measure 5. CObject ← Get best matching object from the base case objects that are matching

candidates (structural constraints must be met) 6. M apping ← Get the mapping between P Object and CObject 7. Remove P Object from Objects

8. Remove CObject from the base case objects that are matching candidates.

9. Add M apping to M appingList

10. Add to Objects all the objects adjacent to P Object which are not already mapped (an adjacent object B to an object A, is every object that has a relation with A).

11. ENDWHILE 12. Return M appingList

Figure 4.7: The matching algorithm guided by object mapping.

matching relation from the case relations, taking into account structural constraints;

the objects in both relations are mapped and they are added to the MappingList;

the mapped relations are removed from the respective lists; the adjacent relations are added to the Relations list expanding the mapping. At the end the algorithm returns the MappingList.

The second matching algorithm, guided by object mappings, takes as input a problem list comprising UML objects and a case6 object list. The algorithm starts by selecting the best object from the problem list and adds it to the list of current objects (Objects). The best object is the one which has the highest independence measure.

The list of mappings (MappingList) is setup to empty. Then the algorithm enters into a cycle while there are objects to be processed in Objects. The instructions performed in this cycle are: get the best object from Objects based on the independence measure;

choose the best matching object from the case object list, based on the structural constraints and on the semantic distance between both objects’ synsets; map the

6Also called base case, since it is the case used for matching.

chosen objects and add this mapping to the MappingList; remove both objects from the respective lists; add the adjacent objects that are not mapped to the query object to Objects. At the end the algorithm returns the MappingList.

Relation Matching Algorithm

This algorithm is used in the inter-class similarity metric (see subsection 4.1.2) to match the problem relations with the retrieved case relations. The input are two lists: LR1and LR2. It returns a list of relation mappings with the respective mapping similarities and the number of unmatched relations in LR1 discriminated by the type of relation. The algorithm is presented in figure 4.8.

The relation matching algorithm performs the same process for each of the four types of relations: associations, generalizations, dependencies and realizations. The list of mappings (Mappings) is setup to empty. Then the algorithm gets all associa-tions, generalizaassocia-tions, dependencies and realizations in LR1, respectively, to lists A1, G1, D1 and R1, and all the associations, generalizations, dependencies and realiza-tions in LR2, respectively, to lists A2, G2, D2 and R2. The relations in A1 are ranked using the independence measure. Then for each relation (Rel1) in A1 it is performed the following: rank the relations in A2 regarding to the similarity with Rel1; if this results into relations in the ranked list, then select the best one (Rel2) and map it to Rel1 and add it to Mappings; both relations are removed from the respective lists.

Then the same mapping process is performed for generalizations (lines 19 to 27), de-pendencies (lines 28 to 36) and realizations (lines 37 to 45). At the end the algorithm returns Mappings and the number of unmatched relations discriminated by relation type.

Attribute Matching Algorithm

The attribute matching algorithm is used in the attribute list similarity metric (see subsection 4.1.2) to match two lists of attributes. The input comprises two lists: LA1 and LA2. It returns a list of attribute mappings with the respective mapping simi-larities and the number of unmatched attributes in LA1. The algorithm is presented in figure 4.9.

CHAPTER 4. CBR Engine

1. M appings ←?

2. A1← Get all associations from LR1

3. G1← Get all generalizations from LR1

4. D1← Get all dependencies from LR1

5. R1← Get all realizations from LR1

6. A2← Get all associations from LR2

7. G2← Get all generalizations from LR2

8. D2← Get all dependencies from LR2

9. R2← Get all realizations from LR2

10. Rank A1 using the independence measures 11. FORALL Relation (Rel1) in A1DO 12. Ranked1← Rank A2 in relation to Rel1

13. IF Ranked16= ∅ THEN

14. Rel2← Best relation in Ranked1

15. Add (Rel1, Rel2, Sim(Rel1, Rel2)) to M appings 16. Remove Rel1 from A1 and Rel2from A2

17. ENDIF

18. ENFOR

19. Rank G1using the independence measures 20. FORALL Relation (Rel1) in G1 DO 21. Ranked1← Rank G2in relation to Rel1

22. IF Ranked16= ∅ THEN

23. Rel2← Best relation in Ranked1

24. Add (Rel1, Rel2, Sim(Rel1, Rel2)) to M appings 25. Remove Rel1 from G1and Rel2 from G2

26. ENDIF

27. ENFOR

28. Rank D1 using the independence measures 29. FORALL Relation (Rel1) in D1DO 30. Ranked1← Rank D2 in relation to Rel1

31. IF Ranked16= ∅ THEN

32. Rel2← Best relation in Ranked1

33. Add (Rel1, Rel2, Sim(Rel1, Rel2)) to M appings 34. Remove Rel1 from D1 and Rel2from D2

35. ENDIF

36. ENFOR

37. Rank R1 using the independence measures 38. FORALL Relation (Rel1) in R1DO 39. Ranked1← Rank R2in relation to Rel1

40. IF Ranked16= ∅ THEN

41. Rel2← Best relation in Ranked1

42. Add (Rel1, Rel2, Sim(Rel1, Rel2)) to M appings 43. Remove Rel1 from R1 and Rel2from R2

44. ENDIF

45. ENFOR

46. RETURN M appings, #A1+ #G1+ #D1+ #R1

Figure 4.8: The relation matching algorithm used in REBUILDER.

1. M appings ←?

12. RETURN M appings, #LA1

Figure 4.9: The attribute matching algorithm used in REBUILDER.

1. M appings ←?

2. FORALL Method (M1) in LM1 DO

3. List1← Get all methods from LM2with the same Input and Output parameters as M1

4. Ranked1← Rank List1 by similarity to M1

5. IF Ranked16= ∅ THEN

6. M2← Best method in Ranked1

7. Add (M1, M2, Sim(M1, M2)) to M appings 8. Remove M1 from LM1and M2 from LM2

9. ENDIF

10. ENFOR

11. RETURN M appings, #LM1

Figure 4.10: The method matching algorithm used in REBUILDER.

The attribute matching algorithm starts by initializing the list of mappings (Mappins) to empty. Then for each attribute (A1) in LA1 it performs: get all at-tributes in LA2 with the same synset as A1; rank the resulting list based on the similarity with A1 (note that the name is also taken into account); if this results into attributes in the ranked list, then select the best one (A2) and map it with A1, adding it to Mappings; remove both attributes from the respective lists. At the end the algorithm returns Mappings and the number of elements in LA1.

Method Matching Algorithm

The method matching algorithm is used in the method list similarity metric (see subsection 4.1.2) to match two lists of methods. The input are two lists: LM1 and LM2. It returns a list of method mappings with the respective mapping similarities and the number of unmatched methods in LM1. The algorithm is presented in figure 4.10.

The method matching algorithm starts by initializing the list of mappings (Mappins) to empty. Then for each method (M1) in LM1 it performs: get all the methods in LM2 with the same input and output parameters as M1; rank the result-ing list based on the similarity with M1; if this results into methods in the ranked list, then select the best one (M2) and map it with M1, adding the mapping to Mappings;

both methods are removed from the respective lists. At the end the algorithm returns Mappings and the number of elements in LM1.

CHAPTER 4. CBR Engine

11. RETURN M appings, #LP1

Figure 4.11: The parameter matching algorithm used in REBUILDER.

Parameter Matching Algorithm

The parameter matching algorithm is used in the parameter list similarity metric (see subsection 4.1.2) to match two lists of parameters. The input comprises two lists: LP1 and LP2, it returns a list of parameter mappings with the respective map-ping similarities and the number of unmatched parameters in LP1. The algorithm is presented in figure 4.11. It is similar to the method matching algorithm.