diff --git a/src/components/Main.js b/src/components/Main.js index d079ab17..b2d1a99a 100644 --- a/src/components/Main.js +++ b/src/components/Main.js @@ -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()); diff --git a/src/model/Interpreter.ts b/src/model/Interpreter.ts index 157f9889..81e46b8e 100644 --- a/src/model/Interpreter.ts +++ b/src/model/Interpreter.ts @@ -5,7 +5,6 @@ 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'); @@ -13,8 +12,13 @@ const parse = require('dotparser'); export default class ModelInterpreter { nativeModel: any; jsonModel: Object; + modelMap: { [key: string]: Map }; constructor(model: any) { + this.modelMap = { + 'nodes': new Map(), + 'links': new Map() + }; this.nativeModel = model; this.jsonModel = this._convertModel(model); } @@ -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; }); @@ -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]: [], @@ -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 }) + : 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.'); @@ -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] = {} @@ -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: @@ -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: { @@ -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: diff --git a/src/model/links/ProjectionLink.ts b/src/model/links/ProjectionLink.ts index 0ff61f58..8330f718 100644 --- a/src/model/links/ProjectionLink.ts +++ b/src/model/links/ProjectionLink.ts @@ -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 { @@ -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, diff --git a/src/model/nodes/composition/CompositionNode.ts b/src/model/nodes/composition/CompositionNode.ts index 9a7fb71f..94bd3123 100644 --- a/src/model/nodes/composition/CompositionNode.ts +++ b/src/model/nodes/composition/CompositionNode.ts @@ -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 = [] - // 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, diff --git a/src/model/nodes/mechanism/MechanismNode.ts b/src/model/nodes/mechanism/MechanismNode.ts index 121159ea..f786e6f7 100644 --- a/src/model/nodes/mechanism/MechanismNode.ts +++ b/src/model/nodes/mechanism/MechanismNode.ts @@ -7,7 +7,7 @@ export default class MechanismNode implements IMetaDiagramConverter { name: string; icon: string; isExpanded: Boolean; - ports: { [key: string]: Array } ; + ports: { [key: string]: Array }; extra: Object; innerClass: String; parent: MechanismNode|CompositionNode|undefined;