| 1 | /* This file is part of the project "Hilbert II" - 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 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | |
| 21 | import org.qedeq.base.utility.Enumerator; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * Interpret function variables. |
| 26 | * |
| 27 | * @author Michael Meyling |
| 28 | */ |
| 29 | public final class FunctionVariableInterpreter { |
| 30 | |
| 31 | /** Model contains entities, functions, predicates. */ |
| 32 | private Model model; |
| 33 | |
| 34 | /** List of function variables. */ |
| 35 | private List functionVariables; |
| 36 | |
| 37 | /** List of counters for function variables. */ |
| 38 | private List functionVariableCounters; |
| 39 | |
| 40 | /** |
| 41 | * Constructor. |
| 42 | * |
| 43 | * @param model Model we work on. |
| 44 | */ |
| 45 | public FunctionVariableInterpreter(final Model model) { |
| 46 | this.model = model; |
| 47 | functionVariables = new ArrayList(); |
| 48 | functionVariableCounters = new ArrayList(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get selection for function of model for given function variable. |
| 53 | * |
| 54 | * @param var Function variable we want an interpretation for. |
| 55 | * @return Function selection number of model. |
| 56 | */ |
| 57 | private int getFunctionVariableSelection(final FunctionVariable var) { |
| 58 | int selection; |
| 59 | if (functionVariables.contains(var)) { |
| 60 | final int index = functionVariables.indexOf(var); |
| 61 | selection = ((Enumerator) functionVariableCounters.get(index)).getNumber(); |
| 62 | } else { |
| 63 | // System.out.println("added function variable " + var); |
| 64 | selection = 0; |
| 65 | functionVariables.add(var); |
| 66 | functionVariableCounters.add(new Enumerator()); |
| 67 | } |
| 68 | return selection; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get interpretation for function of model for given function variable. |
| 73 | * |
| 74 | * @param var Function variable we want an interpretation for. |
| 75 | * @return Function. |
| 76 | */ |
| 77 | public Function getFunction(final FunctionVariable var) { |
| 78 | return model.getFunction(var.getArgumentNumber(), |
| 79 | getFunctionVariableSelection(var)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Change to next valuation. |
| 84 | * |
| 85 | * @return Is there a next new valuation? |
| 86 | */ |
| 87 | public boolean next() { |
| 88 | boolean next = true; |
| 89 | for (int i = functionVariables.size() - 1; i >= -1; i--) { |
| 90 | if (i < 0) { |
| 91 | next = false; |
| 92 | break; |
| 93 | } |
| 94 | final FunctionVariable var = (FunctionVariable) functionVariables.get(i); |
| 95 | final Enumerator number = (Enumerator) functionVariableCounters.get(i); |
| 96 | if (number.getNumber() + 1 < model.getFunctionSize(var.getArgumentNumber())) { |
| 97 | number.increaseNumber(); |
| 98 | break; |
| 99 | } |
| 100 | number.reset(); |
| 101 | } |
| 102 | return next; |
| 103 | } |
| 104 | |
| 105 | public String toString() { |
| 106 | final StringBuffer buffer = new StringBuffer(); |
| 107 | buffer.append("function variables {"); |
| 108 | for (int i = 0; i < functionVariables.size(); i++) { |
| 109 | if (i > 0) { |
| 110 | buffer.append(", "); |
| 111 | } |
| 112 | FunctionVariable var = (FunctionVariable) functionVariables.get(i); |
| 113 | buffer.append(functionVariables.get(i)); |
| 114 | buffer.append("="); |
| 115 | buffer.append(getFunction(var)); |
| 116 | } |
| 117 | buffer.append("}"); |
| 118 | return buffer.toString(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Clear variable interpretation. |
| 123 | */ |
| 124 | public void clear() { |
| 125 | functionVariables.clear(); |
| 126 | functionVariableCounters.clear(); |
| 127 | } |
| 128 | |
| 129 | } |