Skip to content

Commit

Permalink
Exposing unified model for JGraphT analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
avishek-sen-gupta committed Jul 14, 2024
1 parent 76f8995 commit bd13ae9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is an evolving toolkit of capabilities helpful for reverse engineering lega
- Injecting execution traces from the SMOJOL interpreter into Neo4J
- Injecting record dependency connections (MOVE, COMPUTE, etc.) into Neo4J
- Integration with OpenAI GPT to summarise nodes using bottom-up node traversal
- Exposes a unified model (AST, CFG, Data Structures with appropriate interconnections) which can be analysed through [JGraphT](https://jgrapht.org/) (which is embedded in the library)
- Support for namespaces to allow unique addressing of (possibly same) graphs
- Exporting ASTs, CFGs, and record dependencies to GraphML format

Expand Down Expand Up @@ -155,6 +156,12 @@ CALL apoc.export.graphml.all("<export.graphml>", {})

The file will be in the ```import``` directory inside the directory where the current database files are stored (in Neo4J Desktop).

## Analysis through JGraphT

The library embeds [JGraphT](https://jgrapht.org/), a powerful library of graph algorithms. The ```JGraphTBuilder``` class converts the unified model (AST, CFG, Data Structures) into a DirectedPseudograph (because there can be both loops and parallel edges between two nodes), for consequent analysis through the JGraphT API.

Custom analyses are a work in progress.

## How to Build

Run: ```mvn clean install```.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.smojol.analysis.graph.graphml;

import lombok.Getter;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.jgrapht.Graph;
import org.jgrapht.graph.DirectedPseudograph;
Expand All @@ -22,22 +23,23 @@
import static com.mojo.woof.NodeProperties.*;
import static com.mojo.woof.NodeRelations.*;

public class GraphMLExportCommands {
public class JGraphTGraphBuilder {
private final CobolDataStructure dataRoot;
private final NodeSpecBuilder qualifier;
private final FlowNode astRoot;
private final FlowNode cfgRoot;
private final Graph<TypedGraphVertex, TypedGraphEdge> astGraph;
private final Graph<TypedGraphVertex, TypedGraphEdge> cfgGraph;
private final Graph<TypedGraphVertex, TypedGraphEdge> dataStructuresGraph;
@Getter private final Graph<TypedGraphVertex, TypedGraphEdge> model;
private final JGraphTCodeOperations astGraphOperations;
private final JGraphTDataOperations dataGraphOperations;

public GraphMLExportCommands(CobolDataStructure dataStructures, FlowNode procedureRoot, NodeSpecBuilder qualifier) {
public JGraphTGraphBuilder(CobolDataStructure dataStructures, FlowNode procedureRoot, NodeSpecBuilder qualifier) {
this.dataRoot = dataStructures;
this.astRoot = this.cfgRoot = procedureRoot;
this.qualifier = qualifier;
cfgGraph = astGraph = dataStructuresGraph = new DirectedPseudograph<>(TypedGraphEdge.class);
model = cfgGraph = astGraph = dataStructuresGraph = new DirectedPseudograph<>(TypedGraphEdge.class);
// astGraph = new DirectedAcyclicGraph<>(TypedGraphEdge.class);
// cfgGraph = new DefaultDirectedGraph<>(TypedGraphEdge.class);
// dataStructuresGraph = new DefaultDirectedGraph<>(TypedGraphEdge.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import java.util.List;
import java.util.Map;

public class Neo4JASTExporter {
public class Neo4JGraphBuilder {
private final GraphSDK sdk;
private final CobolDataStructure data;
private final NodeSpecBuilder qualifier;
private final NodeReferenceStrategy astNodeReferenceStrategy;
private final NodeReferenceStrategy dependencyAttachmentStrategy;
private Graph<FlowNode, DefaultEdge> graph;

public Neo4JASTExporter(GraphSDK sdk, CobolDataStructure dataStructures, NodeSpecBuilder qualifier, NodeReferenceStrategy nodeReferenceStrategy, NodeReferenceStrategy dependencyAttachmentStrategy) {
public Neo4JGraphBuilder(GraphSDK sdk, CobolDataStructure dataStructures, NodeSpecBuilder qualifier, NodeReferenceStrategy nodeReferenceStrategy, NodeReferenceStrategy dependencyAttachmentStrategy) {
this.sdk = sdk;
this.data = dataStructures;
this.qualifier = qualifier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.neo4j.driver.Record;
import org.smojol.analysis.ParsePipeline;
import org.smojol.analysis.graph.*;
import org.smojol.analysis.graph.graphml.GraphMLExportCommands;
import org.smojol.analysis.graph.graphml.JGraphTGraphBuilder;
import org.smojol.analysis.graph.neo4j.*;
import org.smojol.common.flowchart.FlowNode;
import org.smojol.common.flowchart.FlowNodeService;
Expand Down Expand Up @@ -52,7 +52,7 @@ public void run(ParsePipeline pipeline) throws IOException {
}

private static void exportToGraphML(FlowNode astRoot, CobolDataStructure dataStructures, NodeSpecBuilder qualifier) {
GraphMLExportCommands graphMLExporter = new GraphMLExportCommands(dataStructures, astRoot, qualifier);
JGraphTGraphBuilder graphMLExporter = new JGraphTGraphBuilder(dataStructures, astRoot, qualifier);
graphMLExporter.buildAST();
graphMLExporter.buildCFG();
graphMLExporter.buildDataStructures();
Expand All @@ -64,7 +64,7 @@ private void exportToNeo4J(FlowNode root, CobolDataStructure dataStructures, Nod
root.accept(new Neo4JFlowCFGVisitor(sdk, qualifier), -1);

// Builds AST
Neo4JASTExporter neo4JExporter = new Neo4JASTExporter(sdk, dataStructures, qualifier, this.astNodeReferenceStrategy, this.dataDependencyAttachmentStrategy);
Neo4JGraphBuilder neo4JExporter = new Neo4JGraphBuilder(sdk, dataStructures, qualifier, this.astNodeReferenceStrategy, this.dataDependencyAttachmentStrategy);
neo4JExporter.buildAST(root);

// Builds data structures
Expand Down

0 comments on commit bd13ae9

Please sign in to comment.