SubjectVariableInterpreter.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  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                 (SubjectVariableAllocationsubjectVariableAllocations.get(i);
060             if (allocation.getValue() < model.getEntitiesSize()) {
061                 allocation.increaseNumber();
062                 break;
063             else {
064                 allocation.resetNumber();
065             }
066         }
067         return next;
068     }
069 
070     /**
071      * Add subject variable. This is usually done for interpreting a quantifier.
072      *
073      @param   var Subject variable to add to our interpretation.
074      */
075     public synchronized void addSubjectVariable(final SubjectVariable var) {
076         // if we forbid quantifing with same variable twice, we must activate the following
077 //        if (subjectVariables.contains(var)) {
078 //            throw new RuntimeException("variable already exists: " + var);
079 //        }
080 //        System.out.println("added subject variable " + var);
081         subjectVariableAllocations.add(new SubjectVariableAllocation(var));
082     }
083 
084     /**
085      * Add subject variable even if already existing.
086      *
087      @param   var     Remove this subject variable.
088      @param   value   Set interpretation to this entity number.
089      */
090     public synchronized void forceAddSubjectVariable(final SubjectVariable var, final int value) {
091         subjectVariableAllocations.add(new SubjectVariableAllocation(var, value));
092     }
093 
094     /**
095      * Remove existing subject variable interpretation.
096      *
097      @param   var Remove this subject variable.
098      */
099     public synchronized void forceRemoveSubjectVariable(final SubjectVariable var) {
100         int index = getIndex(var);
101         if (index < 0) {
102             throw new RuntimeException("variable does not exist: " + var);
103         }
104         final SubjectVariableAllocation current
105             (SubjectVariableAllocationsubjectVariableAllocations.get(index);
106         if (!current.isFixed()) {
107             throw new RuntimeException("trying to remove not fixed allocation: " + current);
108         }
109         subjectVariableAllocations.remove(index);
110 //        System.out.println("removed subject variable " + var);
111     }
112     /**
113      * Remove existing subject variable interpretation.
114      *
115      @param   var Remove this subject variable.
116      */
117     public synchronized void removeSubjectVariable(final SubjectVariable var) {
118         int index = getIndex(var);
119         if (index < 0) {
120             throw new RuntimeException("variable does not exist: " + var);
121         }
122         final SubjectVariableAllocation current
123             (SubjectVariableAllocationsubjectVariableAllocations.get(index);
124         if (current.isFixed()) {
125             throw new RuntimeException("trying to remove fixed allocation: " + current);
126         }
127         subjectVariableAllocations.remove(index);
128 //        System.out.println("removed subject variable " + var);
129     }
130 
131     private synchronized int getSubjectVariableSelection(final SubjectVariable var) {
132         int selection = 0;
133         int index = getIndex(var);
134         if (index >= 0) {
135             selection = ((SubjectVariableAllocationsubjectVariableAllocations.get(index))
136                 .getValue();
137         else {
138             addSubjectVariable(var);
139         }
140         return selection;
141     }
142 
143     /**
144      * Get current interpretation of subject variable.
145      *
146      @param   var     Subject variable we are interested in.
147      @return  Current entity for subject variable.
148      */
149     public synchronized Entity getEntity(final SubjectVariable var) {
150         return model.getEntity(getSubjectVariableSelection(var));
151     }
152 
153     private int getIndex(final SubjectVariable var) {
154         int index = -1;
155         for (index = subjectVariableAllocations.size() 1; index >= 0; index--) {
156             final SubjectVariableAllocation current
157                 (SubjectVariableAllocationsubjectVariableAllocations.get(index);
158             if (var.equals(current.getVariable())) {
159                 break;
160             }
161         }
162         return index;
163     }
164 
165     /**
166      * Set interpretation of subject variable to next entity.
167      *
168      @param   var Switch to next entity for this subject variable.
169      */
170     public synchronized void increaseSubjectVariableSelection(final SubjectVariable var) {
171         ((SubjectVariableAllocationsubjectVariableAllocations.get(getIndex(var))).increaseNumber();
172     }
173 
174     public synchronized String toString() {
175         final StringBuffer buffer = new StringBuffer();
176         buffer.append("subject variables {");
177         for (int i = 0; i < subjectVariableAllocations.size(); i++) {
178             if (i > 0) {
179                 buffer.append(", ");
180             }
181             SubjectVariableAllocation var = (SubjectVariableAllocationsubjectVariableAllocations.get(i);
182             buffer.append(var.getVariable());
183             buffer.append("=");
184             buffer.append(model.getEntity(var.getValue()));
185         }
186         buffer.append("}");
187         return buffer.toString();
188     }
189 
190     /**
191      * Clear variable interpretation.
192      */
193     public synchronized void clear() {
194         subjectVariableAllocations.clear();
195     }
196 
197 }