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

📄 liboaa.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   * @param params  parameters to register, if any
   *
   * @return true if registration was successful, or false if the connection between client
   *       and Facilitator is not open or if the registration otherwise fails.
   * @see LibCom#comConnect
   * @see #oaaRegisterCallback
   * @see #oaaMainLoop
   * @see #oaaDisconnect
   */
  public final boolean oaaRegister(String connectionId, String agentName,
                             IclTerm solvables, IclList params) {
    IclTerm tempTerm = null;

    boolean isClient = true;
    String localHostName;

    if(logger.isDebugEnabled()) {
      logger.debug("LibOaa.oaaRegister() called with solvables: " + solvables);
    }

    // Determine whether connection is server or client type
    tempTerm =
            mLibCom.comGetInfo(connectionId, IclTermCache.getNoAnon("type(T)"));
    if (tempTerm != null) {
        isClient = ((tempTerm.toIdentifyingString()).equals("client"));
    } else {
        return false;
    }

    if (!oaaHandshake(connectionId, agentName, params)) {
        return false;
    }
    // Register local host
    if ((localHostName = getLocalHostName())!=null) {

      IclTerm myAddress = oaaAddress("parent", null).getTerm(0);

      IclTerm agent_host = new IclStruct("agent_host", myAddress,
					 new IclStr(agentName),
					 new IclStr(localHostName));

      if (isClient) {
        oaaAddData(agent_host, new IclList(new IclStruct("address", new IclStr("parent"))));
      }
      else {
        oaaAddData(agent_host, new IclList(new IclStruct("address", new IclStr("self"))));
      }

      IclList myListenAddress = oaaAddress("client_listener", null);

        if (myListenAddress != null && myListenAddress.size() > 0 &&
            !myListenAddress.getTerm(0).toIdentifyingString().equals("no_address")) {
          IclTerm agent_listener = new IclStruct("agent_listener", myAddress,
                         myListenAddress.getTerm(0));
          IclTerm agent_listener_host = new IclStruct("agent_listener",
                         new IclStruct("name", new IclStr(oaaName())),
                         myListenAddress.getTerm(0));

            logger.info("Registering agent listener: " + agent_listener);
            // Add the listener address
            oaaAddData(agent_listener, new IclList(new IclStruct("address",
                    new IclStr("parent"))));
            logger.info("Registering agent listener host: " + agent_listener_host);
            oaaAddData(agent_listener_host, new IclList(new IclStruct("address",
                    new IclStr("parent"))));
        }
    }

    try {
      if(logger.isDebugEnabled()) {
        logger.debug("LibOaa.oaaRegister() calling oaaDeclare() with solvables: " + solvables);
      }
      oaaDeclare(solvables, new IclList(), new IclList(),
                 IclTermCache.getNoAnon("[if_exists(overwrite)]"));
    }
    catch(Exception e) {
      RuntimeException re = new RuntimeException(e.toString());
      re.fillInStackTrace();
      throw re;
    }

    return true;
  }

  /**
   * Retrieve local host name
   * <p>
   * @return    NULL if can't calculate host
   * <p>
   */
  private String getLocalHostName() {
    try {
      return (InetAddress.getLocalHost()).getHostName();
    }catch (UnknownHostException e){
      return null;
    }
  }

  /**
   * Changes the agent's 'open' status to 'ready', indicating that the
   *      agent is now ready to receive message.
   *<p>
   * @param   shouldPrint  if true, prints 'Ready' to standard out.
   */
  public final void oaaReady(boolean shouldPrint) {
    String name;
    // If client, send ready message to Facilitator
    if (!oaaClass("root") && ((name = oaaName())!=null)) {
      oaaPostEvent(new IclStruct ("ev_ready", new IclStr(name)),
		   new IclList());
    }
    if (shouldPrint)
      logger.info("Ready.");
  }


  /**
   * Extracts params, if any, from an event content. (An event has the form
   * event(Content, EventParams); here we are concerned with params contained
   * WITHIN Content). If Content contains no params, returns empty list.
   * <p>
   * @param    term  the ICL expression containing the params
   * @return  the params list extracted
   */
  private IclTerm icl_content_params(IclTerm term) {
    IclTerm params = new IclList();
    if ((term != null) && term.isStruct()) {
      String func = ToFunctor.getInstance().from(term);
      int arity = term.size();

      // Define where the PARAMS argument is for each type of known event
      if (func.equals("ev_connect") && (arity == 1)) params = term.getTerm(0); else
	if (func.equals("ev_solve") && (arity == 3)) params = term.getTerm(2); else
	  if (func.equals("ev_update_data") && (arity == 4)) params = term.getTerm(3); else
	    if (func.equals("ev_data_updated") && (arity == 6)) params = term.getTerm(3); else
	      if (func.equals("ev_post_declare") && (arity == 3)) params = term.getTerm(2); else
		if (func.equals("ev_reply_declared") && (arity == 4)) params = term.getTerm(2); else
		  if (func.equals("ev_register_solvables") && (arity == 4)) params = term.getTerm(3); else
		    if (func.equals("ev_update_trigger") && (arity == 6)) params = term.getTerm(5); else
		      if (func.equals("ev_trigger_updated") && (arity == 8)) params = term.getTerm(5); else
			if (func.equals("ev_solved") && (arity == 6)) params = term.getTerm(3);
    }
    return params;
  }

  /**
   * Extracts goal, if any, from an event content; otherwise, returns NULL.
   * <p>
   * @param    term  the ICL expression containing the goal
   * @return  the goal extracted
   */
  private IclTerm icl_content_goal(IclTerm term) {
    IclTerm goal = null;
    if ((term != null) && term.isStruct()) {
      String func = ToFunctor.getInstance().from(term);
      int arity = term.size();

      // Define where the PARAMS argument is for each type of known event
      if (func.equals("ev_solve") && (arity == 3)) goal = term.getTerm(1); else
	if (func.equals("ev_solved") && (arity == 6)) goal = term.getTerm(2); else
	  if (func.equals("ev_update_data") && (arity == 4)) goal = term.getTerm(2); else
	    if (func.equals("ev_data_updated") && (arity == 6)) goal = term.getTerm(2); else
	      if (func.equals("ev_update_trigger") && (arity == 6)) goal = term.getTerm(3); else
		if (func.equals("ev_trigger_updated") && (arity == 8)) goal = term.getTerm(3);
    }
    return goal;
  }

  /**
   * Succeeds if the given event Content is related to a solvable, and returns
   * the declaration for the solvable. Otherwise, returns null.
   * <p>
   * @param    content the event content
   * @return  the matching solvable
   */
  private IclTerm icl_content_related_solvable(IclTerm content) {
    IclTerm goal = icl_content_goal(content);
    if (goal != null) {
      IclTerm[] result = oaa_goal_matches_solvables(goal,oaa_solvables);
      return result[1];  // returns matching solvable
    }
    else
      return null;
  }


  /* *********************************************************
   * Constants and global definitions
   ***********************************************************/
  static String string_perm_defaults = "[call(true),read(true),write(true)]";
  static IclTerm perm_default_list =
    IclTermCache.getNoAnon(string_perm_defaults);

  static String string_param_defaults =
    "[from(unknown),priority(5),utility(5),"
    + "if_exists(append),type(procedure),"
    + "callback(app_do_event),private(false),"
    + "single_value(false),unique_values(false),"
    + "rules_ok(false),bookkeeping(true),"
    + "persistent(false),at_beginning(false),"
    + "do_all(false),reflexive(true),"
    + "parallel_ok(true),reply(true), block(true),"
    + "cache(false),flush_events(false),"
    + "recurrence(when)]";
  static IclTerm param_default_list =
    IclTermCache.getNoAnon(string_param_defaults);

  static String oaa_built_in_solvables_str ="[solvable(oaa_trigger(_TriggerId,"
    + "_Type, _Condition, _Action, _Params),"
    + " [type(data)], [write(true)])]";

  static IclTerm oaa_built_in_solvables =
    IclTermCache.getNoAnon(oaa_built_in_solvables_str);

  /* ************************************************************************
   * Goal manipulation functions
   **************************************************************************/

  /**
   * Test whether an expression is an ICL built-in goal.
   * <p>
   * <code>iclBuiltIn()</code> differs significantly from the Quintus Prolog predicate
   *     built_in, in that here we do not include basic constructors such
   *     as ',' and ';'.
   * <p>
   * <code>oaaInterpret/2</code> must be defined for every goal for which
   *     icl_BuiltIn succeeds.
   * <p>
   * @see  #oaaInterpret
   */
  public final boolean iclBuiltin(IclTerm goal) {
    if ((goal!=null) &&  goal.isStruct()) {
      String s  = ToFunctor.getInstance().from(goal);
      int arity = goal.size();
      return (
       ((arity == 2) && s.equals("="))
       || ((arity == 2) && s.equals("=="))
       || ((arity == 2) && s.equals("\\=="))
       || ((arity == 2) && s.equals("=<"))
       || ((arity == 2) && s.equals(">="))
       || ((arity == 2) && s.equals("<"))
       || ((arity == 2) && s.equals(">"))
       || ((arity == 2) && s.equals("member"))
       || ((arity == 2) && s.equals("memberchk"))
       || ((arity == 3) && s.equals("findall"))
       || ((arity == 2) && s.equals("iclConsistentParams"))
       );
    }
    return false;
  }

  /**
   * Test whether an expression is an ICL basic (non-compound) goal;
   *  that is, just a functor with 0 or more arguments.
   * <p>
   * Basic goals include built-in's as well as solvables.
   * <p>
   * This is a syntactic test; that is, we're not checking whether the
   *     Goal is a declared solvable.
   * <p>
   * @param goal  A term to test
   * @see #iclCompoundGoal
   */
  public final boolean iclBasicGoal(IclTerm goal) {
    return ((goal != null) && goal.isStruct() && !iclCompoundGoal(goal));
  }

  /**
   * Test whether an expression is an ICL compound goal.
   * <p>
   * @param goal  A term to test
   * @see #iclCompoundGoal
   */
  public final boolean iclCompoundGoal(IclTerm goal) {
    if (goal != null) {
      String s = goal.toIdentifyingString();
      return ((s!=null) &&
	      s.equals(":") ||
	      s.equals("::") ||
	      s.equals("\\+") ||
	      s.equals("->") ||
	      s.equals(":") ||
	      s.equals(",") ||
	      s.equals("|") ||
	      s.equals(";"));
    }
    return false;
  }

  /**
   * Assemble, disassemble, or match against the top-level components
   * of an ICL goal.
   * <pre>
   * name:  iclGoalComponents(+ICLGoal, -A, -G, -P).
   *      iclGoalComponents(-ICLGoal, +A, +G, +P).
   *      iclGoalComponents(+ICLGoal, +A, +G, +P).
   *  The top-level structure of an ICL goal is Address:Goal::Params,
   *  with Address and Params BOTH OPTIONAL.  Thus, every ICL goal
   *  either explicitly or implicitly includes all three components.
   * </pre>
   * This may be used with any ICL goal, basic or compound.
   *   When P is missing, its value is returned or matched as [].  When A is
   *   missing, its value is returned or matched as 'unknown'.
   * <p>
   * @return An array of ICLTerms representing the parts of the goal.
   * <UL>
   *    <LI>  array[0] : Address
   *    <LI>  array[1] : Goal
   *    <LI>  array[2] : Param
   * </UL>
   * <P>
   */
  public final IclTerm[] iclGoalComponents(IclTerm fullGoal) {
    IclTerm[] results = {null, null, null};
    if ((fullGoal.isStruct()) && (ToFunctor.getInstance().from(fullGoal).equals("::"))) {
      // Full goal a::g:p
      if (fullGoal.getTerm(0).isStruct() &&
	  ToFunctor.getInstance().from(fullGoal.getTerm(0)).equals(":")) {
        results[0]=fullGoal.getTerm(0).getTerm(0);
        results[1]=fullGoal.getTerm(0).getTerm(1);
        results[2]=fullGoal.getTerm(1);
      }else {
        // Simple goal goal::param
        results[0]=new IclStr("unknown");
        results[1]=fullGoal.getTerm(0);
        results[2]=fullGoal.getTerm(1);
      }
    }else {
      if ((fullGoal.isStruct()) && (ToFunctor.getInstance().from(fullGoal).equals(":"))) {
        // Simple goal a:g
        results[0]=fullGoal.getTerm(0);
        results[1]=fullGoal.getTerm(1);
        results[2]= new IclList();
      }else {
        results[0]=new IclStr("unknown");

⌨️ 快捷键说明

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