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

📄 solverecursive.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/* $Id: SolveRecursive.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.com.LibCom;
import com.sri.oaa2.com.LibComTcpProtocol;
import com.sri.oaa2.icl.IclInt;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.lib.LibOaa;
import com.sri.oaa2.test.agent.OaaTest_T;
import junit.framework.TestCase;

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;
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 SolveRecursive extends TestCase {
    String testAgentName = "TestAgent";
    boolean useFullGoal = false;

    OaaTest_T oaaAgent;

    public SolveRecursive(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(500);
        } catch (InterruptedException ex) {
        }
    }

    public void tetConnectDisconnect() throws Exception {
        int n = 0;

        while (true) {
            try {
                Thread.sleep(50L);
            } catch (InterruptedException ex) {
            }
            debug("Connecting...");
            LibOaa myOaa = new LibOaa(new LibCom(new LibComTcpProtocol(), new String[]{}));

            if (!myOaa.oaaSetupCommunication("TestAgentRec")) {
                throw new Exception("Failed to setup OAA communication");
            }

            if (!myOaa.oaaRegister("parent", "TestAgentRec",
                    IclTerm.fromString("[]"), new IclList())) {
                throw new Exception("Failed to register");
            }

            myOaa.oaaReady(true);

            //try { Thread.sleep(5000L); } catch (InterruptedException ex) {}

            debug("Disconnecting...");
            myOaa.oaaDisconnect(null);
            n++;
            debug("Number of times connect and disconnected: " + n);
        }
    }

    public void testConnectInifinite() throws Exception {
        debug("Connecting...");
        newAgent();
        oaaAgent.facilitatorConnect(new String[]{});

        double nTests = 0;
        double batchRun = 5000;
        double test1 = 0, test2 = 0, test3 = 0, test4 = 0, test5 = 0, test6 = 0;
        long time = System.currentTimeMillis();

        while (true) {
            for (double i = 0; i < batchRun; i++) {
                int run = (int) Math.round(Math.random() * 5.0) + 1;

                if (run == 1) {
                    connect_test0((IclList) IclTerm.fromString(true, "[]"));
                    test1++;
                } else if (run == 2) {
                    connect_test0((IclList) IclTerm.fromString(true, "[direct_connect(true)]"));
                    test2++;
                } else if (run == 3) {
                    connect_test1((IclList) IclTerm.fromString(true, "[]"));
                    test3++;
                } else if (run == 4) {
                    connect_test1((IclList) IclTerm.fromString(true, "[direct_connect(true)]"));
                    test4++;
                } else if (run == 5) {
                    connect_test2((IclList) IclTerm.fromString(true, "[]"));
                    test5++;
                } else if (run == 6) {
                    connect_test2((IclList) IclTerm.fromString(true, "[direct_connect(true)]"));
                    test6++;
                }
            }
            nTests++;
            long diff = System.currentTimeMillis() - time;
            PrintWriter out = new PrintWriter(new FileWriter("testInfo.txt"));
            out.println("Total number tests: " + nTests * batchRun);
            out.println("Total time milliseconds: " + diff);
            out.println("Start Time: " + new Date(time));
            out.println("End Time: " + new Date(System.currentTimeMillis()));
            out.println("Total number tests through fac    (test 1): " + test1);
            out.println("Total number tests through fac    (test 2): " + test3);
            out.println("Total number tests through fac    (test 3): " + test5);
            out.println("Total number tests direct_connect (test 1): " + test2);
            out.println("Total number tests direct_connect (test 2): " + test4);
            out.println("Total number tests direct_connect (test 3): " + test6);
            out.close();
        }
    }

    IclTerm addSolve = IclTerm.fromString(true, "add(10,20,Sum)");
    IclTerm addSolve2 = IclTerm.fromString(true, "add(100,-20,Sum)");
    IclTerm doNothing = IclTerm.fromString(true, "doNothing(N)");

    public void connect_test0(IclList solveArgs) throws Exception {
        // Add two numbers together
        IclList res = oaaAgent.solve(addSolve, solveArgs);

        boolean answer = false;

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

            assertTrue(term.isStruct());
            assertTrue(term.getTerm(2).isInt());
            assertTrue(((IclInt) term.getTerm(2)).toInt() == 30);
            answer = true;
        }

        assertTrue("No Answer found", answer);
    }

    public void connect_test1(IclList solveArgs) throws Exception {
        // Add two numbers together
        IclList res = oaaAgent.solve(addSolve2, solveArgs);

        boolean answer = false;

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

            assertTrue(term.isStruct());
            assertTrue(term.getTerm(2).isInt());
            assertTrue(((IclInt) term.getTerm(2)).toInt() == 80);
            answer = true;
        }

        assertTrue("No Answer found", answer);
    }

    public void connect_test2(IclList solveArgs) throws Exception {
        // Add two numbers together
        IclList res = oaaAgent.solve(doNothing, solveArgs);

        assertTrue("No Answer found", res.size() > 0);
    }
}

⌨️ 快捷键说明

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