Skip to content

Templating hints

Jan Dolejší edited this page Oct 23, 2019 · 3 revisions

Tips and tricks for problem generation from templates

Objects declaration pattern

Let's consider a JSON structure defining all drivers.

    "drivers": [
        {
            "name": "driver1",
            "status": "on-duty",
            "location": "office"
        },
        {
            "name": "driver2",
            "status": "off-duty",
            "location": "home"
        }
    ]

This is a simple way how to list the driver names:

(:objects
    {{data.drivers|map("name")|join(" ")}} - driver
)

The template above outputs this PDDL:

(:objects
    driver1 driver2 - driver
)

Initializing predicates

;;( drivers
    {% for driver in data.drivers %}
    ; Driver: {{driver.name}}
        (at {{driver.name}} {{driver.location}})
        {% if driver.status == 'on-duty' %}
            (available {{driver.name}})
        {% endif %}
    {% endfor %}
;;)

... outputs ...

;;( drivers
    ; Driver: driver1
        (at driver1 office)
            (available driver1)
    ; Driver: driver2
        (at driver2 home)
;;)

Jinja2 specific useful links

Variables and loops

https://stackoverflow.com/questions/9486393/jinja2-change-the-value-of-a-variable-inside-a-loop

Working with strings

https://stackoverflow.com/questions/2061439/string-concatenation-in-jinja https://stackoverflow.com/questions/19161093/convert-integer-to-string-jinja