oaatest_t.java

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

JAVA
845
字号
    attr.addAttribute("","","value","CDATA","foo\\(.*\\)");
    Constraint c = Constraint.create("regex",attr,null); 
    
    HashMap map = new HashMap();
    map.put("X",IclTerm.fromString("foo()"));
    assertTrue(c.satisfies(map));

    map.clear();
    map.put("X",IclTerm.fromString("foo(jane,bob)"));
    assertTrue(c.satisfies(map));

    map.clear();
    map.put("X",IclTerm.fromString("foo(1,2,bob)"));
    assertTrue(c.satisfies(map));

    map.clear();
    map.put("X",IclTerm.fromString("a(foo(1,2,bob))"));
    assertFalse(c.satisfies(map));
  }
    
  public void testRegex2() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    // Constraint matches regex "'Today I feel ((happy)|(sad))'"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","'Today I feel ((happy)|(sad))'");
    Constraint c = Constraint.create("regex",attr,null); 
    
    HashMap map = new HashMap();
    map.put("X",IclTerm.fromString("'Today I feel sad'"));
    assertTrue(c.satisfies(map));

    map.clear();
    map.put("X",IclTerm.fromString("'Today I feel happy'"));
    assertTrue(c.satisfies(map));
    map.clear();

    map.put("X",IclTerm.fromString("' Today I feel happy'"));
    assertFalse(c.satisfies(map));

    map.put("X",IclTerm.fromString("foo"));
    assertFalse(c.satisfies(map));
  }

  public void testRegex3() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    // Constraint matches regex "'The weather in Boston is .*'"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","'The weather in Boston is .*'");
    Constraint c = Constraint.create("regex",attr,null); 
    
    HashMap map = new HashMap();
    map.put("X",IclTerm.fromString("'The weather in Boston is partly-cloudy.'"));
    assertTrue(c.satisfies(map));

    map.clear();
    map.put("X",IclTerm.fromString("'The weather in Boston is overcast with a chance of rain.'"));
    assertTrue(c.satisfies(map));

    map.clear();
    map.put("X",IclTerm.fromString("'The temperature in Boston is 68 degrees'"));
    assertFalse(c.satisfies(map));
  }

  private void parseGood(String fname) 
    throws SAXException, IOException {

    File file = new File(fname);
    OaaTestCase test = new OaaTestCase(parser,file);
  }

  private void parseBad(String fname) 
    throws SAXException, IOException {
    try {
      File file = new File(fname);
      OaaTestCase test = new OaaTestCase(parser,file);
      fail("Should not have been able to parse " + fname);    
    }
    catch (SAXException e) {
    }
  }

  public void testParseNegative()
    throws SAXException, IOException {
    File file = new File("negative.otml");
    OaaTestCase test = new OaaTestCase(parser,file);
    assertTrue(test != null); 
  }


  public void testParser0() 
    throws SAXException, IOException {

    parseGood("vars.otml");
    parseGood("bigfile.otml");
    parseGood("xmlquote.otml");
    parseGood("prompt.otml");
  }
  
  public void testParser1() 
    throws SAXException, IOException {

    parseBad("broken1.otml");
    parseBad("broken2.otml");
    parseBad("broken3.otml");
    parseBad("broken4.otml");
    parseBad("broken5.otml");
    parseBad("broken6.otml");
    parseBad("broken7.otml");
    parseBad("broken8.otml");
    parseBad("broken9.otml");
    parseBad("broken10.otml");
    parseBad("broken11.otml");
    parseBad("null.otml");
  }
    
  public void testParser2() 
    throws SAXException, IOException {
  
    File file1 = new File("parallel.otml");
    OaaTestCase test = new OaaTestCase(parser,file1);
    
    Iterator iter = test.items.iterator();
    SendItem item0 = (SendItem)iter.next();
    assertTrue(item0.event.toString().equals("a(7,8,9)"));
    
    ParallelItem item1 = (ParallelItem)iter.next();
    assertTrue(item1.getTimeout() == 20000); // In ms
    Iterator pIter = item1.recvItems.iterator();
    FacItem item1_0 = (FacItem)pIter.next();
    assertTrue(item1_0.event.toString().equals("b()"));

    FacItem item1_1 = (FacItem)pIter.next();
    assertTrue(item1_1.event.toString().equals("c(X,Y)"));
    assertTrue(item1_1.constraints.size() == 2);
    Constraint_gt cgt = (Constraint_gt)item1_1.constraints.get(0);
    assertTrue(cgt.getVar().toString().equals("X"));
    assertTrue(cgt.getValue() == 100.0);
    Constraint_regex cregex = (Constraint_regex)item1_1.constraints.get(1);
    assertTrue(cregex.getVar().toString().equals("Y"));
    assertTrue(cregex.getPattern().equals("some .* text"));

    FacItem item1_2 = (FacItem)pIter.next();
    assertTrue(item1_2.event.toString().equals("d()"));
    assertFalse(pIter.hasNext());

    FacItem item2 = (FacItem)iter.next();
    assertTrue(item2.event.toString().equals("foo(bar(jaz,a),d)"));    
  }

  public void testParseTimeout0() 
    throws SAXException, IOException {
  
    File file1 = new File("timeout.otml");
    OaaTestCase test = new OaaTestCase(parser,file1);
    
    Iterator iter = test.items.iterator();
    FacItem item0 = (FacItem)iter.next();
    assertTrue(item0.getTimeout() == 3000);
    
    FacItem item1 = (FacItem)iter.next();
    assertTrue(item1.getTimeout() == 10000);
  }

  public void testParseTimeout1() 
    throws SAXException, IOException {
  
    File file1 = new File("timeout2.otml");
    OaaTestCase test = new OaaTestCase(parser,file1);
    
    Iterator iter = test.items.iterator();
    FacItem item0 = (FacItem)iter.next();
    assertTrue(item0.getTimeout() == 3000);
    
    FacItem item1 = (FacItem)iter.next();
    assertTrue(item1.getTimeout() == OaaTestCase.DEFAULT_TIMEOUT_MS);
  }

  public void testSynonmys() {
    TestItem.gidUnique = 1;

    {
      IclTerm term = TestItem.parseWithSynonyms("fo ## o(bar)",true);
      assertNull(term);
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("",true);
      assertNull(term);
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("foo(bar)",true);
      assertTrue(term.toString().equals("foo(bar)"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("oaa_Solve(foo(X),Params)",true);
      assertTrue(term.toString().equals("ev_solve(g_1,foo(X),Params)"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("oaa_Solve(foo(X),Params)",false);
      assertTrue(term instanceof IclStruct &&
                 ((IclStruct)term).getFunctor().equals("ev_solve") &&
                 term.size() == 3 &&
                 term.getTerm(0) instanceof IclVar &&
                 ((IclVar)term.getTerm(0)).toString().startsWith("_"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("  oaa_AddData(foo(bar),[])",true);
      assertTrue(term.toString().equals("ev_update_data(g_2,add,foo(bar),[])"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("oaa_RemoveData(foo(bar),[])   ",true);
      assertTrue(term.toString().equals("ev_update_data(g_3,remove,foo(bar),[])"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("    oaa_ReplaceData(foo(bar),[])",true);
      assertTrue(term.toString().equals("ev_update_data(g_4,replace,foo(bar),[])"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("\t\t oaa_LoadData(foo(bar),[])",true);
      assertTrue(term.toString().equals("ev_update_data(g_5,load,foo(bar),[])"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("oaa_SaveData(foo(bar),[])",true);
      assertTrue(term.toString().equals("ev_update_data(g_6,save,foo(bar),[])"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("oaa_AddTrigger(data,d(_5939),oaa_Solve(inform_ui(debug,d(_5939))),[on(add),recurrence(whenever)])",true);
      assertTrue(term.toString().equals("ev_update_trigger(g_7,add,data,d(_5939),oaa_Solve(inform_ui(debug,d(_5939))),[on(add),recurrence(whenever)])"));
    }
    {
      IclTerm term = TestItem.parseWithSynonyms("oaa_RemoveTrigger(data,d(_5939),oaa_Solve(inform_ui(debug,d(_5939))),[on(add),recurrence(whenever)])",true);
      assertTrue(term.toString().equals("ev_update_trigger(g_8,remove,data,d(_5939),oaa_Solve(inform_ui(debug,d(_5939))),[on(add),recurrence(whenever)])"));
    }
    {
      // Wrong syntax, leave it alone
      IclTerm term = TestItem.parseWithSynonyms("  oaa_AddTrigger(data,d(_5939),[on(add),recurrence(whenever)])",true);
      assertTrue(term.toString().equals("oaa_AddTrigger(data,d(_5939),[on(add),recurrence(whenever)])"));
    }
  }

  public void testEquals() {
    assertTrue(IclTerm.fromString(true,"3.0").equals(new IclFloat(3)));    
    assertTrue(IclTerm.fromString(true,"3.0").equals(IclTerm.fromString(true,"3.00")));    
    assertTrue(IclTerm.fromString(true,"[a,b,c]").equals(IclTerm.fromString(true,"[ a, b , c ] ")));
    
    assertTrue(IclTerm.fromString(true,"4").equals(IclTerm.fromString(true,"4")));        
    assertTrue((new IclInt(4)).equals((Object)new IclInt(4)));    

    // TODO: This doesn't compile w/ Oaa 2.3Beta
//    assertTrue((new IclInt(4)).equals(new IclInt(4)));    
//    assertTrue(IclTerm.fromString(true,"4").equals(new IclInt(4)));

    assertTrue(IclTerm.fromString(true,"[a,b,c]").equals(IclTerm.fromString(true,"[ a, b , c ] ")));

    assertTrue(IclTerm.fromString(true,"Foo").equals(IclTerm.fromString(true,"Foo")));    

    assertTrue(IclTerm.fromString(true,"_abc").equals(IclTerm.fromString(true,"_abc")));    

    assertTrue(IclTerm.fromString(true,"ev_solved(_a,Requestees,Solvers,manager('Adam Cheyer',[],_b),Params,[manager('Adam Cheyer',[],'Ray Perrault')])")
       .equals(IclTerm.fromString(true,"ev_solved(_a,Requestees,Solvers,manager('Adam Cheyer',[],_b),Params,[manager('Adam Cheyer',[],'Ray Perrault')])")));


/* These are false, and probably should remain so.
    assertTrue(IclTerm.fromString(true,"_").equals(IclTerm.fromString(true,"_")));    
    assertTrue(IclTerm.fromString(true,"ev_solved(_,Requestees,Solvers,manager('Adam Cheyer',[],_),Params,[manager('Adam Cheyer',[],'Ray Perrault')])")
      .equals(IclTerm.fromString(true,"ev_solved(_,Requestees,Solvers,manager('Adam Cheyer',[],_),Params,[manager('Adam Cheyer',[],'Ray Perrault')])")));
  */
  }

  public void setUp() throws Exception {
    // This also has the desired side-effect of forcing
    // OaaTest static initialization.
    parser = OaaTest.createParser();
  }

  SAXParser parser = null;
}

⌨️ 快捷键说明

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