View Javadoc

1   /*
2    * Copyright 2006-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.test.benchmark;
17  
18  
19  import org.apache.commons.lang.NotImplementedException;
20  
21  import net.sourceforge.domian.specification.CompositeSpecification;
22  import net.sourceforge.domian.specification.LeafSpecification;
23  import net.sourceforge.domian.specification.Specification;
24  
25  
26  /** Specification of a {@link QueenPuzzleConstellation} solving the queen puzzle. */
27  class QueenPuzzleSpecification implements LeafSpecification<QueenPuzzleConstellation> {
28  
29      final static int RIGHT = 0;
30      final static int LEFT = 1;
31  
32      @Override
33      public Class<QueenPuzzleConstellation> getType() {
34          return QueenPuzzleConstellation.class;
35      }
36  
37      @Override
38      public Boolean isSatisfiedBy(final QueenPuzzleConstellation candidate) {
39          // Check all the queen piece placings against one another
40          return !(
41                  // Horizontally - Common y-value
42                  straightXLineConnections(candidate.getQueen1Placing(), candidate.getQueen2Placing(), candidate.getQueen3Placing(), candidate.getQueen4Placing(), candidate.getQueen5Placing(), candidate.getQueen6Placing(), candidate.getQueen7Placing(), candidate.getQueen8Placing())
43                  ||
44                  // Vertically - Common x-value
45                  straightYLineConnections(candidate.getQueen1Placing(), candidate.getQueen2Placing(), candidate.getQueen3Placing(), candidate.getQueen4Placing(), candidate.getQueen5Placing(), candidate.getQueen6Placing(), candidate.getQueen7Placing(), candidate.getQueen8Placing())
46                  ||
47                  // Diagonally to the right - Common x - y
48                  diagonalLineConnections(RIGHT, candidate.getQueen1Placing(), candidate.getQueen2Placing(), candidate.getQueen3Placing(), candidate.getQueen4Placing(), candidate.getQueen5Placing(), candidate.getQueen6Placing(), candidate.getQueen7Placing(), candidate.getQueen8Placing())
49                  ||
50                  // Diagonally to the left - Common x - y
51                  diagonalLineConnections(LEFT, candidate.getQueen1Placing(), candidate.getQueen2Placing(), candidate.getQueen3Placing(), candidate.getQueen4Placing(), candidate.getQueen5Placing(), candidate.getQueen6Placing(), candidate.getQueen7Placing(), candidate.getQueen8Placing()));
52      }
53  
54      @Override
55      public Boolean isGeneralizationOf(final Specification<? extends QueenPuzzleConstellation> specification) {
56          throw new NotImplementedException();
57      }
58  
59      @Override
60      public Boolean isSpecialCaseOf(final Specification<? super QueenPuzzleConstellation> specification) {
61          throw new NotImplementedException();
62      }
63  
64      @Override
65      public <F> CompositeSpecification<QueenPuzzleConstellation> where(final String accessibleObjectName, final Specification<F> accessibleObjectSpecification) {
66          throw new NotImplementedException();
67      }
68  
69      @Override
70      public CompositeSpecification<QueenPuzzleConstellation> and(final Specification<? super QueenPuzzleConstellation> otherSpecification) {
71          throw new NotImplementedException();
72      }
73  
74      @Override
75      public CompositeSpecification<QueenPuzzleConstellation> or(final Specification<? super QueenPuzzleConstellation> otherSpecification) {
76          throw new NotImplementedException();
77      }
78  
79      @Override
80      public Boolean isDisjointWith(final Specification specification) {
81          throw new NotImplementedException();
82      }
83  
84      /** @return <code>true</code> if and only if the chess placement constellation has any straight-line connections. */
85      boolean straightXLineConnections(final ChessPiecePlacing placing1,
86                                       final ChessPiecePlacing placing2,
87                                       final ChessPiecePlacing placing3,
88                                       final ChessPiecePlacing placing4,
89                                       final ChessPiecePlacing placing5,
90                                       final ChessPiecePlacing placing6,
91                                       final ChessPiecePlacing placing7,
92                                       final ChessPiecePlacing placing8) {
93          return (
94                  placing1.getX() == placing2.getX() ||
95                  placing1.getX() == placing3.getX() ||
96                  placing1.getX() == placing4.getX() ||
97                  placing1.getX() == placing5.getX() ||
98                  placing1.getX() == placing6.getX() ||
99                  placing1.getX() == placing7.getX() ||
100                 placing1.getX() == placing8.getX() ||
101 
102                 placing2.getX() == placing3.getX() ||
103                 placing2.getX() == placing4.getX() ||
104                 placing2.getX() == placing5.getX() ||
105                 placing2.getX() == placing6.getX() ||
106                 placing2.getX() == placing7.getX() ||
107                 placing2.getX() == placing8.getX() ||
108 
109                 placing3.getX() == placing4.getX() ||
110                 placing3.getX() == placing5.getX() ||
111                 placing3.getX() == placing6.getX() ||
112                 placing3.getX() == placing7.getX() ||
113                 placing3.getX() == placing8.getX() ||
114 
115                 placing4.getX() == placing5.getX() ||
116                 placing4.getX() == placing6.getX() ||
117                 placing4.getX() == placing7.getX() ||
118                 placing4.getX() == placing8.getX() ||
119 
120                 placing5.getX() == placing6.getX() ||
121                 placing5.getX() == placing7.getX() ||
122                 placing5.getX() == placing8.getX() ||
123 
124                 placing6.getX() == placing7.getX() ||
125                 placing6.getX() == placing8.getX() ||
126 
127                 placing7.getX() == placing8.getX()
128         );
129     }
130 
131 
132     /** @return <code>true</code> if and only if the chess placement constellation has any straight-line connections. */
133     boolean straightYLineConnections(final ChessPiecePlacing placing1,
134                                      final ChessPiecePlacing placing2,
135                                      final ChessPiecePlacing placing3,
136                                      final ChessPiecePlacing placing4,
137                                      final ChessPiecePlacing placing5,
138                                      final ChessPiecePlacing placing6,
139                                      final ChessPiecePlacing placing7,
140                                      final ChessPiecePlacing placing8) {
141         return (
142                 placing1.getY() == placing2.getY() ||
143                 placing1.getY() == placing3.getY() ||
144                 placing1.getY() == placing4.getY() ||
145                 placing1.getY() == placing5.getY() ||
146                 placing1.getY() == placing6.getY() ||
147                 placing1.getY() == placing7.getY() ||
148                 placing1.getY() == placing8.getY() ||
149 
150                 placing2.getY() == placing3.getY() ||
151                 placing2.getY() == placing4.getY() ||
152                 placing2.getY() == placing5.getY() ||
153                 placing2.getY() == placing6.getY() ||
154                 placing2.getY() == placing7.getY() ||
155                 placing2.getY() == placing8.getY() ||
156 
157                 placing3.getY() == placing4.getY() ||
158                 placing3.getY() == placing5.getY() ||
159                 placing3.getY() == placing6.getY() ||
160                 placing3.getY() == placing7.getY() ||
161                 placing3.getY() == placing8.getY() ||
162 
163                 placing4.getY() == placing5.getY() ||
164                 placing4.getY() == placing6.getY() ||
165                 placing4.getY() == placing7.getY() ||
166                 placing4.getY() == placing8.getY() ||
167 
168                 placing5.getY() == placing6.getY() ||
169                 placing5.getY() == placing7.getY() ||
170                 placing5.getY() == placing8.getY() ||
171 
172                 placing6.getY() == placing7.getY() ||
173                 placing6.getY() == placing8.getY() ||
174 
175                 placing7.getY() == placing8.getY()
176         );
177     }
178 
179 
180     /** @return <code>true</code> if and only if the chess placement constellation has any diagonal connections. */
181     boolean diagonalLineConnections(final int pDirectionIndex,
182                                     final ChessPiecePlacing placing1,
183                                     final ChessPiecePlacing placing2,
184                                     final ChessPiecePlacing placing3,
185                                     final ChessPiecePlacing placing4,
186                                     final ChessPiecePlacing placing5,
187                                     final ChessPiecePlacing placing6,
188                                     final ChessPiecePlacing placing7,
189                                     final ChessPiecePlacing placing8) {
190         if (pDirectionIndex == RIGHT) {
191             return (
192                     (placing1.getY() - placing1.getX()) == (placing2.getY() - placing2.getX()) ||
193                     (placing1.getY() - placing1.getX()) == (placing3.getY() - placing3.getX()) ||
194                     (placing1.getY() - placing1.getX()) == (placing4.getY() - placing4.getX()) ||
195                     (placing1.getY() - placing1.getX()) == (placing5.getY() - placing5.getX()) ||
196                     (placing1.getY() - placing1.getX()) == (placing6.getY() - placing6.getX()) ||
197                     (placing1.getY() - placing1.getX()) == (placing7.getY() - placing7.getX()) ||
198                     (placing1.getY() - placing1.getX()) == (placing8.getY() - placing8.getX()) ||
199 
200                     (placing2.getY() - placing2.getX()) == (placing3.getY() - placing3.getX()) ||
201                     (placing2.getY() - placing2.getX()) == (placing4.getY() - placing4.getX()) ||
202                     (placing2.getY() - placing2.getX()) == (placing5.getY() - placing5.getX()) ||
203                     (placing2.getY() - placing2.getX()) == (placing6.getY() - placing6.getX()) ||
204                     (placing2.getY() - placing2.getX()) == (placing7.getY() - placing7.getX()) ||
205                     (placing2.getY() - placing2.getX()) == (placing8.getY() - placing8.getX()) ||
206 
207                     (placing3.getY() - placing3.getX()) == (placing4.getY() - placing4.getX()) ||
208                     (placing3.getY() - placing3.getX()) == (placing5.getY() - placing5.getX()) ||
209                     (placing3.getY() - placing3.getX()) == (placing6.getY() - placing6.getX()) ||
210                     (placing3.getY() - placing3.getX()) == (placing7.getY() - placing7.getX()) ||
211                     (placing3.getY() - placing3.getX()) == (placing8.getY() - placing8.getX()) ||
212 
213                     (placing4.getY() - placing4.getX()) == (placing5.getY() - placing5.getX()) ||
214                     (placing4.getY() - placing4.getX()) == (placing6.getY() - placing6.getX()) ||
215                     (placing4.getY() - placing4.getX()) == (placing7.getY() - placing7.getX()) ||
216                     (placing4.getY() - placing4.getX()) == (placing8.getY() - placing8.getX()) ||
217 
218                     (placing5.getY() - placing5.getX()) == (placing6.getY() - placing6.getX()) ||
219                     (placing5.getY() - placing5.getX()) == (placing7.getY() - placing7.getX()) ||
220                     (placing5.getY() - placing5.getX()) == (placing8.getY() - placing8.getX()) ||
221 
222                     (placing6.getY() - placing6.getX()) == (placing7.getY() - placing7.getX()) ||
223                     (placing6.getY() - placing6.getX()) == (placing8.getY() - placing8.getX()) ||
224 
225                     (placing7.getY() - placing7.getX()) == (placing8.getY() - placing8.getX())
226             );
227         }
228         return (
229                 (placing1.getY() + placing1.getX()) == (placing2.getY() + placing2.getX()) ||
230                 (placing1.getY() + placing1.getX()) == (placing3.getY() + placing3.getX()) ||
231                 (placing1.getY() + placing1.getX()) == (placing4.getY() + placing4.getX()) ||
232                 (placing1.getY() + placing1.getX()) == (placing5.getY() + placing5.getX()) ||
233                 (placing1.getY() + placing1.getX()) == (placing6.getY() + placing6.getX()) ||
234                 (placing1.getY() + placing1.getX()) == (placing7.getY() + placing7.getX()) ||
235                 (placing1.getY() + placing1.getX()) == (placing8.getY() + placing8.getX()) ||
236 
237                 (placing2.getY() + placing2.getX()) == (placing3.getY() + placing3.getX()) ||
238                 (placing2.getY() + placing2.getX()) == (placing4.getY() + placing4.getX()) ||
239                 (placing2.getY() + placing2.getX()) == (placing5.getY() + placing5.getX()) ||
240                 (placing2.getY() + placing2.getX()) == (placing6.getY() + placing6.getX()) ||
241                 (placing2.getY() + placing2.getX()) == (placing7.getY() + placing7.getX()) ||
242                 (placing2.getY() + placing2.getX()) == (placing8.getY() + placing8.getX()) ||
243 
244                 (placing3.getY() + placing3.getX()) == (placing4.getY() + placing4.getX()) ||
245                 (placing3.getY() + placing3.getX()) == (placing5.getY() + placing5.getX()) ||
246                 (placing3.getY() + placing3.getX()) == (placing6.getY() + placing6.getX()) ||
247                 (placing3.getY() + placing3.getX()) == (placing7.getY() + placing7.getX()) ||
248                 (placing3.getY() + placing3.getX()) == (placing8.getY() + placing8.getX()) ||
249 
250                 (placing4.getY() + placing4.getX()) == (placing5.getY() + placing5.getX()) ||
251                 (placing4.getY() + placing4.getX()) == (placing6.getY() + placing6.getX()) ||
252                 (placing4.getY() + placing4.getX()) == (placing7.getY() + placing7.getX()) ||
253                 (placing4.getY() + placing4.getX()) == (placing8.getY() + placing8.getX()) ||
254 
255                 (placing5.getY() + placing5.getX()) == (placing6.getY() + placing6.getX()) ||
256                 (placing5.getY() + placing5.getX()) == (placing7.getY() + placing7.getX()) ||
257                 (placing5.getY() + placing5.getX()) == (placing8.getY() + placing8.getX()) ||
258 
259                 (placing6.getY() + placing6.getX()) == (placing7.getY() + placing7.getX()) ||
260                 (placing6.getY() + placing6.getX()) == (placing8.getY() + placing8.getX()) ||
261 
262                 (placing7.getY() + placing7.getX()) == (placing8.getY() + placing8.getX())
263         );
264     }
265 }