📄 discountcalculator.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;
import org.mandarax.examples.crm.domainmodel.Customer;
import org.mandarax.examples.crm.domainmodel.Discount;
import org.mandarax.kernel.Derivation;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.KnowledgeBase;
import org.mandarax.kernel.Predicate;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.ResultSet;
import org.mandarax.reference.ResolutionInferenceEngine;
import org.mandarax.util.LogicFactorySupport;
/**
* The rule based disount calculator. Internally, the discount is calculated by issuing a query
* to the inference engine. This class encapsulates the inference engine and most of
* the mandarax logic, in a EJB context this would be a classic session bean.
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 1.2
*/
public class DiscountCalculator extends LogicFactorySupport {
private ResultSet result = null;
// the name of the variable we use in queries .. could be any string
private static final String QUERY_VARIABLE = "DISCOUNT?";
/**
* Constructor.
*/
public DiscountCalculator() {
super();
}
/**
* Calculate the discount for a customer c using the knowledge base kb.
* @param c the customer
* @param kb the knowledge base
*/
public void calculateDiscount(Customer c, KnowledgeBase kb) {
// build query
Predicate p = PredicatesAndFunctions.getPredicateGetDiscount();
Query query = query(fact(p, c, QUERY_VARIABLE),"get discount");
// issue query
InferenceEngine ie = new ResolutionInferenceEngine();
try {
result = ie.query(query, kb,InferenceEngine.ONE,InferenceEngine.BUBBLE_EXCEPTIONS);
result.next();
}
catch (InferenceException x) {
System.err.println("Inference Error, details:");
x.printStackTrace(System.err);
}
}
/**
* Get the derivation used to calculate the result
* @return the derivation used to calculate the discount
*/
public Derivation getDerivation() {
try {
return (result == null) ? null : (Derivation) result.getProof();
}
catch (InferenceException x) {
return null;
}
}
/**
* Get the calculated discount
* @return the calculated discount
*/
public Discount getDiscount() {
try {
return (result == null) ? null : (Discount) result.getResult(Discount.class,QUERY_VARIABLE);
}
catch (InferenceException x) {
return null;
}
}
/**
* Reset the calculator. Should be used between queries if the same instance is used
* for multiple queries.
*/
public void reset() {
result = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -