001package ball.game.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * %%
006 * Copyright (C) 2010 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.activation.ReaderWriterDataSource;
022import ball.game.crossword.Puzzle;
023import ball.util.ant.taskdefs.AnnotatedAntTask;
024import ball.util.ant.taskdefs.AntTask;
025import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
026import ball.util.ant.taskdefs.ConfigurableAntTask;
027import ball.util.ant.taskdefs.NotNull;
028import java.io.BufferedReader;
029import java.io.File;
030import java.util.Iterator;
031import java.util.Set;
032import java.util.stream.Stream;
033import lombok.Getter;
034import lombok.NoArgsConstructor;
035import lombok.Setter;
036import lombok.ToString;
037import lombok.experimental.Accessors;
038import org.apache.tools.ant.BuildException;
039import org.apache.tools.ant.Task;
040import org.apache.tools.ant.util.ClasspathUtils;
041
042import static lombok.AccessLevel.PROTECTED;
043
044/**
045 * Abstract XD {@link.uri http://ant.apache.org/ Ant} {@link Task}
046 * base class.
047 *
048 * {@bean.info}
049 *
050 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
051 */
052@NoArgsConstructor(access = PROTECTED)
053public abstract class XDTask extends Task implements AnnotatedAntTask, ClasspathDelegateAntTask, ConfigurableAntTask {
054    @Getter @Setter @Accessors(chain = true, fluent = true)
055    private ClasspathUtils.Delegate delegate = null;
056
057    @Override
058    public void init() throws BuildException {
059        super.init();
060        ClasspathDelegateAntTask.super.init();
061        ConfigurableAntTask.super.init();
062    }
063
064    @Override
065    public void execute() throws BuildException {
066        super.execute();
067        AnnotatedAntTask.super.execute();
068    }
069
070    /**
071     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to load a
072     * {@link Puzzle} from an
073     * {@link.uri https://github.com/century-arcade/xd target=newtab XD}
074     * {@link File}.
075     *
076     * {@bean.info}
077     */
078    @AntTask("xd-load")
079    @NoArgsConstructor @ToString
080    public static class Load extends XDTask {
081        @NotNull @Getter @Setter
082        private File file = null;
083        protected Puzzle puzzle = null;
084
085        @Override
086        public void execute() throws BuildException {
087            super.execute();
088
089            try {
090                puzzle = Puzzle.load(getFile().getAbsolutePath());
091
092                var ds = new ReaderWriterDataSource(null, null);
093
094                puzzle.writeTo(ds.getPrintWriter());
095
096                try (var reader = ds.getBufferedReader()) {
097                    log(reader.lines());
098                }
099            } catch (BuildException exception) {
100                throw exception;
101            } catch (Throwable throwable) {
102                throwable.printStackTrace();
103                throw new BuildException(throwable);
104            }
105        }
106    }
107
108    /**
109     * {@link.uri http://ant.apache.org/ Ant} {@link Task} to load a
110     * {@link Puzzle} from an
111     * {@link.uri https://github.com/century-arcade/xd target=newtab XD}
112     * {@link File} and return a {@link Stream} of possible solutions.
113     *
114     * {@bean.info}
115     */
116    @AntTask("xd-solve")
117    @NoArgsConstructor @ToString
118    public static class Solve extends Load {
119        @Override
120        public void execute() throws BuildException {
121            super.execute();
122
123            try {
124                Stream<Puzzle> stream = puzzle.solve(getWordList());
125                Iterator<Puzzle> iterator = stream.iterator();
126
127                while (iterator.hasNext()) {
128                    log();
129
130                    Puzzle solution = iterator.next();
131
132                    /* log(solution.isSolved() ? "COMPLETE" : "PARTIAL"); */
133                    log(solution);
134                }
135            } catch (BuildException exception) {
136                throw exception;
137            } catch (Throwable throwable) {
138                throwable.printStackTrace();
139                throw new BuildException(throwable);
140            }
141        }
142
143        private Set<CharSequence> getWordList() {
144            return new ball.game.scrabble.wordlist.TWL06().keySet();
145        }
146    }
147}