Commit 4395fa57 authored by Kai-Holger Brassel's avatar Kai-Holger Brassel
Browse files

Added services methods usable in Sirius scripts.

parent feb01c02
Pipeline #4804 passed with stage
in 2 minutes and 43 seconds
package de.hftstuttgart.cityunits.model;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EAttribute;
/**
* The services class used by VSM (Sirius).
*/
public class Services {
/**
* Return unit string from given string representation of a Quantity. The
* substring before the first blank in the argument is regarded as the value of
* the quantity and the substring after that blank as its unit. Note, that unit
* part or value part or both can be missing, implying that no blank is present.
*
* @param someEObject (required to make this method available for Sirius scripts)
* @param quantity String representing a quantity
* @return unit substring of given string if present, otherwise the empty string
*/
public String extractUnit(EObject someEObject, String quantity) {
String quant = quantity.trim();
int i = quant.indexOf(' ');
if (i < 0) {
try {
Double.parseDouble(quant);
return "";
} catch (Exception ex) {
// quant does not contain a number: should be unit
return quant;
}
} else {
return quant.substring(i + 1);
}
}
/**
* Return value from given string representation of a Quantity as string. The
* substring before the first blank in the argument is regarded as the value of
* the quantity and the substring after that blank as its unit. Note, that value
* part or unit part or both can be missing, implying that no blank is present.
*
* @param someEObject (required to make this method available for Sirius scripts)
* @param quantity String representing a quantity
* @return numerical value of given quantity as string if present, otherwise the empty string
*/
public String extractValue(EObject someEObject, String quantity) {
String quant = quantity.trim();
int i = quant.indexOf(' ');
if (i < 0) {
try {
Double.parseDouble(quant);
return quant;
} catch (Exception ex) {
// quant does not contain a number: should be unit
return "";
}
} else {
return quant.substring(0, i);
}
}
/**
* If <code>newValue</code> is a valid number not within the range defined by annotation
* http://www.hft-stuttgart.de/UomQuantities, adapt it to the minimum or maximum value of the range
* and return its string. Otherwise (no valid number, no annotation or missing (valid) min/max numbers
* in the annotation), rust return the given <code>newValue</code> unchanged.
*
* @param eAttribute a numerical Ecore attribute
* @param newValue String with a new value for the given attribute
* @return String with the new value for the given attribute, possibly adapted to the declared range.
*/
public String minMaxAnnotation(EObject eAttribute, String newValue) {
double value;
try {
value = Double.parseDouble(newValue);
} catch (Exception ex) {
return newValue;
}
EAttribute attr = (EAttribute) eAttribute;
EAnnotation annot = attr.getEAnnotation("http://www.hft-stuttgart.de/UomQuantities");
if (annot == null) {
return newValue;
}
EMap<String, String> details = annot.getDetails();
try {
value = Math.max(Double.parseDouble(details.get("min")), value);
} catch (Exception ex) {
; // do not change value
}
try {
value = Math.min(Double.parseDouble(details.get("max")), value);
} catch (Exception ex) {
; // do not change value
}
return Double.toString(value);
}
}
......@@ -9,4 +9,5 @@ Import-Package: de.hftstuttgart.cityunits.model,
org.junit.jupiter.api;version="5.6.0"
Require-Bundle: javax.measure.unit-api,
tech.units.indriya,
uom-lib-common;bundle-version="[2.1.0,3.0.0)"
uom-lib-common;bundle-version="[2.1.0,3.0.0)",
org.eclipse.emf.ecore
package de.hftstuttgart.cityunits.tests;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.eclipse.emf.ecore.EObject;
import org.junit.jupiter.api.Test;
import de.hftstuttgart.cityunits.model.Services;
class ServicesTest {
@Test
void testExtractValue() {
var services = new Services();
assertEquals("1.23", services.extractValue((EObject)null, "1.23"), "Wrong value extraction from quantity!");
assertEquals("1", services.extractValue((EObject)null, "1"), "Wrong value extraction from quantity!");
assertEquals("2.34", services.extractValue((EObject)null, "2.34 km"), "Wrong value extraction from quantity!");
assertEquals("wrong", services.extractValue((EObject)null, "wrong number km"), "Wrong value extraction from quantity!");
assertEquals("", services.extractValue((EObject)null, ""), "Empty quantity must return empty value!");
}
@Test
void testExtractUnit() {
var services = new Services();
assertEquals("kg", services.extractUnit((EObject)null, "kg"), "Wrong value extraction from quantity!");
assertEquals("km", services.extractUnit((EObject)null, "2.34 km"), "Wrong value extraction from quantity!");
assertEquals("wrong unit", services.extractUnit((EObject)null, "12 wrong unit"), "Wrong unit extraction from quantity!");
assertEquals("", services.extractUnit((EObject)null, ""), "Empty quantity must return empty unit!");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment