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 static org.apache.commons.lang.math.RandomUtils.nextInt;
20  
21  import net.sourceforge.domian.repository.Repository;
22  import static net.sourceforge.domian.test.benchmark.QueenPuzzleUtils.conditionalLogging;
23  import net.sourceforge.domian.util.StopWatch;
24  
25  
26  class RandomSequenceQueenPuzzleConstellationFactory {
27  
28      final long maximumNumberOfConstellationToProduce;
29      final long logInterval;
30  
31      RandomSequenceQueenPuzzleConstellationFactory(final Long maximumNumberOfConstellationToProduce,
32                                                    final Long logInterval) {
33          this.maximumNumberOfConstellationToProduce = maximumNumberOfConstellationToProduce;
34          this.logInterval = logInterval;
35      }
36  
37      /* No constraint */
38      protected boolean constellationConstraintOk(final int[] queenPlacingNumbers, final int index, final int number) {
39          return true;
40      }
41  
42      void generateAndPopulate(final Repository repo) {
43          long constellationNumber;
44          QueenPuzzleConstellation queenPuzzleConstellation;
45  
46          StopWatch stopWatch = new StopWatch().start();
47          for (constellationNumber = 1; constellationNumber < maximumNumberOfConstellationToProduce; ++constellationNumber) {
48              final int[] constellation = new int[8];
49              int index = 0;
50              while (index < 8) {
51                  int number = nextInt(64);
52                  while (!constellationConstraintOk(constellation, index, number)) {
53                      number = nextInt(64);
54                  }
55                  constellation[index] = number;
56                  ++index;
57              }
58              queenPuzzleConstellation = new QueenPuzzleConstellation(new ChessPiecePlacing(constellation[0]),
59                                                                      new ChessPiecePlacing(constellation[1]),
60                                                                      new ChessPiecePlacing(constellation[2]),
61                                                                      new ChessPiecePlacing(constellation[3]),
62                                                                      new ChessPiecePlacing(constellation[4]),
63                                                                      new ChessPiecePlacing(constellation[5]),
64                                                                      new ChessPiecePlacing(constellation[6]),
65                                                                      new ChessPiecePlacing(constellation[7]));
66              repo.put(queenPuzzleConstellation);
67              conditionalLogging(logInterval, constellationNumber, stopWatch, queenPuzzleConstellation, "generated and added to repo so far...");
68          }
69      }
70  }