Skip to content

Commit

Permalink
cqfn#397 graph infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
HDouss committed Feb 28, 2020
1 parent 72d5a81 commit 54e1520
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 4 deletions.
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();

/**
* 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);
}

@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) {
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",
node.name(),
new IsEqual<>(name)
).affirm();
}

}
82 changes: 82 additions & 0 deletions src/test/java/org/jpeek/graph/XmlGraphTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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
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

0 comments on commit 54e1520

Please sign in to comment.