Skip to content

Running Epsilon Programs on POJOs

Epsilon's languages can be used to query and modify plain-old Java objects (POJOs). The following Maven-based example demonstrates setting up a Project object with two Tasks, and passing it to an EOL program (return project.tasks.size();) to query.

package org.eclipse.epsilon.examples.pojos;

import org.eclipse.epsilon.eol.EolModule;
import org.eclipse.epsilon.eol.execute.context.Variable;

public class EOLExample {

    public static void main(String[] args) throws Exception {

        // Set up the project POJO (plain-old Java object)
        Project project = new Project();

        Task analysis = new Task();
        analysis.setName("Analysis");
        analysis.setDuration(3);
        project.getTasks().add(analysis);

        Task design = new Task();
        design.setName("Design");
        design.setDuration(6);
        project.getTasks().add(design);

        // Parse the EOL program to run on the POJO
        EolModule module = new EolModule();
        module.parse("return project.tasks.size();");

        // Make the POJO available to the EOL program
        module.getContext().getFrameStack().put(Variable.createReadOnlyVariable("project", project));

        // Execute the EOL program and print its output
        Object result = module.execute();

        System.out.println("Result: " + result);
    }
}
package org.eclipse.epsilon.examples.pojos;

import java.util.ArrayList;
import java.util.List;

public class Project {

    protected String name;
    protected List<Task> tasks = new ArrayList<>();

    public List<Task> getTasks() {
        return tasks;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package org.eclipse.epsilon.examples.pojos;

public class Task {

    protected String name;
    protected int duration;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.eclipse.epsilon</groupId>
    <artifactId>examples.pojos</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <epsilon.version>2.5.0</epsilon.version>
        <epsilon.scope>compile</epsilon.scope>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.epsilon</groupId>
            <artifactId>org.eclipse.epsilon.eol.engine</artifactId>
            <version>${epsilon.version}</version>
            <scope>${epsilon.scope}</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.epsilon</groupId>
            <artifactId>org.eclipse.epsilon.egl.engine</artifactId>
            <version>${epsilon.version}</version>
            <scope>${epsilon.scope}</scope>
        </dependency>       
    </dependencies>

</project>

Running an EGL Template against the POJO

The example below demonstrates processing the same POJO using Epsilon's template language (EGL), to generate text from it.

package org.eclipse.epsilon.examples.pojos;

import org.eclipse.epsilon.egl.EglModule;
import org.eclipse.epsilon.eol.execute.context.Variable;

public class EGLExample {
    public static void main(String[] args) throws Exception {

        // Set up the project POJO (plain-old Java object)
        Project project = new Project();

        Task analysis = new Task();
        analysis.setName("Analysis");
        analysis.setDuration(3);
        project.getTasks().add(analysis);

        Task design = new Task();
        design.setName("Design");
        design.setDuration(6);
        project.getTasks().add(design);

        // Parse the EGL template to run on the POJO
        EglModule module = new EglModule();
        module.parse(
            "[%for (task in project.tasks){%]\n" +
            "- Task: [%=task.name%] ([%=task.duration%] months)\n" +
            "[%}%]"
        );

        // Make the project POJO available to the EGL template
        module.getContext().getFrameStack().put(Variable.createReadOnlyVariable("project", project));

        // Execute the EGL template and print its output
        Object result = module.execute();

        System.out.println("Result");
        System.out.println(result);
    }
}
package org.eclipse.epsilon.examples.pojos;

import java.util.ArrayList;
import java.util.List;

public class Project {

    protected String name;
    protected List<Task> tasks = new ArrayList<>();

    public List<Task> getTasks() {
        return tasks;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package org.eclipse.epsilon.examples.pojos;

public class Task {

    protected String name;
    protected int duration;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.eclipse.epsilon</groupId>
    <artifactId>examples.pojos</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <epsilon.version>2.5.0</epsilon.version>
        <epsilon.scope>compile</epsilon.scope>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.epsilon</groupId>
            <artifactId>org.eclipse.epsilon.eol.engine</artifactId>
            <version>${epsilon.version}</version>
            <scope>${epsilon.scope}</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.epsilon</groupId>
            <artifactId>org.eclipse.epsilon.egl.engine</artifactId>
            <version>${epsilon.version}</version>
            <scope>${epsilon.scope}</scope>
        </dependency>       
    </dependencies>

</project>

The complete source of the example is on GitHub.