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 org.mandarax.kernel.Clause;
21: import org.mandarax.kernel.KnowledgeBaseChangeEvent;
22:
23: /**
24: * Test whether the appropriate events are fired when adding / removing clauses.
25: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
26: * @version 3.4 <7 March 05>
27: * @since 1.1
28: */
29: public class TestKnowledgeBaseEvents1 extends TestKnowledgeBase {
30:
31: /**
32: * Constructor.
33: * @param aKnowledgeBase a knowledge base
34: */
35: public TestKnowledgeBaseEvents1(
36: org.mandarax.kernel.KnowledgeBase aKnowledgeBase) {
37: super (aKnowledgeBase);
38: }
39:
40: /**
41: * Run the test.
42: */
43: public void testKnowledgeBase() {
44: kb.removeAll();
45:
46: Clause c1 = Person.getFact("isFatherOf", "Jens", "Max");
47: Clause c2 = Person.getRule("isGrandFatherOf", "x", "z",
48: "isFatherOf", "y", "z", "isFatherOf", "x", "y");
49:
50: // register listener
51: EventHandler handler = new EventHandler();
52:
53: kb.addKnowledgeBaseChangeListener(handler);
54:
55: // manipulate knowledge
56: kb.add(c1);
57: kb.add(c2);
58: kb.remove(c1);
59: kb.removeKnowledgeBaseChangeListener(handler);
60: kb.remove(c2);
61:
62: // now check the stack of events handled by the listener
63: int eventsSize = handler.handledEvents.size();
64: KnowledgeBaseChangeEvent e1 = (KnowledgeBaseChangeEvent) handler.handledEvents
65: .get(eventsSize - 1);
66: KnowledgeBaseChangeEvent e2 = (KnowledgeBaseChangeEvent) handler.handledEvents
67: .get(eventsSize - 2);
68: KnowledgeBaseChangeEvent e3 = (KnowledgeBaseChangeEvent) handler.handledEvents
69: .get(eventsSize - 3);
70:
71: // check events
72: boolean result = true;
73:
74: result = result
75: && (e1.getType() == KnowledgeBaseChangeEvent.CLAUSE_REMOVED)
76: && (e1.getSource() == kb)
77: && (e1.getAffectedClauses().size() == 1)
78: && e1.getAffectedClauses().contains(c1);
79: result = result
80: && (e2.getType() == KnowledgeBaseChangeEvent.CLAUSE_ADDED)
81: && (e2.getSource() == kb)
82: && (e2.getAffectedClauses().size() == 1)
83: && e2.getAffectedClauses().contains(c2);
84: result = result
85: && (e3.getType() == KnowledgeBaseChangeEvent.CLAUSE_ADDED)
86: && (e3.getSource() == kb)
87: && (e3.getAffectedClauses().size() == 1)
88: && e3.getAffectedClauses().contains(c1);
89:
90: assertTrue(result);
91: }
92: }
|