01: /*
02: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package test.org.mandarax.reference;
19:
20: import java.util.HashSet;
21: import java.util.Iterator;
22:
23: import org.mandarax.kernel.Clause;
24: import org.mandarax.kernel.ClauseSetException;
25: import org.mandarax.util.ClauseIterator;
26:
27: /**
28: * Test adding a fact to a knowledge base. Here we add a set of clauses
29: * and check whether the set of clauses returned by <code>clauses(Clause query,Object additionalParameter)</code>
30: * is the expected subset. Note that we compare instances of <code>HashSet</code>.
31: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
32: * @version 3.4 <7 March 05>
33: * @since 1.1
34: */
35: public class TestKnowledgeBaseAddClause2 extends TestKnowledgeBase {
36:
37: /**
38: * Constructor.
39: * @param aKnowledgeBase a knowledge base
40: */
41: public TestKnowledgeBaseAddClause2(
42: org.mandarax.kernel.KnowledgeBase aKnowledgeBase) {
43: super (aKnowledgeBase);
44: }
45:
46: /**
47: * Run the test.
48: */
49: public void testKnowledgeBase() {
50: kb.removeAll();
51:
52: // input knowledge
53: HashSet input = new HashSet();
54:
55: input.add(Person.getFact("isFatherOf", "Jens", "Max"));
56: input.add(Person.getFact("isFatherOf", "Klaus", "Jens"));
57: input.add(Person.getRule("isFatherOf", "x", "y", "isFatherOf",
58: "y", "x"));
59:
60: // add additional knowledge
61: kb.add(Person.getRule("isGrandFatherOf", "x", "z",
62: "isFatherOf", "y", "z", "isFatherOf", "x", "y"));
63:
64: // add the knowledge base
65: for (Iterator it = input.iterator(); it.hasNext();) {
66: kb.add((Clause) it.next());
67: }
68:
69: // add additional knowledge
70: kb.add(Person.getFact("isBrotherOf", "Lutz", "Klaus"));
71:
72: // collect output in a set
73: HashSet output = new HashSet();
74:
75: try {
76: for (ClauseIterator it = kb.clauses(Person.getFact(
77: "isFatherOf", "Jens", "Max"), null); it
78: .hasMoreClauses();) {
79: output.add(it.nextClause());
80: }
81:
82: // compare the two sets
83: assertTrue(input.equals(output));
84: } catch (ClauseSetException x) {
85: assertTrue(false);
86: }
87: }
88: }
|