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 import java.util.ArrayList;
019 import java.util.List;
020
021 import org.qedeq.kernel.bo.logic.common.SubjectVariable;
022
023 /**
024 * This class interpretation.
025 *
026 * @author Michael Meyling
027 */
028 public final class SubjectVariableInterpreter {
029
030 /** List of subject variables allocations. */
031 private List subjectVariableAllocations;
032
033 /** Model contains entities. */
034 private Model model;
035
036 /**
037 * Constructor.
038 *
039 * @param model Model we work on.
040 */
041 public SubjectVariableInterpreter(final Model model) {
042 this.model = model;
043 subjectVariableAllocations = new ArrayList();
044 }
045
046 /**
047 * Change to next valuation.
048 *
049 * @return Is there a next new valuation?
050 */
051 public synchronized boolean next() {
052 boolean next = true;
053 for (int i = subjectVariableAllocations.size() - 1; i >= -1; i--) {
054 if (i < 0) {
055 next = false;
056 break;
057 }
058 final SubjectVariableAllocation allocation
059 = (SubjectVariableAllocation) subjectVariableAllocations.get(i);
060 if (allocation.getValue() + 1 < model.getEntitiesSize()) {
061 allocation.increaseNumber();
062 break;
063 }
064 allocation.resetNumber();
065 }
066 return next;
067 }
068
069 /**
070 * Add subject variable. This is usually done for interpreting a quantifier.
071 *
072 * @param var Subject variable to add to our interpretation.
073 */
074 public synchronized void addSubjectVariable(final SubjectVariable var) {
075 // if we forbid quantifing with same variable twice, we must activate the following
076 // if (subjectVariables.contains(var)) {
077 // throw new RuntimeException("variable already exists: " + var);
078 // }
079 // System.out.println("added subject variable " + var);
080 subjectVariableAllocations.add(new SubjectVariableAllocation(var));
081 }
082
083 /**
084 * Add subject variable even if already existing.
085 *
086 * @param var Remove this subject variable.
087 * @param value Set interpretation to this entity number.
088 */
089 public synchronized void forceAddSubjectVariable(final SubjectVariable var, final int value) {
090 subjectVariableAllocations.add(new SubjectVariableAllocation(var, value));
091 }
092
093 /**
094 * Remove existing subject variable interpretation.
095 *
096 * @param var Remove this subject variable.
097 */
098 public synchronized void forceRemoveSubjectVariable(final SubjectVariable var) {
099 int index = getIndex(var);
100 if (index < 0) {
101 throw new RuntimeException("variable does not exist: " + var);
102 }
103 final SubjectVariableAllocation current
104 = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
105 if (!current.isFixed()) {
106 throw new RuntimeException("trying to remove not fixed allocation: " + current);
107 }
108 subjectVariableAllocations.remove(index);
109 // System.out.println("removed subject variable " + var);
110 }
111 /**
112 * Remove existing subject variable interpretation.
113 *
114 * @param var Remove this subject variable.
115 */
116 public synchronized void removeSubjectVariable(final SubjectVariable var) {
117 int index = getIndex(var);
118 if (index < 0) {
119 throw new RuntimeException("variable does not exist: " + var);
120 }
121 final SubjectVariableAllocation current
122 = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
123 if (current.isFixed()) {
124 throw new RuntimeException("trying to remove fixed allocation: " + current);
125 }
126 subjectVariableAllocations.remove(index);
127 // System.out.println("removed subject variable " + var);
128 }
129
130 private synchronized int getSubjectVariableSelection(final SubjectVariable var) {
131 int selection = 0;
132 int index = getIndex(var);
133 if (index >= 0) {
134 selection = ((SubjectVariableAllocation) subjectVariableAllocations.get(index))
135 .getValue();
136 } else {
137 addSubjectVariable(var);
138 }
139 return selection;
140 }
141
142 /**
143 * Get current interpretation of subject variable.
144 *
145 * @param var Subject variable we are interested in.
146 * @return Current entity for subject variable.
147 */
148 public synchronized Entity getEntity(final SubjectVariable var) {
149 return model.getEntity(getSubjectVariableSelection(var));
150 }
151
152 private int getIndex(final SubjectVariable var) {
153 int index = -1;
154 for (index = subjectVariableAllocations.size() - 1; index >= 0; index--) {
155 final SubjectVariableAllocation current
156 = (SubjectVariableAllocation) subjectVariableAllocations.get(index);
157 if (var.equals(current.getVariable())) {
158 break;
159 }
160 }
161 return index;
162 }
163
164 /**
165 * Set interpretation of subject variable to next entity.
166 *
167 * @param var Switch to next entity for this subject variable.
168 */
169 public synchronized void increaseSubjectVariableSelection(final SubjectVariable var) {
170 ((SubjectVariableAllocation) subjectVariableAllocations.get(getIndex(var))).increaseNumber();
171 }
172
173 public synchronized String toString() {
174 final StringBuffer buffer = new StringBuffer();
175 buffer.append("subject variables {");
176 for (int i = 0; i < subjectVariableAllocations.size(); i++) {
177 if (i > 0) {
178 buffer.append(", ");
179 }
180 SubjectVariableAllocation var = (SubjectVariableAllocation) subjectVariableAllocations.get(i);
181 buffer.append(var.getVariable());
182 buffer.append("=");
183 buffer.append(model.getEntity(var.getValue()));
184 }
185 buffer.append("}");
186 return buffer.toString();
187 }
188
189 /**
190 * Clear variable interpretation.
191 */
192 public synchronized void clear() {
193 subjectVariableAllocations.clear();
194 }
195
196 }
|