Skip to content

Commit

Permalink
#91 implement MMAC via XML/XSL
Browse files Browse the repository at this point in the history
  • Loading branch information
vmotsak committed Jan 15, 2018
1 parent c2b5c34 commit df37337
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/jpeek/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public void analyze() throws IOException {
final XML skeleton = new Skeleton(base).xml();
this.save(skeleton.toString(), "skeleton.xml");
final Iterable<Report> reports = new ListOf<>(
new Report(skeleton, "LCOM", this.params, 10.0d, -5.0d)
new Report(skeleton, "LCOM", this.params, 10.0d, -5.0d),
new Report(skeleton, "MMAC", this.params, 0.5d, 0.25d)
);
new IoCheckedScalar<>(
new AndInThreads(
Expand Down
95 changes: 95 additions & 0 deletions src/main/resources/org/jpeek/metrics/MMAC.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0"?>
<!--
The MIT License (MIT)
Copyright (c) 2017 Yegor Bugayenko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="skeleton">
<metric>
<xsl:apply-templates select="@*"/>
<title>MMAC</title>
<description>
Method-Method through Attributes Cohesion (MMAC).
The MMAC is the average cohesion of all pairs of methods.
In simple words this metric shows how much methods have the
same parameters or return types. When class has some number
of methods and most of them operate the same parameters it
assumes better. It looks like class contains overloaded
methods. Preferably when class has only one method with
parameters and/or return type and it assumes that class
do only one thing. Value of MMAC metric is better for these
one classes.
Metric value is in interval [0, 1]. Value closer to 1 is better.
</description>
<xsl:apply-templates select="node()"/>
</metric>
</xsl:template>
<xsl:template match="class">
<xsl:variable name="class" select="."/>
<xsl:variable name="methods_count" select="count($class/methods/method[@ctor='false'])"/>
<xsl:variable name="types"
select="distinct-values($class/methods/method/args/arg[@type!='V']/@type)"/>
<xsl:variable name="types_count" select="count($types)"/>
<xsl:variable name="type_methods">
<xsl:for-each select="$types">
<xsl:variable name="type" select="."/>
<xsl:variable name="count"
select="count($class/methods/method[@ctor='false' and args/arg/@type=$type])"/>
<count>
<xsl:value-of select="$count * ($count - 1)"/>
</count>
</xsl:for-each>
</xsl:variable>
<xsl:copy>
<xsl:attribute name="value">
<xsl:choose>
<xsl:when test="$methods_count = 0 or $types_count = 0">
<xsl:text>0</xsl:text>
</xsl:when>
<xsl:when test="$methods_count = 1">
<xsl:text>1</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of
select="format-number(sum($type_methods/count)
div ($methods_count * $types_count * ($methods_count - 1)),
'0.####')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates select="@*"/>
<vars>
<var id="methods">
<xsl:value-of select="$methods_count"/>
</var>
<var id="types">
<xsl:value-of select="$types_count"/>
</var>
</vars>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
11 changes: 10 additions & 1 deletion src/test/java/org/jpeek/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ public static Collection<Object[]> targets() {
new Object[] {"OverloadMethods", "LCOM", 0.0d},
new Object[] {"TwoCommonAttributes", "LCOM", 3.0d},
new Object[] {"WithoutAttributes", "LCOM", 0.0d},
new Object[] {"OneMethodCreatesLambda", "LCOM", 1.0d}
new Object[] {"OneMethodCreatesLambda", "LCOM", 1.0d},
new Object[] {"Bar", "MMAC", 1.0d},
new Object[] {"Foo", "MMAC", 1.0d},
new Object[] {"MethodsWithDiffParamTypes", "MMAC", 0.037d},
new Object[] {"NoMethods", "MMAC", 0.0d},
new Object[] {"OneVoidMethodWithoutParams", "MMAC", 0.0d},
new Object[] {"OverloadMethods", "MMAC", 0.7222d},
new Object[] {"TwoCommonAttributes", "MMAC", 0.3333d},
new Object[] {"WithoutAttributes", "MMAC", 1.0d},
new Object[] {"OneMethodCreatesLambda", "MMAC", 0.0d}
);
}

Expand Down

0 comments on commit df37337

Please sign in to comment.