package jess; import java.io.Serializable; import java.util.Map; /** ********************************************************************** * Pattern represents a single conditional element on a rule LHS. * A Pattern consists mainly of a two-dimensional array of Test1 structures. * Each Test1 contains information about a specific characteristic of a slot. * *

* (C) 1997 Ernest J. Friedman-Hill and Sandia National Laboratories * @author Ernest J. Friedman-Hill */ public class Pattern implements ConditionalElement, LHSComponent, Serializable, Visitable { /** The deftemplate corresponding to this pattern */ private Deftemplate m_deft; private static final int INITSIZE = 1; public Object clone() { try { Pattern p = (Pattern) super.clone(); if (m_slotLengths != null) p.m_slotLengths = (int[]) m_slotLengths.clone(); if (m_tests != null) p.m_tests = (Test1[][]) m_tests.clone(); for (int i=0; i 0 && m_tests[idx][j-1].m_subIdx > aTest.m_subIdx) throw new JessException("Pattern.addTest", "Attempt to add out-of-order test: index ", m_tests[idx][j-1].m_subIdx + " > " + aTest.m_subIdx); m_tests[idx][j] = aTest; } void replaceTests(int slotIndex, Test1[] theTests) { m_tests[slotIndex] = theTests; } /* Add the name of every variable that is "directly positively matched" in this pattern to the Map as a key/value pair. If a name is already there, that's OK. */ public void addDirectlyMatchedVariables(Map map) throws JessException { for (int i=0; i< getNSlots(); i++) { for (int j=0; j< getNTests(i); j++) { Test1 test = getTest(i, j); Value val = test.m_slotValue; boolean eq = (test.m_test == Test1.EQ); if (val instanceof Variable && eq) { String name = val.variableValue(null); map.put(name, name); } } } if (getBoundName() != null) map.put(getBoundName(), getBoundName()); } // TODO V&V public void renameUnmentionedVariables(Map map, Map substitutes, int size, HasLHS unused) throws JessException { String preFix = "_" + size + "_"; for (int i=0; i< getNSlots(); i++) { for (int j=0; j< getNTests(i); j++) { Test1 test = getTest(i, j); Value val = test.m_slotValue; boolean eq = (test.m_test == Test1.EQ); if (val instanceof Variable && eq) { String name = val.variableValue(null); if (map.get(name) == null && !name.startsWith(preFix)) { String sub; if (substitutes.get(name) == null) { sub = preFix + name; substitutes.put(name, sub); } else sub = (String) substitutes.get(name); test.m_slotValue = new Variable(sub, val.type()); } } } } substituteVariableNamesInFuncalls(substitutes); } private void substituteVariableNamesInFuncalls(Map substitutes) throws JessException { for (int i=0; i< getNSlots(); i++) { if (getNTests(i) == 0) continue; for (int j=0; j< getNTests(i); j++) { Value val = getTest(i, j).m_slotValue; if (val.type() == RU.FUNCALL) substFuncall(val.funcallValue(null), substitutes); } } } private void substFuncall(Funcall f, Map substitutes) throws JessException { for (int i=1; i 0) throw new IllegalArgumentException(); else return this; } public boolean equals(Object o) { if (! (o instanceof Pattern)) return false; Pattern p = (Pattern) o; if (!getName().equals(p.getName())) return false; if (m_negated != p.m_negated) return false; for (int i=0; i