Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCM fix #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/jpeek/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public final class Main {
* Ctor.
*/
private Main() {
this.metrics = "LCOM5,NHD,MMAC,SCOM,CAMC";
this.metrics = "CCM";
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/jpeek/calculus/java/fix/CCM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.jpeek.calculus.java.fix;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jpeek.javagraph.FindConnectedComponents;

public class CCM {

public XML getFixedResult(XML skeleton, XML tempRes) {
List<XML> methods = skeleton.nodes("//methods/method[@ctor='false' and @abstract='false']");
List<XML> edges = tempRes.nodes("//edge");

Map<String, Integer> methodIds = new HashMap<>();

for (int i = 0; i < methods.size(); i++) {
methodIds.put(((XMLDocument) methods.get(i)).xpath("//method/@name").get(i), i);
}

final Pattern pattern = Pattern
.compile("(<method>)(\\w+)(</method>)(\\r\\n|\\r|\\n )(<method>)(\\w+)(</method>)");

FindConnectedComponents f = new FindConnectedComponents(methods.size());

for (XML method : edges) {
Matcher name = pattern.matcher(method.toString());

if (name.find()) {
f.addEdge(methodIds.get(name.group(2)), methodIds.get(name.group(6)));
}
}

int ncc = f.connectedComponents();

double nc = Double.parseDouble(tempRes.xpath("//var[@id=\"nc\"]/text()").get(0));
double nmp = Double.parseDouble(tempRes.xpath("//var[@id=\"nmp\"]/text()").get(0));
double value = nc / (nmp * ncc);

final Pattern nccReplacePattern = Pattern.compile("ncc\\\">(\\w+)");
final Pattern valueReplacePattern = Pattern.compile("value=\\\"([0-9]+.[0-9]+)");
final Pattern finalReplacePattern = Pattern
.compile("( )(<var id=\\\"edges\\\">)(?s).*(</vars>)");

String tempWithNccRes = nccReplacePattern.matcher(tempRes.toString())
.replaceAll(String.format("ncc\">%d", ncc));
String tempWithValueRes = valueReplacePattern.matcher(tempWithNccRes)
.replaceAll(String.format(Locale.US, "value=\"%.2f", value));
String finalRes = finalReplacePattern.matcher(tempWithValueRes).replaceAll("</vars>");

return new XMLDocument(finalRes);
}
}
38 changes: 23 additions & 15 deletions src/main/java/org/jpeek/calculus/xsl/XslCalculus.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,34 @@
import org.cactoos.text.FormattedText;
import org.cactoos.text.TextOf;
import org.jpeek.calculus.Calculus;
import org.jpeek.calculus.java.fix.CCM;

/**
* Metrics xsl calculus. Use an xsl sheet to transform the input skeleton into
* the xml containing the calculation.
* Metrics xsl calculus. Use an xsl sheet to transform the input skeleton into the xml containing
* the calculation.
*
* @since 0.30.9
*/
public final class XslCalculus implements Calculus {

@Override
public XML node(final String metric, final Map<String, Object> params,
final XML skeleton) throws IOException {
return new XSLDocument(
new TextOf(
new ResourceOf(
new FormattedText("org/jpeek/metrics/%s.xsl", metric)
)
).asString(),
Sources.DUMMY,
params
).transform(skeleton);
}
@Override
public XML node(final String metric, final Map<String, Object> params,
final XML skeleton) throws IOException {

XML tempRes = new XSLDocument(
new TextOf(
new ResourceOf(
new FormattedText("org/jpeek/metrics/%s.xsl", metric)
)
).asString(),
Sources.DUMMY,
params
).transform(skeleton);

if (metric.equals("CCM")) {
return new CCM().getFixedResult(skeleton, tempRes);
} else {
return tempRes;
}
}
}
47 changes: 47 additions & 0 deletions src/main/java/org/jpeek/javagraph/FindConnectedComponents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jpeek.javagraph;

import java.util.ArrayList;

public class FindConnectedComponents {

int V;
ArrayList<ArrayList<Integer>> graph;

public FindConnectedComponents(int V) {
this.V = V;
graph = new ArrayList<>();

for (int i = 0; i < V; i++) {
graph.add(i, new ArrayList<>());
}
}

void DFS(int v, boolean[] visited) {
visited[v] = true;
System.out.print(v + " ");
for (int x : graph.get(v)) {
if (!visited[x]) {
DFS(x, visited);
}
}
}

public int connectedComponents() {
int countConnectedComponents = 0;
boolean[] visited = new boolean[V];
for (int v = 0; v < V; ++v) {
if (!visited[v]) {
DFS(v, visited);
System.out.println();
countConnectedComponents++;
}
}
return countConnectedComponents;
}


public void addEdge(int src, int dest) {
graph.get(src).add(dest);
graph.get(dest).add(src);
}
}
16 changes: 16 additions & 0 deletions src/main/resources/org/jpeek/metrics/CCM.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ SOFTWARE.
</edge>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="$method/preceding-sibling::method">
<xsl:variable name="other" select="."/>
<xsl:if test="$method/ops/op/text()[. = $other/ops/op/text()]">
<edge>
<method>
<xsl:value-of select="$method/@name"/>
</method>
<method>
<xsl:value-of select="$other/@name"/>
</method>
</edge>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
<xsl:copy>
Expand Down Expand Up @@ -88,6 +101,9 @@ SOFTWARE.
<var id="nmp">
<xsl:value-of select="$nmp"/>
</var>
<var id="edges">
<xsl:copy-of select="$edges"/>
</var>
</vars>
</xsl:copy>
</xsl:template>
Expand Down