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

Improve domainmodel showcase #178

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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
146 changes: 146 additions & 0 deletions hugo/assets/scripts/domainmodel/d3tree.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import React, { useRef, useEffect } from 'react';
import * as d3 from 'd3';
import tailwindConfig from '../../../../tailwind/tailwind.config';
import { DomainModelElementTypeNames } from './domainmodel-tools';

export interface TreeNode {
name: string;
children?: TreeNode[];

tags?: TreeNodeTag[];
$type?: DomainModelElementTypeNames;
}

export type TreeNodeTag = 'supertype' | 'many';
interface TreeProps {
data: TreeNode;
}


const D3Tree: React.FC<TreeProps> = ({ data }) => {
const svgRef = useRef<SVGSVGElement | null>(null);

useEffect(() => {
if (!svgRef.current) return;


// base height and width on the size tree
const getChildSize = (child: TreeNode): number => {
if (!child.children) return 1;
let amount = child.children.length;

// fastest way to iterate over an array
const length = child.children.length;
for (let i = 0; i < length; i++) {
amount += getChildSize(child.children[i]);
}
return amount;
};
const size = getChildSize(data);

const height = size * 20;
const width = size * 18;

const svg = d3.select(svgRef.current);
svg.selectAll('*').remove();
svg.attr('width', '100%').attr('height', '100%')

const zoom = d3.zoom<SVGSVGElement, unknown>().on('zoom', (event) => {
g.attr('transform', event.transform);
});

svg.call(zoom, d3.zoomIdentity.translate(50, 50));


const hierarchy = d3.hierarchy(data);
const treeLayout = d3.tree<TreeNode>().size([height, width]);

const treeData = treeLayout(hierarchy);
const g = svg.append('g');

// zoom to show the whole tree
svg.call(zoom.transform, d3.zoomIdentity.translate(width / size * 3, height / size * 2).scale(3 / (0.1 * size)));


const link = g.selectAll('.link')
.data(treeData.links())
.enter().append('path')
.attr('class', 'link')
.attr('d', d => {
// connect parent node to child node with a bezier curve (quadratic)
return `M${d.source.y},${d.source.x}C${d.source.y + 100},${d.source.x} ${d.target.y - 100},${d.target.x} ${d.target.y},${d.target.x}`;
})
.style('fill', 'none')
.style('stroke', 'white')
.style('stroke-width', '1px')
.style('stroke-dasharray', function (d) {
if(d.target.data.tags?.includes('supertype')) return '10,5';
if(d.source.data.tags?.includes('many')) return '5,5';
return 'none';
})
.style('stroke-opacity', '0.4');


const node = g.selectAll('.node')
.data(treeData.descendants())
.enter().append('g')
.attr('class', 'node')
.attr('transform', d => `translate(${d.y},${d.x})`);

node.append('circle')
.attr('r', 5)
.style('fill', function (d) {
switch (d.data.$type) {
case 'PackageDeclaration':
return '#8c2626'; // accentRed
case 'DataType':
return '#B6F059';
case 'Entity':
return '#D568E7'; // accentViolet
case 'Feature':
return '#1FCDEB'; // accentBlue
default:
return '#26888C';
}
});

node.append('text')
.attr('dy', '0.31em')
.attr('x', d => d.children ? -6 : 6)
.attr('text-anchor', d => d.children ? 'end' : 'start')
.text(d => d.data.name)
.attr("transform", d => d.children ? `translate(${d.data.name.length * 5}, -20)` : 'translate(5, 0)')
.style('font-size', '1em')
.style('font-weight', function (d) {
switch (d.data.$type) {
case 'Domainmodel':
return 'bold';
default:
return 'normal';
}
})
.style('fill', function (d) {
switch (d.data.$type) {
case 'PackageDeclaration':
return '#26888C';
case 'DataType':
return 'green';
case 'Entity':
return '#207578'
default:
return '#26888C';
}
});



}, [data]);

return (
<svg ref={svgRef} width="100%" height="100%" >
{/* SVG content will be rendered here */}
</svg>
);
};

export default D3Tree;
132 changes: 130 additions & 2 deletions hugo/assets/scripts/domainmodel/domainmodel-tools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,133 @@
import { monaco } from "monaco-editor-wrapper/.";

import { AstNode } from "../langium-utils/langium-ast";
import { TreeNode } from "./d3tree";


export function getTreeNode(ast: AstNode): TreeNode {
const astNode = getDomainModelAst(ast as DomainModelAstNode);

const packageDeclarations = astNode.packageDeclarations.map(p => {
const result: TreeNode = {
...p,
children: p.elements
}
return result;
});

const getDataTypeTreeNode = (d: DataType): TreeNode => {
const result: TreeNode = {
...d,
children: []
}
return result;
}


const getEntityTreeNode = (entity: Entity): TreeNode => {

const getFeatureTreeNode = (feature: Feature): TreeNode => {
const result: TreeNode = {
name: `${feature.name}${feature.many ? '[]' : ''}`,
$type: feature.$type,
tags: feature.many ? ['many'] : [],
children: [getDataTypeTreeNode(feature.type.ref)]
}
return result;
}

const getCildren = (e: Entity): TreeNode[] => {
const superType = astNode.entities.find(entity => entity.name === e.superType?.ref.name);
const features = e.features.map(f => { return getFeatureTreeNode(f) });

const children: TreeNode[] = superType ? [...features, {
name: superType.name,
$type: superType.$type,
tags: ['supertype'],
children: superType?.features.map(f => { return getFeatureTreeNode(f) })
}] : features;

return children;
}
const result: TreeNode = {
...entity,
children: getCildren(entity)
}
return result;
}

const entities = astNode.entities.map(e => { return getEntityTreeNode(e); });
const datatypes = astNode.dataTypes.map(d => { return getDataTypeTreeNode(d); });

const children: TreeNode[] = [
{name: 'DataTypes', $type: 'DataType', children: datatypes},
{name: 'Entities', $type: 'Entity', children: entities},
{name: 'Packages', $type: 'PackageDeclaration', children: packageDeclarations},
];

const result: TreeNode = {
name: astNode.$type,
$type: astNode.$type,
children: children
}
return result;
}

export function getDomainModelAst(ast: DomainModelAstNode ): DomainModelAstNode {
const result: DomainModelAstNode = {
name: ast.name,
$type: 'Domainmodel',
elements: ast.elements,
packageDeclarations: (ast.elements as DomainModelElement[]).filter(e => e.$type === 'PackageDeclaration') as PackageDeclaration[],
entities: (ast.elements as DomainModelElement[]).filter(e => e.$type === 'Entity') as Entity[],
dataTypes: (ast.elements as DomainModelElement[]).filter(e => e.$type === 'DataType') as DataType[],
}
return result;
}

export interface DomainModelAstNode extends AstNode, DomainModelElement {
$type: 'Domainmodel';
elements: DomainModelElementType[];

packageDeclarations: PackageDeclaration[];
entities: Entity[];
dataTypes: DataType[];
}

export interface PackageDeclaration extends DomainModelElement {
$type: 'PackageDeclaration';
elements: DataType[];
}

export interface Entity extends DomainModelElement {
$type: 'Entity';
features: Feature[];
superType?: {
ref: Entity
}
}

export interface Feature extends DomainModelElement {
$type: 'Feature';
type: {
ref: DataType
};
many: boolean;
}

export interface DataType extends DomainModelElement {
$type: 'DataType';
}

export interface DomainModelElement {
$type: string;
name: string;
}

// create a union type of all possible DomainModelElement types
export type DomainModelElementType = PackageDeclaration | Entity | DataType | Feature | DomainModelAstNode;

// create a union type of all possible DomainModelElement types names (string)
export type DomainModelElementTypeNames = DomainModelElementType['$type'];

export const example = `// Define all datatypes
datatype String
Expand Down Expand Up @@ -63,4 +191,4 @@ export const syntaxHighlighting = {
{ regex: /[\/\*]/, action: {"token":"comment"} },
],
}
} as monaco.languages.IMonarchLanguage;
};
Loading