oaatest_t.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 845 行 · 第 1/3 页

JAVA
845
字号
/*
#=========================================================================
# 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 java.io.*;
import java.util.*;
import junit.framework.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import com.sri.oaa2.icl.*;
import antlr_oaa.*;


/** Test OaaTest itself using JUnit.  This is NOT the main 
 * entry point to run OaaTest, use the OaaTest class.
 * IMPORTANT: Must be run such that current directory is the location of the
 * self-test files.
 */
public class OaaTest_T extends TestCase {
  public void testICL() throws ANTLRException {
    IclTerm term = IclTerm.fromString("m_ev(receive,addr(tcp('192.12.5.169',3378),5),ev_solve(g_52,timelineGetEventCount(_6098),[parallel_ok(false),blocking(true),blocking(true),direct_connect(false)]))");
  }

  public void testFacItemMatch() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    ArrayList constraints = new ArrayList();
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X1");
    attr.addAttribute("","","value","CDATA","105");
    constraints.add(Constraint.create("ge",attr,null));
    attr.clear();
    attr.addAttribute("","","var","CDATA","YY");
    attr.addAttribute("","","value","CDATA","3.141592");
    constraints.add(Constraint.create("lt",attr,null));

    try {
      new FacItem(true,"a(b(_1,X2),c(d(YY,foo)))",OaaTestCase.DEFAULT_TIMEOUT_MS,constraints,null);      
      fail("Shouldn't allow constraint on X1 when X1 is not in the event.");
    }
    catch (ParseException e) {      
    }
    
    // Has constraints "X1 >= 105" and "YY < 3.141592".
    FacItem item = new FacItem(true,"a(b(_,X1),c(d(YY,foo)))",OaaTestCase.DEFAULT_TIMEOUT_MS,constraints,null);

    assertTrue(item.matchEvent(new RecvEvent(IclTerm.fromString("a(b(['hello everyone'],305.03),c(d(3.11,foo)))"))));
    assertFalse(item.matchEvent(new RecvEvent(IclTerm.fromString("a(b(['hello everyone'],305.03),c(d(3.15,foo)))"))));
    assertFalse(item.matchEvent(new RecvEvent(IclTerm.fromString("a(b(['hello everyone'],100),c(d(2,foo)))"))));    
    assertTrue(item.matchEvent(new RecvEvent(IclTerm.fromString("a(b(['hello everyone'],105),c(d(3.11,foo)))"))));
    assertFalse(item.matchEvent(new SendEvent(IclTerm.fromString("a(b(['hello everyone'],105),c(d(3.11,foo)))"))));
  }

  public void testParallelMatch() 
    throws ParseException, RecognitionException, TokenStreamException {

    ParallelItem parallel = new ParallelItem(4);
      
    parallel.addFacItem(new FacItem(true,"a(1,_)",10,null,null));

    ArrayList constraints = new ArrayList();
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","7");
    constraints.add(Constraint.create("lt",attr,null));
    parallel.addFacItem(new FacItem(false,"b(X,c(foo))",5,constraints,null));

    parallel.addFacItem(new FacItem(true,"d(foo)",8,null,null));
    
    assertTrue(parallel.getTimeout() == 10);
    
    assertFalse(parallel.matchEvent(new RecvEvent(IclTerm.fromString("b(8,c(foo))"))));
    assertFalse(parallel.matchEvent(new SendEvent(IclTerm.fromString("b(8,c(foo))"))));
    assertFalse(parallel.matchEvent(new RecvEvent(IclTerm.fromString("nothing(here)"))));
    assertFalse(parallel.matchEvent(new RecvEvent(IclTerm.fromString("d(foo)"))));
    assertFalse(parallel.matchEvent(new RecvEvent(IclTerm.fromString("a(1,foo)"))));
    assertFalse(parallel.matchEvent(new RecvEvent(IclTerm.fromString("b(6,c(foo))"))));
    assertTrue(parallel.matchEvent(new SendEvent(IclTerm.fromString("b(6,c(foo))"))));
    // After matching all RecvItems, Parallel item should match anything.
    assertTrue(parallel.matchEvent(new SendEvent(IclTerm.fromString("some(random(event))"))));    
  }

  /** lt, gt */
  public void testConstraints1() 
    throws ParseException, RecognitionException, TokenStreamException {

    try {
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","value","CDATA","3");
      Constraint c = Constraint.create("lt",attr,null); 
      fail("<lt> must have a var attribute");
    }
    catch (ParseException e) {
    }
    
    try {
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      Constraint c = Constraint.create("lt",attr,null); 
      fail("<lt> must have a value attribute");
    }
    catch (ParseException e) {
    }
      
    try {
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","Y");
      Constraint c = Constraint.create("lt",attr,null); 
      fail("<ge> value must be a number");
    }
    catch (ParseException e) {
    }
      
    // Constraint "X > 3"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","3");
    //  Pass in null Locator, don't care about error messages.
    Constraint c = Constraint.create("gt",attr,null); 
    
    // true that 4 > 3
    HashMap map = new HashMap();
    map.put("X",IclTerm.fromString("4"));
    map.put("BB",IclTerm.fromString("foo(bar)"));
    assertTrue(c.satisfies(map));
    
    // false that 3 > 3
    map.clear();
    map.put("X",IclTerm.fromString("3"));
    assertFalse(c.satisfies(map));

    // Try with a float
    map.clear();
    map.put("X",IclTerm.fromString("10000.345"));
    assertTrue(c.satisfies(map));
    map.clear();
    map.put("X",new IclFloat(-10000.345));
    assertFalse(c.satisfies(map));
  }

  /** le, ge */
  public void testConstraints2() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    // Constraint "_g234 <= -2"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","_g234");
    attr.addAttribute("","","value","CDATA","-2");
    //  Pass in null Locator, don't care about error messages.
    Constraint c = Constraint.create("le",attr,null); 
    
    // false that 4 <= -2
    HashMap map = new HashMap();
    map.put("_g234",IclTerm.fromString("4"));
    map.put("BBillyBob",IclTerm.fromString("foo(bar)"));
    assertFalse(c.satisfies(map));
    
    // false that 4 <= foo(some,stuff)
    map.clear();
    map.put("_g234",IclTerm.fromString("foo(some,stuff)"));
    assertFalse(c.satisfies(map));
    
    // false that 4 <= Y
    map.clear();
    map.put("_g234",IclTerm.fromString("Y"));
    assertFalse(c.satisfies(map));
    
    // True that -2 <= -2
    map.clear();
    map.put("_g234",IclTerm.fromString("-2"));
    assertTrue(c.satisfies(map));

    // True that -2001.32 <= -2
    map.clear();
    map.put("_g234",IclTerm.fromString("-2001.32"));
    assertTrue(c.satisfies(map));

    // Try with a float, -2.0 <= -2
    map.clear();
    map.put("_g234",new IclFloat(-2.0));
    assertTrue(c.satisfies(map));
  }

  /** contains */
  public void testConstraints3() 
    throws ParseException, RecognitionException, TokenStreamException {
            
    try {
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      Constraint c = Constraint.create("contains",attr,null); 
      fail("<contains> must have a value attribute");
    }
    catch (ParseException e) {
    }
      
    //  contains c
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","W");
    attr.addAttribute("","","value","CDATA","c");
    Constraint c = Constraint.create("contains",attr,null); 
    
    // true that [a,b,c,d,e,f,g,h,i,j,k] contains c
    HashMap map = new HashMap();
    map.put("W",IclTerm.fromString("[a,b,c,d,e,f,g,h,i,j,k]"));
    assertTrue(c.satisfies(map));
    
    // false that [] contains c
    map.clear();
    map.put("W",IclTerm.fromString("[]"));
    assertFalse(c.satisfies(map));
    
    // true that [a,b(hello,there),c,this(is,it),f,g,h,i,j,k] contains c
    map.clear();
    map.put("W",IclTerm.fromString("[a,b(hello,there),c,this(is,it),f,g,h,i,j,k]"));
    assertTrue(c.satisfies(map));
    
    // false that [a,b(c,there),[c],e,[a,Hello,c],f,g,h,i,j,k] contains c, even 
    // though it contains an element that contains it
    // True that -2 <= -2
    map.clear();
    map.put("W",IclTerm.fromString("[a,b(c,there),[c],e,[a,Hello,c],f,g,h,i,j,k]"));
    assertFalse(c.satisfies(map));

    // c does not contain c
    map.clear();
    map.put("W",IclTerm.fromString("c"));
    assertFalse(c.satisfies(map));
  }

  public void testMemberConstraint() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    // Constraint "X member [3,7,9,22]"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","[3,7,9,22]");
    //  Pass in null Locator, don't care about error messages.
    Constraint c = Constraint.create("member",attr,null); 
    
    HashMap map = new HashMap();
    map.put("X",IclTerm.fromString("3"));
    map.put("BB",IclTerm.fromString("foo(bar)"));
    assertTrue(c.satisfies(map));
    
    map.clear();
    map.put("X",IclTerm.fromString("4"));
    assertFalse(c.satisfies(map));
    
    try {
      AttributesImpl attr2 = new AttributesImpl();
      attr2.addAttribute("","","var","CDATA","X");
      attr2.addAttribute("","","value","CDATA","foo(bar)");
      Constraint.create("member",attr2,null);       
      fail("value must be a list for <member>");
    }
    catch (ParseException e) {
    }

    try {
      AttributesImpl attr2 = new AttributesImpl();
      attr2.addAttribute("","","value","CDATA","foo(bar)");

⌨️ 快捷键说明

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