Skip to content
Snippets Groups Projects
Commit 402e981e authored by Eddie Groh's avatar Eddie Groh
Browse files

Refactored HierarchicElementSizeProvider

Issue-Ref: 4310
Issue-Url: af3#4310


Signed-off-by: default avatarEddie Groh <groh@fortiss.org>
parent ee6c232a
No related branches found
No related tags found
1 merge request!210Setting up Metric extraction plugin for AF3 : Issue 4310
...@@ -5,6 +5,8 @@ Bundle-SymbolicName: org.fortiss.tooling.ext.quality.ui;singleton:=true ...@@ -5,6 +5,8 @@ Bundle-SymbolicName: org.fortiss.tooling.ext.quality.ui;singleton:=true
Bundle-Version: 1.0.0 Bundle-Version: 1.0.0
Automatic-Module-Name: org.fortiss.tooling.ext.quality.ui Automatic-Module-Name: org.fortiss.tooling.ext.quality.ui
Bundle-RequiredExecutionEnvironment: JavaSE-11 Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-Vendor: %providerName
Bundle-Activator: org.fortiss.tooling.ext.quality.AF3QualityActivator
Require-Bundle: org.eclipse.ui.ide;visibility:=reexport, Require-Bundle: org.eclipse.ui.ide;visibility:=reexport,
org.fortiss.tooling.base.ui;visibility:=reexport, org.fortiss.tooling.base.ui;visibility:=reexport,
org.fortiss.tooling.kernel, org.fortiss.tooling.kernel,
......
...@@ -2,10 +2,7 @@ ...@@ -2,10 +2,7 @@
source.. = src/,\ source.. = src/,\
generated-src/ test-src/
output.. = build/
bin.includes = .,\ bin.includes = .,\
model/,\
META-INF/,\ META-INF/,\
plugin.xml,\ plugin.xml
plugin.properties
...@@ -15,12 +15,20 @@ ...@@ -15,12 +15,20 @@
+--------------------------------------------------------------------------*/ +--------------------------------------------------------------------------*/
package org.fortiss.tooling.ext.quality.service; package org.fortiss.tooling.ext.quality.service;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.eclipse.emf.common.util.EList;
import org.fortiss.tooling.base.model.base.HierarchicElementBase;
import org.fortiss.tooling.base.model.element.IConnector;
import org.fortiss.tooling.base.model.element.IHierarchicElement; import org.fortiss.tooling.base.model.element.IHierarchicElement;
import org.fortiss.tooling.base.model.element.IModelElementSpecification;
import org.fortiss.tooling.kernel.model.INamedCommentedElement;
import org.fortiss.tooling.kernel.model.INamedElement;
/** /**
* {@link IModelQualityProvider} to count the ratio of filled out comments of * {@link IModelQualityProvider} to count the ratio of filled out comments of
...@@ -30,315 +38,147 @@ import org.fortiss.tooling.base.model.element.IHierarchicElement; ...@@ -30,315 +38,147 @@ import org.fortiss.tooling.base.model.element.IHierarchicElement;
*/ */
public class HierarchicElementSizeProvider implements IModelQualityProvider<IHierarchicElement> { public class HierarchicElementSizeProvider implements IModelQualityProvider<IHierarchicElement> {
/** /**
* returns an array of integers or <String,Double> Map * returns an array of integers or <String,Double> Map Number of children of the
* Number of children of the hierarchical Element (on this level) * hierarchical Element (on this level) Number of ports on the hierarchical
* Number of ports on the hierarchical Element (on this level) * Element (on this level) Number of channels in the hierarchical element (on
* Number of channels in the hierarchical element (on this level) * this level) Number of totalPorts in whole tree Number of totalElements in
* Number of totalPorts in whole tree * whole tree Average SubTree Size
* Number of totalElements in whole tree
* Average SubTree Size
*/ */
/* */
private final String rootName;
/** */
Map<String, Double> sizes = new HashMap<String, Double>();
/** */ public static final String NUMBER_OF_PORTS = "numberOfPorts";
public HierarchicElementSizeProvider(String rootName) { public static final String NUMBER_OF_CONTAINED_ELEMENTS = "numberOfContainedElements";
super(); public static final String NUMBER_OF_CHANNELS = "numberOfChannels";
this.rootName = rootName;
// TODO Auto-generated constructor stub
}
/** */
public String getName() {
return this.rootName;
}
/** {@inheritDoc} */
@Override
public void apply(IHierarchicElement ele) {
Map<String, Double> sizes = new HashMap<String, Double>();
sizes.put("numberOfPorts", (double)ele.getConnectors().size());
sizes.put("numberOfChildren", (double)ele.getContainedElements().size());
sizes.put("numberOfChannels", (double)ele.getConnections().size());
// depth metrics
Set<ICounter> depthMetrics = new HashSet<HierarchicElementSizeProvider.ICounter>();
PortCounter pc = new PortCounter();
ElementCounter ec = new ElementCounter();
depthMetrics.add(new PortCounter());
depthMetrics.add(new ElementCounter());
// traverse whole tree
traverse(ele, depthMetrics);
// put in entries for subtrees
sizes.put("numberOfTotalPorts:", (double)pc.getCounter());
sizes.put("numberOfTotalElements:", (double)ec.getCounter());
for(IHierarchicElement child : ele.getContainedElements()) { public static final String NUMBER_OF_TOTAL_PORTS = "numberOfTotalPorts";
apply(child); public static final String NUMBER_OF_TOTAL_ELEMENTS = "numberOfTotalElements";
} public static final String NUMBER_OF_TOTAL_COMMENTABLE_ELEMENTS = "numberOfTotalCommentableElements";
this.sizes = sizes; public static final String NUMBER_OF_TOTAL_COMMENTED_ELEMENTS = "numberOfTotalCommentedElements";
}
/** Map containing all metrics concerning the current IHierarchicElement */
private Map<String, Double> storedMetrics;
/** /**
* @param currentElement * List containing all HierarchicElementSizeProvider for components inside the
* @param consumers * IHierarchicElement
*/ */
public void traverse(IHierarchicElement currentElement, Set<ICounter> consumers) { private List<HierarchicElementSizeProvider> containedProviders;
consumers.forEach(c -> c.apply(currentElement));
for(IHierarchicElement child : currentElement.getContainedElements()) {
traverse(child, consumers);
}
}
/** */ public Map<String, Double> getStoredMetrics() {
public interface ICounter { return storedMetrics;
/**
* @param ele
*/
public void apply(IHierarchicElement ele);
/** */
public int getCounter();
} }
/** */ /** */
private class PortCounter implements ICounter { public HierarchicElementSizeProvider() {
/** Returns port number. */ super();
int counter = 0; this.storedMetrics = new HashMap<>();
this.containedProviders = new ArrayList<>();
/** {@inheritDoc} */
@Override
public int getCounter() {
return counter;
}
/** */
@Override
public void apply(IHierarchicElement element) {
counter = getCounter() + element.getConnectors().size();
}
} }
/** */ /** {@inheritDoc} */
private class ElementCounter implements ICounter { @Override
/** Returns port number. */ public void apply(IHierarchicElement currentElement) {
int counter = 0;
/** {@inheritDoc} */ storedMetrics.put(NUMBER_OF_PORTS, (double) currentElement.getConnectors().size());
@Override storedMetrics.put(NUMBER_OF_CONTAINED_ELEMENTS, (double) currentElement.getContainedElements().size());
public int getCounter() { storedMetrics.put(NUMBER_OF_CHANNELS, (double) currentElement.getConnections().size());
return counter;
}
/** */ // depth metrics
@Override storedMetrics.put(NUMBER_OF_TOTAL_PORTS, (double) currentElement.getConnectors().size());
public void apply(IHierarchicElement element) { storedMetrics.put(NUMBER_OF_TOTAL_ELEMENTS, 1.0);
counter += 1;
boolean isElementCommentable = currentElement instanceof INamedCommentedElement;
storedMetrics.put(NUMBER_OF_TOTAL_COMMENTABLE_ELEMENTS, isElementCommentable ? 1.0 : 0.0);
String comment = isElementCommentable ? ((INamedCommentedElement) currentElement).getComment() : null;
storedMetrics.put(NUMBER_OF_TOTAL_COMMENTED_ELEMENTS, comment != null && comment != "" ? 1.0 : 0.0);
for (IHierarchicElement containedElement : currentElement.getContainedElements()) {
HierarchicElementSizeProvider provider = new HierarchicElementSizeProvider();
containedProviders.add(provider);
provider.apply(containedElement);
for (String key : new String[] { NUMBER_OF_TOTAL_PORTS, NUMBER_OF_TOTAL_ELEMENTS,
NUMBER_OF_TOTAL_COMMENTABLE_ELEMENTS, NUMBER_OF_TOTAL_COMMENTED_ELEMENTS }) {
storedMetrics.merge(key, provider.storedMetrics.get(key), Double::sum);
}
} }
// System.out.println("Finished traversing Node: "
// + ((currentElement instanceof INamedElement) ? ((INamedElement) currentElement).getName()
// : "Not Named"));
} }
}
/* /*
* public abstract class MultiMethodDemo implements IElementConsumer { *
* * CLONE DETECTION START public class CloneDetection {
* @Override *
* public void apply(IHierarchicElement element) { * private Map<Integer, List<IHierarchicElement>> map = new HashMap<>();
* if(element instanceof INamedCommentedElement) { *
* consume((INamedCommentedElement)element); * public void printResult() { System.out.println("Stuff"); }
* } else { *
* consume(element); * public void apply(IHierarchicElement element) { int hash = 1; for(IConnector
* } * connector : element.getConnectors()) { EList<IModelElementSpecification>
* } * specifications = connector.getSpecifications(); if(!specifications.isEmpty()
* * && connector.getSpecifications().get(0) instanceof PortSpecification) {
* public abstract void consume(IHierarchicElement element); *
* * PortSpecification port =
* public abstract void consume(INamedCommentedElement element); * (PortSpecification)connector.getSpecifications().get(0); hash = 31 * hash +
* * (connector instanceof InputPort ? 1 : 2) *
* } * port.getType().getClass().hashCode(); } else { // Clone Detection for State
*/ * diagrams not supported return; } }map.putIfAbsent(hash,new ArrayList<>());
*
// public class CloneDetection implements IElementConsumer { * List<IHierarchicElement> elements = map.get(hash);
// *
// private Map<Integer, List<IHierarchicElement>> map = new HashMap<>(); * for( IHierarchicElement element_b:elements) {
// *
// @Override * List<String> element_a_input = new ArrayList<String>(); List<String>
// public void printResult() { * element_b_input = new ArrayList<String>(); List<String> element_a_output =
// System.out.println("Stuff"); * new ArrayList<String>(); List<String> element_b_output = new
// } * ArrayList<String>();
// *
// @Override * for(IConnector connector : element.getConnectors()) { PortSpecification port
// public void apply(IHierarchicElement element) { * = (PortSpecification)connector.getSpecifications().get(0); String type =
// int hash = 1; * port.getType().getClass().getSimpleName(); if(connector instanceof InputPort)
// for(IConnector connector : element.getConnectors()) { * { element_a_input.add(type); } else { element_a_output.add(type); } }
// EList<IModelElementSpecification> specifications = connector.getSpecifications(); *
// //if(!specifications.isEmpty() && * for(IConnector connector : element_b.getConnectors()) { PortSpecification
// // connector.getSpecifications().get(0) instanceof PortSpecification) { * port = (PortSpecification)connector.getSpecifications().get(0); String type =
// * port.getType().getClass().getSimpleName(); if(connector instanceof InputPort)
// // PortSpecification port = * { element_b_input.add(type); } else { element_b_output.add(type); } }
// // (PortSpecification)connector.getSpecifications().get(0); *
// // hash = 31 * hash + (connector instanceof InputPort ? 1 : 2) * * boolean equals = true;
// // port.getType().getClass().hashCode(); *
// } else { * for(String type_a_input : element_a_input) {
// // Clone Detection for State diagrams not supported * if(!element_b_input.remove(type_a_input)) { equals = false; break; }
// return; *
// } * }
// }map.putIfAbsent(hash,new ArrayList<>()); *
// * for(String type_a_output : element_a_output) {
// List<IHierarchicElement> elements = map.get(hash); * if(!element_b_output.remove(type_a_output)) { equals = false; break; }
// *
// for( * } if(equals) { System.out .println("Found duplicate: " +
// IHierarchicElement element_b:elements) * ((INamedCommentedElement)element).getName() + ", " +
// { * ((INamedCommentedElement)element_b).getName()); break; } else {
// * System.out.println("Same but not duplicate"); } }elements.add(element); }
// List<String> element_a_input = new ArrayList<String>(); * CLONE DETECTION END
// List<String> element_b_input = new ArrayList<String>(); *
// List<String> element_a_output = new ArrayList<String>(); *
// List<String> element_b_output = new ArrayList<String>(); * public void printShare(HierarchicElementBase element) {
// *
// for(IConnector connector : element.getConnectors()) { * Set<Tuple<String, Integer>> counts = new HashSet<>(); this.apply(element);
// PortSpecification port = (PortSpecification)connector.getSpecifications().get(0); * counts.add(new Tuple<>("[Self]" + element.getName(), count)); this.count = 0;
// String type = port.getType().getClass().getSimpleName(); *
// if(connector instanceof InputPort) { * for(IHierarchicElement child : element.getContainedElements()) {
// element_a_input.add(type); *
// } else { * HierarchicElementBase real_child = (HierarchicElementBase)child;
// element_a_output.add(type); *
// } * recursiveDescent(real_child, this);
// } *
// * counts.add(new Tuple<>(real_child.getName(), count)); count = 0; }
// for(IConnector connector : element_b.getConnectors()) { *
// PortSpecification port = (PortSpecification)connector.getSpecifications().get(0); * double total = counts.stream().mapToInt(t -> t.getElement2()).sum();
// String type = port.getType().getClass().getSimpleName(); *
// if(connector instanceof InputPort) { * System.out.println("[" + this.getClass().getSimpleName() + "] shares:");
// element_b_input.add(type); * for(Tuple<String, Integer> tuple : counts) { System.out.println("\t " +
// } else { * String.format("%05.2f%%", tuple.element2 / total * 100) + ", " +
// element_b_output.add(type); * tuple.element2 + ": " + tuple.element1); } }
// } *
// } */
// }
// boolean equals = true;
//
// for(String type_a_input : element_a_input) {
// if(!element_b_input.remove(type_a_input)) {
// equals = false;
// break;
// }
//
// }
//
// for(String type_a_output : element_a_output) {
// if(!element_b_output.remove(type_a_output)) {
// equals = false;
// break;
// }
//
// }
// if(equals) {
// System.out
// .println("Found duplicate: " + ((INamedCommentedElement)element).getName() +
// ", " + ((INamedCommentedElement)element_b).getName());
// break;
// } else {
// System.out.println("Same but not duplicate");
// }
// }elements.add(element);
// }
/*
* public class CommentCompleteness extends MultiMethodDemo {
* private int total_amount;
* private int with_comments;
*
* public CommentCompleteness() {
* total_amount = 0;
* with_comments = 0;
* }
*
* @Override
* public void consume(IHierarchicElement element) {
* total_amount++;
* }
*
* @Override
* public void consume(INamedCommentedElement element) {
* if(element.getComment() != "") {
* with_comments++;
* }
* total_amount++;
* }
* }
*/
/*
* public class Tuple<T1, T2> {
* private T1 element1;
* private T2 element2;
*
* public Tuple(T1 element1, T2 element2) {
* this.element1 = element1;
* this.element2 = element2;
* }
*
* public T1 getElement1() {
* return element1;
* }
*
* public T2 getElement2() {
* return element2;
* }
* }
*/
/*
* public void printShare(HierarchicElementBase element) {
*
* Set<Tuple<String, Integer>> counts = new HashSet<>();
* this.apply(element);
* counts.add(new Tuple<>("[Self]" + element.getName(), count));
* count = 0;
*
* for(IHierarchicElement child : element.getContainedElements()) {
*
* HierarchicElementBase real_child = (HierarchicElementBase)child;
*
* recursiveDescent(real_child, this);
*
* counts.add(new Tuple<>(real_child.getName(), count));
* count = 0;
* }
*
* double total = counts.stream().mapToInt(t -> t.getElement2()).sum();
*
* System.out.println("[" + this.getClass().getSimpleName() + "] shares:");
* for(Tuple<String, Integer> tuple : counts) {
* System.out.println("\t " + String.format("%05.2f%%", tuple.element2 / total * 100) +
* ", " + tuple.element2 + ": " + tuple.element1);
* }
* }
*/
/*
* protected void recursiveDescent(HierarchicElementBase element, CounterConsumer consumer) {
* consumer.apply(element);
*
* for(IHierarchicElement child : element.getContainedElements()) {
* HierarchicElementBase real_child = (HierarchicElementBase)child;
* recursiveDescent(real_child, consumer);
* }
* }
*/
/*
* public class ConnectionCounter {
* public void apply(IHierarchicElement element) {
* count += element.getConnections().size();
* }
*
* }
*
* private void extractComment(IHierarchicElement c) {
* strings.add(((INamedCommentedElement)c).getComment());
* }
* }
* }
*/
...@@ -107,13 +107,20 @@ public class ModelQualityService extends EObjectAwareServiceBase<IModelQualityPr ...@@ -107,13 +107,20 @@ public class ModelQualityService extends EObjectAwareServiceBase<IModelQualityPr
*/ */
public List<IModelQualityProvider<? extends EObject>> performMetricAnalysis(FileProject fp) { public List<IModelQualityProvider<? extends EObject>> performMetricAnalysis(FileProject fp) {
List<IModelQualityProvider<? extends EObject>> result = List<IModelQualityProvider<? extends EObject>> result =
new LinkedList<IModelQualityProvider<? extends EObject>>(); new LinkedList<>();
for(IProjectRootElement ele : fp.getRootElements()) { for(IProjectRootElement ele : fp.getRootElements()) {
if(ele instanceof IHierarchicElement) { if(ele instanceof IHierarchicElement) {
//TODO check get(0)
IHierarchicElement firstNode = ((IHierarchicElement) ele).getContainedElements().get(0);
HierarchicElementSizeProvider hes = HierarchicElementSizeProvider hes =
new HierarchicElementSizeProvider(ele.getName()); new HierarchicElementSizeProvider();
hes.apply((IHierarchicElement)ele); hes.apply(firstNode);
System.out.println("checked in hierarchic metrics for: " + hes.getName()); System.out.println("checked in hierarchic metrics for: " + ele.getName());
double commentable = hes.getStoredMetrics().getOrDefault(HierarchicElementSizeProvider.NUMBER_OF_TOTAL_COMMENTABLE_ELEMENTS, 0.0);
double commented = hes.getStoredMetrics().getOrDefault(HierarchicElementSizeProvider.NUMBER_OF_TOTAL_COMMENTED_ELEMENTS, 0.0);
System.out.println("Commented to total: "+commented+", "+commentable+", "+commented/commentable);
result.add(hes); result.add(hes);
} }
} }
...@@ -134,6 +141,7 @@ public class ModelQualityService extends EObjectAwareServiceBase<IModelQualityPr ...@@ -134,6 +141,7 @@ public class ModelQualityService extends EObjectAwareServiceBase<IModelQualityPr
// metricsList.addAll(metrics); // metricsList.addAll(metrics);
// } // }
// } // }
return result; return result;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment