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 java.text.DateFormat;
023: import java.util.*;
024: import org.mandarax.kernel.*;
025: import org.mandarax.reference.AdvancedKnowledgeBase;
026: import org.mandarax.util.*;
027: import org.mandarax.xkb.XKBManager;
028: import org.mandarax.xkb.framework.XKBDriver_2_1;
029: import org.mandarax.zkb.ZKBManager;
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 TestKB2 {
039: static LogicFactorySupport lfs = new LogicFactorySupport();
040: static String XKB_FILE = "_testkb2.xkb";
041: static String ZKB_FILE = "_testkb2.zkb";
042: private static KnowledgeBase kb = null;
043: static boolean initialized = false;
044: private static Predicate predicate1 = new SimplePredicate(
045: "predicate1", new Class[] { String.class,
046: java.sql.Date.class, Integer.class, Double.class });
047: private static DateFormat df = DateFormat.getDateInstance(
048: DateFormat.SHORT, Locale.US);
049:
050: /**
051: * Main method - can be used to create the kbs.
052: */
053: public static void main(String[] args) throws Exception {
054: setup();
055: }
056:
057: /**
058: * Build the knowledge base.
059: * @return a knowledge base
060: */
061: static KnowledgeBase createKB() throws Exception {
062: if (kb != null)
063: return kb;
064: kb = new AdvancedKnowledgeBase();
065: addFact("Max", df.parse("7.10.93"), 42, 1.50);
066: addFact("Xiomara", df.parse("19.07.00"), 15, 1.0);
067: addFact("Jens", df.parse("12.01.66"), 79, 1.80);
068: addFact("Yadira", df.parse("15.04.68"), 58, 1.65);
069: return kb;
070: }
071:
072: /**
073: * Create a fact in the kb.
074: */
075: static void addFact(String name, Date dob, int weight, double size) {
076: kb.add(lfs.fact(predicate1, new Term[] {
077: lfs.cons(name, String.class),
078: lfs.cons(dob, Date.class),
079: lfs.cons(new Integer(weight), Integer.class),
080: lfs.cons(new Double(size), Double.class) }));
081: }
082:
083: /**
084: * Create the xkb file if it does not exist.
085: */
086: static void createXKB() throws Exception {
087: File file = new File(XKB_FILE);
088: if (!initialized || !file.exists()) {
089: KnowledgeBase kb = createKB();
090: XKBManager xkbManager = new XKBManager();
091: xkbManager.setDriver(new XKBDriver_2_1());
092: xkbManager.exportKnowledgeBase(file, kb);
093: }
094: }
095:
096: /**
097: * Create the zkb file if it does not exist.
098: */
099: static void createZKB() throws Exception {
100: File file = new File(ZKB_FILE);
101: if (!initialized || !file.exists()) {
102: KnowledgeBase kb = createKB();
103: ZKBManager zkbManager = new ZKBManager();
104: zkbManager.exportKnowledgeBase(file, kb);
105: }
106: }
107:
108: /**
109: * Setup - create all files,
110: */
111: static void setup() throws Exception {
112: createXKB();
113: createZKB();
114: initialized = true;
115: }
116: }
|