blob: 94f1ab0c41a584d7b85eaf6c8d3a4bf19d1d53e5 [file] [log] [blame]
/*****************************************************************************
* Copyright (c) 2018 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* Contributors:
* Ansgar Radermacher ansgar.radermacher@cea.fr
*
*****************************************************************************/
package org.eclipse.papyrus.robotics.ros2.codegen.utils;
import org.eclipse.emf.common.util.URI;
import org.eclipse.papyrus.designer.languages.common.base.ElementUtils;
import org.eclipse.papyrus.designer.languages.common.base.UriConstants;
import org.eclipse.papyrus.designer.languages.cpp.profile.C_Cpp.External;
import org.eclipse.papyrus.designer.languages.cpp.profile.C_Cpp.Ptr;
import org.eclipse.papyrus.robotics.ros2.codegen.Activator;
import org.eclipse.papyrus.uml.tools.utils.PackageUtil;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.TypedElement;
import org.eclipse.uml2.uml.util.UMLUtil;
public class Helpers {
public static final URI ROS_LIBRARY_URI = URI.createURI("pathmap://ROS2_LIBRARY/ros2Library.uml"); //$NON-NLS-1$
/**
* Get a type from the ROS library
*
* @param element
* an element of the source model (used to identify resource set)
* @param qualifiedName
* the qualified name of the element to load
* @return the element, if found, null otherwise
*/
public static Type getRosType(Element element, String qualifiedName) {
NamedElement ne = ElementUtils.getQualifiedElementFromRS(element, qualifiedName);
if (ne == null) {
PackageUtil.loadPackage(ROS_LIBRARY_URI, element.eResource().getResourceSet());
ne = ElementUtils.getQualifiedElementFromRS(element, qualifiedName);
}
if (ne instanceof Type) {
return (Type) ne;
}
Activator.log.debug(String.format("Can't find type with name %s", qualifiedName)); //$NON-NLS-1$
return null;
}
/**
* Get a type from the UML primitive types library
*
* @param element
* an element of the source model (used to identify resource set)
* @param qualifiedName
* the qualified name of the element to load
* @return the element, if found, null otherwise
*/
public static Type getPrimitiveType(Element element, String qualifiedName) {
NamedElement ne = ElementUtils.getQualifiedElementFromRS(element, qualifiedName);
if (ne == null) {
PackageUtil.loadPackage(UriConstants.UML_PRIMITIVE_TYPES, element.eResource().getResourceSet());
ne = ElementUtils.getQualifiedElementFromRS(element, qualifiedName);
}
if (ne instanceof Type) {
return (Type) ne;
}
return null;
}
/**
* Return the scoped name of the communication object, based on its external stereotype.
* @param type a type with the "External" stereotype
* @return the external name used for that type
*/
public static String externalName(Type type) {
if (type != null) {
External external = UMLUtil.getStereotypeApplication(type, External.class);
if (external != null) {
return external.getName();
}
return type.getQualifiedName();
}
return null;
}
/**
* use Ptr declaration to add ::SharedPtr postfix
* @param typedElement
*/
public static void useSharedPtr(TypedElement typedElement) {
Ptr ptr = StereotypeUtil.applyApp(typedElement, Ptr.class);
ptr.setDeclaration("::SharedPtr"); //$NON-NLS-1$
}
/**
* Return a small_caps string (for file names) in which CamlCase is replaced by "_"
* e.g. ServiceType is transformed into service_type. The _ is only added, if the previous
* character was a lower-case character
* @param str a string
* @return the transformed string
*/
public static String escapeCamlCase(String str) {
String out = ""; //$NON-NLS-1$
boolean previousLC = false;
for (char c : str.toCharArray()) {
if (Character.isUpperCase(c)) {
if (previousLC) {
out += "_"; //$NON-NLS-1$
}
}
previousLC = Character.isLowerCase(c) || Character.isDigit(c);
out += Character.toLowerCase(c);
}
return out;
}
}