001package ball.game.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: LifeTask.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/ant/taskdefs/LifeTask.java $
007 * %%
008 * Copyright (C) 2010 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.game.life.Board;
024import ball.game.life.Game;
025import ball.util.ant.taskdefs.AnnotatedAntTask;
026import ball.util.ant.taskdefs.AntTask;
027import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
028import ball.util.ant.taskdefs.ConfigurableAntTask;
029import java.math.BigInteger;
030import lombok.Getter;
031import lombok.NoArgsConstructor;
032import lombok.Setter;
033import lombok.ToString;
034import lombok.experimental.Accessors;
035import org.apache.tools.ant.BuildException;
036import org.apache.tools.ant.Task;
037import org.apache.tools.ant.util.ClasspathUtils;
038
039import static org.apache.commons.lang3.StringUtils.EMPTY;
040
041/**
042 * {@link.uri http://ant.apache.org/ Ant} {@link Task} to start {@link Game}
043 * of Life simulation.
044 *
045 * {@ant.task}
046 *
047 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
048 * @version $Revision: 5285 $
049 */
050@AntTask("life")
051@NoArgsConstructor @ToString
052public class LifeTask extends Task implements AnnotatedAntTask,
053                                              ClasspathDelegateAntTask,
054                                              ConfigurableAntTask {
055    @Getter @Setter @Accessors(chain = true, fluent = true)
056    private ClasspathUtils.Delegate delegate = null;
057    @Getter @Setter
058    private int height = 0;
059    @Getter @Setter
060    private int width = 0;
061    @Getter
062    private BigInteger state0 = BigInteger.ZERO;
063
064    public void setState0(String state0) { this.state0 = parse(state0); }
065
066    public void addText(String text) { setState0(text); }
067
068    private BigInteger parse(String string) {
069        BigInteger state = null;
070
071        try {
072            state = new BigInteger(string);
073        } catch (NumberFormatException exception) {
074            state = BigInteger.ZERO;
075            string = string.replaceAll("[\\p{Space}]+", EMPTY);
076
077            for (int i = 0, n = string.length(); i < n; i += 1) {
078                switch (string.charAt(i)) {
079                case '+':
080                    state = state.setBit(i);
081                    break;
082
083                case '-':
084                default:
085                    state = state.clearBit(i);
086                    break;
087                }
088            }
089        }
090
091        return state;
092    }
093
094    @Override
095    public void init() throws BuildException {
096        super.init();
097        ClasspathDelegateAntTask.super.init();
098        ConfigurableAntTask.super.init();
099    }
100
101    @Override
102    public void execute() throws BuildException {
103        super.execute();
104        AnnotatedAntTask.super.execute();
105
106        try {
107            Game game = new Game(getHeight(), getWidth(), getState0());
108            Board board = new Board(game);
109
110            for (;;) {
111                log();
112                log("Generation #" + String.valueOf(game.size() - 1));
113                log(board);
114
115                BigInteger state = game.automata().next(game.getLast());
116
117                if (! game.contains(state)) {
118                    game.addLast(state);
119                    continue;
120                } else {
121                    log("Steady state: Returned to Generation #"
122                        + String.valueOf(game.indexOf(state)));
123                    break;
124                }
125            }
126        } catch (BuildException exception) {
127            throw exception;
128        } catch (Throwable throwable) {
129            throwable.printStackTrace();
130            throw new BuildException(throwable);
131        }
132    }
133}