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 52 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
195 changes: 154 additions & 41 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,32 @@
package org.jpeek.calculus.java;

import com.jcabi.xml.XML;
import com.jcabi.xml.XSL;
import com.jcabi.xml.XSLDocument;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.jpeek.XslReport;
import org.jpeek.calculus.Calculus;
import org.jpeek.calculus.java.implementations.UnionFind;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* 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 +63,159 @@ 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 UncheckedInput(
new ResourceOf("org/jpeek/metrics/CCM.xsl")
).stream()
);
final XML meta = addMetaInformation(skeleton, params);
return doc.transform(meta);
}

/**
* Updates the transformed xml with proper NCC value.
* @param transformed The transformed XML skeleton.
* @param skeleton XML Skeleton
* @return XML with fixed NCC.
* Adds meta information to the skeleton XML document.
* This method modifies the skeleton XML document by adding meta information
* about the computed CCM metric.
* @param skeleton The skeleton XML document representing the code structure.
* @param params Parameters for the computation.
* @return The modified XML document containing meta information.
*/
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 addMetaInformation(final XML skeleton, final Map<String, Object> params) {
try {
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not understand why you ignore the comment below. This part of the code is very difficult to support. You can add child element to the class without coping all this structure.

.newDocument();
final Element meta = doc.createElement("meta");
Copy link
Contributor

Choose a reason for hiding this comment

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

it is not required to change XML document and add some data, it is easier to call XSL with parameter, so you need to read ncc value in XSL and use it.

Copy link
Contributor

Choose a reason for hiding this comment

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

You do not understand, we can run XSL for class skeleton with parameter cmm, so we do not need to create package and all others inside, as all this arledy implemented and can be changed in case if it is needed in xsl using common way. it is absolutely not readable in code, xsl is much clear

final List<XML> packages = skeleton.nodes("//package");
for (final XML pack : packages) {
final Element tag = doc.createElement("package");
tag.setAttribute(
"id", pack.node().getAttributes().getNamedItem("id").getNodeValue()
);
final List<XML> classes = pack.nodes("class");
for (final XML clazz: classes) {
final Element sub = doc.createElement("class");
sub.appendChild(addNccTag(doc, clazz, params));
sub.setAttribute(
"id",
clazz.node().getAttributes().getNamedItem("id").getNodeValue()
);
tag.appendChild(sub);
}
meta.appendChild(tag);
}
final XSL modifier = new XSLDocument(
XslReport.class.getResourceAsStream(
"xsl/meta-info.xsl"
)
).with("meta", meta);
return modifier.transform(skeleton);
} catch (final ParserConfigurationException ex) {
throw new IllegalStateException(ex);
}
return transformed;
}

/**
* 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 NCC (Number of Component Connections) tag to the XML document.
* This method calculates the NCC for a given class and adds it as a tag to the XML document.
* @param doc The XML document to which the NCC tag will be added.
* @param clazz The XML representation of the class.
* @param params Parameters for the computation (unused).
* @return The NCC node.
*/
private static void updateNcc(
final XML skeleton, final String pack, final XML clazz
private static Node addNccTag(final Document doc, final XML clazz,
final Map<String, Object> params
) {
throw new UnsupportedOperationException(
new Joined(
"",
skeleton.toString(),
pack,
clazz.toString()
).toString()
final Element ncc = doc.createElement("ncc");
ncc.appendChild(doc.createTextNode(calculateComponents(clazz, params).toString()));
Copy link
Contributor

Choose a reason for hiding this comment

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

Now incapsulation is better, but according to OOP we should have
ncc.appendChild(doc.createTextNode(new GraphClass(clazz, params).components().lenght().toString()));
and all other calculations should be inside of that class

return ncc;
}

/**
* Calculates the number of components for a given class.
* This method calculates the number of components for a class using the Union-Find algorithm.
* @param clazz The XML representation of the class.
* @param params Parameters for the computation.
* @return The number of components.
*/
private static Integer calculateComponents(final XML clazz, final Map<String, Object> params) {
starkda marked this conversation as resolved.
Show resolved Hide resolved
final Map<String, List<String>> connections = new HashMap<>();
final Map<String, String> parents = new HashMap<>();
final List<XML> allowed = new ArrayList<>(0);
for (final XML method : clazz.nodes("methods/method")) {
final String name = method.xpath("@name").get(0);
if (isConstructorExcluded(params, method) || isStaticMethodExcluded(params, method)) {
continue;
}
allowed.add(method);
parents.put(name, name);
}
final UnionFind<String> find = new UnionFind<>(parents);
for (final XML method : allowed) {
final String name = method.xpath("@name").get(0);
final List<XML> ops = method.nodes("ops/op");
for (final XML operation : ops) {
final String code = operation.xpath("@code").get(0);
if (code.equals("call")) {
final String classpath = operation.nodes("name").get(0).node().getTextContent();
final List<String> splits = Arrays.asList(classpath.split("\\."));
if (parents.keySet().contains(splits.get(splits.size() - 1))) {
find.unite(name, splits.get(splits.size() - 1));
}
} else {
final String var = operation.node().getTextContent();
if (connections.containsKey(var)) {
connections.get(var).add(name);
} else {
final List<String> init = new ArrayList<>(0);
init.add(name);
connections.put(var, init);
}
}
}
}
return connectNodes(find, connections);
}

/**
* Connects nodes and calculates the number of components.
* This method connects nodes based and calculates the number of components
* using the Union-Find algorithm.
* @param find The Union-Find data structure to manage node connections.
* @param connections A map containing the relationships between nodes.
* @return The number of components after connecting nodes.
*/
private static int connectNodes(final UnionFind<String> find,
final Map<String, List<String>> connections
) {
connections.values().stream().forEach(
conns -> {
final String init = conns.get(0);
conns.stream().forEach(current -> find.unite(init, current));
}
);
return find.getSize();
}

/**
* Checks if a static method should be excluded based on parameters.
* @param params Parameters for filtering.
* @param method The method XML node.
* @return True if the method should be excluded, false otherwise.
*/
private static boolean isStaticMethodExcluded(final Map<String, Object> params,
final XML method) {
return !params.containsKey("include-static-methods") && method.xpath("@static").get(0)
.equals("true");
}

/**
* Checks if a constructor should be excluded based on parameters.
* @param params Parameters for filtering.
* @param method The method XML node.
* @return True if the constructor should be excluded, false otherwise.
*/
private static boolean isConstructorExcluded(final Map<String, Object> params,
final XML method) {
return !params.containsKey("include-ctors") && method.xpath("@ctor").get(0).equals("true");
}
}
109 changes: 109 additions & 0 deletions src/main/java/org/jpeek/calculus/java/implementations/UnionFind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.implementations;

import java.util.HashSet;
import java.util.Map;

/**
* Utility class implementing the Union-Find algorithm.
* The UnionFind class provides methods to perform the Union-Find algorithm,
* which is used to calculate the number of components in a given structure.
* @param <T> The type of elements in the Union-Find structure.
* @since 0.30.25
*/
public class UnionFind<T> {
/**
* The map representing the parent-child relationships of elements.
*/
private final Map<T, T> parents;

/**
* The number of components in the Union-Find structure.
*/
private int size;

/**
* Constructs a Union-Find data structure.
* This constructor initializes a Union-Find data structure with the given sets.
* It calculates the size of the structure based on the unique values in the sets.
* The Union-Find algorithm is used to manage the relationships between elements in the sets.
* @param sets The initial sets representing the elements and their relationships.
*/
public UnionFind(final Map<T, T> sets) {
this.parents = sets;
this.size = new HashSet<>(sets.values()).size();
}

/**
* Unites two nodes using the Union-Find algorithm.
* This method unites two nodes using the Union-Find algorithm.
* @param node The first node.
* @param son The second node.
*/
public void unite(final T node, final T son) {
if (!(this.parents.containsKey(node) && this.parents.containsKey(son))) {
throw new IllegalStateException("some of the nodes are not from the initial set");
}
final T root = this.getParent(node);
final T attachable = this.getParent(son);
if (!root.equals(attachable)) {
this.size -= 1;
this.parents.put(attachable, root);
}
}

/**
* Gets the size of the Union-Find structure.
* This method retrieves the number of components in the Union-Find structure.
* The size represents the number of disjoint sets or components in the structure.
* @return The size of the Union-Find structure, which is the number of components.
*/
public int getSize() {
return this.size;
}

/**
* Gets the parent of a node using the Union-Find algorithm.
* This method retrieves the parent of a node using the Union-Find algorithm.
* @param node The node whose parent is to be found.
* @return The parent of the node.
*/
private T getParent(final T node) {
if (!this.parents.containsKey(node)) {
throw new IllegalStateException("node is not from the initial set");
}
T ancestor = node;
while (!this.parents.get(ancestor).equals(ancestor)) {
ancestor = this.parents.get(ancestor);
}
T current = node;
while (!this.parents.get(current).equals(current)) {
final T temp = this.parents.get(current);
this.parents.put(current, ancestor);
current = temp;
}
return ancestor;
}
}
Loading
Loading