logicfactorysupport.java
来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 879 行 · 第 1/3 页
JAVA
879 行
/*
* 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.util;
import java.util.List;
import java.util.Vector;
import org.mandarax.kernel.ComplexTerm;
import org.mandarax.kernel.ConstantTerm;
import org.mandarax.kernel.Fact;
import org.mandarax.kernel.Function;
import org.mandarax.kernel.LogicFactory;
import org.mandarax.kernel.Predicate;
import org.mandarax.kernel.Prerequisite;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.Term;
/**
* Utility class to facilitate creating rules, facts and terms.
* @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 LogicFactorySupport {
private LogicFactory lfactory = LogicFactory.getDefaultFactory ();
/**
* Constructor.
*/
public LogicFactorySupport() {
super ();
}
/**
* Constructor.
* @param aFactory the logic factory to be used
*/
public LogicFactorySupport(LogicFactory aFactory) {
super ();
}
/**
* Create a constant term.
* @return the constant term created
* @param obj the object
* @param type the type
*/
public ConstantTerm cons(Object obj, Class type) {
return lfactory.createConstantTerm (obj, type);
}
/**
* Create a complex term.
* See <code>toTerm</code> how the object is converted to a term.
* @return the complex term created
* @param function the function
* @param objs objects (will be interpreted as terms)
*/
public ComplexTerm cplx(Function function, Object[] objs) {
Class[] structure = function.getStructure ();
if(structure.length != objs.length) {
throw new IllegalArgumentException (
"Not the right number of terms for this function, the number is "
+ objs.length + ", but must be " + structure.length);
}
Term[] terms = new Term[structure.length];
for(int i = 0; i < structure.length; i++) {
terms[i] = toTerm (objs[i], structure[i]);
}
return lfactory.createComplexTerm (function, terms);
}
/**
* Create a complex term with one (sub)term.
* See <code>toTerm</code> how the object is converted to a term.
* @return the complex term created
* @param function the function
* @param obj an object
*/
public ComplexTerm cplx(Function function, Object obj1) {
Class[] structure = function.getStructure ();
if(structure.length != 1) {
throw new IllegalArgumentException (
"Not the right number of terms for this function, the number is 1, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj1, structure[0]) };
return lfactory.createComplexTerm (function, terms);
}
/**
* Create a complex term with two (sub)terms.
* See <code>toTerm</code> how the object is converted to a term.
* @return the complex term created
* @param function the function
* @param obj1 an object
* @param obj2 an object
*/
public ComplexTerm cplx(Function function, Object obj1, Object obj2) {
Class[] structure = function.getStructure ();
if(structure.length != 2) {
throw new IllegalArgumentException (
"Not the right number of terms for this function, the number is 2, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj1, structure[0]),
toTerm (obj2, structure[1]) };
return lfactory.createComplexTerm (function, terms);
}
/**
* Create a complex term with three (sub)terms.
* See <code>toTerm</code> how the object is converted to a term.
* @return the complex term created
* @param function the function
* @param obj1 an object
* @param obj2 an object
* @param obj3 an object
*/
public ComplexTerm cplx(Function function, Object obj1, Object obj2,
Object obj3) {
Class[] structure = function.getStructure ();
if(structure.length != 3) {
throw new IllegalArgumentException (
"Not the right number of terms for this function, the number is 3, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj1, structure[0]),
toTerm (obj2, structure[1]),
toTerm (obj3, structure[2]) };
return lfactory.createComplexTerm (function, terms);
}
/**
* Create a complex term with four (sub)terms.
* See <code>toTerm</code> how the object is converted to a term.
* @return the complex term created
* @param function the function
* @param obj1 an object
* @param obj2 an object
* @param obj3 an object
* @param obj4 an object
*/
public ComplexTerm cplx(Function function, Object obj1, Object obj2,
Object obj3, Object obj4) {
Class[] structure = function.getStructure ();
if(structure.length != 4) {
throw new IllegalArgumentException (
"Not the right number of terms for this function, the number is 4, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj1, structure[0]),
toTerm (obj2, structure[1]),
toTerm (obj3, structure[2]),
toTerm (obj4, structure[3]) };
return lfactory.createComplexTerm (function, terms);
}
/**
* Create a fact.
* See <code>toTerm</code> how the object is converted to a term.
* @return the fact created
* @param predicate the predicate
* @param objs objects (will be interpreted as terms)
*/
public Fact fact(Predicate predicate, Object[] objs) {
Class[] structure = predicate.getStructure ();
if(structure.length != objs.length) {
throw new IllegalArgumentException (
"Not the right number of terms for this predicate, the number is "
+ objs.length + ", but must be " + structure.length);
}
Term[] terms = new Term[structure.length];
for(int i = 0; i < structure.length; i++) {
terms[i] = toTerm (objs[i], structure[i]);
}
return lfactory.createFact (predicate, terms);
}
/**
* Create a fact with one term.
* See <code>toTerm</code> how the object is converted to a term.
* @return the fact created
* @param predicate the predicate
* @param obj an object
*/
public Fact fact(Predicate predicate, Object obj) {
Class[] structure = predicate.getStructure ();
if(structure.length != 1) {
throw new IllegalArgumentException (
"Not the right number of terms for this predicate, the number is 1, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj, structure[0]) };
return lfactory.createFact (predicate, terms);
}
/**
* Create a fact with two terms.
* See <code>toTerm</code> how the object is converted to a term.
* @return the fact created
* @param predicate the predicate
* @param obj1 an object
* @param obj2 an object
*/
public Fact fact(Predicate predicate, Object obj1, Object obj2) {
Class[] structure = predicate.getStructure ();
if(structure.length != 2) {
throw new IllegalArgumentException (
"Not the right number of terms for this predicate, the number is 2, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj1, structure[0]),
toTerm (obj2, structure[1]) };
return lfactory.createFact (predicate, terms);
}
/**
* Create a fact with three terms.
* See <code>toTerm</code> how the object is converted to a term.
* @return the fact created
* @param predicate the predicate
* @param obj1 an object
* @param obj2 an object
* @param obj3 an object
*/
public Fact fact(Predicate predicate, Object obj1, Object obj2, Object obj3) {
Class[] structure = predicate.getStructure ();
if(structure.length != 3) {
throw new IllegalArgumentException (
"Not the right number of terms for this predicate, the number is 3, but must be "
+ structure.length);
}
Term[] terms = { toTerm (obj1, structure[0]),
toTerm (obj2, structure[1]),
toTerm (obj3, structure[2]) };
return lfactory.createFact (predicate, terms);
}
/**
* Create a fact with four terms.
* See <code>toTerm</code> how the object is converted to a term.
* @return the fact created
* @param predicate the predicate
* @param obj1 an object
* @param obj2 an object
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?