⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcut8.java

📁 Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规则
💻 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;


import org.mandarax.kernel.Fact;
import org.mandarax.kernel.InferenceEngine;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.Query;
import org.mandarax.kernel.ResultSet;
import org.mandarax.kernel.Rule;
import org.mandarax.kernel.SimplePredicate;

/**
 * Test case for cut. kb contributed by 
 * Alex Kozlenkov (A.Kozlenkov@city.ac.uk)
 * d(x,y) :- a(x,y)
 * d(u1,y) :- a(u1,y)
 * a(x,z) :- b(y),!,c(y,z)
 * a(x,y) :- b(x)
 * b(u1)
 * b(u2)
 * c(u1,u2)
 * c(u1,u3)
 * c(u2,u1).
 * The expected result is {(x,u2),(x,u3),(u1,y)}.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 2.1
 */
public class TestCut8 extends TestCut {
	/**
	 * @see test.org.mandarax.reference.cut.TestCut#feedKnowledgeBase(KnowledgeBase)
	 */
	public void feedKnowledgeBase(org.mandarax.kernel.KnowledgeBase knowledge) {
        Rule rule = lfs.rule(
                lfs.prereq(a(),new String("x"),lfs.variable("y",String.class)),
                lfs.fact(d(),new String("x"),lfs.variable("y",String.class))
                );
        kb.add(rule);
        rule = lfs.rule(
                lfs.prereq(a(),new String("u1"),lfs.variable("y",String.class)),
                lfs.fact(d(),new String("u1"),lfs.variable("y",String.class))
                );
        kb.add(rule);
        rule = lfs.rule(
                lfs.prereq(b(),lfs.variable("y",String.class)),cut(),                
				lfs.prereq(c(),lfs.variable("y",String.class),lfs.variable("z",String.class)),
                lfs.fact(a(),new String("x"),lfs.variable("z",String.class))
                );
        kb.add(rule);
        rule = lfs.rule(
                lfs.prereq(b(),lfs.variable("x",String.class)),
                lfs.fact(a(),lfs.variable("x",String.class),new String("y"))
                );
        kb.add(rule);
        kb.add(lfs.fact(b(),new String("u1")));
        kb.add(lfs.fact(b(),new String("u2")));
        kb.add(lfs.fact(c(),new String("u1"),new String("u2")));
        kb.add(lfs.fact(c(),new String("u1"),new String("u3")));
        kb.add(lfs.fact(c(),new String("u2"),new String("u1")));

	}
	/**
	 * Get the expected results.
	 * @return an array of expected results.
	 */
	protected String[] getExpectedResults() {
		return new String[]{"b"};
	}
	/**
	 * Convert the object to a string.
	 * @return a string
	 */
	public String toString() {
		return "Test case for cut 8";
	}
	/**
	 * Build a predicate.
	 * @return a predicate.
	 */
    private static SimplePredicate a() {
        Class[] struct = { String.class, String.class };
        SimplePredicate p = new SimplePredicate ("a", struct);
        return p;
    }
	/**
	 * Build a predicate.
	 * @return a predicate.
	 */
    private static SimplePredicate b() {
        Class[] struct = { String.class };
        SimplePredicate p = new SimplePredicate ("b", struct);
        return p;
    }
	/**
	 * Build a predicate.
	 * @return a predicate.
	 */
    private static SimplePredicate c() {
        Class[] struct = { String.class, String.class };
        SimplePredicate p = new SimplePredicate ("c", struct);
        return p;
    }
	/**
	 * Build a predicate.
	 * @return a predicate.
	 */
    private static SimplePredicate d() {
        Class[] struct = { String.class, String.class };
        SimplePredicate p = new SimplePredicate ("d", struct);
        return p;
    }
    
    /**
     * Run the test.
     */
    public void testInferenceEngine() {
        LOG_TEST.info ("Start Testcase " + getClass ().getName () + " , test method: " + "testInferenceEngine()");
        Fact queryfact = lfs.fact(d(),lfs.variable("X",String.class),lfs.variable("Y",String.class));
        Query query = lfs.query(queryfact,"enumerate state predicates");

        try {
            ResultSet rs = ie.query(query,kb,InferenceEngine.ALL,InferenceEngine.BUBBLE_EXCEPTIONS);
            while( rs.next() ) {
                System.out.println(rs.getResult(String.class,"X")+" "+rs.getResult(String.class,"Y"));
            }
            rs.close();
        }
        catch (InferenceException exc) {
            exc.printStackTrace(System.out);
        }
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -