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 org.mandarax.examples.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:
030: /**
031: * Utility class to create test knowledge bases.
032: * This is a slightly modified copy from a class in the test package!
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: class DemoKB {
039: static LogicFactorySupport lfs = new LogicFactorySupport();
040: static String XKB_FILE = "example-family.xkb";
041: static String ZKB_FILE = "example-family.zkb";
042: static String RULE_ML_FILE = "example-family.ruleml";
043: private static KnowledgeBase kb = null;
044: static boolean initialized = false;
045:
046: /**
047: * Main method - can be used to create the kbs.
048: */
049: public static void main(String[] args) throws Exception {
050: setup();
051: }
052:
053: /**
054: * Build the knowledge base.
055: * @return a knowledge base
056: */
057: static KnowledgeBase createKB() {
058: if (kb != null)
059: return kb;
060:
061: kb = new AdvancedKnowledgeBase();
062:
063: // create predicates
064: Class[] struct = { String.class, String.class };
065: Predicate predicate_is_brother = new SimplePredicate(
066: "is_brother_of", struct);
067: Predicate predicate_is_oncle = new SimplePredicate(
068: "is_oncle_of", struct);
069: Predicate predicate_is_son = new SimplePredicate("is_son_of",
070: struct);
071: Predicate predicate_is_grandfather = new SimplePredicate(
072: "is_grandfather_of", struct);
073: Predicate predicate_is_father = new SimplePredicate(
074: "is_father_of", struct);
075:
076: predicate_is_father
077: .setSlotNames(new String[] { "father", "son" });
078: predicate_is_grandfather.setSlotNames(new String[] {
079: "grandchild", "grandfather" });
080: predicate_is_son.setSlotNames(new String[] { "father", "son" });
081: predicate_is_brother.setSlotNames(new String[] { "brother1",
082: "brother2" });
083: predicate_is_oncle.setSlotNames(new String[] { "nephew",
084: "oncle" });
085: // add rules and facts
086:
087: Rule rule1 = lfs.rule(lfs.prereq(predicate_is_father, lfs
088: .variable("person 1"), lfs.variable("person 2")), lfs
089: .fact(predicate_is_son, lfs.variable("person 2"), lfs
090: .variable("person 1")));
091: kb.add(rule1);
092: Rule rule2 = lfs.rule(lfs.prereq(predicate_is_father, lfs
093: .variable("person 1"), lfs.variable("person 2")), lfs
094: .prereq(predicate_is_father, lfs.variable("person 2"),
095: lfs.variable("person 3")), lfs.fact(
096: predicate_is_grandfather, lfs.variable("person 1"), lfs
097: .variable("person 3")));
098: kb.add(rule2);
099: Rule rule3 = lfs
100: .rule(
101: lfs.prereq(predicate_is_father, lfs
102: .variable("person 1"), lfs
103: .variable("person 3")),
104: lfs.prereq(predicate_is_father, lfs
105: .variable("person 2"), lfs
106: .variable("person 3")),
107: lfs
108: .prereq(
109: org.mandarax.lib.text.StringArithmetic.NOT_EQUAL,
110: lfs.variable("person 1"), lfs
111: .variable("person 2")),
112: lfs.fact(predicate_is_brother, lfs
113: .variable("person 1"), lfs
114: .variable("person 2")));
115: kb.add(rule3);
116: Rule rule4 = lfs.rule(lfs.prereq(predicate_is_father, lfs
117: .variable("person 1"), lfs.variable("person 2")), lfs
118: .prereq(predicate_is_brother, lfs.variable("person 2"),
119: lfs.variable("person 3")), lfs.fact(
120: predicate_is_oncle, lfs.variable("person 1"), lfs
121: .variable("person 3")));
122: kb.add(rule4);
123: kb.add(lfs.fact(predicate_is_father, "Frank", "Lutz"));
124: kb.add(lfs.fact(predicate_is_father, "Guenther", "Otto"));
125: kb.add(lfs.fact(predicate_is_father, "Jens", "Klaus"));
126: kb.add(lfs.fact(predicate_is_father, "Klaus", "Otto"));
127: kb.add(lfs.fact(predicate_is_father, "Lutz", "Otto"));
128: kb.add(lfs.fact(predicate_is_father, "Max", "Jens"));
129: kb.add(lfs.fact(predicate_is_father, "Ralf", "Lutz"));
130: kb.add(lfs.fact(predicate_is_father, "Werner", "Otto"));
131:
132: return kb;
133: }
134:
135: /**
136: * Create the xkb file if it does not exist.
137: */
138: static void createXKB() throws Exception {
139: File file = new File(XKB_FILE);
140: if (!initialized || !file.exists()) {
141: KnowledgeBase kb = createKB();
142: XKBManager xkbManager = new XKBManager();
143: xkbManager.setDriver(new XKBDriver_2_1());
144: xkbManager.exportKnowledgeBase(file, kb);
145: }
146: }
147:
148: /**
149: * Create the ruleml file if it does not exist.
150: */
151: static void createRULE_ML() throws Exception {
152: File file = new File(RULE_ML_FILE);
153: if (!initialized || !file.exists()) {
154: KnowledgeBase kb = createKB();
155: XKBManager xkbManager = new XKBManager();
156: xkbManager.setDriver(new RuleML0_8_1Driver());
157: xkbManager.exportKnowledgeBase(file, kb);
158: }
159: }
160:
161: /**
162: * Create the zkb file if it does not exist.
163: */
164: static void createZKB() throws Exception {
165: File file = new File(ZKB_FILE);
166: if (!initialized || !file.exists()) {
167: KnowledgeBase kb = createKB();
168: ZKBManager zkbManager = new ZKBManager();
169: zkbManager.exportKnowledgeBase(file, kb);
170: }
171: }
172:
173: /**
174: * Setup - create all files.
175: */
176: public static void setup() throws Exception {
177: createXKB();
178: createZKB();
179: createRULE_ML();
180: initialized = true;
181: }
182: }
|