All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ggp.base.util.gdl.grammar.GdlFunction Maven / Gradle / Ivy

There is a newer version: 0.0.15
Show newest version
package org.ggp.base.util.gdl.grammar;

import java.util.List;

import com.google.common.collect.ImmutableList;

public final class GdlFunction extends GdlTerm
{
    private static final long serialVersionUID = 1L;

    private final ImmutableList body;
    private transient Boolean ground;
    private final GdlConstant name;

    GdlFunction(GdlConstant name, ImmutableList body)
    {
        this.name = name;
        this.body = body;
        ground = null;
    }

    public int arity()
    {
        return body.size();
    }

    private boolean computeGround()
    {
        for (GdlTerm term : body)
        {
            if (!term.isGround())
            {
                return false;
            }
        }

        return true;
    }

    public GdlTerm get(int index)
    {
        return body.get(index);
    }

    public GdlConstant getName()
    {
        return name;
    }

    public List getBody()
    {
        return body;
    }

    @Override
    public boolean isGround()
    {
        if (ground == null)
        {
            ground = computeGround();
        }

        return ground;
    }

    @Override
    public GdlSentence toSentence()
    {
        return GdlPool.getRelation(name, body);
    }

    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();

        sb.append("( " + name + " ");
        for (GdlTerm term : body)
        {
            sb.append(term + " ");
        }
        sb.append(")");

        return sb.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy