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

📄 multiplicationclient.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
字号:
/* $Id: MultiplicationClient.java,v 1.1 2004/02/05 23:02:24 giuli Exp $
 * @(#)MultiplicationClient.java  02/2004
 *
 * 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.agentlib.example;

import com.sri.oaa2.icl.IclFloat;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.agentlib.AgentException;
import com.sri.oaa2.agentlib.DirectConnectFailedException;

import org.apache.log4j.Logger;

/**
 * A simple pure-client agent that exercises the
 * MultiplicationAgent; demonstrates
 * the use of the OAA AgentLib library.
 * See {@link com.sri.oaa2.agentlib.example} for details on
 * running this agent.
 */
public class MultiplicationClient extends com.sri.oaa2.agentlib.AgentImpl {
    /** The name of this agent. */
    public final static String AGENT_NAME = "MultiplicationClient";

    /**
     * Starts the MultiplicationAgent.
     *
     * @param args the command line arguments passed to the OAA library.
     */
    public static void main(String[] args) {
        // Start this agent...it is a pure-client with
        // no solvables of its own.
        try {
            com.sri.oaa2.agentlib.Agent agent = new MultiplicationClient();
            agent.facilitatorConnect(args);
            agent.start();
        }
        catch (com.sri.oaa2.agentlib.AgentException ex) {
            System.err.println("Failed to start MultiplicationClient");
            ex.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * Default constructor.
     */
    public MultiplicationClient() {
        super();
    }

    public String getAgentName() {
        return AGENT_NAME;
    }

    private float doMultiply(float multiplier, float multiplicand)
        throws AgentException {
        // Prepare goal and params
        String goal = "multiply(" + multiplier + ", "
                                  + multiplicand + ", Product)";
        String params = "[]";

        // Request that the goal be satisfied
        IclList answers = solve(goal, params);

        // Grab the third term of the first answer.
        float product
            = ((IclFloat) answers.getTerm(0).getTerm(2)).toFloat();

        return product;
    }

    public void start() {
        // Be sure to invoke the superclass' start() method.
        super.start();

        // Get the default logger, for convenience.
        Logger log = getLogger();

        // Have this agent request the solution of four
        // different multiplication problems, each using
        // a different OAA technique.
        try {

            log.info(""); // blank line
            log.info("Requesting to multiply 3 and 4 using the");
            log.info("\"classic\" OAA technique (no Direct Connect).");
            float product = doMultiply(3, 4);
            log.info("Result: 3 times 4 is " + product);

            log.info("");
            log.info("Requesting to multiply 5 and 6 using the");
            log.info("\"classic\" OAA technique and a specified agent name.");
            setSolvingAgent(MultiplicationAgent.AGENT_NAME);
            product = doMultiply(5, 6);
            log.info("Result: 5 times 6 is " + product);

            log.info("");
            log.info("Requesting to multiply 7 and 8 using Direct Connect.");
            setUseDirectConnect(true);
            setSolvingAgent(null); // undo last invocation of setSolvingAgent()
            product = doMultiply(7, 8);
            log.info("Result: 7 times 8 is " + product);

            log.info("");
            log.info("Requesting to multiply 9 and 10 using Direct Connect");
            log.info("and a specified agent name.");
            setUseDirectConnect(true);
            setSolvingAgent(MultiplicationAgent.AGENT_NAME);
            product = doMultiply(9, 10);
            log.info("Result: 9 times 10 is " + product);
        }
        catch (DirectConnectFailedException dcfe) {
            log.error("Direct Connect failed...did you start the");
            log.error("MultiplicationAgent with the \"-oaa_listen\"");
            log.error("command-line switch and argument "
                      + "\"tcp(localhost,PortNumber)\"?");
            log.error(dcfe);
            // Note: Even though Direct Connect failed,
            // answers may still have been produced.
            // Use the code
            //     IclList answers = dcfe.getAnswers();
            // to obtain a list of answers that were
            // produced by non-Direct-Connect means.
            // See the Javadoc for the Agent interface.
        }
        catch (AgentException ae) {
            log.error(ae);
        }

        // Disconnect and quit
        log.info("\nFinished...exiting....");
        facilitatorDisconnect();
        System.exit(0);
     }
}

⌨️ 快捷键说明

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