From 429229d682143262907cf2539c2679c652cd75e0 Mon Sep 17 00:00:00 2001 From: cbrun Date: Fri, 29 Jun 2007 12:46:46 +0000 Subject: [PATCH] UPDATE : changes in metrics computing giving better results and performances FIX : merge stuffs and dirty thingies --- .../match/statistic/similarity/NameSimilarity.java | 49 ++++++++++++---------- .../statistic/similarity/StructureSimilarity.java | 37 +++++++++------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/NameSimilarity.java b/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/NameSimilarity.java index ecf38d212..3908c9580 100644 --- a/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/NameSimilarity.java +++ b/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/NameSimilarity.java @@ -13,6 +13,7 @@ package org.eclipse.emf.compare.match.statistic.similarity; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.WeakHashMap; import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.compare.match.statistic.MetamodelFilter; @@ -32,6 +33,8 @@ public final class NameSimilarity { private static final int MAX_FEATURE_VALUE_LENGTH = 40; private static final String EOBJECT_NAME_FEATURE = "name"; //$NON-NLS-1$ + + private static final WeakHashMap nameFeature = new WeakHashMap(); private NameSimilarity() { // prevents instantiation @@ -206,30 +209,32 @@ public final class NameSimilarity { * Thrown if an operation on current fails. */ public static EAttribute findNameFeature(EObject current) throws FactoryException { - final EObject eclass = current.eClass(); - - List eclassAttributes = new LinkedList(); - if (eclass instanceof EClass) - eclassAttributes = ((EClass)eclass).getEAllAttributes(); - EAttribute bestFeature = null; - if (eclassAttributes.size() > 0) { - bestFeature = (EAttribute)eclassAttributes.get(0); - } - // first, find the eclass structural feature most similar with name - if (eclassAttributes.size() > 0) { - double max = 0; - final Iterator it = eclassAttributes.iterator(); - while (it.hasNext()) { // for each metamodel feature - final Object next = it.next(); - if (next instanceof EObject) { - final EObject obj = (EObject)next; - final String attributeName = EFactory.eGetAsString(obj, EOBJECT_NAME_FEATURE); - // if the attributeName is more similar with "name" than the other one - if (nameSimilarityMetric(attributeName, EOBJECT_NAME_FEATURE) > max) { - max = nameSimilarityMetric(attributeName, EOBJECT_NAME_FEATURE); - bestFeature = (EAttribute)obj; + final EClass eclass = current.eClass(); + EAttribute bestFeature = nameFeature.get(eclass); + + if (bestFeature == null) { + List eclassAttributes = new LinkedList(); + eclassAttributes = eclass.getEAllAttributes(); + if (eclassAttributes.size() > 0) { + bestFeature = (EAttribute)eclassAttributes.get(0); + } + // first, find the eclass structural feature most similar with name + if (eclassAttributes.size() > 0) { + double max = 0; + final Iterator it = eclassAttributes.iterator(); + while (it.hasNext()) { // for each metamodel feature + final Object next = it.next(); + if (next instanceof EObject) { + final EObject obj = (EObject)next; + final String attributeName = EFactory.eGetAsString(obj, EOBJECT_NAME_FEATURE); + // if the attributeName is more similar with "name" than the other one + if (nameSimilarityMetric(attributeName, EOBJECT_NAME_FEATURE) > max) { + max = nameSimilarityMetric(attributeName, EOBJECT_NAME_FEATURE); + bestFeature = (EAttribute)obj; + } } } + nameFeature.put(eclass, bestFeature); } } // now we should return the feature value diff --git a/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/StructureSimilarity.java b/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/StructureSimilarity.java index b00cd85c5..e0c650adf 100644 --- a/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/StructureSimilarity.java +++ b/plugins/org.eclipse.emf.compare.match/src/org/eclipse/emf/compare/match/statistic/similarity/StructureSimilarity.java @@ -20,6 +20,7 @@ import org.eclipse.emf.compare.util.EFactory; import org.eclipse.emf.compare.util.FactoryException; import org.eclipse.emf.ecore.EClass; import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; import org.eclipse.emf.ecore.EStructuralFeature; /** @@ -28,8 +29,6 @@ import org.eclipse.emf.ecore.EStructuralFeature; * @author Cedric Brun cedric.brun@obeo.fr */ public final class StructureSimilarity { - private static final int MAX_OBJECT_RELATIONS = 14; - private StructureSimilarity() { // prevents instantiation } @@ -154,27 +153,33 @@ public final class StructureSimilarity { public static String relationsValue(EObject current, MetamodelFilter filter) throws FactoryException { final EObject eclass = current.eClass(); final StringBuffer result = new StringBuffer(); - List eclassAttributes = new LinkedList(); + List eObjectFeatures = new LinkedList(); if (eclass instanceof EClass) { if (filter != null) - eclassAttributes = filter.getFilteredFeatures(current); + eObjectFeatures = filter.getFilteredFeatures(current); else - eclassAttributes = ((EClass)eclass).getEAllAttributes(); + eObjectFeatures = ((EClass)eclass).getEAllReferences(); + } + for (EStructuralFeature feature : eObjectFeatures) { + if (feature instanceof EReference && !((EReference)feature).isDerived()) { + Object value = current.eGet(feature); + if (value instanceof List) { + for (final Iterator valueIterator = ((List)value).iterator(); valueIterator.hasNext(); ) { + Object next = valueIterator.next(); + if (next instanceof EObject) { + String objName = NameSimilarity.findName((EObject)next); + result.append(objName); + } + } + } else if (value instanceof EObject) { + String objName = NameSimilarity.findName((EObject)value); + result.append(objName); + } + } } if (current.eContainer() != null) result.append(NameSimilarity.findName(current.eContainer())).append("\n"); //$NON-NLS-1$ - final List objectContents = current.eContents(); - final Iterator it = objectContents.iterator(); - int curIndex = 0; // to keep track and stop if we are too far - while (it.hasNext() && curIndex < MAX_OBJECT_RELATIONS) { - final Object next = it.next(); - if (next instanceof EObject) { - final EObject obj = (EObject)next; - result.append(NameSimilarity.findName(obj)).append("\n"); //$NON-NLS-1$ - } - curIndex += 1; - } return result.toString(); } } -- 2.11.4.GIT