001: package org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.util.*;
021: import org.mandarax.kernel.*;
022: import org.mandarax.util.TermIterator;
023:
024: /**
025: * Class containing some useful utilities for inference engine implementations.
026: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
027: * @version 3.4 <7 March 05>
028: * @since 1.8
029: */
030: public class IEUtils {
031: /**
032: * Get an array of replacements for the variables passed in the query.
033: * @return an array of replacements
034: * @param node a derivation node
035: * @param query a query
036: */
037: public static Replacement[] getReplacements(
038: DerivationNodeImpl node, Clause query) {
039: List vars = getVars(query);
040: return getReplacements(node, vars);
041: }
042:
043: /**
044: * Get an array of replacements for the variables.
045: * @return an array of replacements
046: * @param node a derivation node
047: * @param vars a list of variables
048: */
049: public static Replacement[] getReplacements(
050: DerivationNodeImpl node, List vars) {
051: Replacement[] replacements = new Replacement[vars.size()];
052: for (int i = 0; i < replacements.length; i++) {
053: VariableTerm vt = (VariableTerm) vars.get(i);
054: replacements[i] = new Replacement(vt, node.replace(vt));
055: }
056: return replacements;
057: }
058:
059: /**
060: * Retrieve the variables from a query.
061: * @return a list of variables
062: * @param q a query
063: */
064: public static List getVars(Query q) {
065: return getVars(getGoal(q));
066: }
067:
068: /**
069: * Retrieve the variables from a clause.
070: * @return a list of variables
071: * @param c a clause
072: */
073: public static List getVars(Clause c) {
074: List vars = new ArrayList();
075:
076: // can we assume a fact here .. this could be critical when working with
077: // negation ! (perhaps introduce an interface Literal)
078: Fact nextLiteral = null;
079: Term next = null;
080:
081: for (Iterator itNeg = c.getNegativeLiterals().iterator(); itNeg
082: .hasNext();) {
083: nextLiteral = (Fact) itNeg.next();
084: for (TermIterator e = nextLiteral.terms(); e.hasMoreTerms();) {
085: next = e.nextTerm();
086: addVars(vars, next);
087: }
088: }
089:
090: for (Iterator itPos = c.getPositiveLiterals().iterator(); itPos
091: .hasNext();) {
092: nextLiteral = (Fact) itPos.next();
093:
094: for (TermIterator e = nextLiteral.terms(); e.hasMoreTerms();) {
095: next = e.nextTerm();
096: addVars(vars, next);
097: }
098: }
099:
100: return vars;
101: }
102:
103: /**
104: * Retrieve the variables from a clause.
105: * @return a list of variables
106: * @param c a clause
107: */
108: public static boolean containsVars(Fact f) {
109: Term[] terms = f.getTerms();
110: for (int i = 0; i < terms.length; i++) {
111: if (terms[i].containsVariables())
112: return true;
113: }
114:
115: return false;
116: }
117:
118: /**
119: * Retrieve the variables from a term and add them to the collection passed.
120: * @param vars a collection of variables
121: * @param term the term
122: */
123: public static void addVars(List vars, Term term) {
124: if (term.isVariable()) {
125: vars.add(term);
126: }
127: if (term.isCompound()) {
128: // next line can be improved .. we assume that any compound term is an instance of CompleTerm !!
129: // perhaps add terms() to the interface Term
130: ComplexTerm ct = (ComplexTerm) term;
131: for (TermIterator e = ct.terms(); e.hasMoreTerms();) {
132: addVars(vars, e.nextTerm());
133: }
134: }
135: }
136:
137: /**
138: * Build a goal from the query.
139: * @return a goal
140: * @param query a query
141: */
142: public static Clause getGoal(Query query) {
143: TmpClause nextClause = new TmpClause();
144: Fact[] facts = query.getFacts();
145: List lit = new ArrayList(facts.length);
146: for (int i = 0; i < facts.length; i++)
147: lit.add(facts[i]);
148: nextClause.negativeLiterals = lit;
149: nextClause.positiveLiterals = new ArrayList();
150: return nextClause;
151: }
152: }
|