001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">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:
019: package test.org.mandarax.jdbc;
020:
021: import java.io.*;
022: import org.mandarax.kernel.*;
023: import org.mandarax.reference.AdvancedKnowledgeBase;
024: import org.mandarax.util.*;
025: import org.mandarax.xkb.XKBManager;
026: import org.mandarax.xkb.framework.XKBDriver_2_1;
027: import org.mandarax.xkb.ruleml.RuleML0_8_1Driver;
028: import org.mandarax.zkb.ZKBManager;
029: import test.org.mandarax.testsupport.TestUtils;
030:
031: /**
032: * Utility class to create test knowledge bases.
033: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
034: * @version 3.3.2 <29 December 2004>
035: * @since 3.0
036: */
037:
038: public class TestKB {
039: static LogicFactorySupport lfs = new LogicFactorySupport();
040: static String XKB_FILE = TestUtils.getFileName("_family.xkb");
041: static String ZKB_FILE = TestUtils.getFileName("_family.zkb");
042: static String RULE_ML_FILE = TestUtils
043: .getFileName("_family.ruleml");
044: private static KnowledgeBase kb = null;
045: static boolean initialized = false;
046:
047: /**
048: * Main method - can be used to create the kbs.
049: */
050: public static void main(String[] args) throws Exception {
051: setup();
052: }
053:
054: /**
055: * Build the knowledge base.
056: * @return a knowledge base
057: */
058: static KnowledgeBase createKB() {
059: if (kb != null)
060: return kb;
061:
062: kb = new AdvancedKnowledgeBase();
063:
064: // create predicates
065: Class[] struct = { String.class, String.class };
066: Predicate predicate_is_brother = new SimplePredicate(
067: "is_brother_of", struct);
068: Predicate predicate_is_oncle = new SimplePredicate(
069: "is_oncle_of", struct);
070: Predicate predicate_is_son = new SimplePredicate("is_son_of",
071: struct);
072: Predicate predicate_is_grandfather = new SimplePredicate(
073: "is_grandfather_of", struct);
074: Predicate predicate_is_father = new SimplePredicate(
075: "is_father_of", struct);
076:
077: // add rules and facts
078:
079: Rule rule1 = lfs.rule(lfs.prereq(predicate_is_father, lfs
080: .variable("person 1"), lfs.variable("person 2")), lfs
081: .fact(predicate_is_son, lfs.variable("person 2"), lfs
082: .variable("person 1")));
083: kb.add(rule1);
084: Rule rule2 = lfs.rule(lfs.prereq(predicate_is_father, lfs
085: .variable("person 1"), lfs.variable("person 2")), lfs
086: .prereq(predicate_is_father, lfs.variable("person 2"),
087: lfs.variable("person 3")), lfs.fact(
088: predicate_is_grandfather, lfs.variable("person 1"), lfs
089: .variable("person 3")));
090: kb.add(rule2);
091: Rule rule3 = lfs
092: .rule(
093: lfs.prereq(predicate_is_father, lfs
094: .variable("person 1"), lfs
095: .variable("person 3")),
096: lfs.prereq(predicate_is_father, lfs
097: .variable("person 2"), lfs
098: .variable("person 3")),
099: lfs
100: .prereq(
101: org.mandarax.lib.text.StringArithmetic.NOT_EQUAL,
102: lfs.variable("person 1"), lfs
103: .variable("person 2")),
104: lfs.fact(predicate_is_brother, lfs
105: .variable("person 1"), lfs
106: .variable("person 2")));
107: kb.add(rule3);
108: Rule rule4 = lfs.rule(lfs.prereq(predicate_is_father, lfs
109: .variable("person 1"), lfs.variable("person 2")), lfs
110: .prereq(predicate_is_brother, lfs.variable("person 2"),
111: lfs.variable("person 3")), lfs.fact(
112: predicate_is_oncle, lfs.variable("person 1"), lfs
113: .variable("person 3")));
114: kb.add(rule4);
115: kb.add(lfs.fact(predicate_is_father, "Frank", "Lutz"));
116: kb.add(lfs.fact(predicate_is_father, "Guenther", "Otto"));
117: kb.add(lfs.fact(predicate_is_father, "Jens", "Klaus"));
118: kb.add(lfs.fact(predicate_is_father, "Klaus", "Otto"));
119: kb.add(lfs.fact(predicate_is_father, "Lutz", "Otto"));
120: kb.add(lfs.fact(predicate_is_father, "Max", "Jens"));
121: kb.add(lfs.fact(predicate_is_father, "Ralf", "Lutz"));
122: kb.add(lfs.fact(predicate_is_father, "Werner", "Otto"));
123:
124: return kb;
125: }
126:
127: /**
128: * Create the xkb file if it does not exist.
129: */
130: static void createXKB() throws Exception {
131: File file = new File(XKB_FILE);
132: if (!initialized || !file.exists()) {
133: KnowledgeBase kb = createKB();
134: XKBManager xkbManager = new XKBManager();
135: xkbManager.setDriver(new XKBDriver_2_1());
136: xkbManager.exportKnowledgeBase(file, kb);
137: }
138: }
139:
140: /**
141: * Create the ruleml file if it does not exist.
142: */
143: static void createRULE_ML() throws Exception {
144: File file = new File(RULE_ML_FILE);
145: if (!initialized || !file.exists()) {
146: KnowledgeBase kb = createKB();
147: XKBManager xkbManager = new XKBManager();
148: xkbManager.setDriver(new RuleML0_8_1Driver());
149: xkbManager.exportKnowledgeBase(file, kb);
150: }
151: }
152:
153: /**
154: * Create the zkb file if it does not exist.
155: */
156: static void createZKB() throws Exception {
157: File file = new File(ZKB_FILE);
158: if (!initialized || !file.exists()) {
159: KnowledgeBase kb = createKB();
160: ZKBManager zkbManager = new ZKBManager();
161: zkbManager.exportKnowledgeBase(file, kb);
162: }
163: }
164:
165: /**
166: * Setup - create all files.
167: */
168: public static void setup() throws Exception {
169: createXKB();
170: createZKB();
171: createRULE_ML();
172: initialized = true;
173: }
174: }
|