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

📄 solve_b_nfg_t.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/* $Id: Solve_B_NFG_T.java,v 1.4 2004/02/05 23:29:00 giuli Exp $*/
/*
 * The contents of this file are subject to the OAA Community Research
 * License Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License
 * at http://www.ai.sri.com/~oaa/.  Software distributed under the License
 * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * rights and limitations under the License.  Portions of the software are
 * Copyright (c) SRI International, 1999.  All rights reserved.
 * "OAA" is a registered trademark, and "Open Agent Architecture" is a
 * trademark, of SRI International, a California nonprofit public benefit
 * corporation.
 */
package com.sri.oaa2.test.client;

import com.sri.oaa2.icl.IclInt;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclStruct;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.test.agent.OaaTest_T;
import com.sri.oaa2.test.agent.TestAgent;
import com.sri.oaa2.agentlib.DirectConnectFailedException;
import junit.framework.TestCase;

import java.util.Iterator;

/**
 * Before executing this test, the add agent should be run listening on the
 * local host on port 4012. From the test directory, run:
 * <pre><code>
 *     ./addAgent.sh -oaa_listen "tcp('localhost',4012)"
 * </code></pre>
 */
public class Solve_B_NFG_T extends TestCase {
    String testAgentName = "TestAgent";
    boolean useFullGoal = false;

    OaaTest_T oaaAgent;

    public Solve_B_NFG_T(String name) {
        super(name);
    }

    private void debug(String mess) {
        System.out.println(mess);
    }

    void newAgent() {
        oaaAgent = new OaaTest_T() {
            public String getAgentName() {
                return "oaa2_unitTest_agent_client";
            }
        };
        try {
            Thread.sleep(50L);
        } catch (InterruptedException ex) {
        }
    }

    public void testConnect0() throws Exception {
        connect_test0(new String[]{}, false, (IclList) IclTerm.fromString(true, "[]"), false);
    }

    public void testConnect1() throws Exception {
        connect_test0(new String[]{}, false, (IclList) IclTerm.fromString(true, "[address('" + testAgentName + "')]"), false);
    }

    public void testConnect2() throws Exception {
        connect_test0(new String[]{}, false, (IclList) IclTerm.fromString(true, "[address(name('" + testAgentName + "'))]"), false);
    }

    public void testConnect3() throws Exception {

        connect_test0(new String[]{}, false, (IclList) IclTerm.fromString(true, "[address(" + getTestAgentAddr() + ")]"), false);
    }

    public void testConnect4() throws Exception {
        connect_test0(new String[]{}, true, (IclList) IclTerm.fromString(true, "[]"), true);
    }

    public void testConnect5() throws Exception {
        connect_test0(new String[]{"-oaa_listen", "tcp('localhost',4020)"},
                false, (IclList) IclTerm.fromString(true, "[]"), true);
    }

    public void testConnect6() throws Exception {
        connect_test0(new String[]{}, false,
                (IclList) IclTerm.fromString(true, "[provider_limit(1)]"), true);
    }

    public void testConnect7() throws Exception {
        connect_test0(new String[]{}, false,
                (IclList) IclTerm.fromString(true, "[address('" + testAgentName + "')]"), true);
    }

    public void testConnect8() throws Exception {
        connect_test0(new String[]{}, false,
                (IclList) IclTerm.fromString(true, "[address(name('" + testAgentName + "'))]"), true);
    }

    public void testConnect9() throws Exception {
        connect_test0(new String[]{}, false,
                (IclList) IclTerm.fromString(true, "[address('" + testAgentName + "')]"), true);
    }

    public void testConnect10() throws Exception {
        connect_test0(new String[]{}, false,
                (IclList) IclTerm.fromString(true, "[address(" + getTestAgentAddr() + ")]"), true);
    }

    public void testConnect11() throws Exception {
        connect_test0(new String[]{}, false,
                (IclList) IclTerm.fromString(true, "[]"), true);
    }

    public void testConnect12() throws Exception {
        connect_testDelay(new String[]{},
                IclTerm.fromString(true, "[direct_connect(true)]"));
    }

    public String getTestAgentAddr() throws Exception {
        newAgent();
        oaaAgent.facilitatorConnect(new String[]{});
        IclList res = oaaAgent.solve("agent_host(Addr,'" + testAgentName + "',Host)", "[]");
        assertTrue("No answer for agent_host", res.size() > 0);
        String addAgentAddr = res.getTerm(0).getTerm(0).toString();
        oaaAgent.facilitatorDisconnect();
        return addAgentAddr;
    }

    public void connect_test0(String[] agentArgs, boolean setListen,
                              IclList solveArgs, boolean useDirectConnect) throws Exception {
        try {
            connect_test0_impl(agentArgs, setListen, solveArgs, useDirectConnect);
        } catch (DirectConnectFailedException ex) {
            debug("drect_connect failed. Is the server agent listening?");
            debug("Answers from faciliator = " + ex.getAnswers());
            throw ex;
        }
    }

    private void connect_test0_impl(String[] agentArgs, boolean setListen,
                              IclList solveArgs, boolean useDirectConnect) throws Exception {
        if (setListen) {
            System.setProperty("OAA_LISTEN", "tcp('localhost',4020)");
        }

        debug("Connecting...");
        newAgent();
        oaaAgent.facilitatorConnect(agentArgs);
        oaaAgent.setUseDirectConnect(useDirectConnect);

        // Add two numbers together
        IclList res = null;
        if (useFullGoal) {
            IclList newArgs = (IclList) solveArgs.clone();
            newArgs.add(IclTerm.fromString(true, "full_goal(add(10,20,Sum))"));
            debug("Using full goal, params = " + newArgs);
            res = oaaAgent.interpret(new IclStruct("oaa_Solve",
                    IclTerm.fromString(true, "add(Num1,Num2,Sum)"), newArgs), new IclList());
        } else {
            res = oaaAgent.interpret(new IclStruct("oaa_Solve",
                    IclTerm.fromString(true, "add(10,20,Sum)"), solveArgs), new IclList());
        }

        if (setListen) {
            System.setProperty("OAA_LISTEN", "");
        }

        debug("answer = " + res);

        boolean answer = false;

        for (Iterator itr = res.iterator(); itr.hasNext();) {
            IclTerm term = (IclTerm) itr.next();

            debug("10 + 20 = " + term);
            assertTrue(term.isStruct());
            assertTrue(term.getTerm(2).isInt());
            assertTrue(((IclInt) term.getTerm(2)).toInt() == 30);
            answer = true;
        }

        assertTrue("No Answer found", answer);

        // Add two more numbers together
        if (useFullGoal) {
            IclList newArgs = (IclList) solveArgs.clone();
            newArgs.add(IclTerm.fromString(true, "full_goal(add(100,-20,Sum))"));
            debug("Using full goal, params = " + newArgs);
            res = oaaAgent.solve("add(Num1,Num2,Sum)", newArgs.toString());
        } else {
            res = oaaAgent.solve("add(100,-20,Sum)", solveArgs.toString());
        }

        debug("answer = " + res);

        answer = false;

        for (Iterator itr = res.iterator(); itr.hasNext();) {
            IclTerm term = (IclTerm) itr.next();

            debug("100 - 20 = " + term);
            assertTrue(term.isStruct());
            assertTrue(term.getTerm(2).isInt());
            assertTrue(((IclInt) term.getTerm(2)).toInt() == 80);
            answer = true;
        }

        assertTrue("No Answer found", answer);

        oaaAgent.facilitatorDisconnect();
    }

    public void connect_testDelay(String[] agentArgs, IclTerm solveArgs) throws Exception {
        // Use a different add name so the add agent which is running won't
        // get called.
        newAgent();
        TestAgent addAgent = new TestAgent("add_test");
        addAgent.setDelay("test_delay_id");
        addAgent.facilitatorConnect(new String[]{"-oaa_listen", "tcp('localhost',4016)"});
        oaaAgent.facilitatorConnect(agentArgs);

        try {
            Thread.sleep(2000L);
        } catch (Exception ex) {
        }

        // Add two numbers together
        IclList res = null;

        // Add two more numbers together
        if (useFullGoal) {
            IclList newArgs = (IclList) solveArgs.clone();
            newArgs.add(IclTerm.fromString(true, "full_goal(add(10,20,Sum))"));
            debug("Using full goal, params = " + newArgs);
            res = oaaAgent.solve("add(Num1,Num2,Sum)", newArgs.toString());
        } else {
            res = oaaAgent.solve("add(10,20,Sum)", solveArgs.toString());
        }

        debug("delayed answer = " + res);

        boolean answer = false;

        for (Iterator itr = res.iterator(); itr.hasNext();) {
            IclTerm term = (IclTerm) itr.next();

            debug("10 + 20 = " + term);
            assertTrue(term.isStruct());
            assertTrue(term.getTerm(2).isInt());
            assertTrue(((IclInt) term.getTerm(2)).toInt() == 30);
            answer = true;
        }

        assertTrue("No Answer found", answer);

        addAgent.facilitatorDisconnect();
        oaaAgent.facilitatorDisconnect();
    }
}

⌨️ 快捷键说明

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