booleanvariable.java

来自「Java版的SAT求解器」· Java 代码 · 共 192 行

JAVA
192
字号
/* * BooleanVariable.java	1.01 06/01/04 * * Copyright 2004-2006 Positronic Software. * * */ /** * A class which represents a Boolean variable. BooleanVariable is essentially a * named boolean variable. * NOTE: Alternative implementations of BooleanVariable must not only implement * IBooleanVariable, but must also ensure that any two instances of * BooleanVariable are equal if and only if they have the same name. * * @author  Kerry Michael Soileau * <blockquote><pre> * ksoileau@yahoo.com * http://web.wt.net/~ksoileau/index.htm * </pre></blockquote> * @version 1.0, 06/01/04 * @see BooleanVariable * @see IBooleanVariable * @see BooleanLiteral * @see IBooleanLiteral * @see String * @see HashSet * @see List * @see Collections */package positronic.satisfiability.elements;import java.util.Arrays;import java.util.Collections;import java.util.HashSet;import java.util.List;import java.util.Set;import positronic.satisfiability.exceptions.BooleanVariableException;public class BooleanVariable implements IBooleanVariable{  private static int boolCount;  private static HashSet instances=new HashSet();  private static boolean verbose;    public static int getBoolCount()	{		return boolCount;	}	  public static IBooleanVariable getBooleanVariable() throws Exception  {    return new BooleanVariable();  }	  public static IBooleanVariable getBooleanVariable(boolean x) throws Exception  {    IBooleanVariable res=BooleanVariable.getBooleanVariable();    res.setValue(x);    return res;  }    public static IBooleanVariable getBooleanVariable(String n) throws Exception  {  	if(n==null || "".compareTo(n)==0)  		throw new BooleanVariableException("Empty string or null was passed to getBooleanVariable method.");  	  	Object[] inarray=BooleanVariable.getInstances().toArray();  	for(int i=0;i<inarray.length;i++)  	{  		IBooleanVariable nx=(IBooleanVariable)(inarray[i]);  		if(n.compareTo(nx.getName())==0)  			return nx;  	}  	return new BooleanVariable(n);  }  public static IBooleanVariable getBooleanVariable(String n, boolean x) throws Exception  {  	if(n==null || "".compareTo(n)==0)  		throw new BooleanVariableException(  				"Null or empty String passed to getBooleanVariable method.");  	else  	{  		IBooleanVariable res=BooleanVariable.getBooleanVariable(n);  		res.setValue(x);  		return res;  	}  }  public static Set getInstances()   {    return instances;  }  public static void listVariables()  {    BooleanVariable[] ary=(BooleanVariable[])BooleanVariable.getInstances().toArray(new BooleanVariable[0]);    List lis=Arrays.asList(ary);    Collections.sort(lis);    System.out.println(lis);  }  public static void setVerbose(boolean verbose)	{		BooleanVariable.verbose = verbose;	}  private String name;  private boolean value;  private BooleanVariable() throws Exception	{	  this("BooleanVariable-"+ boolCount,false);	  boolCount++;	}	private BooleanVariable(String n) throws Exception	{	  this(n,false);	}  private BooleanVariable(String n, boolean x) throws Exception  {  	if(n==null || "".compareTo(n)==0)  		throw new BooleanVariableException("Null or empty string was passed to a constructor.");  	this.setName(n);    this.setValue(x);    BooleanVariable.getInstances().add(this);    if(this.isVerbose())	  	System.out.println(this.getName());  }  public int compareTo(Object o)  {    String thisName=this.getName();    String oName=((IBooleanVariable)o).getName();    return thisName.compareTo(oName);  }  /**	 * Two IBooleanVariables are considered equal if and only if they have the same	 * name. Their respective values are unimportant.	 */  public boolean equals(Object anObject)  {  	if(anObject==null)  		return false; //this is never equal to null.    if(anObject instanceof IBooleanVariable)    {      BooleanVariable bv=(BooleanVariable)anObject;      if(this.getName().compareTo(bv.getName())==0) return true;      else return false;    }    else return false;  }  public String getName()  {    return this.name;  }  public boolean getValue()  {    return this.value;  }    public boolean isVerbose()	{		return verbose;	}	public void setName(String name) throws BooleanVariableException	{		if(name==null || "".compareTo(name)==0)  		throw new BooleanVariableException(  				"Null or empty String passed to setName method.");		this.name = name;	}	public void setValue(boolean x)  {    this.value=x;  }		public String toString()  {    return "<"+getName()+"="+this.getValue()+">";  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?