View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.bo.logic.model;
17  
18  
19  /**
20   * A model for our mathematical world. It has entities, functions and predicates.
21   * There are also predicate and function constants.
22   *
23   * @author  Michael Meyling
24   */
25  public final class SixDynamicModel extends DynamicModel {
26  
27      /** "Zero" or empty set. */
28      public static final Entity EMPTY = new Entity(0, "{}", "{} or empty set");
29  
30      /** "One" or set that contains the empty set. */
31      public static final Entity ZERO_ONE = new Entity(1, "1", "1");
32  
33      /** "Two" or set that contains "zero" and "one". */
34      public static final Entity ZERO_TWO = new Entity(2, "2", "2");
35  
36      /** "One" or set that contains the empty set. */
37      public static final Entity ONE_ONE = new Entity(3, "{1}", "{1}");
38  
39      /** "Two" or set that contains "zero" and "one". */
40      public static final Entity ONE_TWO = new Entity(4, "{2}", "{2}");
41  
42      /** "Two" or set that contains "zero" and "one". */
43      public static final Entity TWO_ONE_TWO = new Entity(5, "{1, 2}", "{1, 2}");
44  
45      /** Map to empty class. */
46      public static final Function FUNCTION_EMPTY = Function.createConstant(EMPTY);
47  
48      /** Map to 1. */
49      public static final Function FUNCTION_ZERO_ONE = Function.createConstant(ZERO_ONE);
50  
51      /** Map to 2. */
52      public static final Function FUNCTION_ZERO_TWO = Function.createConstant(ZERO_TWO);
53  
54      /** Map to {1}. */
55      public static final Function FUNCTION_ONE_ONE = Function.createConstant(ONE_ONE);
56  
57      /** Map to {2}. */
58      public static final Function FUNCTION_ONE_TWO = Function.createConstant(ONE_TWO);
59  
60      /** Map to {1, 2}. */
61      public static final Function FUNCTION_TWO_ONE_TWO = Function.createConstant(TWO_ONE_TWO);
62  
63      /** Return true if all values are zero. */
64      public static final Predicate IS_EMPTY = Predicate.isEntity(EMPTY);
65  
66      /** Return true if all values are 1. */
67      public static final Predicate IS_ZERO_ONE = Predicate.isEntity(ZERO_ONE);
68  
69      /** Return true if all values are 2. */
70      public static final Predicate IS_ZERO_TWO = Predicate.isEntity(ZERO_TWO);
71  
72      /** Return true if all values are {1}. */
73      public static final Predicate IS_ONE_ONE = Predicate.isEntity(ONE_ONE);
74  
75      /** Return true if all values are {2}. */
76      public static final Predicate IS_ONE_TWO = Predicate.isEntity(ONE_TWO);
77  
78      /** Return true if all values are {2}. */
79      public static final Predicate IS_TWO_ONE_TWO = Predicate.isEntity(TWO_ONE_TWO);
80  
81      /** Are the entities are not all equal to {2}? */
82      public static final Predicate NOT_IS_ONE_TWO = Predicate.not(IS_ONE_TWO);
83  
84      /** Map to one. */
85      /** Modulo 3. */
86      private final Function functionModulo3 = new Function(0, 99, "% 3", "modulo 3") {
87          public Entity map(final Entity[] entities) {
88              int result = 0;
89              for (int i = 0; i < entities.length; i++) {
90                  result += entities[i].getValue() % 3;
91              }
92              result = result % 3;
93              return getEntity(result);
94          }
95      };
96  
97      /** +1 Modulo 3. */
98      private final Function functionPlus1Modulo3 = new Function(0, 99, "+1 % 3", "plus 1 modulo 3") {
99          public Entity map(final Entity[] entities) {
100             int result = 1;
101             for (int i = 0; i < entities.length; i++) {
102                 result += entities[i].getValue() % 3;
103             }
104             result = result % 3;
105             return getEntity(result);
106         }
107     };
108 
109 
110     /**
111      * Constructor.
112      */
113     public SixDynamicModel() {
114         super("six elements");
115 
116         addEntity(EMPTY);
117         addEntity(ZERO_ONE);
118         addEntity(ZERO_TWO);
119         addEntity(ONE_ONE);
120         addEntity(ONE_TWO);
121         addEntity(TWO_ONE_TWO);
122 
123         addFunction(0, FUNCTION_EMPTY);
124         addFunction(0, FUNCTION_ZERO_ONE);
125         addFunction(0, FUNCTION_ZERO_TWO);
126         addFunction(0, FUNCTION_ONE_ONE);
127         addFunction(0, FUNCTION_ONE_TWO);
128         addFunction(0, FUNCTION_TWO_ONE_TWO);
129 
130         addFunction(1, FUNCTION_EMPTY);
131         addFunction(1, FUNCTION_ZERO_ONE);
132         addFunction(1, functionModulo3);
133         addFunction(1, functionPlus1Modulo3);
134 
135         addFunction(2, FUNCTION_EMPTY);
136         addFunction(2, FUNCTION_ZERO_ONE);
137         addFunction(2, functionModulo3);
138         addFunction(2, functionPlus1Modulo3);
139 
140         addPredicate(0, FALSE);
141         addPredicate(0, TRUE);
142 
143         addPredicate(1, FALSE);
144         addPredicate(1, TRUE);
145         addPredicate(1, EVEN);
146         addPredicate(1, IS_EMPTY);
147         addPredicate(1, IS_ZERO_ONE);
148         addPredicate(1, IS_ZERO_TWO);
149         addPredicate(1, IS_ONE_ONE);
150         addPredicate(1, IS_TWO_ONE_TWO);
151 
152         addPredicate(2, FALSE);
153         addPredicate(2, TRUE);
154         addPredicate(2, EVEN);
155         addPredicate(2, LESS);
156         addPredicate(2, EQUAL);
157         addPredicate(2, IS_EMPTY);
158         addPredicate(2, IS_ZERO_ONE);
159         addPredicate(2, IS_ZERO_TWO);
160         addPredicate(2, IS_ONE_ONE);
161         addPredicate(2, IS_ONE_TWO);
162 
163         addPredicateConstant(new ModelPredicateConstant("in", 2), new Predicate(2, 2, "in", "isSet") {
164             public boolean calculate(final Entity[] entities) {
165                 boolean result = false;
166                 final int a = entities[0].getValue();
167                 final int b = entities[1].getValue();
168                 if (a == 1 && (b == 3 || b == 5)) {
169                     result = true;
170                 }
171                 if (a == 2 && (b == 4 || b == 5)) {
172                     result = true;
173                 }
174                 return result;
175             }
176         });
177     }
178 
179     public String getDescription() {
180         return "This model has six entities: {}, {1}, {2}, {{1}}, {{2}} and {1, 2}.";
181     }
182 
183     public Entity comprehension(final Entity[] array) {
184         Entity result = EMPTY;
185         for (int i = 0; i < array.length; i++) {
186             switch (array[i].getValue()) {
187             case 0:
188             case 3:
189             case 4:
190             case 5: break;
191             case 1: if (result.getValue() != 5) {
192                         if (result.getValue() == 0) {
193                             result = ONE_ONE;
194                         } else if (result.getValue() == 4) {
195                             result = TWO_ONE_TWO;
196                         }
197                     }
198                     break;
199             case 2: if (result.getValue() != 5) {
200                         if (result.getValue() == 0) {
201                             result = ONE_TWO;
202                         } else if (result.getValue() == 4) {
203                             result = TWO_ONE_TWO;
204                         }
205                     }
206                     break;
207             default: throw new RuntimeException("unknown value for entity " + array[i]);
208             }
209         }
210         return result;
211     }
212 
213 }