|
| 1 | +/** |
| 2 | + * SysML 2 Pilot Implementation |
| 3 | + * Copyright (C) 2026 tkanov |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the Eclipse Public License, version 2, as published by |
| 7 | + * the Eclipse Foundation. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * Eclipse Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Eclipse Public License |
| 15 | + * along with this program. If not, see <https://www.eclipse.org/legal/epl-2.0/>. |
| 16 | + * |
| 17 | + * @license EPL-2.0 <http://spdx.org/licenses/EPL-2.0> |
| 18 | + */ |
| 19 | +package org.omg.kerml.xtext.resource; |
| 20 | + |
| 21 | +import org.eclipse.emf.ecore.EObject; |
| 22 | +import org.eclipse.xtext.naming.IQualifiedNameConverter; |
| 23 | +import org.eclipse.xtext.naming.QualifiedName; |
| 24 | +import org.eclipse.xtext.resource.EObjectDescription; |
| 25 | +import org.eclipse.xtext.resource.IEObjectDescription; |
| 26 | +import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy; |
| 27 | +import org.eclipse.xtext.util.IAcceptor; |
| 28 | +import org.omg.sysml.lang.sysml.Element; |
| 29 | +import org.omg.sysml.lang.sysml.Namespace; |
| 30 | +import org.omg.sysml.util.ElementUtil; |
| 31 | + |
| 32 | +import com.google.inject.Inject; |
| 33 | + |
| 34 | +/** |
| 35 | + * A resource description strategy that exports root Elements under their |
| 36 | + * <code>declaredShortName</code>, in addition to the <code>declaredName</code>-based |
| 37 | + * qualified name exported by {@link org.omg.kerml.xtext.naming.KerMLQualifiedNameProvider}. |
| 38 | + * |
| 39 | + * <p>Local (same-resource) name resolution matches both the <code>memberName</code> and the |
| 40 | + * <code>memberShortName</code> of a Membership. Without the additional exported names, the |
| 41 | + * first segment of a cross-resource qualified name could only be resolved by |
| 42 | + * <code>declaredName</code>, so a reference such as <code>P::Foo</code>, where <code>P</code> |
| 43 | + * is the short name of a root Package in another resource, would not resolve. |
| 44 | + * |
| 45 | + * <p>Only names of root Elements (that is, names with a single segment) are exported this way, |
| 46 | + * because those are the only ones used to resolve the first segment of a cross-resource |
| 47 | + * qualified name (see |
| 48 | + * {@link org.omg.kerml.xtext.scoping.KerMLGlobalScopeProvider#getScope}). Short names of |
| 49 | + * nested Elements continue to be handled by the local scope provider. |
| 50 | + */ |
| 51 | +public class KerMLResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy { |
| 52 | + |
| 53 | + @Inject |
| 54 | + private IQualifiedNameConverter qualifiedNameConverter; |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) { |
| 58 | + boolean result = super.createEObjectDescriptions(eObject, acceptor); |
| 59 | + QualifiedName shortQualifiedName = getShortQualifiedName(eObject); |
| 60 | + if (shortQualifiedName != null) { |
| 61 | + acceptor.accept(EObjectDescription.create(shortQualifiedName, eObject)); |
| 62 | + result = true; |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Return the qualified name to export for the <code>declaredShortName</code> of the given |
| 69 | + * object, or null if it does not have one, or if it is not a root Element. |
| 70 | + */ |
| 71 | + protected QualifiedName getShortQualifiedName(EObject eObject) { |
| 72 | + if (!(eObject instanceof Element) || getQualifiedNameProvider() == null) { |
| 73 | + return null; |
| 74 | + } |
| 75 | + String shortName = ((Element)eObject).getDeclaredShortName(); |
| 76 | + if (shortName == null || shortName.isEmpty()) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + // Only root Elements are exported under their short name. A root Element is owned by the |
| 80 | + // root Namespace of its resource, which is not, itself, owned by another Namespace. |
| 81 | + Namespace owningNamespace = ((Element)eObject).getOwningNamespace(); |
| 82 | + if (owningNamespace == null || owningNamespace.getOwningNamespace() != null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + QualifiedName qualifiedName = getQualifiedNameProvider().getFullyQualifiedName(eObject); |
| 86 | + QualifiedName shortQualifiedName = |
| 87 | + qualifiedNameConverter.toQualifiedName("'" + ElementUtil.escapeString(shortName) + "'"); |
| 88 | + return shortQualifiedName.equals(qualifiedName)? null: shortQualifiedName; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments