1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.domian.test.benchmark;
17
18
19 import java.text.SimpleDateFormat;
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.Iterator;
23
24 import net.sourceforge.domian.repository.PartitionRepository;
25 import net.sourceforge.domian.repository.PersistenceDefinition;
26 import net.sourceforge.domian.repository.PersistentRepository;
27 import net.sourceforge.domian.repository.Repository;
28 import static net.sourceforge.domian.specification.SpecificationFactory.all;
29 import static net.sourceforge.domian.test.benchmark.QueenPuzzleUtils.conditionalLogging;
30 import net.sourceforge.domian.util.StopWatch;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 final class SequentialQueenPuzzle extends AbstractQueenPuzzle {
50
51 SequentialQueenPuzzle(final RepositoryType repositoryType, final Long numberOfConstellations, final long logInterval) {
52 super(repositoryType, numberOfConstellations, 1, logInterval);
53 }
54
55 Collection<? extends QueenPuzzleConstellation> getSuccessfulQueenPuzzleConstellations() {
56 return this.successfulQueenPuzzleConstellations;
57 }
58
59 void solvePuzzle() {
60 System.out.println();
61 System.out.println("Solving the \"Queen Puzzle\"... [start time: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS").format(new Date()) + "]");
62 System.out.println();
63
64
65 final Repository<QueenPuzzleConstellation> repo = createRepository(this.repositoryType);
66 if (repo instanceof PersistentRepository) {
67 if (((PersistentRepository) repo).getPersistenceDefinition().supportsAsynchronousPersistence()) {
68 repo.remove(all(QueenPuzzleConstellation.class));
69 ((PersistentRepository) repo).persist();
70 }
71 }
72
73 final StopWatch stopWatch = new StopWatch().start();
74
75
76 if (repo instanceof PartitionRepository) {
77
78 ((PartitionRepository<QueenPuzzleConstellation>) repo).addPartitionFor(queenPuzzleConstellationsThatIsProcessedAndMarkedAsToSolveTheQueenPuzzle, "approved-queenpuzzle-constellations");
79 ((PartitionRepository<QueenPuzzleConstellation>) repo).addPartitionFor(unProcessedQueenPuzzleConstellations, "unprocessed-queenpuzzle-constellations");
80 }
81
82
83 addOneWellKnownSuccessfulQueenPuzzleConstellationInto(repo);
84
85
86 generateQueenPuzzleConstellationsAndPutInto(repo);
87
88
89
90 final Iterator<? extends QueenPuzzleConstellation> allUnprocessedConstellations = repo.iterate(unProcessedQueenPuzzleConstellations);
91 System.out.println("Processing unprocessed chess piece constellations...");
92 long numberOfConstellationsFound = 0;
93 while (allUnprocessedConstellations.hasNext()) {
94 QueenPuzzleConstellation queenPuzzleConstellation = allUnprocessedConstellations.next();
95 queenPuzzleConstellation.setSolvesQueenPuzzle(queenPuzzleConstellationsThatInherentlySolvesTheQueenPuzzle.isSatisfiedBy(queenPuzzleConstellation));
96
97
98
99
100 conditionalLogging(logInterval, ++numberOfConstellationsFound, stopWatch, queenPuzzleConstellation, "processed so far...");
101 }
102 doPrintProcessStatistics(stopWatch.getElapsedTime(), this.numberOfConstellations);
103
104
105
106
107
108
109
110 doResultSearch(repo);
111
112
113 doCompletionControl(repo);
114
115 System.out.println();
116 System.out.println("Overall time: " + stopWatch);
117 }
118 }