📄 testkb.java
字号:
/*
* Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package test.org.mandarax.reference.derivationeventlisteners;
import org.mandarax.kernel.*;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.util.*;
import test.org.mandarax.testsupport.TestUtils;
/**
* Utility class to create test knowledge bases.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.2
*/
public class TestKB {
static LogicFactorySupport lfs = new LogicFactorySupport();
private static KnowledgeBase kb = null;
// constants
public static final String QUERY_NAME1 = "search for grandfather - grandson pairs";
public static final VariableTerm QUERY_VAR_1_2 = (VariableTerm)lfs.variable("grandfather",String.class);
public static final VariableTerm QUERY_VAR_1_1 = (VariableTerm)lfs.variable("grandson",String.class);
// the expected results (var replacements) when running the query
public static final String[] EXPECTED_VALUES_1_1 = {"Frank","Jens","Max","Ralf"};
public static final String[] EXPECTED_VALUES_1_2 = {"Otto","Otto","Klaus","Otto"};
/**
* Build the knowledge base.
* @return a knowledge base
*/
static KnowledgeBase createKB() {
if (kb!=null) return kb;
kb = new AdvancedKnowledgeBase();
// create predicates
Class[] struct = { String.class, String.class };
Predicate predicate_is_brother = new SimplePredicate("is_brother_of", struct);
Predicate predicate_is_oncle = new SimplePredicate("is_oncle_of", struct);
Predicate predicate_is_son = new SimplePredicate("is_son_of", struct);
Predicate predicate_is_grandfather = new SimplePredicate("is_grandfather_of", struct);
Predicate predicate_is_father = new SimplePredicate("is_father_of", struct);
// add rules and facts
Rule rule1 = lfs.rule(lfs.prereq(predicate_is_father, lfs.variable("person 1"), lfs.variable("person 2")), lfs.fact(predicate_is_son, lfs.variable("person 2"), lfs.variable("person 1")));
kb.add(rule1);
Rule rule2 =
lfs.rule(
lfs.prereq(predicate_is_father, lfs.variable("person 1"), lfs.variable("person 2")),
lfs.prereq(predicate_is_father, lfs.variable("person 2"), lfs.variable("person 3")),
lfs.fact(predicate_is_grandfather, lfs.variable("person 1"), lfs.variable("person 3")));
kb.add(rule2);
Rule rule3 =
lfs.rule(
lfs.prereq(predicate_is_father, lfs.variable("person 1"), lfs.variable("person 3")),
lfs.prereq(predicate_is_father, lfs.variable("person 2"), lfs.variable("person 3")),
lfs.prereq(org.mandarax.lib.text.StringArithmetic.NOT_EQUAL, lfs.variable("person 1"), lfs.variable("person 2")),
lfs.fact(predicate_is_brother, lfs.variable("person 1"), lfs.variable("person 2")));
kb.add(rule3);
Rule rule4 =
lfs.rule(
lfs.prereq(predicate_is_father, lfs.variable("person 1"), lfs.variable("person 2")),
lfs.prereq(predicate_is_brother, lfs.variable("person 2"), lfs.variable("person 3")),
lfs.fact(predicate_is_oncle, lfs.variable("person 1"), lfs.variable("person 3")));
kb.add(rule4);
kb.add(lfs.fact(predicate_is_father,"Frank", "Lutz"));
kb.add(lfs.fact(predicate_is_father,"Guenther", "Otto"));
kb.add(lfs.fact(predicate_is_father,"Jens", "Klaus"));
kb.add(lfs.fact(predicate_is_father,"Klaus", "Otto"));
kb.add(lfs.fact(predicate_is_father,"Lutz", "Otto"));
kb.add(lfs.fact(predicate_is_father,"Max", "Jens"));
kb.add(lfs.fact(predicate_is_father,"Ralf", "Lutz"));
kb.add(lfs.fact(predicate_is_father,"Werner", "Otto"));
Query query = lfs.query(
lfs.fact(
predicate_is_grandfather,
QUERY_VAR_1_1,
QUERY_VAR_1_2
),
QUERY_NAME1
);
kb.addQuery(query);
return kb;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -