package jcgp.backend.function; import java.util.ArrayList; import java.util.Arrays; import jcgp.backend.exceptions.InvalidArgumentsException; import jcgp.backend.population.Connection; public class BooleanLogic extends FunctionSet { public BooleanLogic() { name = "1-bit Logic"; functionList = new Function[]{ new And(), new Or(), new Not(), new Xor(), new Nand(), new Nor(), new Xnor()}; allowedFunctions = new ArrayList(Arrays.asList(functionList)); } public static class And extends Function { private int arity = 2; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 && arg2; return result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "AND"; } } public static class Or extends Function { private int arity = 2; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 || arg2; return result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "OR"; } } public static class Not extends Function { private int arity = 1; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean result = !arg1; return result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "NOT"; } } public static class Xor extends Function { private int arity = 2; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 ^ arg2; return result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "XOR"; } } public static class Nand extends Function { private int arity = 2; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 && arg2; return !result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "NAND"; } } public static class Nor extends Function { private int arity = 2; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 || arg2; return !result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "NOR"; } } public static class Xnor extends Function { private int arity = 2; @Override public Boolean run(Connection... connections) { if (connections.length < arity) { throw new InvalidArgumentsException("Not enough connections were given."); } else { Boolean arg1 = ((Boolean) connections[0].getValue()); Boolean arg2 = ((Boolean) connections[1].getValue()); Boolean result = arg1 ^ arg2; return !result; } } @Override public int getArity() { return arity; } @Override public String getName() { return "XNOR"; } } }