facitem.java

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

JAVA
141
字号
/*
#=========================================================================
# Copyright 2003 SRI International.  All rights reserved.
#
# The material contained in this file is confidential and proprietary to SRI
# International and may not be reproduced, published, or disclosed to others
# without authorization from SRI International.
#
# DISCLAIMER OF WARRANTIES
#
# SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
# SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
# LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
# OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
#=========================================================================
*/
package com.sri.oaa2.tools.oaatest;

import com.sri.oaa2.icl.*;
import java.util.*;
import org.xml.sax.*;


/** Willl look for an event to/from facilitator subject to a list of
 * Constraints.  
 */
class FacItem extends MatchEventsItem {
  /*
     * @param recv true means look for events received by facilitator, else events sent from facilitator
     * @param eventStr describes the event to look for, ok to use synonyms like oaaSolve for ev_solve.  
     * @param timeout Value in milliseconds, can no longer use 0 as default. 
     * @param constraints may be null or an empty list.  FacItem makes a copy of the list, but
     * not the ToFacItems inside.  Do not refer to the ToFacItems passed in after making this call.
     * Pass in ArrayList instead of List to guarantee the existence of a clone() method.
     * @throws ParseException if can't parse eventStr OR constraints are not appropriate for event.
     * The latter is technically not a parsing exception, but report it to user in same way. 
     * */
  FacItem(boolean recv,String eventStr,int timeout,
          ArrayList constraints,Locator loc)
    throws ParseException {
      
    super(timeout);
    this.recv = recv;
    if (loc != null) {
      this.lineNumber = loc.getLineNumber();
    }
    else {
      this.lineNumber = -1;
    }

    event = parseWithSynonyms(eventStr,false);
    if (event == null) {
      throw new ParseException("Exptected ICL term",loc);
    }

    if (constraints != null && constraints.size() > 0) {
      this.constraints = (List)constraints.clone();
    
      // Check constraints.
      Iterator iter = constraints.iterator();
      while (iter.hasNext()) {
        ((Constraint)iter.next()).checkAppropriate(event,loc);
      }
    }
  }

  // Increase visibility to "public" so ParallelItem and SelfTest can get to it.
  public boolean matchEvent(Event event) {
    // Check send/recv.
    if (recv) {
      if (!(event instanceof RecvEvent)) {
        return false;
      }
    }
    else {
      if (!(event instanceof SendEvent)) {
        return false;
      }
      
    }

    // Check event unifies
    Unifier unifier = Unifier.getInstance();
    HashMap bindings = new HashMap();
    if (unifier.unify(this.event,event.getIclTerm(),bindings) != null) {
      // Check constraints.
      if (constraints != null) {
        Iterator iter = constraints.iterator();
        while (iter.hasNext()) {
          if (!((Constraint)iter.next()).satisfies(bindings)) {
            return false;
          }
        }
      }      
      return true;
    }
    return false;
  }

  protected void describe(MatchEventsItem.DescribeCallback cb) {
    StringBuffer buf = new StringBuffer("[line " + lineNumber +
      (recv ? ",toFac]" : ",fromFac]") + event.toString());
    if (constraints != null) {
      Iterator iter = constraints.iterator();
      boolean first = true;
      while (iter.hasNext()) {
        if (first) {
          buf.append(" WHERE ");
          first = false;
        }
        else {
          buf.append(", ");
        }
        buf.append(((Constraint)iter.next()).toString());
      }    
    }
    cb.event(buf.toString());
  }

  void addTriggerEvents(TestItem.EventAdder e) {
    if (recv) {
      e.addRecv(event);        
    }
    else {
      e.addSend(event);              
    }
  }

  int lineNumber; // Location of this element in XML file, for error reporting.
  boolean recv; // send if false

  // Will be null or non-empty List.
  // Package-visible for SelfTest.
  List constraints = null;
  
  // package-visible for SelfTest
  IclTerm event;
}

⌨️ 快捷键说明

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