001package ball.game.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: XDTask.java 6143 2020-06-12 16:21:29Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/ant/taskdefs/XDTask.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.activation.ReaderWriterDataSource;
024import ball.game.crossword.Puzzle;
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 ball.util.ant.taskdefs.NotNull;
030import java.io.BufferedReader;
031import java.io.File;
032import java.util.Iterator;
033import java.util.Set;
034import java.util.stream.Stream;
035import lombok.Getter;
036import lombok.NoArgsConstructor;
037import lombok.Setter;
038import lombok.ToString;
039import lombok.experimental.Accessors;
040import org.apache.tools.ant.BuildException;
041import org.apache.tools.ant.Task;
042import org.apache.tools.ant.util.ClasspathUtils;
043
044import static lombok.AccessLevel.PROTECTED;
045
046/**
047 * Abstract XD {@link.uri http://ant.apache.org/ Ant} {@link Task}
048 * base class.
049 *
050 * {@bean.info}
051 *
052 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
053 * @version $Revision: 6143 $
054 */
055@NoArgsConstructor(access = PROTECTED)
056public abstract class XDTask extends Task implements AnnotatedAntTask,
057                                                     ClasspathDelegateAntTask,
058                                                     ConfigurableAntTask {
059    @Getter @Setter @Accessors(chain = true, fluent = true)
060    private ClasspathUtils.Delegate delegate = null;
061
062    @Override
063    public void init() throws BuildException {
064        super.init();
065        ClasspathDelegateAntTask.super.init();
066        ConfigurableAntTask.super.init();
067    }
068
069    @Override
070    public void execute() throws BuildException {
071        super.execute();
072        AnnotatedAntTask.super.execute();
073    }
074
075    /**
076     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to load a
077     * {@link Puzzle} from an
078     * {@link.uri https://github.com/century-arcade/xd target=newtab XD}
079     * {@link File}.
080     *
081     * {@bean.info}
082     */
083    @AntTask("xd-load")
084    @NoArgsConstructor @ToString
085    public static class Load extends XDTask {
086        @NotNull @Getter @Setter
087        private File file = null;
088        protected Puzzle puzzle = null;
089
090        @Override
091        public void execute() throws BuildException {
092            super.execute();
093
094            try {
095                puzzle = Puzzle.load(getFile().getAbsolutePath());
096
097                ReaderWriterDataSource ds =
098                    new ReaderWriterDataSource(null, null);
099
100                puzzle.writeTo(ds.getPrintWriter());
101
102                try (BufferedReader reader = ds.getBufferedReader()) {
103                    log(reader.lines());
104                }
105            } catch (BuildException exception) {
106                throw exception;
107            } catch (Throwable throwable) {
108                throwable.printStackTrace();
109                throw new BuildException(throwable);
110            }
111        }
112    }
113
114    /**
115     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to load a
116     * {@link Puzzle} from an
117     * {@link.uri https://github.com/century-arcade/xd target=newtab XD}
118     * {@link File} and return a {@link Stream} of possible solutions.
119     *
120     * {@bean.info}
121     */
122    @AntTask("xd-solve")
123    @NoArgsConstructor @ToString
124    public static class Solve extends Load {
125        @Override
126        public void execute() throws BuildException {
127            super.execute();
128
129            try {
130                Stream<Puzzle> stream = puzzle.solve(getWordList());
131                Iterator<Puzzle> iterator = stream.iterator();
132
133                while (iterator.hasNext()) {
134                    log();
135
136                    Puzzle solution = iterator.next();
137
138                    /* log(solution.isSolved() ? "COMPLETE" : "PARTIAL"); */
139                    log(solution);
140                }
141            } catch (BuildException exception) {
142                throw exception;
143            } catch (Throwable throwable) {
144                throwable.printStackTrace();
145                throw new BuildException(throwable);
146            }
147        }
148
149        private Set<CharSequence> getWordList() {
150            return new ball.game.scrabble.wordlist.TWL06().keySet();
151        }
152    }
153}