Skip to content

Commit

Permalink
#36 more on the composition implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ddelpiano committed Sep 7, 2022
1 parent cc20d77 commit 80f8911
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/components/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Main extends React.Component {
const interpreter = new ModelInterpreter(mockModel);
const model = interpreter.getModel();
const metaModel = buildModel(model);
console.log(interpreter.getModelElementsMap())

const componentsMap = new ComponentsMap(new Map(), new Map());

Expand Down
37 changes: 23 additions & 14 deletions src/model/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import QueryService from '../services/queryService';
import MechanismNode from './nodes/mechanism/MechanismNode';
import CompositionNode from './nodes/composition/CompositionNode';

const html2json = require('html2json').html2json
const typesArray = Object.values(GVTypes);
const parse = require('dotparser');


export default class ModelInterpreter {
nativeModel: any;
jsonModel: Object;
modelMap: { [key: string]: Map<String, CompositionNode|MechanismNode|ProjectionLink|any> };

constructor(model: any) {
this.modelMap = {
'nodes': new Map(),
'links': new Map()
};
this.nativeModel = model;
this.jsonModel = this._convertModel(model);
}
Expand All @@ -26,13 +30,13 @@ export default class ModelInterpreter {
};

parsedModel[PNLClasses.COMPOSITION] = model[PNLClasses.COMPOSITION].map((singleModel: any) => {
const newModel = parse(singleModel).map((elem: any) => ModelInterpreter.castObject(elem, undefined));
const newModel = parse(singleModel).map((elem: any) => this.castObject(elem, undefined, this.modelMap));
return newModel;
});

parsedModel[PNLClasses.MECHANISM] = model[PNLClasses.MECHANISM].map((singleNode: any) => {
let tempNode = parse(singleNode)[0].children.filter((elem: { node_id: { id: string; }; }) => elem.node_id.id !== 'graph');
let newNode = tempNode.map((elem: any) => ModelInterpreter.castObject(elem, undefined));
let newNode = tempNode.map((elem: any) => this.castObject(elem, undefined, this.modelMap));
return newNode;
});

Expand All @@ -51,7 +55,11 @@ export default class ModelInterpreter {
return this.nativeModel;
}

static parseNodePorts(name: string, type: string): { [key: string]: any } {
getModelElementsMap() {
return this.modelMap;
}

parseNodePorts(name: string, type: string): { [key: string]: any } {
let ports: { [key: string]: any[] } = {
[PortTypes.INPUT_PORT]: [],
[PortTypes.OUTPUT_PORT]: [],
Expand Down Expand Up @@ -80,7 +88,11 @@ export default class ModelInterpreter {
return ports;
}

static castObject(item: MechanismNode|CompositionNode|ProjectionLink|any, parent: any|undefined) : MechanismNode|CompositionNode|ProjectionLink {
castObject(
item: MechanismNode|CompositionNode|ProjectionLink|any,
parent: any|undefined,
modelMap: { [key: string]: Map<String, CompositionNode|MechanismNode|ProjectionLink|any> })
: MechanismNode|CompositionNode|ProjectionLink {
let newNode = item;
if (item?.type === undefined) {
throw new TypeError('type is missing, object cannot be casted to the right class type.');
Expand All @@ -95,6 +107,7 @@ export default class ModelInterpreter {
[PNLClasses.COMPOSITION]: [],
}
newNode = new CompositionNode(item.id, parent, '', false, ports, extra, children);
modelMap['nodes'].set(newNode.getName(), newNode);
item.children.forEach((element: any) => {
if (element.type === 'attr_stmt') {
extra[element.target] = {}
Expand All @@ -108,15 +121,15 @@ export default class ModelInterpreter {
if (typesArray.includes(element.type)) {
switch (element.type) {
case GVTypes.COMPOSITION: {
children[PNLClasses.COMPOSITION].push(ModelInterpreter.castObject(element, newNode));
children[PNLClasses.COMPOSITION].push(this.castObject(element, newNode, modelMap));
break;
}
case GVTypes.MECHANISM: {
children[PNLClasses.MECHANISM].push(ModelInterpreter.castObject(element, newNode));
children[PNLClasses.MECHANISM].push(this.castObject(element, newNode, modelMap));
break;
}
case GVTypes.PROJECTION: {
children[PNLClasses.PROJECTION].push(ModelInterpreter.castObject(element, newNode));
children[PNLClasses.PROJECTION].push(this.castObject(element, newNode, modelMap));
break;
}
default:
Expand All @@ -132,17 +145,12 @@ export default class ModelInterpreter {
let ports: { [key: string]: any } = this.parseNodePorts(item?.node_id?.id, PNLClasses.MECHANISM);
let extra: { [key: string]: any } = {};
item.attr_list.forEach((singleAttr: any) => {
if (singleAttr.id === 'label') {
// TODO: implement the parsing of the json structure generated below
// in order to detect ports and other elements of the node.
let parsedHtml = html2json(singleAttr.eq.value);
// console.log(parsedHtml)
}
if (singleAttr.type === 'attr') {
extra[singleAttr?.id] = singleAttr?.eq;
}
});
newNode = new MechanismNode(item?.node_id?.id, parent, '', false, ports, extra);
modelMap['nodes'].set(newNode.getName(), newNode);
break;
}
case GVTypes.PROJECTION: {
Expand All @@ -165,6 +173,7 @@ export default class ModelInterpreter {
receiverPort = item.edge_list[1]['port']['id'];
}
newNode = new ProjectionLink(name, sender, senderPort, receiver, receiverPort, false, extra);
modelMap['links'].set(newNode.getName(), newNode);
break;
}
default:
Expand Down
7 changes: 4 additions & 3 deletions src/model/links/ProjectionLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export default class ProjectionLink implements IMetaLinkConverter {
this.extra = extra !== undefined ? extra : {};
this.isExpanded = isExpanded !== undefined ? isExpanded : false;
this.innerClass = PNLClasses.PROJECTION;

if (this.name === '') {
this.name = 'link_' + this.sender + '-to-' + this.receiver;
}
}

getName() : string {
Expand Down Expand Up @@ -57,9 +61,6 @@ export default class ProjectionLink implements IMetaLinkConverter {
}

getMetaLink() : MetaLink {
if (this.name === '') {
this.name = 'link_' + this.sender + '-to-' + this.receiver;
}
return new MetaLink(
this.name,
this.name,
Expand Down
5 changes: 0 additions & 5 deletions src/model/nodes/composition/CompositionNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ export default class CompositionNode extends MechanismNode {
let y = 200 + Math.random() * 600;
let parent = this.parent ? this.parent.getMetaNode() : undefined;
let ports: Array<MetaPort> = []
// TODO: the MetaPort has the enum prefix cause the projections are created with that prefix
// from the graphviz data we get.
// this.ports[PortTypes.INPUT_PORT].forEach((port: any) => ports.push(new MetaPort(PortTypes.INPUT_PORT + '-' + port, PortTypes.INPUT_PORT + '-' + port, PortTypes.INPUT_PORT, new Position(0, 0), new Map())));
// this.ports[PortTypes.OUTPUT_PORT].forEach((port: any) => ports.push(new MetaPort(PortTypes.OUTPUT_PORT + '-' + port, PortTypes.OUTPUT_PORT + '-' + port, PortTypes.OUTPUT_PORT, new Position(0, 0), new Map())));
// this.ports[PortTypes.PARAMETER_PORT].forEach((port: any) => ports.push(new MetaPort(PortTypes.PARAMETER_PORT + '-' + port, PortTypes.PARAMETER_PORT + '-' + port, PortTypes.PARAMETER_PORT, new Position(0, 0), new Map())));
return new MetaNode(
this.name,
this.name,
Expand Down
2 changes: 1 addition & 1 deletion src/model/nodes/mechanism/MechanismNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class MechanismNode implements IMetaDiagramConverter {
name: string;
icon: string;
isExpanded: Boolean;
ports: { [key: string]: Array<any> } ;
ports: { [key: string]: Array<any> };
extra: Object;
innerClass: String;
parent: MechanismNode|CompositionNode|undefined;
Expand Down

0 comments on commit 80f8911

Please sign in to comment.