01: package org.mandarax.reference;
02:
03: /*
04: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: import java.util.ArrayList;
21: import java.util.List;
22: import org.mandarax.kernel.Clause;
23: import org.mandarax.kernel.Derivation;
24: import org.mandarax.kernel.Replacement;
25: import org.mandarax.kernel.Result;
26: import org.mandarax.kernel.Term;
27: import org.mandarax.util.CachedResultSet;
28:
29: /**
30: * Implementation of a result set for ResolutionInferenceEngine2. Could be an inner class, the only
31: * reason to have it as a separate class is that tool support (e.g. debugging) for separate classes is better.
32: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
33: * @version 3.4 <7 March 05>
34: * @since 1.8
35: */
36: class ResultSetImpl2 extends CachedResultSet {
37:
38: /**
39: * Constructor
40: * @param root the root of the derivation
41: * @param resultNodes a list of result nodes
42: * @param query the query
43: */
44: ResultSetImpl2(DerivationNodeImpl root, List resultNodes,
45: Clause query) {
46: super ();
47:
48: // root.dump(System.out);
49: vars = IEUtils.getVars(query);
50:
51: if (vars.isEmpty()) {
52: // fully instanciated queries !
53: // @todo remove limitation: at most one derivation (different proofs are not shown)
54: results = new ArrayList(1);
55: if (!root.isFailed())
56: results.add(new Result(new Derivation(root, 0),
57: new Replacement[0]));
58: } else {
59: // variables in queries - must apply var replacements from nodes
60: Term[] varArray = (Term[]) vars.toArray(new Term[0]);
61: results = new ArrayList(resultNodes.size());
62: int resultNumber = 0;
63: List[] repls = new List[varArray.length];
64: for (int i = 0; i < repls.length; i++)
65: repls[i] = new ArrayList();
66: root.replace(repls, varArray);
67: resultNumber = repls.length == 0 ? 0 : repls[0].size();
68:
69: // DerivationNodeImpl proofRootNode = (new DerivationTreeBuilder()).buildTree(root);
70: for (int i = 0; i < resultNumber; i++) {
71: Replacement[] replacements = new Replacement[varArray.length];
72: for (int j = 0; j < varArray.length; j++) {
73: replacements[j] = new Replacement(varArray[j],
74: (Term) repls[j].get(i));
75: }
76: Result result = new Result(new Derivation(root, results
77: .size()), replacements);
78: results.add(result);
79: }
80: }
81: }
82: }
|