/**
 * PDDL Graph Planner
 * Zeyn Saigol <zeyn@zeyn.net>
 * School of Computer Science
 * University of Birmingham
 *
 * $Id: UnaryMinusExpr.java 32 2007-02-21 16:29:39Z zas $
 */
package uk.ac.bham.cs.zas.pddl.goalseffects;

import java.util.*;
import uk.ac.bham.cs.zas.pddl.domain.*;

/**
 * Numeric expresssion (fExp in PDDL) for a negated expression.
 */
public class UnaryMinusExpr implements NumericExpr
{
    private final boolean evaluable;
    
    private final NumericExpr operand;
    
    public UnaryMinusExpr(final NumericExpr operand) {
        this.operand = operand;
        
        evaluable = operand.isEvaluable();
    }

    public boolean isEvaluable() {
        return evaluable;
    }
    
    public NumericExpr instantiate(Map<FormalArgument, PDDLObject> parameters, Set<PDDLObject> objects) {
        if (evaluable) {
            return this;
        } else {
            return new UnaryMinusExpr((NumericExpr) operand.instantiate(parameters, objects));
        }
    }
    
    /* (non-Javadoc)
     * @see uk.ac.bham.cs.zas.pddl.domain.NumericExpr#evaluate(uk.ac.bham.cs.zas.pddl.domain.ConsistentLiteralSet)
     */
    public int evaluate(ConsistentLiteralSet literals) {
        return - (operand.evaluate(literals));
    }
    
    public void getLiteralsUsed(Set<LiteralInstance> resultSet)
    {
        if (!evaluable) {
            throw new IllegalStateException("Attempt to get literals used by non-evaluable function expr");
        }
        operand.getLiteralsUsed(resultSet);
    }
    
    public String toString()
    {
        return " -" + operand;
    }
}
