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 6 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
121 changes: 113 additions & 8 deletions src/main/java/org/jpeek/calculus/java/Ccm.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,26 @@
*/
package org.jpeek.calculus.java;

import com.jcabi.xml.Sources;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.jcabi.xml.XSLDocument;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.cactoos.io.ResourceOf;
import org.cactoos.io.UncheckedInput;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.jpeek.calculus.Calculus;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
* CCM metric Java calculus.
Expand All @@ -52,14 +63,19 @@ public XML node(
).toString()
);
}
return Ccm.withFixedNcc(
new XSLDocument(
new UncheckedInput(
new ResourceOf("org/jpeek/metrics/CCM.xsl")
).stream()
).transform(skeleton),
skeleton
final XSLDocument doc = new XSLDocument(
new UncheckedText(
new TextOf(
new ResourceOf(
new FormattedText("org/jpeek/metrics/%s.xsl", metric)
)
)
).asString(),
Sources.DUMMY,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not clear why do we need to apply xsl for dymmy sources, while ccm contains 5 columns for now and we have a problem with 2 of them only
https://user-images.githubusercontent.com/63350301/117303386-4ff0da00-ae85-11eb-82c3-869f1a66521d.png

params
);
final XML meta = addMetaTag(skeleton);
return doc.transform(meta);
}

/**
Expand All @@ -68,6 +84,7 @@ public XML node(
* @param skeleton XML Skeleton
* @return XML with fixed NCC.
*/
@SuppressWarnings("PMD.UnusedPrivateMethod")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

really bed suppression, method is not used and confusing for all, why do we need it

private static XML withFixedNcc(final XML transformed, final XML skeleton) {
final List<XML> packages = transformed.nodes("//package");
for (final XML elt : packages) {
Expand Down Expand Up @@ -105,4 +122,92 @@ private static void updateNcc(
).toString()
);
}

/**
* Adds a meta tag to the skeleton XML.
* @param skeleton The skeleton XML to which the meta tag will be added
* @return The modified XML with the meta tag added
*/
private static XML addMetaTag(final XML skeleton) {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
final Document doc;
try {
builder = factory.newDocumentBuilder();
doc = builder.newDocument();
final Element meta = doc.createElement("meta");
Node packages = skeleton.node().getFirstChild().getFirstChild().getNextSibling()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really near to impossible to support this code, I can not remember the order of the nodes in XML and nobody wants, that's why there are a lot of ways to work with XML using XSL, XPATH, tag names.
If you walk with order only, your code will be broken by any sceleton structure changes.

.getFirstChild();
while (Objects.nonNull(packages)) {
if (packages.getTextContent().matches("\\s*")) {
packages = packages.getNextSibling();
continue;
}
final Node child = createPackageTag(doc, packages);
meta.appendChild(child);
packages = packages.getNextSibling();
}
doc.appendChild(meta);
} catch (final ParserConfigurationException ex) {
throw new IllegalStateException(ex);
}
final Node repr = skeleton.node().getFirstChild();
final Node node = repr.getOwnerDocument()
.importNode(doc.getDocumentElement(), true);
repr.appendChild(node);
return new XMLDocument(repr);
}

/**
* Creates a package tag element in the XML document.
* @param doc The XML document to which the package tag will be added
* @param packages The node representing the package
* @return The package tag element created in the XML document
*/
private static Node createPackageTag(final Document doc, final Node packages) {
final NamedNodeMap map = packages.getAttributes();
final Element root = doc.createElement("package");
final Node id = map.getNamedItem("id");
root.setAttribute("id", id.getNodeValue());
Node classes = packages.getFirstChild();
while (Objects.nonNull(classes)) {
if (classes.getTextContent().matches("\\s*")) {
classes = classes.getNextSibling();
continue;
}
final Node child = createClassTag(doc, classes);
root.appendChild(child);
classes = classes.getNextSibling();
}
return root;
}

/**
* Creates a class tag element in the XML document.
*
* This method constructs a class tag element in the XML document based on the
* information provided by the given node representing a class. It traverses
* through the attributes of the class node to extract necessary information
* and constructs the class tag element accordingly.
*
* Additionally, this method adds a child element representing a metric called
* "ncc" to the class tag.
*
* @param doc The XML document to which the class tag will be added
* @param classes The node representing the class
* @return The class tag element created in the XML document
* @todo #522:30m/DEV Implement method that would build graph where methods are nodes and
* field references are edges and find connected components in that graph.
*/
private static Node createClassTag(final Document doc, final Node classes) {
final NamedNodeMap map = classes.getAttributes();
final Element root = doc.createElement("class");
final Node id = map.getNamedItem("id");
root.setAttribute("id", id.getNodeValue());
final Integer value = 1;
final Element ncc = doc.createElement("ncc");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not only ncc metric should be presented, you can check current implementation
https://user-images.githubusercontent.com/63350301/117303386-4ff0da00-ae85-11eb-82c3-869f1a66521d.png

ncc.setTextContent(value.toString());
root.appendChild(ncc);
return root;
}
}
4 changes: 4 additions & 0 deletions src/main/resources/org/jpeek/metrics/CCM.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ SOFTWARE.
</xsl:variable>
<xsl:copy>
<xsl:variable name="nc" select="count($edges/edge)"/>
<!--
@todo #522:15m/DEV ncc is already calculated during skeleton build. It has to be captured
from that skeleton and put into the corresponding variable
-->
<xsl:variable name="ncc" select="count(distinct-values($edges/edge/method/text()))"/>
<xsl:variable name="nmp" select="(count($methods) * (count($methods) - 1)) div 2"/>
<xsl:attribute name="value">
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/org/jpeek/calculus/java/CcmTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.
*/
package org.jpeek.calculus.java;

import com.jcabi.matchers.XhtmlMatchers;
import com.jcabi.xml.XML;
import java.io.IOException;
import java.util.HashMap;
import org.jpeek.FakeBase;
import org.jpeek.skeleton.Skeleton;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsTrue;

/**
* Test class for CCM calculus.
* @since 0.30.9
*/
final class CcmTest {
@Test
@Disabled
void createsSkeletonWithMeta() throws IOException {
final XML result = new Ccm().node(
"CCM", new HashMap<>(0), new Skeleton(
new FakeBase(
"Foo", "Bar"
)
).xml()
);
new Assertion<>(
"Must create CCM report",
result.toString(),
XhtmlMatchers.hasXPath(
"/metric/app/package/class/vars/var"
)
).affirm();
new Assertion<>(
"Must have 2 ncc vars",
result.xpath("/metric/app/package/class/vars/var[@id=\'ncc\']/text()").size() == 2,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we have a specific problem with specific class, lets create a tests to check metric values, not just their existing.

new IsTrue()
).affirm();
}
}
Loading