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.Fact;
021: import org.mandarax.kernel.InferenceEngine;
022: import org.mandarax.kernel.InferenceException;
023: import org.mandarax.kernel.Query;
024: import org.mandarax.kernel.ResultSet;
025: import org.mandarax.kernel.Rule;
026: import org.mandarax.kernel.SimplePredicate;
027:
028: /**
029: * Test case for cut. kb contributed by
030: * Alex Kozlenkov (A.Kozlenkov@city.ac.uk)
031: * d(x,y) :- a(x,y)
032: * d(u1,y) :- a(u1,y)
033: * a(x,z) :- b(y),!,c(y,z)
034: * a(x,y) :- b(x)
035: * b(u1)
036: * b(u2)
037: * c(u1,u2)
038: * c(u1,u3)
039: * c(u2,u1).
040: * The expected result is {(x,u2),(x,u3),(u1,y)}.
041: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
042: * @version 3.4 <7 March 05>
043: * @since 2.1
044: */
045: public class TestCut8 extends TestCut {
046: /**
047: * @see test.org.mandarax.reference.cut.TestCut#feedKnowledgeBase(KnowledgeBase)
048: */
049: public void feedKnowledgeBase(
050: org.mandarax.kernel.KnowledgeBase knowledge) {
051: Rule rule = lfs.rule(lfs.prereq(a(), new String("x"), lfs
052: .variable("y", String.class)), lfs.fact(d(),
053: new String("x"), lfs.variable("y", String.class)));
054: kb.add(rule);
055: rule = lfs.rule(lfs.prereq(a(), new String("u1"), lfs.variable(
056: "y", String.class)), lfs.fact(d(), new String("u1"),
057: lfs.variable("y", String.class)));
058: kb.add(rule);
059: rule = lfs.rule(lfs
060: .prereq(b(), lfs.variable("y", String.class)), cut(),
061: lfs.prereq(c(), lfs.variable("y", String.class), lfs
062: .variable("z", String.class)), lfs.fact(a(),
063: new String("x"), lfs
064: .variable("z", String.class)));
065: kb.add(rule);
066: rule = lfs.rule(lfs
067: .prereq(b(), lfs.variable("x", String.class)), lfs
068: .fact(a(), lfs.variable("x", String.class), new String(
069: "y")));
070: kb.add(rule);
071: kb.add(lfs.fact(b(), new String("u1")));
072: kb.add(lfs.fact(b(), new String("u2")));
073: kb.add(lfs.fact(c(), new String("u1"), new String("u2")));
074: kb.add(lfs.fact(c(), new String("u1"), new String("u3")));
075: kb.add(lfs.fact(c(), new String("u2"), new String("u1")));
076:
077: }
078:
079: /**
080: * Get the expected results.
081: * @return an array of expected results.
082: */
083: protected String[] getExpectedResults() {
084: return new String[] { "b" };
085: }
086:
087: /**
088: * Convert the object to a string.
089: * @return a string
090: */
091: public String toString() {
092: return "Test case for cut 8";
093: }
094:
095: /**
096: * Build a predicate.
097: * @return a predicate.
098: */
099: private static SimplePredicate a() {
100: Class[] struct = { String.class, String.class };
101: SimplePredicate p = new SimplePredicate("a", struct);
102: return p;
103: }
104:
105: /**
106: * Build a predicate.
107: * @return a predicate.
108: */
109: private static SimplePredicate b() {
110: Class[] struct = { String.class };
111: SimplePredicate p = new SimplePredicate("b", struct);
112: return p;
113: }
114:
115: /**
116: * Build a predicate.
117: * @return a predicate.
118: */
119: private static SimplePredicate c() {
120: Class[] struct = { String.class, String.class };
121: SimplePredicate p = new SimplePredicate("c", struct);
122: return p;
123: }
124:
125: /**
126: * Build a predicate.
127: * @return a predicate.
128: */
129: private static SimplePredicate d() {
130: Class[] struct = { String.class, String.class };
131: SimplePredicate p = new SimplePredicate("d", struct);
132: return p;
133: }
134:
135: /**
136: * Run the test.
137: */
138: public void testInferenceEngine() {
139: LOG_TEST.info("Start Testcase " + getClass().getName()
140: + " , test method: " + "testInferenceEngine()");
141: Fact queryfact = lfs.fact(d(), lfs.variable("X", String.class),
142: lfs.variable("Y", String.class));
143: Query query = lfs
144: .query(queryfact, "enumerate state predicates");
145:
146: try {
147: ResultSet rs = ie.query(query, kb, InferenceEngine.ALL,
148: InferenceEngine.BUBBLE_EXCEPTIONS);
149: while (rs.next()) {
150: System.out.println(rs.getResult(String.class, "X")
151: + " " + rs.getResult(String.class, "Y"));
152: }
153: rs.close();
154: } catch (InferenceException exc) {
155: exc.printStackTrace(System.out);
156: }
157: }
158:
159: }
|