001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package test.org.mandarax.comparators;
019:
020: import java.util.Comparator;
021: import java.util.List;
022:
023: import junit.framework.TestCase;
024:
025: import org.mandarax.kernel.ClauseSet;
026: import org.mandarax.kernel.Fact;
027: import org.mandarax.kernel.Predicate;
028: import org.mandarax.kernel.Rule;
029: import org.mandarax.kernel.SimplePredicate;
030: import org.mandarax.reference.AdvancedKnowledgeBase;
031: import org.mandarax.util.LogicFactorySupport;
032: import org.mandarax.util.logging.LogCategories;
033:
034: /**
035: * An abstract test case class for testing the default comparator.
036: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037: * @version 3.4 <7 March 05>
038: * @since 2.2
039: */
040: public abstract class ComparatorTest extends TestCase implements
041: LogCategories {
042:
043: protected org.mandarax.kernel.ExtendedKnowledgeBase kb = new AdvancedKnowledgeBase();
044: protected static LogicFactorySupport lfs = new LogicFactorySupport();
045:
046: // some predicates
047: private static Predicate A = new SimplePredicate("A", new Class[] {
048: Integer.class, Integer.class });
049: private static Predicate B = new SimplePredicate("B", new Class[] {
050: Integer.class, Integer.class });
051: private static Predicate C = new SimplePredicate("C", new Class[] {
052: Integer.class, Integer.class });
053:
054: // facts with 0,1 and 2 variables
055: protected static Fact F0 = lfs.fact(A, new Integer(1), new Integer(
056: 2));
057: protected static Fact F1 = lfs.fact(A, lfs.variable("x",
058: Integer.class), new Integer(2));
059: protected static Fact F2 = lfs.fact(A, lfs.variable("x",
060: Integer.class), lfs.variable("y", Integer.class));
061:
062: // some rules
063:
064: // 1 prerequisite, 4 variables (occurrences), no negated prerequisite
065: protected static Rule R0 = lfs.rule(lfs.prereq(B, lfs.variable("x",
066: Integer.class), lfs.variable("y", Integer.class)), lfs
067: .fact(A, lfs.variable("y", Integer.class), lfs.variable(
068: "x", Integer.class)));
069: // 2 prerequisites, 6 variables (occurrences), no negated prerequisite
070: protected static Rule R1 = lfs.rule(lfs.prereq(B, lfs.variable("x",
071: Integer.class), lfs.variable("y", Integer.class)), lfs
072: .prereq(C, lfs.variable("y", Integer.class), lfs.variable(
073: "z", Integer.class)), lfs.fact(A, lfs.variable("x",
074: Integer.class), lfs.variable("z", Integer.class)));
075: // 2 prerequisites, 6 variables (occurrences), 1 prerequisite is negated
076: protected static Rule R2 = lfs.rule(lfs.prereq(C, lfs.variable("x",
077: Integer.class), lfs.variable("y", Integer.class)), lfs
078: .prereq(B, lfs.variable("y", Integer.class), lfs.variable(
079: "y", Integer.class), true), lfs.fact(A, lfs
080: .variable("x", Integer.class), lfs.variable("y",
081: Integer.class)));
082:
083: /**
084: * Constructor.
085: * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
086: */
087: public ComparatorTest() {
088: super ("testComparator");
089: }
090:
091: /**
092: * Get a description of this test case.
093: * This is used by the <code>org.mandarax.demo</code>
094: * package to display the test cases.
095: * @return java.lang.String
096: */
097: public String getDescription() {
098: return "test case " + getClass().getName();
099: }
100:
101: /**
102: * Sets up the fixture.
103: */
104: protected void setUp() {
105: ClauseSet[] clauses = getInputOrder();
106: for (int i = 0; i < clauses.length; i++) {
107: kb.add(clauses[i]);
108: }
109: kb.setComparator(getComparator());
110: }
111:
112: /**
113: * Get the expected order.
114: * @return an array of clause sets
115: */
116: protected abstract ClauseSet[] getExpectedOrder();
117:
118: /**
119: * Get the input order.
120: * @return an array of clause sets
121: */
122: protected abstract ClauseSet[] getInputOrder();
123:
124: /**
125: * Get the comparator.
126: * @return a comparator
127: */
128: protected abstract Comparator getComparator();
129:
130: /**
131: * Run the test.
132: */
133: public void testComparator() {
134: LOG_TEST.info("Start Testcase " + getClass().getName()
135: + " , test method: " + "testClauses()");
136:
137: List clauses = kb.getClauseSets(A);
138: ClauseSet[] expected = getExpectedOrder();
139: for (int i = 0; i < clauses.size(); i++) {
140: if (clauses.get(i) != expected[i]) {
141: LOG_TEST.info("Clause set expected at position " + i
142: + " is " + expected[i] + " but is "
143: + clauses.get(i));
144: assertTrue(false);
145: }
146: }
147: assertTrue(true);
148: }
149:
150: }
|