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

feat(dependency graph): add option to create a dependency graph #119

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/common/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ConcertoGraphVisitor extends DiagramVisitor {
if (classDeclaration.getSuperType()){
parameters.graph.addEdge(classDeclaration.getFullyQualifiedName(), classDeclaration.getSuperType());
// this "if" block adds the types that extend the Super type
if(classDeclaration.getSuperType() !== '[email protected]') {
if(!parameters.createDependencyGraph && classDeclaration.getSuperType() !== '[email protected]') {
parameters.graph.addVertex(classDeclaration.getSuperType());
parameters.graph.addEdge(classDeclaration.getSuperType(), classDeclaration.getFullyQualifiedName());
}
Expand Down
137 changes: 123 additions & 14 deletions test/common/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

'use strict';

const { DirectedGraph, ConcertoGraphVisitor } = require('../../lib/common/common.js');
const {
DirectedGraph,
ConcertoGraphVisitor,
} = require('../../lib/common/common.js');
const { ModelManager } = require('@accordproject/concerto-core');
const fs = require('fs');
const { expect } = require('expect');
Expand All @@ -26,7 +29,7 @@ chai.should();
chai.use(require('chai-as-promised'));
chai.use(require('chai-things'));

describe('graph', function () {
describe('graph', function() {
let modelManager = null;

before(function() {
Expand All @@ -35,15 +38,20 @@ describe('graph', function () {

beforeEach(function() {
modelManager = new ModelManager();
const hrBase = fs.readFileSync('./test/codegen/fromcto/data/model/hr_base.cto', 'utf-8');
const hrBase = fs.readFileSync(
'./test/codegen/fromcto/data/model/hr_base.cto',
'utf-8'
);
modelManager.addCTOModel(hrBase, 'hr_base.cto');
const hr = fs.readFileSync('./test/codegen/fromcto/data/model/hr.cto', 'utf-8');
const hr = fs.readFileSync(
'./test/codegen/fromcto/data/model/hr.cto',
'utf-8'
);
modelManager.addCTOModel(hr, 'hr.cto');
});


describe('#visitor', function () {
it('should visit a model manager', function () {
describe('#visitor', function() {
it('should visit a model manager', function() {
const visitor = new ConcertoGraphVisitor();
visitor.should.not.be.null;
const writer = new InMemoryWriter();
Expand Down Expand Up @@ -136,23 +144,32 @@ describe('graph', function () {
`);
});


it('should visit find a connected subgraph', function () {
it('should visit find a connected subgraph', function() {
const visitor = new ConcertoGraphVisitor();
visitor.should.not.be.null;
const writer = new InMemoryWriter();

const graph = new DirectedGraph();
modelManager.accept(visitor, { graph });

const connectedGraph = graph.findConnectedGraph('[email protected]');
expect(connectedGraph.hasEdge('[email protected]', '[email protected]'));
const connectedGraph = graph.findConnectedGraph(
'[email protected]'
);
expect(
connectedGraph.hasEdge(
'[email protected]',
'[email protected]'
)
);

const filteredModelManager = modelManager
.filter(declaration => connectedGraph.hasVertex(declaration.getFullyQualifiedName()));
const filteredModelManager = modelManager.filter((declaration) =>
connectedGraph.hasVertex(declaration.getFullyQualifiedName())
);

expect(filteredModelManager.getModelFiles()).toHaveLength(2);
expect(filteredModelManager.getModelFiles()[0].getAllDeclarations()).toHaveLength(5);
expect(
filteredModelManager.getModelFiles()[0].getAllDeclarations()
).toHaveLength(5);

writer.openFile('graph.mmd');
connectedGraph.print(writer);
Expand Down Expand Up @@ -226,6 +243,98 @@ describe('graph', function () {
\`[email protected]\` <--> \`[email protected]\`
\`[email protected]\`
\`[email protected]\`
`);
});

it('should visit a model manager and create a dependency graph', function() {
const visitor = new ConcertoGraphVisitor();
visitor.should.not.be.null;
const writer = new InMemoryWriter();

const graph = new DirectedGraph();
modelManager.accept(visitor, {
graph,
createDependencyGraph: true,
});

writer.openFile('graph.mmd');
graph.print(writer);
writer.closeFile();
expect(writer.data.get('graph.mmd')).toEqual(`flowchart LR
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`String\`
\`[email protected]\`
\`[email protected]\` --> \`String\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`String\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`String\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` <--> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\` --> \`[email protected]\`
\`[email protected]\`
\`[email protected]\`
`);
});
});
Expand Down
Loading