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

added ccmCalculus especially to find ncc value for ccm metric. The lo… #548

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
aebacaa
added ccmCalculus especially to find ncc value for ccm metric. The lo…
starkda Mar 20, 2024
c9293ab
moved cmm stuff into ccm class
starkda Mar 21, 2024
fd730b6
moved cmm stuff into ccm class
starkda Mar 21, 2024
4443ead
moved cmm stuff into ccm class
starkda Mar 21, 2024
e0f8a2f
moved cmm stuff into ccm class
starkda Mar 21, 2024
0ce54fd
moved cmm stuff into ccm class
starkda Mar 22, 2024
aaa584a
added todos on tests
starkda Mar 22, 2024
9557ee8
added ccmCalculus especially to find ncc value for ccm metric. The lo…
starkda Mar 20, 2024
3b47211
moved cmm stuff into ccm class
starkda Mar 21, 2024
144e4e1
moved cmm stuff into ccm class
starkda Mar 21, 2024
1f7f100
moved cmm stuff into ccm class
starkda Mar 21, 2024
dd92650
moved cmm stuff into ccm class
starkda Mar 21, 2024
61dbe52
moved cmm stuff into ccm class
starkda Mar 22, 2024
cdc7ae4
added todos on tests
starkda Mar 22, 2024
5017e4a
Merge remote-tracking branch 'origin/ccmCalculus' into ccmCalculus
starkda Mar 25, 2024
1071ee1
fixed constructor name
starkda Mar 25, 2024
5f37cbb
fixed constructor name
starkda Mar 25, 2024
5258ae7
fixed constructor name
starkda Mar 25, 2024
346d3d2
fixed constructor name
starkda Mar 25, 2024
5a5a305
fixed constructor name
starkda Mar 25, 2024
83a8137
Merge remote-tracking branch 'origin/ccm-constructor' into ccmCalculus
starkda Mar 25, 2024
d051565
fixed constructor name
starkda Mar 25, 2024
d62314d
added calculation of ncc & fixed xsl formatting
starkda Mar 25, 2024
0874e82
added calculation of ncc & fixed xsl formatting
starkda Mar 25, 2024
d06c483
added calculation of ncc & fixed xsl formatting
starkda Mar 25, 2024
d4ffcde
added calculation of ncc & fixed xsl formatting
starkda Mar 25, 2024
0a64225
fixed constructor name
starkda Mar 25, 2024
4dc4951
fixed constructor name
starkda Mar 25, 2024
fe2b446
formatting
starkda Mar 25, 2024
969c63e
formatting
starkda Mar 25, 2024
f8e30c1
formatting
starkda Mar 25, 2024
0bfe7a7
formatting
starkda Mar 25, 2024
cb0d747
fixes
starkda Mar 25, 2024
e0f6596
fixes
starkda Mar 25, 2024
71dedfe
fixes
starkda Mar 25, 2024
db39bd7
fixes
starkda Mar 25, 2024
6bbc1ee
fixes
starkda Mar 25, 2024
8084432
fixes
starkda Mar 25, 2024
d26f4e7
fixes
starkda Mar 25, 2024
47254cc
ncc calculation using java
starkda Apr 1, 2024
a8684bd
fixed tests
starkda Apr 1, 2024
f6489cb
fixed tests
starkda Apr 1, 2024
93668c3
fixed tests
starkda Apr 2, 2024
4f56dfb
fixes
starkda Apr 2, 2024
762b62f
license added
starkda Apr 2, 2024
ddce255
fixes
starkda Apr 2, 2024
b4cf48b
fixes
starkda Apr 2, 2024
debef9d
fixes
starkda Apr 2, 2024
39b8f2f
fixes
starkda Apr 2, 2024
da5370e
fixes
starkda Apr 2, 2024
69c8980
fixes
starkda Apr 4, 2024
202e99e
fixes
starkda Apr 4, 2024
bb8e09f
fixes
starkda Apr 17, 2024
ec8dd38
license
starkda Apr 18, 2024
b6404bc
fix
starkda Apr 18, 2024
4b0e358
fix
starkda Apr 18, 2024
52c404c
fix
starkda Apr 19, 2024
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
8 changes: 7 additions & 1 deletion src/main/java/org/jpeek/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.cactoos.scalar.IoChecked;
import org.cactoos.scalar.LengthOf;
import org.jpeek.calculus.Calculus;
import org.jpeek.calculus.java.Ccm;
import org.jpeek.calculus.xsl.XslCalculus;
import org.jpeek.skeleton.Skeleton;
import org.xembly.Directives;
Expand Down Expand Up @@ -250,14 +251,19 @@ private void buildReport(final Collection<XSL> layers, final Collection<Report>
final Base base = new DefaultBase(this.input);
final XML skeleton = new Skeleton(base).xml();
final XSL chain = new XSLChain(layers);
final Calculus xsl = new XslCalculus();
this.save(skeleton.toString(), "skeleton.xml");
Arrays.stream(Metrics.values())
.filter(
metric -> this.params.containsKey(metric.name())
)
.forEach(
metric -> {
final Calculus xsl;
if (metric == Metrics.CCM) {
xsl = new Ccm();
} else {
xsl = new XslCalculus();
}
if (Objects.nonNull(metric.getSigma())) {
reports.add(
new XslReport(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jpeek/XslReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*
* @since 0.1
*/
final class XslReport implements Report {
public final class XslReport implements Report {

/**
* Location to the schema file.
Expand Down
114 changes: 70 additions & 44 deletions src/main/java/org/jpeek/calculus/java/Ccm.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,27 @@
package org.jpeek.calculus.java;

import com.jcabi.xml.XML;
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLChain;
import com.jcabi.xml.XSLDocument;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.UncheckedInput;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.jpeek.XslReport;
import org.jpeek.calculus.Calculus;
import org.jpeek.graph.Disjoint;
import org.jpeek.graph.XmlGraph;

/**
* CCM metric Java calculus.
* This class implements the Calculus interface to provide functionality
* for computing the CCM metric for Java code.
* @since 0.30.25
*/
public final class Ccm implements Calculus {

@Override
public XML node(
final String metric,
Expand All @@ -52,57 +58,77 @@ public XML node(
).toString()
);
}
return Ccm.withFixedNcc(
new XSLDocument(
new UncheckedInput(
new ResourceOf("org/jpeek/metrics/CCM.xsl")
).stream()
).transform(skeleton),
skeleton
);
List<XSL> layers = new ArrayList<>(0);
if (!params.containsKey("include-static-methods")) {
layers = addXslFilter(layers, "no-static-methods.xsl");
}
if (!params.containsKey("include-ctors")) {
layers = addXslFilter(layers, "no-ctors.xsl");
}
if (!params.containsKey("include-private-methods")) {
layers = addXslFilter(layers, "no-private-methods.xsl");
}
final XSLChain chain = new XSLChain(layers);
final XML meta = new XSLDocument(
XslReport.class.getResourceAsStream(
"xsl/meta/meta-creater.xsl"
)
).transform(chain.transform(skeleton));
final XML modified = findNcc(skeleton, meta);
return new XSLDocument(
new UncheckedInput(
new ResourceOf("org/jpeek/metrics/CCM.xsl")
).stream()
).transform(modified);
}

/**
* Updates the transformed xml with proper NCC value.
* @param transformed The transformed XML skeleton.
* @param skeleton XML Skeleton
* @return XML with fixed NCC.
* Find NCC.
*
* @param skeleton The XML skeleton to operate on
* @param meta The XML containing metadata
* @return The modified XML
*/
private static XML withFixedNcc(final XML transformed, final XML skeleton) {
final List<XML> packages = transformed.nodes("//package");
for (final XML elt : packages) {
final String pack = elt.xpath("/@id").get(0);
final List<XML> classes = elt.nodes("//class");
for (final XML clazz : classes) {
Ccm.updateNcc(skeleton, pack, clazz);
}
private static XML findNcc(final XML skeleton, final XML meta) {
XML result = meta;
final XSL ncc = new XSLDocument(
XslReport.class.getResourceAsStream(
"xsl/meta/meta-ncc.xsl"
)
);
for (final XML clazz : meta.nodes("//class")) {
final XML pack = clazz.nodes("..").get(0);
final XmlGraph graph = new XmlGraph(
skeleton,
pack.xpath("@id").get(0),
clazz.xpath("@id").get(0)
);
final String size = String.valueOf(new Disjoint(graph).value().size());
result = ncc
.with("package", pack.xpath("@id").get(0))
.with("class", clazz.xpath("@id").get(0))
.with("value", size)
.transform(result);
}
return transformed;
return new XSLDocument(
XslReport.class.getResourceAsStream(
"xsl/meta/skeleton-appender.xsl"
)
).with("meta", result.node()).transform(skeleton);
}

/**
* Updates the xml node of the class with proper NCC value.
* @param skeleton XML Skeleton
* @param pack Package name
* @param clazz Class node in the resulting xml
* @todo #449:30min Implement NCC calculation with `XmlGraph` and use this
* class to fix CCM metric (see issue #449). To do this, this class, once
* it works correctly, should be integrated with XSL based calculuses in
* `XslReport` (see `todo #449` in Calculus). Write a test to make sure
* the metric is calculated correctly. Also, decide whether the
* whole CCM metric should be implemented in Java, or only the NCC part.
* Update this `todo` accordingly.
* Adds an XSL filter to the list of layers.
*
* @param layers The list of XSL filters
* @param name The name of the XSL file to append
* @return The updated list of XSL filters
*/
private static void updateNcc(
final XML skeleton, final String pack, final XML clazz
) {
throw new UnsupportedOperationException(
new Joined(
"",
skeleton.toString(),
pack,
clazz.toString()
).toString()
private static List<XSL> addXslFilter(final List<XSL> layers, final String name) {
final XSLDocument doc = new XSLDocument(
XslReport.class.getResourceAsStream(String.format("xsl/layers/%s", name))
);
layers.add(doc);
return layers;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jpeek/graph/Disjoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Disjoint(final Graph graph) {
}

@Override
public List<Set<Node>> value() throws Exception {
public List<Set<Node>> value() {
final Set<Node> unvisited = new HashSet<>(this.graph.nodes());
final List<Set<Node>> result = new ArrayList<>(unvisited.size());
while (!unvisited.isEmpty()) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/jpeek/graph/XmlGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.FormattedText;
import org.jpeek.skeleton.Skeleton;

/**
* Graph implementation built on skeleton.
Expand All @@ -54,7 +53,7 @@ public final class XmlGraph implements Graph {
* @param pname Package of the class this graph is for
* @param cname Class in the skeleton this graph is for
*/
public XmlGraph(final Skeleton skeleton, final String pname, final String cname) {
public XmlGraph(final XML skeleton, final String pname, final String cname) {
this.nds = new Unchecked<>(
new Sticky<>(
() -> XmlGraph.build(skeleton, pname, cname)
Expand All @@ -74,13 +73,13 @@ public List<Node> nodes() {
* @param cname Class in the skeleton this graph is for
* @return List of nodes
*/
private static List<Node> build(final Skeleton skeleton, final String pname,
private static List<Node> build(final XML skeleton, final String pname,
final String cname) {
final Map<XML, Node> byxml = new MapOf<>(
method -> method,
method -> new Node.Simple(
new XmlMethodSignature(
skeleton.xml()
skeleton
.nodes(
new FormattedText(
"//package[@id='%s']", pname
Expand All @@ -94,7 +93,7 @@ private static List<Node> build(final Skeleton skeleton, final String pname,
method
).asString()
),
skeleton.xml().nodes(
skeleton.nodes(
"//methods/method[@ctor='false' and @abstract='false']"
)
);
Expand Down
7 changes: 6 additions & 1 deletion src/main/resources/org/jpeek/metrics/CCM.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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/meta"/>
<xsl:template match="skeleton">
<metric>
<xsl:apply-templates select="@*"/>
Expand All @@ -40,7 +41,11 @@ SOFTWARE.
<xsl:apply-templates select="node()"/>
</metric>
</xsl:template>
<xsl:variable name="met" select="/skeleton/meta"/>
<xsl:template match="class">
<xsl:variable name="currentClassId" select="@id"/>
<xsl:variable name="currentPackageId" select="../@id"/>
<xsl:variable name="currentClassNcc" select="$met/package[@id = $currentPackageId]/class[@id = $currentClassId]/ncc"/>
<xsl:variable name="methods" select="methods/method[@ctor='false']"/>
<xsl:variable name="edges">
<xsl:for-each select="$methods">
Expand All @@ -62,7 +67,7 @@ SOFTWARE.
</xsl:variable>
<xsl:copy>
<xsl:variable name="nc" select="count($edges/edge)"/>
<xsl:variable name="ncc" select="count(distinct-values($edges/edge/method/text()))"/>
<xsl:variable name="ncc" select="$currentClassNcc"/>
<xsl:variable name="nmp" select="(count($methods) * (count($methods) - 1)) div 2"/>
<xsl:attribute name="value">
<xsl:choose>
Expand Down
46 changes: 46 additions & 0 deletions src/main/resources/org/jpeek/xsl/meta/meta-creater.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)

Copyright (c) 2017-2024 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" name="meta">
<meta>
<xsl:apply-templates select="descendant::package"/>
</meta>
</xsl:template>
<xsl:template match="package">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="class">
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
45 changes: 45 additions & 0 deletions src/main/resources/org/jpeek/xsl/meta/meta-ncc.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License (MIT)

Copyright (c) 2017-2024 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">
<!-- Parameters -->
<xsl:param name="package"/>
<xsl:param name="class"/>
<xsl:param name="value"/>
<!-- Identity template: copies everything as is by default -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Template to add <ncc> tag inside <class> if package and class match parameters -->
<xsl:template match="package[@id=$package]//class[@id=$class]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<ncc>
<xsl:value-of select="$value"/>
</ncc>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Loading
Loading