package jcgp.modules.function; import jcgp.exceptions.InvalidArgumentsException; import jcgp.population.Connection; public class BooleanLogic { 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } }