View Javadoc

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