package jcgp.backend.function; 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()}; enableAll(); } public static class And extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 2) { 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 2; } @Override public String getName() { return "AND"; } } public static class Or extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 2) { 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 2; } @Override public String getName() { return "OR"; } } public static class Not extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 1) { 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 1; } @Override public String getName() { return "NOT"; } } public static class Xor extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 2) { 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 2; } @Override public String getName() { return "XOR"; } } public static class Nand extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 2) { 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 2; } @Override public String getName() { return "NAND"; } } public static class Nor extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 2) { 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 2; } @Override public String getName() { return "NOR"; } } public static class Xnor extends Function { @Override public Boolean run(Connection... connections) { if (connections.length < 2) { 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 2; } @Override public String getName() { return "XNOR"; } } }