From 8151ad31a9c2df9d5e74db4b2f022186693d9307 Mon Sep 17 00:00:00 2001 From: "stephan.pirnbaum" Date: Mon, 29 Jul 2019 19:38:28 +0200 Subject: [PATCH] #27 fixed execution of user defined rules --- .../ClassificationConfigurationExecutor.java | 67 ++++++++++--------- .../repository/ComponentRepository.java | 6 +- .../DependencyCriterionDescriptor.java | 2 + .../PackageNamingCriterionDescriptor.java | 2 + .../TypeNamingCriterionDescriptor.java | 2 + 5 files changed, 48 insertions(+), 31 deletions(-) diff --git a/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/configuration/ClassificationConfigurationExecutor.java b/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/configuration/ClassificationConfigurationExecutor.java index 7f52442..4e8cc18 100644 --- a/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/configuration/ClassificationConfigurationExecutor.java +++ b/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/configuration/ClassificationConfigurationExecutor.java @@ -4,11 +4,10 @@ import com.buschmais.sarf.core.framework.metamodel.ComponentDescriptor; import com.buschmais.sarf.core.framework.repository.ComponentRepository; import com.buschmais.sarf.core.framework.repository.TypeRepository; -import com.buschmais.sarf.core.plugin.api.ExecutedBy; import com.buschmais.sarf.core.plugin.api.Executor; import com.buschmais.sarf.core.plugin.api.criterion.ClassificationCriterionDescriptor; -import com.buschmais.sarf.core.plugin.api.criterion.ClassificationCriterionExecutor; import com.buschmais.sarf.core.plugin.api.criterion.RuleBasedCriterionDescriptor; +import com.buschmais.sarf.core.plugin.api.criterion.RuleBasedCriterionExecutor; import com.buschmais.sarf.core.plugin.chorddiagram.ChordDiagramExporter; import com.buschmais.sarf.core.plugin.cohesion.CohesionCriterionDescriptor; import com.buschmais.sarf.core.plugin.cohesion.CohesionCriterionExecutor; @@ -56,6 +55,7 @@ public class ClassificationConfigurationExecutor implements Executor executeRuleBaseCriteria(Set executorClass = ruleBasedCriteria.getClass().getAnnotation(ExecutedBy.class).value(); - if (executorClass.isAssignableFrom(ClassificationCriterionExecutor.class)) { - @SuppressWarnings("unchecked") - ClassificationCriterionExecutor executor = - this.beanFactory.getBean((Class) executorClass); - components.addAll(executor.execute(ruleBasedCriterion)); - } + Set componentDescriptors = ruleBasedCriterionExecutor.execute(ruleBasedCriterion); + components.addAll(componentDescriptors); } this.xoManager.currentTransaction().commit(); @@ -296,33 +291,45 @@ private Set executeCohesionCriterion(CohesionCriterionDescr private Set mergeComponents(Set cohesionResult, Set userResult, Integer iteration) { //so we have several solutions, time to make one out of them :) - // 2 types of identified components exist: - // -user-defined ones - // -self-created ones (start with a hash # sign) - // - for (ComponentDescriptor userComponent : userResult) { - for (ComponentDescriptor cohesionComponent : cohesionResult) { - this.xoManager.currentTransaction().begin(); - LOGGER.debug("Trying to merge user component: {}:{} with cohesion component: {}:{}", - userComponent.getShape(), userComponent.getName(), cohesionComponent.getShape(), cohesionComponent.getName()); - Double tversky = computeTverskyIndex(componentRepository, userComponent, cohesionComponent, iteration); - if (tversky > 0.75) { - LOGGER.info("Merging user component {}:{} with cohesion component: {}:{} (Tversky Index: {})", - userComponent.getShape(), userComponent.getName(), cohesionComponent.getShape(), cohesionComponent.getName(), tversky); - cohesionComponent.setShape(userComponent.getShape()); - cohesionComponent.setName(userComponent.getName()); - } - this.xoManager.currentTransaction().commit(); + this.xoManager.currentTransaction().begin(); - // check for contained components - mergeComponents(cohesionComponent.getContainedComponents(), userResult, iteration); + Set cohesionComponents = getComponentHierarchy(cohesionResult); + + for (ComponentDescriptor cohesionComponent : cohesionComponents) { + ComponentDescriptor bestUserComponent = null; + Double bestUserComponentSimilarity = 0.75; + for (ComponentDescriptor userComponent : userResult) { + Double similarity = computeTverskyIndex(userComponent, cohesionComponent, iteration); + System.out.println(similarity); + if (similarity > bestUserComponentSimilarity) { + bestUserComponent = userComponent; + bestUserComponentSimilarity = similarity; + } + } + if (bestUserComponent != null) { + LOGGER.info("Merging user component {}:{} with cohesion component: {}:{} (Tversky Index: {})", + bestUserComponent.getShape(), bestUserComponent.getName(), cohesionComponent.getShape(), cohesionComponent.getName(), bestUserComponentSimilarity); } } + + this.xoManager.currentTransaction().commit(); return cohesionResult; } - private Double computeTverskyIndex(ComponentRepository componentRepository, ComponentDescriptor userComponent, ComponentDescriptor cohesionComponent, Integer iteration) { - double jaccard = componentRepository.computeJaccardSimilarity( + private Set getComponentHierarchy(Set componentDescriptors) { + Set childs = new HashSet<>(); + for (Object o : componentDescriptors) { + if (o instanceof ComponentDescriptor) { + ComponentDescriptor c = (ComponentDescriptor) o; + childs.add(c); + childs.addAll(getComponentHierarchy(c.getContainedComponents())); + } + } + return childs; + } + + private Double computeTverskyIndex(ComponentDescriptor userComponent, ComponentDescriptor cohesionComponent, Integer iteration) { + Double jaccard = componentRepository.computeJaccardSimilarity( cohesionComponent.getShape(), cohesionComponent.getName(), userComponent.getShape(), userComponent.getName(), iteration); diff --git a/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/repository/ComponentRepository.java b/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/repository/ComponentRepository.java index dd69050..55eea51 100644 --- a/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/repository/ComponentRepository.java +++ b/sar-framework-core/src/main/java/com/buschmais/sarf/core/framework/repository/ComponentRepository.java @@ -18,6 +18,10 @@ @Repository public interface ComponentRepository extends TypedNeo4jRepository { + @ResultOf + @Cypher("MATCH (c:SARF:Component) RETURN c") + Result findAll(); + @ResultOf @Cypher("MATCH" + " (conf:ClassificationConfiguration) " + @@ -73,7 +77,7 @@ public interface ComponentRepository extends TypedNeo4jRepository {} diff --git a/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/packagenaming/PackageNamingCriterionDescriptor.java b/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/packagenaming/PackageNamingCriterionDescriptor.java index 8bfeb44..f1ee100 100644 --- a/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/packagenaming/PackageNamingCriterionDescriptor.java +++ b/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/packagenaming/PackageNamingCriterionDescriptor.java @@ -1,10 +1,12 @@ package com.buschmais.sarf.core.plugin.packagenaming; +import com.buschmais.sarf.core.plugin.api.ExecutedBy; import com.buschmais.sarf.core.plugin.api.criterion.RuleBasedCriterionDescriptor; import com.buschmais.xo.neo4j.api.annotation.Label; /** * @author Stephan Pirnbaum */ +@ExecutedBy(PackageNamingRuleExecutor.class) @Label("PackageNamingCriterion") public interface PackageNamingCriterionDescriptor extends RuleBasedCriterionDescriptor {} diff --git a/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/typenaming/TypeNamingCriterionDescriptor.java b/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/typenaming/TypeNamingCriterionDescriptor.java index e6e8c4d..36fda8d 100644 --- a/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/typenaming/TypeNamingCriterionDescriptor.java +++ b/sar-framework-core/src/main/java/com/buschmais/sarf/core/plugin/typenaming/TypeNamingCriterionDescriptor.java @@ -1,10 +1,12 @@ package com.buschmais.sarf.core.plugin.typenaming; +import com.buschmais.sarf.core.plugin.api.ExecutedBy; import com.buschmais.sarf.core.plugin.api.criterion.RuleBasedCriterionDescriptor; import com.buschmais.xo.neo4j.api.annotation.Label; /** * @author Stephan Pirnbaum */ +@ExecutedBy(TypeNamingRuleExecutor.class) @Label("TypeNamingCriterion") public interface TypeNamingCriterionDescriptor extends RuleBasedCriterionDescriptor {}