question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about factories vs DiagramModel assembling

See original GitHub issue

Hi guys,

first of all thank you very much for your time and effort put into making this awesome library based on modern technologies. I’m trying to utilize it for a specific challenge I’m facing and it looks very promising. I’m a little confused about proper usage of factories / model creation and would appreciate if someone provides me some start aid.

I want to (later) customize elements appearance, so I created my factory for that.

export class OntologyNodeFactory extends AbstractReactFactory<OntologyNodeModel, DiagramEngine> {
    ontology: Ontology;

	constructor(ontology: Ontology) {
		super('ont-custom-node');
		this.ontology = ontology
	}

        generateModel(initialConfig: any) {
               return new ModelCreator(this.ontology).createModel();
        }

	generateReactWidget(event: any): JSX.Element {
		return <DefaultNodeWidget engine={this.engine} node={event.model} />;
	}
}
  • ModelCreator class is responsible for traversing the data model and creating a custom node model.
export class ModelCreator {

    ontology: Ontology;

    constructor(ontology: Ontology) {
        this.ontology = ontology;
    }

    createModel(): OntologyNodeModel {
        let root = ModelCreator.createRootNode(this.ontology);
        let rootOut = root.getPort('out') as unknown as DefaultPortModel;

        Object.entries(this.ontology.properties).forEach(([key, value]) => {
            let node = ModelCreator.createNode(key, value.type.type);
            let portIn = node.getPort('in');
            rootOut.link<DefaultLinkModel>(portIn!);
            root.children.push(node)
        });
        return root;
    }

    static createRootNode(ontology: Ontology): OntologyNodeModel {
        return new OntologyNodeModel({
            name: ontology.title,
            color: 'rgb(0,192,255)',
        });
    }

    static createNode(property: string, type: string): OntologyNodeModel {
        return new OntologyNodeModel({
            name: property + ' (' + type + ')',
            color: 'rgb(0,192,255)',
        });
    }

}

What seems strange to me, however, is that now, my component which I expected to care about layout of nodes, needs to traverse the node model somehow in order to get all elements into DiagramModel. I even added a “children” element to my node model to make it easier (which is arguably robust) but it still looks like I’m doing things twice.

const AppDiagram: React.FC<Props> = (props: Props) => {

    const {ontology} = props;

    let factory = new OntologyNodeFactory(ontology);
    engine.getNodeFactories().registerFactory(factory);

    const root = factory.generateModel({});
    root.setPosition(100, 100);
    let rootOut = root.getPort('out') as DefaultPortModel;

    const model = new DiagramModel();
    model.addNode(root);

    root.children.forEach(childNode => {
        model.addNode(childNode);
        childNode.setPosition(random(1, 1000, false), random(1, 600, false));
        let portIn = childNode.getPort('in');
        let link = rootOut!.link<DefaultLinkModel>(portIn!);
        model.addLink(link);
    });

    engine.setModel(model);

    return (
        <OntologyCanvasWidget>
            <CanvasWidget engine={engine} />
        </OntologyCanvasWidget>
    )
};

I have a feeling I’m misinterpreting something. What is the factory method “generateModel” for - am I using it wrong?

Appreciate any hint, thanks!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cattiredcommented, Apr 10, 2020

will do =)

1reaction
renato-bohlercommented, Apr 10, 2020

Hmm I’m not 100% sure if node factories are only used on deserialization… maybe you should try to not implement it and see what happens haha

Read more comments on GitHub >

github_iconTop Results From Across the Web

35 Assembly Line Worker Interview Questions (With Example ...
Learn about assembly line worker interview questions a hiring manager may ask so ... What is your experience in manufacturing or production?
Read more >
IELTS Diagram: Model Answer Band Score 9
This IELTS diagram model answer is estimated at band score 9. It is possible to have a diagram in your IELTS writing task...
Read more >
Factory system | Overview, History, & Facts | Britannica
Factory production became increasingly globalized, with parts for products originating in different countries and being shipped to their point of assembly. As ...
Read more >
Assembly Line Efficiency Measurement and Line Balancing-A Case ...
The factors influencing the assembly time in manufacturing systems are analysed, the precedence diagram model for the above assembly line is built and...
Read more >
Data Flow Diagram Model | Computer Assembling Factory Work Flow
The DFD (Yourdon and Coad notation) example "Model of small traditional production enterprise" below was created using the ConceptDraw DIAGRAM diagramming ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found