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 FourDynamicModel extends DynamicModel {
26  
27      /** "Zero" or empty set. */
28      public static final Entity ZERO = new Entity(0, "{}", "{} or empty set");
29  
30      /** "One" or set that contains the empty set. */
31      public static final Entity ONE = new Entity(1, "{{}}", "{{}} or 1");
32  
33      /** "Two" or set that contains "zero" and "one". */
34      public static final Entity TWO = new Entity(2, "{{}, {{}}}", "{{}, {{}}} or 2");
35  
36      /** "Three" or set that contains "one". */
37      public static final Entity THREE = new Entity(3, "{{{}}}", "{{{}}} or 3");
38  
39      /** Map to empty class. */
40      public static final Function FUNCTION_ZERO = Function.createConstant(ZERO);
41  
42      /** Map to 1. */
43      public static final Function FUNCTION_ONE = Function.createConstant(ONE);
44  
45      /** Map to 2. */
46      public static final Function FUNCTION_TWO = Function.createConstant(TWO);
47  
48      /** Map to 2. */
49      public static final Function FUNCTION_THREE = Function.createConstant(THREE);
50  
51      /** Return true if all values are zero. */
52      public static final Predicate IS_ZERO = Predicate.isEntity(ZERO);
53  
54      /** Return true if all values are 1. */
55      public static final Predicate IS_ONE = Predicate.isEntity(ONE);
56  
57      /** Return true if all values are 2. */
58      public static final Predicate IS_TWO = Predicate.isEntity(TWO);
59  
60      /** Return true if all values are 2. */
61      public static final Predicate IS_THREE = Predicate.isEntity(THREE);
62  
63      /** Map to one. */
64      /** Modulo 3. */
65      private final Function functionModulo3 = new Function(0, 99, "% 3", "modulo 3") {
66          public Entity map(final Entity[] entities) {
67              int result = 0;
68              for (int i = 0; i < entities.length; i++) {
69                  result += entities[i].getValue() % 3;
70              }
71              result = result % 3;
72              return getEntity(result);
73          }
74      };
75  
76      /** +1 Modulo 3. */
77      private final Function functionPlus1Modulo3 = new Function(0, 99, "+1 % 3", "plus 1 modulo 3") {
78          public Entity map(final Entity[] entities) {
79              int result = 1;
80              for (int i = 0; i < entities.length; i++) {
81                  result += entities[i].getValue() % 3;
82              }
83              result = result % 3;
84              return getEntity(result);
85          }
86      };
87  
88  
89      /**
90       * Constructor.
91       */
92      public FourDynamicModel() {
93          super("four elements");
94  
95          addEntity(ZERO);
96          addEntity(ONE);
97          addEntity(TWO);
98          addEntity(THREE);
99  
100         addFunction(0, FUNCTION_ZERO);
101         addFunction(0, FUNCTION_ONE);
102         addFunction(0, FUNCTION_TWO);
103         addFunction(0, FUNCTION_THREE);
104 
105         addFunction(1, FUNCTION_ZERO);
106         addFunction(1, FUNCTION_ONE);
107         addFunction(0, FUNCTION_TWO);
108         addFunction(0, FUNCTION_THREE);
109         addFunction(1, functionModulo3);
110         addFunction(1, functionPlus1Modulo3);
111 
112         addFunction(2, FUNCTION_ZERO);
113         addFunction(2, FUNCTION_ONE);
114         addFunction(0, FUNCTION_TWO);
115         addFunction(0, FUNCTION_THREE);
116         addFunction(2, functionModulo3);
117         addFunction(2, functionPlus1Modulo3);
118 
119         addPredicate(0, FALSE);
120         addPredicate(0, TRUE);
121 
122         addPredicate(1, FALSE);
123         addPredicate(1, TRUE);
124         addPredicate(1, EVEN);
125         addPredicate(1, IS_ZERO);
126         addPredicate(1, IS_ONE);
127         addPredicate(1, IS_TWO);
128         addPredicate(1, IS_THREE);
129 
130         addPredicate(2, FALSE);
131         addPredicate(2, TRUE);
132         addPredicate(2, EVEN);
133         addPredicate(2, LESS);
134         addPredicate(2, EQUAL);
135         addPredicate(2, IS_ZERO);
136         addPredicate(2, IS_ONE);
137         addPredicate(2, IS_TWO);
138         addPredicate(2, IS_THREE);
139 
140         addPredicateConstant(new ModelPredicateConstant("in", 2), new Predicate(2, 2, "in", "Element of") {
141             public boolean calculate(final Entity[] entities) {
142                 if (entities.length != 2) {
143                     return false;
144                 }
145                 final int element = entities[0].getValue();
146                 switch (entities[1].getValue()) {
147                 case 0: return false;
148                 case 1: if (element == 0) {
149                             return true;
150                         }
151                         return false;
152                 case 2: if (element < 2) {
153                             return true;
154                         }
155                         return false;
156                 case 3: if (element == 1) {
157                             return true;
158                         }
159                         return false;
160                 default: return false;
161                 }
162             }
163         });
164     }
165 
166     public String getDescription() {
167         return "This model has four entities: {}, {{}}, {{}, {{}}}, {{{}}}.";
168     }
169 
170     public Entity comprehension(final Entity[] array) {
171         Entity result = ZERO;
172         for (int i = 0; i < array.length; i++) {
173             final int element = array[i].getValue();
174             switch (result.getValue()) {
175             case 0:
176                 if (element == 0) {
177                     result = ONE;
178                 } else if (element == 1) {
179                     result = THREE;
180                 }
181                 break;
182             case 1:
183                 if (element == 1) {
184                     result = TWO;
185                 }
186                 break;
187             case 2:
188                 break;
189             case 3:
190                 if (element == 0) {
191                     result = TWO;
192                 }
193                 break;
194             default: throw new RuntimeException("unknown value for entity " + array[i]);
195             }
196         }
197         return result;
198     }
199 
200 }