FourDynamicModel.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.logic.model;
017 
018 
019 /**
020  * A model for our mathematical world. It has entities, functions and predicates.
021  * There are also predicate and function constants.
022  *
023  @author  Michael Meyling
024  */
025 public final class FourDynamicModel extends DynamicModel {
026 
027     /** "Zero" or empty set. */
028     public static final Entity ZERO = new Entity(0"{}""{} or empty set");
029 
030     /** "One" or set that contains the empty set. */
031     public static final Entity ONE = new Entity(1"{{}}""{{}} or 1");
032 
033     /** "Two" or set that contains "zero" and "one". */
034     public static final Entity TWO = new Entity(2"{{}, {{}}}""{{}, {{}}} or 2");
035 
036     /** "Three" or set that contains "one". */
037     public static final Entity THREE = new Entity(3"{{{}}}""{{{}}} or 3");
038 
039     /** Map to empty class. */
040     public static final Function FUNCTION_ZERO = Function.createConstant(ZERO);
041 
042     /** Map to 1. */
043     public static final Function FUNCTION_ONE = Function.createConstant(ONE);
044 
045     /** Map to 2. */
046     public static final Function FUNCTION_TWO = Function.createConstant(TWO);
047 
048     /** Map to 2. */
049     public static final Function FUNCTION_THREE = Function.createConstant(THREE);
050 
051     /** Return true if all values are zero. */
052     public static final Predicate IS_ZERO = Predicate.isEntity(ZERO);
053 
054     /** Return true if all values are 1. */
055     public static final Predicate IS_ONE = Predicate.isEntity(ONE);
056 
057     /** Return true if all values are 2. */
058     public static final Predicate IS_TWO = Predicate.isEntity(TWO);
059 
060     /** Return true if all values are 2. */
061     public static final Predicate IS_THREE = Predicate.isEntity(THREE);
062 
063     /** Map to one. */
064     /** Modulo 3. */
065     private final Function functionModulo3 = new Function(099"% 3""modulo 3") {
066         public Entity map(final Entity[] entities) {
067             int result = 0;
068             for (int i = 0; i < entities.length; i++) {
069                 result += entities[i].getValue() 3;
070             }
071             result = result % 3;
072             return getEntity(result);
073         }
074     };
075 
076     /** +1 Modulo 3. */
077     private final Function functionPlus1Modulo3 = new Function(099"+1 % 3""plus 1 modulo 3") {
078         public Entity map(final Entity[] entities) {
079             int result = 1;
080             for (int i = 0; i < entities.length; i++) {
081                 result += entities[i].getValue() 3;
082             }
083             result = result % 3;
084             return getEntity(result);
085         }
086     };
087 
088 
089     /**
090      * Constructor.
091      */
092     public FourDynamicModel() {
093         super("four elements");
094 
095         addEntity(ZERO);
096         addEntity(ONE);
097         addEntity(TWO);
098         addEntity(THREE);
099 
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(22"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 0return false;
148                 case 1if (element == 0) {
149                             return true;
150                         }
151                         return false;
152                 case 2if (element < 2) {
153                             return true;
154                         }
155                         return false;
156                 case 3if (element == 1) {
157                             return true;
158                         }
159                         return false;
160                 defaultreturn 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             defaultthrow new RuntimeException("unknown value for entity " + array[i]);
195             }
196         }
197         return result;
198     }
199 
200 }