/*
* File name: Conjunction.java (package eas.users.lukas.tnt.propositionalCalculus)
* Author(s): Lukas König
* Java version: 6.0
* Generation date: 08.09.2011 (15:41:11)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.lukas.tnt.propositionalCalculus;
import java.util.LinkedList;
import java.util.List;
import eas.users.lukas.tnt.Statement;
import eas.users.lukas.tnt.TNTMethods;
import eas.users.lukas.tnt.Variable;
/**
* @author Lukas König
*
*/
public class Conjunction extends Statement {
private Statement x;
private Statement y;
public Conjunction(Statement first, Statement second) {
this.x = first;
this.y = second;
}
@Override
public String toString() {
return "<" + x + " & " + y + ">";
}
public Statement getLeftHand() {
return this.x;
}
public Statement getRightHand() {
return this.y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.x == null) ? 0 : this.x.hashCode());
result = prime * result + ((this.y == null) ? 0 : this.y.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Conjunction other = (Conjunction) obj;
if (this.x == null) {
if (other.x != null) {
return false;
}
} else if (!this.x.equals(other.x)) {
return false;
}
if (this.y == null) {
if (other.y != null) {
return false;
}
} else if (!this.y.equals(other.y)) {
return false;
}
return true;
}
@Override
public boolean existsVariable(Variable var) {
return this.x.existsVariable(var) || this.y.existsVariable(var);
}
@Override
public boolean renameVariable(Variable var1, Variable var2) {
boolean isChanged1, isChanged2;
isChanged1 = this.x.renameVariable(var1, var2);
isChanged2 = this.y.renameVariable(var1, var2);
return isChanged1 || isChanged2;
}
@Override
public Conjunction clone() throws CloneNotSupportedException {
Conjunction cloned = new Conjunction(x.clone(), y.clone());
return cloned;
}
@Override
public boolean isStructurallyEqual(Statement other) {
if (other == null || !this.getClass().equals(other.getClass())) {
return false;
}
Conjunction otherConj = (Conjunction) other;
return this.x.isStructurallyEqual(otherConj.x) && this.y.isStructurallyEqual(otherConj.y);
}
@Override
public List<Variable> getAllVariables() {
List<Variable> set = this.x.getAllVariables();
set.addAll(this.y.getAllVariables());
return set;
}
@Override
public void renameDuplicateVariables() {
List<Variable> allUsedVarsX = x.getAllVariables();
List<Variable> allUsedVars = new LinkedList<Variable>(allUsedVarsX);
allUsedVars.addAll(y.getAllVariables());
int firstFreeVarSpace = TNTMethods.getFirstFreeSpaceVar(allUsedVars);
if (firstFreeVarSpace > TNTMethods.FIRST_FREE_VAR_SPACE) {
TNTMethods.FIRST_FREE_VAR_SPACE = firstFreeVarSpace;
}
for (Variable v : allUsedVarsX) {
if (y.renameVariable(new Variable(v.getVarNum()), new Variable(TNTMethods.FIRST_FREE_VAR_SPACE))) {
TNTMethods.FIRST_FREE_VAR_SPACE++;
}
}
}
}