2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
17 import React from 'react';
18 import Application from './components/application/Application';
19 import Common from './common/Common';
20 import Options from './common/Options';
21 import Model from './model/Model';
22 import Metamodel from './model/Metamodel';
23 import Metamodels from './model/Metamodels';
24 import Scenarios from './model/demo/scenarios/Scenarios';
25 import '../../../../res/sdc-sequencer.scss';
27 * ASDC Sequencer entry point.
29 export default class Sequencer extends React.Component {
31 // //////////////////////////////////////////////////////////////////////////////////////////////
33 constructor(props, context) {
34 super(props, context);
37 this.setMetamodel.bind(this);
38 this.setModel.bind(this);
39 this.getModel.bind(this);
40 this.getMetamodel.bind(this);
41 this.getSVG.bind(this);
42 this.getDemoScenarios.bind(this);
43 this.newModel.bind(this);
47 this.options = new Options(props.options);
51 const scenarios = this.getDemoScenarios();
52 this.setMetamodel(scenarios.getMetamodels());
54 // this.setModel(scenarios.getBlank());
55 this.setModel(scenarios.getDimensions());
56 // this.setModel(scenarios.getECOMP());
60 // //////////////////////////////////////////////////////////////////////////////////////////////
63 * Optionally save known metamodels so that subsequent loading and unloading
64 * of models needn't include the corresponding metamodel.
65 * @param metamodels array of conformant metamodel JSON definitions.
68 setMetamodel(metamodels) {
69 Common.assertType(metamodels, 'Array');
70 this.metamodels = new Metamodels(metamodels);
74 // //////////////////////////////////////////////////////////////////////////////////////////////
77 * Set current diagram.
78 * @param modelJSON JSON diagram spec.
79 * @param metamodelIdOrDefinition optional metamodel definition or reference. Defaults to
80 * the model's metadata @ref, or the default (permissive) metamodel.
83 setModel(modelJSON, metamodelIdOrDefinition) {
84 Common.assertType(modelJSON, 'Object');
85 const ref = (modelJSON.metadata) ? modelJSON.metadata.ref : undefined;
86 const metamodel = this.getMetamodel(metamodelIdOrDefinition || ref);
87 Common.assertInstanceOf(metamodel, Metamodel);
88 this.model = new Model(modelJSON, metamodel);
89 if (this.application) {
90 this.application.setModel(this.model);
95 // //////////////////////////////////////////////////////////////////////////////////////////////
98 * Get current diagram state. At any given instant the diagram might not make *sense*
99 * but it should always be syntactically valid.
100 * @return current Model.
104 if (this.application) {
105 const model = this.application.getModel();
107 return model.unwrap();
114 // //////////////////////////////////////////////////////////////////////////////////////////////
117 * Extract SVG element.
118 * @return stringified SVG element.
121 return this.application.getSVG();
124 // //////////////////////////////////////////////////////////////////////////////////////////////
127 * Get demo scenarios, allowing initialization in demo mode from the outside.
128 * @returns {Scenarios}
131 return new Scenarios();
134 // //////////////////////////////////////////////////////////////////////////////////////////////
138 * @param metamodelIdOrDefinition
139 * @return newly-created model.
141 newModel(metamodelIdOrDefinition) {
142 const metamodel = this.getMetamodel(metamodelIdOrDefinition);
143 Common.assertInstanceOf(metamodel, Metamodel);
144 const model = new Model({}, metamodel);
145 if (this.application) {
146 this.application.setModel(model);
151 // //////////////////////////////////////////////////////////////////////////////////////////////
154 * Get Metamodel instance corresponding to an ID or JSON definition.
155 * @param metamodelIdOrDefinition String ID or JSON definition.
156 * @returns Metamodel instance.
159 getMetamodel(metamodelIdOrDefinition) {
160 const metamodelType = Common.getType(metamodelIdOrDefinition);
161 if (metamodelType === 'Object') {
162 return new Metamodel(metamodelIdOrDefinition);
164 return this.metamodels.getMetamodelOrDefault(metamodelIdOrDefinition);
167 // //////////////////////////////////////////////////////////////////////////////////////////////
170 * Render current diagram state.
174 if (this.props.model) {
176 // If a model was specified as a property, apply it. Otherwise
177 // fall back to the demo model.
179 const scenarios = this.getDemoScenarios();
180 const metamodel = [scenarios.getBlankMetamodel(), scenarios.getECOMPMetamodel()];
181 if (this.props.metamodel) {
182 metamodel.push(this.props.metamodel);
184 this.setMetamodel(metamodel);
185 this.setModel(this.props.model);
189 <Application options={this.options} sequencer={this} ref={(a) => { this.application = a; }} />
195 Sequencer.propTypes = {
196 options: React.PropTypes.object.isRequired,
197 model: React.PropTypes.object,
198 metamodel: React.PropTypes.object,