VariableTermImpl.java :  » Rule-Engine » Mandarax » org » mandarax » reference » Java Open Source

Java Open Source » Rule Engine » Mandarax 
Mandarax » org » mandarax » reference » VariableTermImpl.java
package org.mandarax.reference;

/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import org.mandarax.kernel.*;
import org.mandarax.util.TermIterator;
import java.util.*;

/**
 * Reference implementation for variable terms.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.1
 * Prova re-integration modifications
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 3.4 <7 March 05>
 */
public class VariableTermImpl extends LObject implements VariableTerm {

  private String name = null;
  private Class  type = null;

  /** Cache the string representation for optimized debugging. */
  private transient String cachedToString = null;

  /**
   * Create a new instance with the name and the type passed as parameter.
   */
  public VariableTermImpl() {
    super ();
  }

  /**
   * Create a new instance with the name and the type passed as parameter.
   * @param n the name of the variable
   * @param t the type of the variable
   */
  VariableTermImpl(String n, Class t) {
    super ();
    setName (n);
    setType (t);
  }
  /**
   * Get a (trivial) term iterator for the subterms.
   * @return an iterator for all sub terms
   */
  public TermIterator allSubterms() {
    Term[] t = { this };

    return new TermIterator (t);
  }
  /**
   * Apply a replacement to a term.
   * @return the term resulting from applying the replacement
   * @param r the replacement
   */
  public Term apply(Replacement r) {
    if(r.original.sameAs (this)) {
      return r.replacement;
    } else {
      return this;
    }
  }

  public void getAllSubtermsA( List result ) {
    result.add( this );
  }

  public void getAllSubtermsA( List result, boolean left ) {
    result.add( this );
  }

  public boolean getAllSubtermsB( List result, Iterator aux ) {
    Object t = ConstantTermImpl.nextNotNull( aux );
    if( t!=null ) {
      result.add(this);
      return (t instanceof VariableTermImpl) && (this==t);
    }
    return false;
  }

  public boolean getAllSubtermsB( List result, Iterator aux, boolean left ) {
    Object t = ConstantTermImpl.nextNotNull( aux );
    if( t!=null ) {
      result.add(this);
      return (t instanceof VariableTermImpl) && (this==t);
    }
    return false;
  }

  /**
   * Indicates whether the term containes variables.
   * @return true
   */
  public boolean containsVariables() {
    return true;
  }

  /**
   * Indicates whether the term contains the provided variable term.
   * @return true if the term contains the variable term provided, false otherwise
   * @param var a variable term
   */
  public boolean containsVariable( VariableTerm var ) {
    return this==var;
  }

  /**
   * Indicates whether the objects are equal.
   * @return true if the objects are equal, false otherwise
   * @param t the object to compare this object with
   */
  public boolean equals(Object t) {
    if( !(t instanceof org.mandarax.kernel.VariableTerm)) {
      return false;
    }

    return type.equals (((VariableTerm) t).getType ())
         && name.equals (((VariableTerm) t).getName ());
  }
  /**
   * Get all subterms as array.
   * @return an array of terms only containing this object
   */
  public Term[] getAllSubterms() {
    Term[] t = { this };

    return t;
  }
  /**
   * Get the name of the variable.
   * @return the name of the variable
   */
  public String getName() {
    return name;
  }
  /**
   * Get the type of the variable.
   * @return the term type
   */
  public Class getType() {
    return type;
  }
  /**
   * Returns the hashcode of the object.
   * @return the hash value
   */
  public int hashCode() {
    return type.hashCode () ^ name.hashCode () * 31;
  }
  /**
   * Indicates whether the term is compound.
   * @return false
   */
  public boolean isCompound() {
    return false;
  }
  /**
   * Indicates whether the term is constant.
   * @return false
   */
  public boolean isConstant() {
    return false;
  }
  /**
   * Indicates whether the object (usually a term or a clause set) can be performed
   * using the java semantics. Variable terms do not support the java semantics!
   * @return false
   */
  public boolean isExecutable() {
    return false;
  }
  /**
   * Indicates whether the term is a variable.
   * @return true
   */
  public boolean isVariable() {
    return true;
  }
  /**
   * Resolve a constant term. This is trivial: just
   * throw an excpetion (variables cannot be resolved).
   * @param session a session object
   * @return the result of resolving the term
   * @throws java.lang.UnsupportedOperationException
   * @throws java.lang.IllegalArgumentException
   */
  public Object resolve(Session session) throws UnsupportedOperationException, IllegalArgumentException {
    throw new UnsupportedOperationException ();
  }

//  /**
//   * Resolve a constant term. This is trivial: just
//   * throw an excpetion (variables cannot be resolved).
//   * @return the result of resolving the term
//   * @throws java.lang.UnsupportedOperationException
//   * @throws java.lang.IllegalArgumentException
//   */
//  public Object resolve()
//      throws UnsupportedOperationException, IllegalArgumentException {
//    throw new UnsupportedOperationException ();
//  }

  /**
   * Indicates whether the object is the same as the parameter.
   * @return true if the both objects are the same
   * @param t another term
   */
  public boolean sameAs(Term t) {
    if( !(t instanceof org.mandarax.kernel.VariableTerm)) {
      return false;
    }

    return type.equals (((VariableTerm) t).getType ())
         && name.equals (((VariableTerm) t).getName ());
  }
  /**
   * Set a name.
   * @param n the new name
   */
  public void setName(String n) {
    name = n;
  }
  /**
   * Set a type.
   * @param t the type
   */
  public void setType(Class t) {
    type = t;
  }
  /**
   * Convert the receiver to a string.
   * @return the string representation of this object
   */
  public String toString() {
    if (cachedToString == null) {
      StringBuffer buf = new StringBuffer ();

      buf.append ("<");
      buf.append (name);
      buf.append (">");

      cachedToString = new String (buf);
    }
    return cachedToString;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.