UNPKG

@yellicode/elements

Version:

The meta model API for Yellicode - an extensible code generator.

82 lines (81 loc) 3.74 kB
/* * Copyright (c) 2020 Yellicode * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import * as Interfaces from "./interfaces"; var ProfileUtility = /** @class */ (function () { function ProfileUtility() { } ProfileUtility.getStereotypes = function (profile) { if (!profile.packagedElements) return []; return profile.packagedElements.filter(function (e) { return e.elementType === Interfaces.ElementType.stereotype; }); }; ProfileUtility.hasStereotypeId = function (element, stereotypeId) { if (!element || !element.appliedStereotypes) return false; return ProfileUtility.hasStereotypeIdRecursive(element.appliedStereotypes, stereotypeId); }; ProfileUtility.hasStereotypeIdRecursive = function (stereotypes, stereotypeId) { for (var index = 0; index < stereotypes.length; index++) { var stereotype = stereotypes[index]; if (stereotype.id === stereotypeId) return true; // If the stereotype has parents, check these too var stereotypeGenerals = stereotype.getParents(); if (stereotypeGenerals.length > 0 && ProfileUtility.hasStereotypeIdRecursive(stereotypeGenerals, stereotypeId)) { return true; } } return false; }; ProfileUtility.getMetaClassesExtendedBy = function (stereotype) { // First add the stereotype's own meta classes return stereotype.extends.map(function (ext) { return ext.metaClass; }); }; ProfileUtility.getAllMetaClassesExtendedBy = function (stereotype) { // First add the stereotype's own meta classes var metaClasses = ProfileUtility.getMetaClassesExtendedBy(stereotype); // Then add meta classes of the specializing stereotypes stereotype.getSpecializations().forEach(function (derivedStereotype) { derivedStereotype.extends.forEach(function (extension) { if (metaClasses.indexOf(extension.metaClass) === -1) { metaClasses.push(extension.metaClass); } }); }); return metaClasses; }; ProfileUtility.hasProfileId = function (pack, profileId) { if (!pack || !pack.appliedProfiles) return false; return pack.appliedProfiles.some(function (s) { return s.id === profileId; }); }; /** * Filters the array of elements by only including the elements that have a particular stereotype applied. */ ProfileUtility.filterByStereotypeId = function (elements, stereotypeId, elementType) { if (!elements) return []; if (elementType) { return elements.filter(function (e) { return e.elementType === elementType && ProfileUtility.hasStereotypeId(e, stereotypeId); }); } return elements.filter(function (e) { return ProfileUtility.hasStereotypeId(e, stereotypeId); }); }; /** * Filters the array of packageable element by only including the packages that have a particular profile applied. */ ProfileUtility.filterByProfileId = function (elements, profileId) { if (!elements) return []; return elements.filter(function (e) { return e.elementType === Interfaces.ElementType.package && ProfileUtility.hasProfileId(e, profileId); }); }; return ProfileUtility; }()); export { ProfileUtility };