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.reference;
019:
020: import org.mandarax.kernel.ClauseSet;
021: import org.mandarax.kernel.InferenceEngine;
022: import org.mandarax.kernel.KnowledgeBase;
023: import org.mandarax.kernel.Query;
024:
025: /**
026: * Same as sample 5, but use a <code>JClauseSet</code> to build the <code>notEquals</code> dynamically.
027: * @see test.org.mandarax.reference.TestInferenceEngine6
028: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
029: * @version 3.4 <7 March 05>
030: * @since 1.1
031: */
032: public class TestInferenceEngine6 extends TestInferenceEngine {
033:
034: /**
035: * Constructor.
036: * @param aKnowledgeBase a new, uninitialized knowledge base that will be used
037: * @param anInferenceEngine the inference engine that will be tested
038: */
039: public TestInferenceEngine6(KnowledgeBase aKnowledgeBase,
040: InferenceEngine anInferenceEngine) {
041: super (aKnowledgeBase, anInferenceEngine);
042: }
043:
044: /**
045: * Add facts and rules to the knowledge base.
046: * @param knowledge org.mandarax.kernel.KnowledgeBase
047: */
048: public void feedKnowledgeBase(KnowledgeBase knowledge) {
049: knowledge.removeAll();
050:
051: // the actual inference rules
052: knowledge.add(Person.getRule("isGrandFatherOf", "x", "z",
053: "isFatherOf", "y", "z", "isFatherOf", "x", "y"));
054: knowledge.add(Person.getRule("isOncleOf", "x", "z",
055: "isFatherOf", "y", "z", "isBrotherOf", "x", "y"));
056: knowledge.add(Person.getRule("isOncleOf", "x", "z",
057: "isGrandFatherOf", "y", "z", "isBrotherOf", "x", "y"));
058: knowledge.add(Person.getRule("isGrandFatherOf", "z", "x",
059: "isFatherOf", "y", "x", "isFatherOf", "z", "y"));
060:
061: // rules defining predicates
062: knowledge.add(Person.getRule("isFatherOf", "x", "y", "equals",
063: "x", "father(y)"));
064: knowledge.add(Person.getRule("isBrotherOf", "x", "y", "equals",
065: "z", "father(y)", "equals", "z", "father(x)",
066: "equalsNot", "x", "y"));
067:
068: // facts
069: knowledge
070: .add(Person.getFact("equals", "Klaus", "father(Jens)"));
071: knowledge
072: .add(Person.getFact("equals", "Otto", "father(Klaus)"));
073: knowledge.add(Person.getFact("equals", "Otto", "father(Lutz)"));
074: knowledge.add(Person.getFact("equals", "Jens", "father(Max)"));
075:
076: // facts just loop and add for all possible combinations either an equal or equals not fact
077: java.util.Vector names = new java.util.Vector();
078:
079: names.addElement(Person.get("Max"));
080: names.addElement(Person.get("Jens"));
081: names.addElement(Person.get("Klaus"));
082: names.addElement(Person.get("Otto"));
083: names.addElement(Person.get("Lutz"));
084:
085: java.lang.reflect.Method equals = Person
086: .getPredicate("equalsNot");
087: java.util.Vector[] parSets = new java.util.Vector[2];
088:
089: parSets[0] = names;
090: parSets[1] = names;
091:
092: try {
093: ClauseSet cs = new org.mandarax.kernel.meta.JClauseSet(
094: new org.mandarax.kernel.meta.JPredicate(equals),
095: parSets);
096:
097: knowledge.add(cs);
098: } catch (Throwable t) {
099: System.err.println(t.getMessage());
100: }
101:
102: // knowledge.add(Person.getFact("isBrotherOf","Lutz","Klaus"));
103: }
104:
105: /**
106: * Get a description of this test case.
107: * This is used by the <code>org.mandarax.demo</code>
108: * package to display the test cases.
109: * @return a brief description of the test case
110: */
111: public String getDescription() {
112: return "Same as sample 6,\nbut use a JClauseSet to build the notEquals dynamically.";
113: }
114:
115: /**
116: * Get the name of the person we are looking for.
117: * More precisely, this is the name of the person expected to
118: * replace the query variable as a result of the query.
119: * @return the name of the person
120: */
121: String getPersonName() {
122: return "Lutz";
123: }
124:
125: /**
126: * Get the query.
127: * @return a query
128: */
129: public Query getQuery() {
130: return Person.getQuery("isOncleOf", QUERY_VARIABLE, "Max");
131: }
132: }
|