UNPKG

@tripsnek/tmf

Version:

TypeScript Modeling Framework - A TypeScript port of the Eclipse Modeling Framework (EMF)

239 lines 8.63 kB
import { BasicEList } from './basicelist'; import { ENamedElementImpl } from './enamed-element-impl'; import { EAttributeImpl } from './eattribute-impl'; import { EClassImpl } from './eclass-impl'; import { EOperationImpl } from './eoperation-impl'; import { EReferenceImpl } from './ereference-impl'; import { EEnumImpl } from './eenum-impl'; import { EDataTypeImpl } from './edata-type-impl'; import { EEnumLiteralImpl } from './eenum-literal-impl'; import { EParameterImpl } from './eparameter-impl'; export class EPackage extends ENamedElementImpl { static EPACKAGE; // Initialized in EClass static ECORE; // Initialized in EClass /** * A map from {@link EPackage#getNsURI() namespace URI} to {@link EPackage}. * * NOTE: This is a significant simplication of the EPackage.Registry in EMF, in * that it consolidates several classes into one. */ static Registry = class Registry extends Map { static INSTANCE = new Registry(); register(pkg) { if (!this.has(pkg.getNsURI())) { this.set(pkg.getNsURI(), pkg); } } /** * Looks up the value in the map. */ getEPackage(nsURI) { return this.get(nsURI); } /** * Looks up the value in the map. */ getEFactory(nsURI) { return this.get(nsURI).getEFactoryInstance(); } }; _eClassifiers = new BasicEList(); _eSubPackages = new BasicEList(); _eSuperPackage; _nsPrefix; _nsURI; _eFactoryInstance; constructor(name, nsUri, nsPrefix) { super(name); if (nsUri) { this.setNsURI(nsUri); EPackage.Registry.INSTANCE.register(this); } if (nsPrefix) { this.setNsPrefix(nsPrefix); } } getEClassifier(name) { return this._eClassifiers.find((e) => e.getName() === name); } getESubPackageByName(name) { return this._eSubPackages.find((e) => e.getName() === name); } // TODO: Protected is probably not right // This method is probably to be invoked by setting an inverse field addEClassifier(eClassifier) { this._eClassifiers.add(eClassifier); } getEClassifiers() { return this._eClassifiers; } getESubPackages() { return this._eSubPackages; } getESuperPackage() { return this._eSuperPackage; } setESuperPackage(superPkg) { //TODO: should handle inverse reference on both ends this._eSuperPackage = superPkg; if (!this._eSuperPackage.getESubPackages().contains(this)) this._eSuperPackage.getESubPackages().add(this); } getNsPrefix() { return this._nsPrefix; } setNsPrefix(value) { this._nsPrefix = value; } getNsURI() { return this._nsURI; } setNsURI(value) { this._nsURI = value; } //====================================================================== // Factory-related methods getEFactoryInstance() { return this._eFactoryInstance; } setEFactoryInstance(value) { this._eFactoryInstance = value; } createEEnum(id) { const c = new EEnumImpl(); c.setClassifierId(id); c.setEPackage(this); this.getEClassifiers().add(c); return c; } createEDataType(id) { const c = new EDataTypeImpl(); c.setClassifierId(id); c.setEPackage(this); this.getEClassifiers().add(c); return c; } createEClass(id) { const c = new EClassImpl(); c.setClassifierId(id); this.getEClassifiers().add(c); c.setEPackage(this); return c; } addEEnumLiteral(owner, literal, value) { //NOTE: This is different from the EMF implementation, which exploits the //fact that Java allows enums to implement interfaces. To replicate that in //typescript, we would probably have to use types. If so, we should need only //to pass in an instance of that base class (Enumerator in EMF) as 'literal' const l = new EEnumLiteralImpl(); l.setLiteral(literal); l.setName(literal); l.setInstance(literal); l.setValue(value); l.setEEnum(owner); owner.getELiterals().add(l); } initEClass(c, instanceClassName, isAbstract, isInterface, isGenerated) { this.initEClassifier(c, instanceClassName, isGenerated); c.setAbstract(isAbstract); c.setInterface(isInterface); return c; } initEEnum(e, instanceClassName) { this.initEClassifier(e, instanceClassName, true); return e; } initEDataType(e, instanceClassName) { this.initEClassifier(e, instanceClassName, true); return e; } initEClassifier(o, instanceClassName, isGenerated) { o.setName(instanceClassName); // if (instanceClassName != null) { // o.setInstanceClass(instanceClassName); // } if (isGenerated) { //TODO: Should we add this generated class name stuff to EClassifier? // setGeneratedClassName(o); } } initEReference(r, type, otherEnd, name, defaultValue, lowerBound, upperBound, containerClass, isTransient, isVolatile, isChangeable, isContainment, isResolveProxies, isUnsettable, isUnique, isDerived, isOrdered) { this.initEStructuralFeature(r, type, name, defaultValue, lowerBound, upperBound, containerClass, isTransient, isVolatile, isChangeable, isUnsettable, isUnique, isDerived, isOrdered); r.setContainment(isContainment); if (otherEnd != null) { r.setEOpposite(otherEnd); } // r.setResolveProxies(isResolveProxies); return r; } initEAttribute(a, type, name, defaultValue, lowerBound, upperBound, containerClass, isTransient, isVolatile, isChangeable, isUnsettable, isID, isUnique, isDerived, isOrdered) { this.initEStructuralFeature(a, type, name, defaultValue, lowerBound, upperBound, containerClass, isTransient, isVolatile, isChangeable, isUnsettable, isUnique, isDerived, isOrdered); a.setId(isID); return a; } initEStructuralFeature(s, type, name, defaultValue, lowerBound, upperBound, containerClass, isTransient, isVolatile, isChangeable, isUnsettable, isUnique, isDerived, isOrdered) { s.setName(name); // s.setContainerClass(containerClass); s.setTransient(isTransient); s.setVolatile(isVolatile); s.setChangeable(isChangeable); // s.setUnsettable(isUnsettable); s.setUnique(isUnique); // s.setDerived(isDerived); // s.setOrdered(isOrdered); s.setLowerBound(lowerBound); s.setUpperBound(upperBound); s.setEType(type); if (defaultValue != null) { s.setDefaultValueLiteral(defaultValue); } } initEOperation(eOperation, type, name, lowerBound, upperBound, isUnique, isOrdered) { if (type) eOperation.setEType(type); eOperation.setName(name); if (lowerBound) eOperation.setLowerBound(lowerBound); if (upperBound) eOperation.setUpperBound(upperBound); eOperation.setUnique(isUnique ?? false); // eOperation.setOrdered(isOrdered ?? false); return eOperation; } createEOperation(owner, id) { const o = new EOperationImpl(); o.setOperationID(id); o.setEContainingClass(owner); owner.getEOperations().add(o); } createEParameter(owner, name, upperBound, type) { const param = new EParameterImpl(); param.setName(name); if (type) param.setEType(type); param.setUpperBound(upperBound); //TODO: Would not have to do both if model was source generated owner.getEParameters().add(param); } createEAttribute(owner, id) { const a = new EAttributeImpl(); a.setFeatureID(id); owner.getEStructuralFeatures().add(a); a.setEContainingClass(owner); } createEReference(owner, id) { const r = new EReferenceImpl(); r.setFeatureID(id); owner.getEStructuralFeatures().add(r); r.setEContainingClass(owner); } getRootPackage() { if (!this.getESuperPackage()) return this; let cursor = this.getESuperPackage(); while (cursor.getESuperPackage() != null) cursor = cursor.getESuperPackage(); return cursor; } } //# sourceMappingURL=epackage.js.map