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

toxgene.core.genes.trees.ToxIf Maven / Gradle / Ivy

/**
 * Implements an IF-THEN-ELSE construct for querying tox-lists.
 *
 * @author Denilson Barbosa
 * @version 0.1
 */

package toxgene.core.genes.trees;


import java.io.PrintStream;
import java.util.Vector;

import toxgene.core.genes.ContainerGene;
import toxgene.core.genes.lists.ListGene;
import toxgene.core.genes.lists.ToxListElement;
import toxgene.core.genes.lists.ToxListElementException;
import toxgene.core.genes.lists.WhereClause;

public class ToxIf implements TreeGene, ListGene, ContainerGene{
  private WhereClause clause;
  private ToxOption thenBlock;
  private ToxOption elseBlock;
  private boolean hasElse;
  private boolean clauseResult;
  private boolean evaluated;

  public ToxIf(WhereClause where, ToxOption thenBlock){
		this.clause = where;
		this.thenBlock = thenBlock;
		hasElse = false;
		evaluated = false;
  }

  public void addElseBlock(ToxOption elseBlock){
		this.elseBlock = elseBlock;
		hasElse = true;
  }

  /**
   * Resets the gene.
   */
  public void reset(){
		thenBlock.reset();
		if (hasElse){
			elseBlock.reset();
		}
  }

  public void generate(PrintStream outputStream){ 
		if (!evaluated){
			evaluate();
		}
		if (clauseResult){
			thenBlock.generate(outputStream);
		}
		else{
			if (hasElse){
				elseBlock.generate(outputStream);
			}
		}
		evaluated = false;
  }

  public void generate(ToxListElement parent){
		if (!evaluated){
			evaluate();
		}
		if (clauseResult){
			thenBlock.generate(parent);
		}
		else{
			if (hasElse){
				elseBlock.generate(parent);
			}
		}
		evaluated = false;
  }

  public void generateAttributes(PrintStream outputStream){
		if (!evaluated){
			evaluate();
		}
		if (clauseResult){
			if(thenBlock.hasAttributes()){
				thenBlock.generateAttributes(outputStream);
			}
		}
		else{
			if (hasElse && elseBlock.hasAttributes()){
				elseBlock.generateAttributes(outputStream);
			}
		}
  }

  private void evaluate(){
		clauseResult = clause.evaluate();
		evaluated = true;
  }

  public String getType(){
		return "complex";
  }

  public String name(){
		return "tox-if";
  }

  public Vector children(){
		Vector result = new Vector();
		Vector temp = thenBlock.children();
		result.addAll(temp);

		if (hasElse){
			temp = elseBlock.children();
			result.addAll(temp);
		}
		return result;
  }
  
  public Vector getChildren(){
		Vector result = new Vector();
		Vector temp = thenBlock.getChildren();
		result.addAll(temp);

		if (hasElse){
			temp = elseBlock.getChildren();
			result.addAll(temp);
		}

		return result;
  }

  public Vector getChildrenByName(String name) throws ToxListElementException{
		Vector result = null;
		Vector temp = thenBlock.getChildrenByName(name);

		if (temp != null){
			result = new Vector();
			result.addAll(temp);
		}
		if (hasElse){
			temp = elseBlock.getChildrenByName(name);
			if (temp != null){
				if (result == null){
					result = new Vector();
				}
				result.addAll(temp);
			}
		}
		return result;
  }

  public boolean hasAttributes(){
		if (thenBlock.hasAttributes()){
			return true;
		}
		if ((hasElse) && (elseBlock.hasAttributes())){
			return true;
		}
		return false;
  }
	
	public int numIterators(){
		if (elseBlock != null)
			return (thenBlock.numIterators() + elseBlock.numIterators());
		else
			return (thenBlock.numIterators());
	}

	public void destroy(){
		thenBlock.destroy();
		if (elseBlock != null){
			elseBlock.destroy();
		}		
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy