Skip to content

Epsilon and EMF Datatypes

This articles provides advice for working with different datatypes supported by EMF.

Decimal Numbers

Arithmetic computations with Ecore double and float attributes can produce floating point precision errors as shown in the example below (also on the playground), as these are underpinned by Java's double and float primitive data types. When precision is required, it is advisable to use EBigDecimal instead, which is underpinned by Java's BigDecimal.

Number.all.dec.sum().println(); // Prints 0.24
Number.all.dbl.sum().println(); // Prints 0.24000000000000002
Number.all.flt.sum().println(); // Prints 0.24000001
<?nsuri numbers?>
<_>
    <number dec="0.1" dbl="0.1" flt="0.1"/>
    <number dec="0.14" dbl="0.14" flt="0.14"/>
</_>
@namespace(uri="numbers", prefix="")
package numbers;

class Number {
    attr EBigDecimal dec;
    attr double dbl;
    attr float flt;
}

To set the value of an EBigDecimal attribute you can use its underpinning Java type as shown below.

Number.all.first().dec = new Native("java.math.BigDecimal")("0.2");

Dates

Ecore supports a built-in EDate type, underpinned by Java's Date type. EMF provides built-in support for the following date formats:

yyyy-MM-dd'T'HH:mm:ss'.'SSSZ
yyyy-MM-dd'T'HH:mm:ss'.'SSS
yyyy-MM-dd'T'HH:mm:ss
yyyy-MM-dd'T'HH:mm
yyyy-MM-dd

The example below (also on the playground) shows how to work with date attributes in Epsilon.

var newton = Person.all.first();

newton.dob.println(); // Prints Sun Jan 04 00:00:00 UTC 1643
newton.dob.class.println(); // Prints class java.util.Date

var einstein = new Person;
einstein.name = "Albert Einstein";
einstein.dob = new Native("java.text.SimpleDateFormat")
    ("yyyy-MM-dd").parse("1879-03-14");
einstein.dob.println(); // Prints Fri Mar 14 00:00:00 UTC 1879
<?nsuri persons?>
<_>
    <person name="Isaac Newton" dob="1643-01-04"/>
    <person name="Charles Darwin" dob="1809-02-12"/>
    <person name="Marie Curie" dob="1867-11-07"/>
</_>
package persons;

class Person {
    attr String name;
    attr EDate dob;
}