constraint.java

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

JAVA
164
字号
/*
#=========================================================================
# 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
#=========================================================================
  Author : shardt
  Date: Aug 11, 2003
*/
package com.sri.oaa2.tools.oaatest;

import org.xml.sax.*;
import java.util.*;

import com.sri.oaa2.icl.*;


/** When inheriting from this class, be sure to include a toString() method. */
abstract class Constraint {
  /** @param loc is for reporting errors */
  static Constraint create(String name,Attributes attribs,Locator loc) 
                           throws ParseException {
    // Idea: Could get rid of switch, use reflection on "Constraint_{name}" class.
    if (name.equals("gt")) {
      return new Constraint_gt(attribs,loc);
    }
    else if (name.equals("ge")) {
      return new Constraint_ge(attribs,loc);
    }
    else if (name.equals("lt")) {
      return new Constraint_lt(attribs,loc);
    }
    else if (name.equals("le")) {
      return new Constraint_le(attribs,loc);
    }
    else if (name.equals("regex")) {
      return new Constraint_regex(attribs,loc);
    }
    else if (name.equals("member")) {
      return new Constraint_member(attribs,loc);
    }
    else if (name.equals("contains")) {
      return new Constraint_contains(attribs,loc);
    }
    else if (name.equals("subset")) {
      return new Constraint_subset(attribs,loc);
    }
    else if (name.equals("superset")) {
      return new Constraint_superset(attribs,loc);
    }
    else if (name.equals("equalset")) {
      return new Constraint_equalset(attribs,loc);
    }
    
    throw new ParseException(name + " is not a valid constraint for <recv>",loc);
  }
  
  protected Constraint(Attributes attribs,Locator loc) throws ParseException {
    // Could check that value of var attribute is a valid prolog identifier
    var = new IclVar(getAttribute(attribs,"var",loc));
  }
  
  /** Convenience method to lookup a single attribute from a SAX Attributes list. */
  static protected String getAttribute(Attributes attribs,String name,Locator loc)
    throws ParseException {
    for (int i = 0; i < attribs.getLength(); i++) {
      if (name.equals(attribs.getQName(i))) {
        return attribs.getValue(i);
      }
    }
    throw new ParseException("Couldn't find expected attribute " + name,loc);
  }

  /** Do the given variable bindings satisfy this constraint. */
  boolean satisfies(HashMap bindings) {
    Object actual = bindings.get(var.toString());
    if (actual == null || !(actual instanceof IclTerm)) {
      return false;
    }    
    return checkValue((IclTerm)actual);
  }

  /** Is this an appropriate Constraint to put on the given term.  E.g. can't put a constraint
   * on variable "X" if "X" doesn't appear in the term.
   * 
   * @throws ParseException if not appropriate
   */
  void checkAppropriate(IclTerm term,Locator loc) throws ParseException {
    if (!hasVar(term)) {
      throw new ParseException("Attempt to put constraint on variable " + var + 
                               ", not found in " + term,loc);
    }
  }
  
  private boolean hasVar(IclTerm node) {
    if (node instanceof IclVar) {
      // var.equals(node) give NullPointerExceptions
      return var.toString().equals(node.toString());
    }
    else {
      for (int i = 0; i < node.getNumChildren(); i++) {
        if (hasVar(node.getTerm(i))) {
          return true;
        }
      }
    }
    return false;
  }
  
  protected abstract boolean checkValue(IclTerm actual);

  /** For SelfTest */
  IclVar getVar() {
    return var;
  }
  
  public String toString() {
    // Make sure that each subclass of Constraint implements a meaningful
    // toString().  RecvItem.describe() requires it.
    throw new RuntimeException("Did not implement " + Constraint.class + ".toString()");
  }
  
  static protected IclTermWrapper wrapIclTerm(IclTerm term) {
    // As an optimization, could use reusable wrapper, see commented out code below.
    return new IclTermWrapper(term);
  }

  /** DANGER: Not thread-safe. 
   * As an optimization, provide a shared IclTermWrapper that can be reused for
   * making quick comparisons w/o creating a new object. Don't keep a reference to
   * the returned value. */
//  static protected IclTermWrapper wrapIclTerm(IclTerm term) {
//    sharedWrapper.setTerm(term);
//    return sharedWrapper;
//  }

  private IclVar var;

  protected static List wrapIclList(IclTerm term) {
    if (!(term instanceof IclList)) {
      return null;
    }
  
    ArrayList ret = new ArrayList();
    Iterator iter = term.iterator();
    while (iter.hasNext()) {
      IclTerm t = (IclTerm)iter.next();
      ret.add(new IclTermWrapper(t));
    }
    return ret;
  }
}

⌨️ 快捷键说明

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