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

#397 Graph infrastructure #402

Merged
merged 7 commits into from
Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 4 additions & 4 deletions src/main/java/org/jpeek/Calculus.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
/**
* Metrics calculus interface.
* @since 0.30.9
* @todo #390:30min We have a separate interface to calculate XML metrics.
* We should continue to the goal defined in #296 where we should have a java
* implementation to calculate metrics. The motivation is to be able to make
* calculations impossible or too difficult to implement in xsl, like LCOM4.
* @todo #397:30min We start having the infrastructure for graph building from skeleton.
* The graph have just nodes for now without any connection between them.
* We should continue to build nodes connections to reflect methods (inter-)calls.
* After that, we should continue to implement an LCOM4 metrics calculus based on the graph.
*/
public interface Calculus {

Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/jpeek/graph/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.graph;

import java.util.List;

/**
* Graph containing list of nodes.
* @since 0.30.9
*/
public interface Graph {

/**
* Nodes composing this graph.
* @return List of the nodes belonging to this graph
*/
List<Node> nodes();
}
81 changes: 81 additions & 0 deletions src/main/java/org/jpeek/graph/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.graph;

import java.util.ArrayList;
import java.util.List;

/**
* Graph node description. It should at least provide its name and its neighbors.
* @since 0.30.9
*/
public interface Node {

/**
* Node name.
* @return A identifier for the node
*/
String name();

/**
* Calculates ingoing and outgoing connected nodes.
* @return List of nodes connected to this node.
*/
List<Node> connected();

Choose a reason for hiding this comment

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

@HDouss What about connections naming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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


/**
* Simple implementation.
* @since 0.30.9
*/
final class Simple implements Node {
/**
* Node name.
*/
private final String nme;

/**
* Nodes connected to this node.
*/
private final List<Node> connect;

/**
* Ctor.
* @param name Node name
*/
public Simple(final String name) {
this.nme = name;
this.connect = new ArrayList<Node>(1);
}

@Override
public String name() {
return this.nme;
}

@Override
public List<Node> connected() {
return this.connect;
}
}
}
80 changes: 80 additions & 0 deletions src/main/java/org/jpeek/graph/XmlGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.graph;

import com.jcabi.xml.XML;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.cactoos.text.Joined;
import org.jpeek.skeleton.Skeleton;

/**
* Graph implementation built on skeleton.
* @since 0.30.9
*/
public final class XmlGraph implements Graph {

/**
* List of the nodes of this graph.
*/
private final List<Node> nds;

/**
* Ctor.
* @param skeleton XMl representation on whiwh to build the graph
* @throws IOException If fails
*/
public XmlGraph(final Skeleton skeleton) throws IOException {
this.nds = XmlGraph.build(skeleton);
Copy link

@fanifieiev fanifieiev Feb 29, 2020

Choose a reason for hiding this comment

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

@HDouss I don't think it is a good idea to execute code inside constructor.
https://www.yegor256.com/2015/05/07/ctors-must-be-code-free.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fanifieiev generally we can execute code if it is to initialize a final field. I think it would be better than to make the field non final and execute the code later. What do you think?

Choose a reason for hiding this comment

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

@HDouss You can use org.cactoos.Scalar for that case.
In Cactoos project we have a lot of such cases.
The main idea is to make a late execution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fanifieiev Thanks.

}

@Override
public List<Node> nodes() {
return this.nds;
}

/**
* Builds the graph from the skeleton.
* @param skeleton XML representation on whiwh to build the graph
* @return List of nodes
* @throws IOException If fails
*/
private static List<Node> build(final Skeleton skeleton) throws IOException {
final List<XML> methods = skeleton.xml().nodes(
"//methods/method[@ctor='false' and @abstract='false']"
);
final List<Node> result = new ArrayList<>(methods.size());
for (final XML method:methods) {

Choose a reason for hiding this comment

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

@HDouss I think Mapped list would be perfect here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fanifieiev absolutely, did not about this class. Thanks!

result.add(
new Node.Simple(
new Joined(
"", method.xpath("@name").get(0), method.xpath("@desc").get(0)
).asString()
)
);
}
return result;
}
}
30 changes: 30 additions & 0 deletions src/main/java/org/jpeek/graph/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.
*/

/**
* JPeek graphs.
*
* @since 0.30.9
*/
package org.jpeek.graph;
48 changes: 48 additions & 0 deletions src/test/java/org/jpeek/graph/NodeSimpleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.graph;

import java.io.IOException;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;

/**
* Test case for {@link Node.Simple}.
* @since 0.30.9
*/
public final class NodeSimpleTest {

@Test
public void givesName() throws IOException {
final String name = "name";
final Node.Simple node = new Node.Simple(name);
new Assertion<>(
"Must be more the 2 files",
Copy link
Collaborator

Choose a reason for hiding this comment

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

@HDouss I don't think that this message is related to what is being asserted, please fix it

node.name(),
new IsEqual<>(name)
).affirm();
}

}
83 changes: 83 additions & 0 deletions src/test/java/org/jpeek/graph/XmlGraphTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2019 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.graph;

import java.io.IOException;
import java.util.List;
import org.cactoos.list.ListOf;
import org.hamcrest.core.AllOf;
import org.jpeek.FakeBase;
import org.jpeek.skeleton.Skeleton;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasValuesMatching;

/**
* Test case for {@link XmlGraph}.
* @since 0.30.9
*/
public final class XmlGraphTest {

@Test
@SuppressWarnings("unchecked")
public void buildsMethodsAsNodes() throws IOException {
final List<Node> nodes = new XmlGraph(
new Skeleton(new FakeBase("MethodMethodCalls"))
).nodes();
new Assertion<>(
"Must build nodes representing methods",
nodes,
new AllOf<Iterable<Node>>(
new ListOf<>(
new HasValuesMatching<>(
node -> {
return node.name().equals("methodOne()I");
}
),
new HasValuesMatching<>(
node -> {
return node.name().equals("methodTwo()I");
}
),
new HasValuesMatching<>(
node -> {
return node.name().equals("methodThree()I");
}
),
new HasValuesMatching<>(
node -> {
return node.name().equals("methodFour()I");
}
),
new HasValuesMatching<>(
node -> {
return node.name().equals("methodFive()I");
}
)
)
)
).affirm();
}

}
Loading