multiplicationagent.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 141 行

JAVA
141
字号
/* $Id: MultiplicationAgent.java,v 1.1 2004/02/05 23:02:23 giuli Exp $
 * @(#)MultiplicationAgent.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.icl.IclStruct;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.icl.ToFloat;
import com.sri.oaa2.icl.ToInt;
import com.sri.oaa2.agentlib.AgentImpl;
import com.sri.oaa2.agentlib.Agent;
import com.sri.oaa2.agentlib.AgentException;

/**
 * A simple agent with a single solvable
 * (multiply(Multiplier, Multiplicand, Product)) that demonstrates
 * the use of the OAA AgentLib Java library.
 * See {@link com.sri.oaa2.agentlib.example} for details on
 * running this agent.
 */
public class MultiplicationAgent extends AgentImpl {
    /** The name of this agent. */
    public final static String AGENT_NAME = "MultiplicationAgent";

    /**
     * Starts the MultiplicationAgent.
     *
     * @param args the command line arguments passed to the OAA library.
     */
    public static void main(String[] args) {
        try {
            Agent agent = new MultiplicationAgent();
            agent.facilitatorConnect(args);
            agent.start();
        }
        catch (AgentException ex) {
            System.err.println("Failed to start MultiplicationAgent");
            ex.printStackTrace();
            System.exit(1);
        }
    }

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

    public String getAgentCapabilities() {
        return "[multiply(Multiplier, Multiplicand, Product)]";
    }

    public String getAgentName() {
        return AGENT_NAME;
    }

    /**
     * Return the float value of a term. If the term
     * does not represent a number, then throw an Exception.
     *
     * @param term a term that presents a number.
     * @return value of the term as a double
     * @throws AgentException if the term does not represent a number
     */
    private float getValue(IclTerm term) throws AgentException {
        try {
            if (term.isInt()) {
                return (float) ToInt.getInstance().from(term);
            }
            else if (term.isFloat()) {
                return ToFloat.getInstance().from(term);
            }
            else {
                return Float.parseFloat(term.toIdentifyingString());
            }
        }
        catch (Exception ex) {
            throw new AgentException(
                "Not a number: " + term.toIdentifyingString(), ex);
        }
    }

    public boolean oaaDoEventCallback(IclTerm goal,
                                      IclList params,
                                      IclList answers) {
        boolean result = false;

        // Handle the multiply solvable
        if (goal.toIdentifyingString().equals("multiply")) {

            IclTerm multiplierTerm = goal.getTerm(0);
            IclTerm multiplicandTerm = goal.getTerm(1);

            try {
                // Extract the float values of the parameters
                // and multiply.
                float multiplier = getValue(multiplierTerm);
                float multiplicand = getValue(multiplicandTerm);
                float product = multiplier * multiplicand;

                // The answer is in the same form as the goal,
                // but with the product value filled in.
                IclTerm answer
                    = new IclStruct("multiply",
                                    (IclTerm) multiplierTerm.clone(),
                                    (IclTerm) multiplicandTerm.clone(),
                                    new IclFloat(product));

                // Add our answer to the list of answers
                answers.add(answer);

                // Return true to indicate success
                result = true;
            }
            catch (Exception ex) {
                // Use a log4J logger instead of
                // System.out to report the error.
                getLogger().error("Failed to add", ex);
            }
        }
        return result;
    }
}

⌨️ 快捷键说明

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