Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package jason.asSyntax;
  
  
  import java.util.Iterator;
 
Represents an arithmetic function, like math.max(arg1,arg2) -- a functor (math.max) and two arguments. A Structure is thus used to store the data.

Author(s):
Jomi
Composed:
- "arguments (from Structure.terms)" 0..* Term
 
 public class ArithFunctionTerm extends Structure implements NumberTerm {
 
     private static final long serialVersionUID = 1L;
 
     private static Logger logger = Logger.getLogger(ArithFunctionTerm.class.getName());
     
     private NumberTerm value = null// value, when evaluated
 
     private ArithFunction function = null;
     
     private Agent agent = null// the agent where this function was used
     
     public ArithFunctionTerm(String functorint termsSize) {
         super(functortermsSize);        
     }
     
     public ArithFunctionTerm(ArithFunction function) {
         super(function.getName(), 2);
         this. = function;   
     }
 
     public ArithFunctionTerm(ArithFunctionTerm af) {
         super(af); // clone args from af
             = af.value;
          = af.function;
             = af.agent;
     }
     
     public NumberTerm getValue() {
         return ;
     }
     
     @Override
     public boolean isNumeric() {
         return true;
     }
     
     @Override
     public boolean isAtom() {
         return false;
     }
     
     @Override
     public boolean isStructure() {
         return false;
     }
     
     @Override
     public Literal makeVarsAnnon(Unifier un) {
         if (isEvaluated()) {
             return null;
         } else {
             return super.makeVarsAnnon(un);            
         }
     }
     
     @Override
     public boolean isLiteral() {
         return false;
     }
 
     @Override
     public boolean isArithExpr() {
         return !isEvaluated();
     }

    
returns true if the function/expression was already evaluated
 
     public boolean isEvaluated() {
         return  != null;
     }
     
     @Override
     public boolean isGround() {
         return isEvaluated() || super.isGround();
     }
 
     public boolean isUnary() {
        return getArity() == 1;
    }
    
    
Does a "normal" apply and then solve the expression and store the result, so future calls of solve do not need to compute the value again
    @Override
    public boolean apply(Unifier u) {
        if (isEvaluated()) 
            return false;
        
        super.apply(u);
        if (( != null && .allowUngroundTerms()) || isGround()) {
            try {
                 = new NumberTermImpl(solve());
                return true;
            } catch (Exception e) {
                .log(.getErrorMsg()+ " -- "e);
            }
        //} else {
        //  logger.warning(getErrorMsg()+ " -- this function has unground arguments and can not be evaluated! Unifier is "+u);
        }
        
        return false;
    }
    public void setAgent(Agent ag) {
         = ag;
    }
    public Agent getAgent() {
        return ;
    }
    
    
computes the value for this arithmetic function (as defined in the NumberTerm interface)
    public double solve() {
        if (isEvaluated())
            return .solve();
        else if ( != null)
            try {
                return .evaluate(( == null ? null : .getTS()),getTermsArray());
            } catch (Exception e) {
                .log(.getErrorMsg()+ " -- error in evaluate!"e);
            }
        else 
            .log(.getErrorMsg()+ " -- the function can not be evalutated, it has no function assigned to it!"new Exception());
        return 0;
    }
    public boolean checkArity(int a) {
        return  != null && .checkArity(a);
    }
    
    @Override
    public Iterator<UnifierlogicalConsequence(Agent agUnifier un)  {
        .log(."Arithmetic term cannot be used for logical consequence!"new Exception());
        return ..iterator();
    }
    
    @Override
    public boolean equals(Object t) {
        if (t == nullreturn false;
        if (isEvaluated()) return .equals(t);
        return super.equals(t);
    }
    @Override
    public int compareTo(Term o) {
        /*if (o instanceof NumberTerm) {
            NumberTerm st = (NumberTerm)o;
            if (solve() > st.solve()) return 1;
            if (solve() < st.solve()) return -1;
        } 
        return 0; */    
        if (o instanceof VarTerm) {
            return o.compareTo(this) * -1;
        }
        if (o instanceof NumberTerm) {
            NumberTerm st = (NumberTerm)o;
            if (solve() > st.solve()) return 1;
            if (solve() < st.solve()) return -1;
            return 0;
        }
        return -1;
    }
    @Override
    protected int calcHashCode() {
        if (isEvaluated())
            return .hashCode();
        else
            return super.calcHashCode();
    }
    @Override
    public String toString() {
        if (isEvaluated())
            return .toString();
        else
            return super.toString();
    }
    @Override    
    public String getErrorMsg() {
        return "Error in '"+this+"' ("super.getErrorMsg() + ")";       
    }
    @Override
    public NumberTerm clone() {
        if (isEvaluated()) 
            return ;
        else 
            return new ArithFunctionTerm(this);
    } 
    
    public Element getAsDOM(Document document) {
        if (isEvaluated()) {
            return .getAsDOM(document);
        } else {
            Element u = (Elementdocument.createElement("expression");
            u.setAttribute("type""arithmetic");
            Element r = (Elementdocument.createElement("right");
            r.appendChild(super.getAsDOM(document)); // put the left argument indeed!
            u.appendChild(r);
            return u;
        }
    }
New to GrepCode? Check out our FAQ X