| 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.module; |
| 17 | |
| 18 | import org.qedeq.kernel.bo.common.NodeBo; |
| 19 | import org.qedeq.kernel.bo.common.QedeqBo; |
| 20 | import org.qedeq.kernel.se.base.list.Element; |
| 21 | import org.qedeq.kernel.se.base.module.ConditionalProof; |
| 22 | import org.qedeq.kernel.se.base.module.FormalProofLineList; |
| 23 | import org.qedeq.kernel.se.base.module.FormalProofList; |
| 24 | import org.qedeq.kernel.se.base.module.NodeType; |
| 25 | import org.qedeq.kernel.se.base.module.Proposition; |
| 26 | import org.qedeq.kernel.se.base.module.Reason; |
| 27 | import org.qedeq.kernel.se.common.CheckLevel; |
| 28 | import org.qedeq.kernel.se.common.ModuleContext; |
| 29 | import org.qedeq.kernel.se.dto.module.NodeVo; |
| 30 | import org.qedeq.kernel.se.visitor.QedeqNumbers; |
| 31 | |
| 32 | /** |
| 33 | * Business object for node access. |
| 34 | * |
| 35 | * @author Michael Meyling |
| 36 | */ |
| 37 | public class KernelNodeBo implements NodeBo, CheckLevel { |
| 38 | |
| 39 | /** The plain node data. */ |
| 40 | private final NodeVo node; |
| 41 | |
| 42 | /** The module context the node is within. */ |
| 43 | private final ModuleContext context; |
| 44 | |
| 45 | /** Parent module the node is within. */ |
| 46 | private final KernelQedeqBo qedeq; |
| 47 | |
| 48 | /** Herein are the results of various counters for the node. */ |
| 49 | private final QedeqNumbers data; |
| 50 | |
| 51 | /** Are all formal formulas of this node well formed. |
| 52 | * See {@link CheckLevel} for value format. */ |
| 53 | private int wellFormedCheck = UNCHECKED; |
| 54 | |
| 55 | /** Is this node been successfully formally proved at least once. |
| 56 | * See {@link CheckLevel} for value format. */ |
| 57 | private int provedCheck = UNCHECKED; |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Constructor. |
| 62 | * |
| 63 | * @param node The plain node data. |
| 64 | * @param context The module context the node is within. |
| 65 | * @param qedeq Parent module the node is within. |
| 66 | * @param data Herein are the results of various counters for the node. |
| 67 | */ |
| 68 | public KernelNodeBo(final NodeVo node, final ModuleContext context, final KernelQedeqBo qedeq, |
| 69 | final QedeqNumbers data) { |
| 70 | this.node = node; |
| 71 | this.context = new ModuleContext(context); |
| 72 | this.qedeq = qedeq; |
| 73 | this.data = new QedeqNumbers(data); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get plain node data. |
| 78 | * |
| 79 | * @return The plain node data. |
| 80 | */ |
| 81 | public NodeVo getNodeVo() { |
| 82 | return node; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get module context the node is within. |
| 87 | * |
| 88 | * @return The module context the node is within. |
| 89 | */ |
| 90 | public ModuleContext getModuleContext() { |
| 91 | return context; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get parent module the node is within. |
| 96 | * |
| 97 | * @return Parent module the node is within. |
| 98 | */ |
| 99 | public QedeqBo getParentQedeqBo() { |
| 100 | return qedeq; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get parent module the node is within. |
| 105 | * |
| 106 | * @return Parent module the node is within. |
| 107 | */ |
| 108 | public KernelQedeqBo getQedeqBo() { |
| 109 | return qedeq; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get the results of various counters for the node. |
| 114 | * |
| 115 | * @return Herein are the results of various counters for the node. |
| 116 | */ |
| 117 | public QedeqNumbers getNumbers() { |
| 118 | return data; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * // FIXME 20110316 m31: we have to solve the uniqueness problem if we have several formal proofs |
| 123 | * Is the given name a proof line label within this node? |
| 124 | * |
| 125 | * @param label Look if this node is a proposition that contains this label name. |
| 126 | * @return Answer. |
| 127 | */ |
| 128 | public boolean isProofLineLabel(final String label) { |
| 129 | if (label == null || label.length() == 0) { |
| 130 | return false; |
| 131 | } |
| 132 | final Proposition theorem = getNodeVo().getNodeType().getProposition(); |
| 133 | if (theorem == null) { |
| 134 | return false; |
| 135 | } |
| 136 | final FormalProofList proofs = theorem.getFormalProofList(); |
| 137 | if (proofs == null) { |
| 138 | return false; |
| 139 | } |
| 140 | // iterate through all formal proofs |
| 141 | for (int i = 0; i < proofs.size(); i++) { |
| 142 | final FormalProofLineList list = proofs.get(i).getFormalProofLineList(); |
| 143 | if (hasProofLineLabel(label, list)) { |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | // nowhere found: |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | private boolean hasProofLineLabel(final String label, final FormalProofLineList list) { |
| 152 | if (list == null) { |
| 153 | return false; |
| 154 | } |
| 155 | for (int j = 0; j < list.size(); j++) { |
| 156 | if (label.equals(list.get(j).getLabel())) { |
| 157 | return true; |
| 158 | } |
| 159 | final Reason reason = list.get(j).getReason(); |
| 160 | if (reason instanceof ConditionalProof) { |
| 161 | final ConditionalProof conditionalProof = (ConditionalProof) reason; |
| 162 | if (conditionalProof.getHypothesis() != null) { |
| 163 | if (label.equals(conditionalProof.getHypothesis().getLabel())) { |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | final FormalProofLineList list2 = conditionalProof.getFormalProofLineList(); |
| 168 | if (hasProofLineLabel(label, list2)) { |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Set proved parameter. See {@link #isWellFormed()}. |
| 178 | * |
| 179 | * @param wellFormedCheck Node well formed state. |
| 180 | * See {@link CheckLevel} for parameter format. |
| 181 | */ |
| 182 | public void setWellFormed(final int wellFormedCheck) { |
| 183 | this.wellFormedCheck = wellFormedCheck; |
| 184 | } |
| 185 | |
| 186 | public boolean isWellFormed() { |
| 187 | return wellFormedCheck >= SUCCESS; |
| 188 | } |
| 189 | |
| 190 | public boolean isNotWellFormed() { |
| 191 | return wellFormedCheck < SUCCESS && wellFormedCheck > UNCHECKED; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Set proved parameter. See {@link #isProved()}. |
| 196 | * |
| 197 | * @param provedCheck Node proved state. |
| 198 | * See {@link CheckLevel} for parameter format. |
| 199 | */ |
| 200 | public void setProved(final int provedCheck) { |
| 201 | this.provedCheck = provedCheck; |
| 202 | } |
| 203 | |
| 204 | public boolean isProved() { |
| 205 | return provedCheck >= SUCCESS; |
| 206 | } |
| 207 | |
| 208 | public boolean isNotProved() { |
| 209 | return provedCheck < SUCCESS && provedCheck > UNCHECKED; |
| 210 | } |
| 211 | |
| 212 | public boolean hasFormula() { |
| 213 | return null != getFormula(); |
| 214 | } |
| 215 | |
| 216 | public Element getFormula() { |
| 217 | if (getNodeVo() == null || getNodeVo().getNodeType() == null) { |
| 218 | return null; |
| 219 | } |
| 220 | final NodeType nodeType = getNodeVo().getNodeType(); |
| 221 | if (nodeType.getProposition() != null) { |
| 222 | if (nodeType.getProposition().getFormula() != null) { |
| 223 | return nodeType.getProposition().getFormula().getElement(); |
| 224 | } |
| 225 | return null; |
| 226 | } else if (nodeType.getPredicateDefinition() != null) { |
| 227 | if (nodeType.getPredicateDefinition().getFormula() != null) { |
| 228 | return nodeType.getPredicateDefinition().getFormula().getElement(); |
| 229 | } |
| 230 | return null; |
| 231 | } else if (nodeType.getFunctionDefinition() != null) { |
| 232 | if (nodeType.getFunctionDefinition().getFormula() != null) { |
| 233 | return nodeType.getFunctionDefinition().getFormula().getElement(); |
| 234 | } |
| 235 | return null; |
| 236 | } else if (nodeType.getAxiom() != null) { |
| 237 | if (nodeType.getAxiom().getFormula() != null) { |
| 238 | return nodeType.getAxiom().getFormula().getElement(); |
| 239 | } |
| 240 | return null; |
| 241 | } |
| 242 | return null; |
| 243 | } |
| 244 | } |