📄 script2generatekb.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 org.mandarax.examples.crm.scripts;
import org.mandarax.examples.crm.KBLoader;
import org.mandarax.examples.crm.PredicatesAndFunctions;
import org.mandarax.examples.crm.domainmodel.Category;
import org.mandarax.examples.crm.domainmodel.Customer;
import org.mandarax.examples.crm.domainmodel.Discount;
import org.mandarax.examples.crm.domainmodel.KindOfPayment;
import org.mandarax.kernel.*;
import org.mandarax.kernel.validation.TestCase;
import org.mandarax.lib.math.DoubleArithmetic;
import org.mandarax.reference.AdvancedKnowledgeBase;
import org.mandarax.util.*;
import org.mandarax.zkb.ZKBManager;
import java.io.*;
/**
* Executable utility class to generate the knowledge base and save it.
* This is usually done using a GUI application such as Oryx !
* This class showas how to create a kb manually and how to save it using ZKB.
* For storage we use a folder. If a file (not a folder) is passed to ZKB, the kb
* would be saved as a zip archive.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 3.4
*/
public class Script2GenerateKB {
private static LogicFactorySupport lfs = new LogicFactorySupport();
/**
* Constructor.
*/
public Script2GenerateKB() {
super();
}
/**
* Run the script.
* @param arg runtime parameters
*/
public static void main(String[] args) throws Exception {
File folder = KBLoader.KB_FOLDER;
if (!folder.exists()) {
folder.mkdir();
System.out.println("Created folder for example: " + folder);
}
else if (!folder.isDirectory()) System.out.println("This should be a folder: " + folder);
KnowledgeBase kb = buildKB();
// this is the actual line to save the kb
new ZKBManager().exportKnowledgeBase(folder,kb);
System.out.println("KB built and exported to: " + folder.getAbsolutePath());
}
/**
* Get the sample knowledge base. The knowledge base is hardcoded here,
* but should actually come from a data source.
* @return a knowledge base
*/
public static KnowledgeBase buildKB() {
KnowledgeBase kb = new AdvancedKnowledgeBase ();
// useful variable .. the name of a customer type variable
String c = "a customer";
// do lazy initialization
kb = new AdvancedKnowledgeBase ();
Rule rule1 = lfs.rule (
lfs.prereq (DoubleArithmetic.GREATER_THAN,
lfs.cplx (PredicatesAndFunctions.getFunctionTurnoverForKindOfPayment (), c, new Integer (12), KindOfPayment.COMPANY_VISA),
new Double (200)
),
lfs.fact (PredicatesAndFunctions.getPredicateHasCategory (),
c,
Category.PLATINUM
)
);
Rule rule2 = lfs.rule (
lfs.prereq (DoubleArithmetic.GREATER_THAN,
lfs.cplx (PredicatesAndFunctions.getFunctionTurnover (), c, new Integer (12)),
new Double (250)
),
lfs.fact (PredicatesAndFunctions. getPredicateHasCategory (), c, Category.GOLD)
);
Rule rule3 = lfs.rule (
lfs.prereq (PredicatesAndFunctions.getPredicateHasCategory (), c, Category.PLATINUM),
lfs.fact (PredicatesAndFunctions.getPredicateGetDiscount (), c, new Discount (12.5))
);
Rule rule4 = lfs.rule (
lfs.prereq (PredicatesAndFunctions.getPredicateHasCategory (), c, Category.GOLD),
lfs.fact (PredicatesAndFunctions.getPredicateGetDiscount (), c, new Discount (7.5))
);
Rule rule5 = lfs.rule (
lfs.prereq (DoubleArithmetic.GREATER_THAN,
lfs.cplx (PredicatesAndFunctions.getFunctionTurnover (), c, new Integer (12)),
new Double (100)
),
lfs.fact (PredicatesAndFunctions.getPredicateGetDiscount (), c, new Discount (5))
);
// last rule is actually a fact! .. by default, each customer deserves a discount (to make him or her feel good and sell more .. that's business logic)
Fact rule6 = lfs.fact (PredicatesAndFunctions.getPredicateGetDiscount (), c,new Discount (2.5));
Predicate[] predicates = { DoubleArithmetic.GREATER_THAN };
kb.add (rule1);
kb.add (rule2);
kb.add (rule3);
kb.add (rule4);
kb.add (rule5);
kb.add (rule6);
// aliases - we use the general purpose properties interface to associate english sentences with rules
// an alternative approach would be to generate them, see Oryx for details on how this can be done
rule1.setProperty("alias","if the turnover of a customer in the last 12 months payed with COMPANY VISA is more than US$ 200.00 then a customer has customer category Platinum");
rule2.setProperty("alias","if the turnover of a customer in the last 12 months is more than US$ 250.00 then a customer has customer category Gold");
rule3.setProperty("alias","if a customer has customer category Platinum then a customer gets a discount of 12.5%");
rule4.setProperty("alias","if a customer has customer category Gold then a customer gets a discount of 7.5%");
rule5.setProperty("alias","if the turnover of a customer in the last 12 months is more than US$ 100.00 then a customer gets a discount of 5.0%");
rule6.setProperty("alias","each customer gets a discount of 2.5%");
// some test cases
Customer customer1 = new Customer(42,"John","Smith");
Fact[] assumptions1 = {
lfs.fact(PredicatesAndFunctions.getPredicateHasCategory (), customer1, Category.PLATINUM)
};
Query query1 = lfs.query(
lfs.fact (PredicatesAndFunctions.getPredicateGetDiscount (), customer1, new Discount (12.5)),
"Test whether platinum customers get 12.5 % discount"
);
TestCase tc1 = LogicFactory.getDefaultFactory().createTestCase(query1,assumptions1,TestCase.AT_BOTTOM,true);
kb.addTestCase(tc1);
// test2 relies on the fact that the dummy method Customer#getTurnover returns 1000 by default
Customer customer2 = new Customer(42,"John","Smith");
Fact[] assumptions2 = {};
Query query2 = lfs.query(
lfs.fact (PredicatesAndFunctions.getPredicateHasCategory (), customer2, Category.PLATINUM),
"Test the condition for a PLATINUM customer"
);
TestCase tc2 = LogicFactory.getDefaultFactory().createTestCase(query2,assumptions2,TestCase.AT_BOTTOM,true);
kb.addTestCase(tc2);
return kb;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -