ThreeDynamicModel.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 ThreeDynamicModel 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     /** Map to empty class. */
037     public static final Function FUNCTION_ZERO = Function.createConstant(ZERO);
038 
039     /** Map to 1. */
040     public static final Function FUNCTION_ONE = Function.createConstant(ONE);
041 
042     /** Map to 2. */
043     public static final Function FUNCTION_TWO = Function.createConstant(TWO);
044 
045     /** Return true if all values are zero. */
046     public static final Predicate IS_ZERO = Predicate.isEntity(ZERO);
047 
048     /** Return true if all values are 1. */
049     public static final Predicate IS_ONE = Predicate.isEntity(ONE);
050 
051     /** Return true if all values are 2. */
052     public static final Predicate IS_TWO = Predicate.isEntity(TWO);
053 
054     /** Map to one. */
055     /** Modulo 3. */
056     private final Function functionModulo3 = new Function(099"% 3""modulo 3") {
057         public Entity map(final Entity[] entities) {
058             int result = 0;
059             for (int i = 0; i < entities.length; i++) {
060                 result += entities[i].getValue() 3;
061             }
062             result = result % 3;
063             return getEntity(result);
064         }
065     };
066 
067     /** +1 Modulo 3. */
068     private final Function functionPlus1Modulo3 = new Function(099"+1 % 3""plus 1 modulo 3") {
069         public Entity map(final Entity[] entities) {
070             int result = 1;
071             for (int i = 0; i < entities.length; i++) {
072                 result += entities[i].getValue() 3;
073             }
074             result = result % 3;
075             return getEntity(result);
076         }
077     };
078 
079 
080     /**
081      * Constructor.
082      */
083     public ThreeDynamicModel() {
084         super("three elements");
085 
086         addEntity(ZERO);
087         addEntity(ONE);
088         addEntity(TWO);
089 
090         addFunction(0, FUNCTION_ZERO);
091         addFunction(0, FUNCTION_ONE);
092         addFunction(0, FUNCTION_TWO);
093 
094         addFunction(1, FUNCTION_ZERO);
095         addFunction(1, FUNCTION_ONE);
096         addFunction(1, functionModulo3);
097         addFunction(1, functionPlus1Modulo3);
098 
099         addFunction(2, FUNCTION_ZERO);
100         addFunction(2, FUNCTION_ONE);
101         addFunction(2, functionModulo3);
102         addFunction(2, functionPlus1Modulo3);
103 
104         addPredicate(0, FALSE);
105         addPredicate(0, TRUE);
106 
107         addPredicate(1, FALSE);
108         addPredicate(1, TRUE);
109         addPredicate(1, EVEN);
110         addPredicate(1, IS_ZERO);
111         addPredicate(1, IS_ONE);
112         addPredicate(1, IS_TWO);
113 
114         addPredicate(2, FALSE);
115         addPredicate(2, TRUE);
116         addPredicate(2, EVEN);
117         addPredicate(2, LESS);
118         addPredicate(2, EQUAL);
119         addPredicate(2, IS_ZERO);
120         addPredicate(2, IS_ONE);
121         addPredicate(2, IS_TWO);
122 
123         addPredicateConstant(new ModelPredicateConstant("in"2), LESS);
124     }
125 
126     public String getDescription() {
127         return "This model has three entities: {}, {{}}, {{}, {{}}}.";
128     }
129 
130     public Entity comprehension(final Entity[] array) {
131         Entity result = ZERO;
132         for (int i = 0; i < array.length; i++) {
133             switch (array[i].getValue()) {
134             case 0:
135                 if (result.getValue() == 0) {
136                     result = ONE;
137                 }
138                 break;
139             case 1:
140                 result = TWO;
141                 break;
142             case 2:
143                 break;
144             defaultthrow new RuntimeException("unknown value for entity " + array[i]);
145             }
146         }
147         return result;
148     }
149 
150 }