oaatest_t.java

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

JAVA
845
字号
      Constraint.create("member",attr2,null);       
      fail("member must have var attribute");
    }
    catch (ParseException e) {
    }
  }

  public void testSubsetConstraint1() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    // Constraint "X subset [3,7,9,22,j,foo,hello,3.786]"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","[3,7,9,22,j,foo,hello,3.786]");
    Constraint c = Constraint.create("subset",attr,null); 
    
    HashMap map = new HashMap();

    map.put("X",IclTerm.fromString("3"));
    assertFalse(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[3]"));
    assertTrue(c.satisfies(map));
    
    // Ok for multiple occurances of foo
    map.put("X",IclTerm.fromString("[foo,foo,3.786,foo]"));
    assertTrue(c.satisfies(map));

    // Shuffle around a bit
    map.put("X",IclTerm.fromString("[22,hello,3,9,j,3.786,foo,7]"));
    assertTrue(c.satisfies(map));

    // A superset
    map.put("X",IclTerm.fromString("[22,hello,3,9,j,90,3.786,foo,7]"));
    assertFalse(c.satisfies(map));
  }

  public void testSubsetConstraint2() 
    throws ParseException, ANTLRException {
      
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","YYY");
    attr.addAttribute("","","value","CDATA","[ev_solve(foo(bar),[]),ev_solve(dave(bill),[blocking(false)]),ev_solve(some(event),[])]");
    Constraint c = Constraint.create("subset",attr,null); 
    
    HashMap map = new HashMap();

    map.put("YYY",IclTerm.fromString("[ev_solve(dave(bill),[blocking(false)])]"));
    assertTrue(c.satisfies(map));
    
    map.put("YYY",IclTerm.fromString("[ev_solve(foo(bar),[nope])]"));
    assertFalse(c.satisfies(map));
    
    map.put("YYY",IclTerm.fromString("[ev_solve(foo(bar),[]),ev_solve(some(event),[])]"));
    assertTrue(c.satisfies(map));

    // change var name
    map.clear();
    map.put("YY",IclTerm.fromString("[ev_solve(foo(bar),[]),ev_solve(some(event),[])]"));
    assertFalse(c.satisfies(map));

    map.put("YYY",IclTerm.fromString("[]"));
    assertTrue(c.satisfies(map));
  }

  public void testSupersetConstraint() 
    throws ParseException, ANTLRException {
      
    // Constraint "X superset [3,7,foo(bar),hello,3.786]"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","[3,7,foo(bar),hello,3.786]");
    Constraint c = Constraint.create("superset",attr,null); 
    
    HashMap map = new HashMap();

    map.put("X",IclTerm.fromString("[7,3.786,foo(bar),3,hello]"));
    assertTrue(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[7,happydays,3.786,844,foo(bar),hello,87,3,yoyo,testme]"));
    assertTrue(c.satisfies(map));
    
    // Duplicates and some whitespace
    map.put("X",IclTerm.fromString("[7, happydays,  3.786,  844,7,7,foo(bar),foo(bar),hello,3,3,3,  3   ,87,3,yoyo,testme]"));
    assertTrue(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[foo,3.786]"));
    assertFalse(c.satisfies(map));

    map.put("X",IclTerm.fromString("not(a(list))"));
    assertFalse(c.satisfies(map));

    map.put("X",IclTerm.fromString("[7,happydays,3.78,844,foo(bar),hello,87,yoyo,testme]"));
    assertFalse(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[]"));
    assertFalse(c.satisfies(map));
  }

  public void testEqualsetConstraint1() 
    throws ParseException, ANTLRException {
      
    // Constraint "X equalset [3,7,foo(bar),hello,3.786]"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","[3,7,foo(bar),hello,3.786]");
    Constraint c = Constraint.create("equalset",attr,null); 
    
    HashMap map = new HashMap();

    map.put("X",IclTerm.fromString("[7,3.786,foo(bar),3,hello]"));
    assertTrue(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[7,happydays,3.786,844,foo(bar),hello,87,3,yoyo,testme]"));
    assertFalse(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[7,3.786,foo(bar),hello,3]"));
    assertTrue(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("not(a(list))"));
    assertFalse(c.satisfies(map));

    map.put("X",IclTerm.fromString("[7,happydays,3.78,844,foo(bar),hello,87,yoyo,testme]"));
    assertFalse(c.satisfies(map));
    
    map.put("X",IclTerm.fromString("[]"));
    assertFalse(c.satisfies(map));

    // Duplicates are ok
    map.put("X",IclTerm.fromString("[7,3.786,3.786,7,7,7,7,foo(bar),3,hello,foo(bar)]"));
    assertTrue(c.satisfies(map));
  }

  public void testEqualsetConstraint2() 
    throws ParseException, ANTLRException {
      
    // Constraint "X equalset []"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");
    attr.addAttribute("","","value","CDATA","[]");
    Constraint c = Constraint.create("equalset",attr,null); 

    HashMap map = new HashMap();

    map.put("X",IclTerm.fromString("[]"));
    assertTrue(c.satisfies(map));

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

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

    map.put("X",IclTerm.fromString("[1,2,3,4,5,6,7]"));
    assertFalse(c.satisfies(map));
  }
    
  public void testIclTermWrapper()  
  throws ParseException, ANTLRException {
    IclTermWrapper w1 = new IclTermWrapper(IclTerm.fromString("foo(bar(3),X)"));
    IclTermWrapper w2 = new IclTermWrapper(IclTerm.fromString("foo(bar(Var),jaz(hello))"));
    assertTrue(w1.equals(w2));
    assertTrue(w2.equals(w1));
  }
    
  public void testUnifySetConstraints() 
    throws ParseException, ANTLRException {
      
    {
      // Constraint "X  contains tree(X)"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","tree(X)");
      Constraint c = Constraint.create("contains",attr,null); 
  
      HashMap map = new HashMap();
  
      map.put("X",IclTerm.fromString("[shrub(3),tree(8),dog(4,4,2)]"));
      assertTrue(c.satisfies(map));
  
      map.put("X",IclTerm.fromString("[shrub(3),tree(8,7),dog(4,4,2)]"));
      assertFalse(c.satisfies(map));
    }
    
    // TODO: One-way unification in set Constraints.  
    // Thus, the pattern foo(X) matches the actual value foo(4),
    // but not foo(4) matches the actual value foo(X).
    // Right now they both match. 
    {
      // Constraint "X  contains tree(4)"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","tree(4)");
      Constraint c = Constraint.create("contains",attr,null); 
  
      HashMap map = new HashMap();
  
      map.put("X",IclTerm.fromString("[shrub(3),tree(X),dog(4,4,2)]"));
      assertTrue(c.satisfies(map));
  
      map.put("X",IclTerm.fromString("[shrub(3),tree(X,A),dog(4,4,2)]"));
      assertFalse(c.satisfies(map));
    }

    {
      // Constraint "X member [tree(X),cat(Y)]"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","[tree(X),cat(Y)]");
      Constraint c = Constraint.create("member",attr,null); 

      HashMap map = new HashMap();

      map.put("X",IclTerm.fromString("cat(4)"));
      assertTrue(c.satisfies(map));
    }
    
    {
      // Constraint "X subset [tree(X),cat(Y),fireengine(Z)]"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","[tree(X),cat(Y),fireengine(Z)]");
      Constraint c = Constraint.create("subset",attr,null); 
  
      HashMap map = new HashMap();
  
      map.put("X",IclTerm.fromString("[fireengine(red),cat(sheba)]"));
      assertTrue(c.satisfies(map));
    }

    {
      // Constraint "X superset [cat(Y),fireengine(Z)]"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","[cat(Y),fireengine(Z)]");
      Constraint c = Constraint.create("superset",attr,null); 
  
      HashMap map = new HashMap();
  
      map.put("X",IclTerm.fromString("[fireengine(red),cat(sheba),some(thing(else),here)]"));
      assertTrue(c.satisfies(map));
    }

    {
      // Constraint "X equalset [tree(X),cat(Y),fireengine(Z)]"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","[tree(X),cat(Y),fireengine(Z)]");
      Constraint c = Constraint.create("equalset",attr,null); 
  
      HashMap map = new HashMap();
  
      map.put("X",IclTerm.fromString("[fireengine(red),tree(oak),cat(sheba)]"));
      assertTrue(c.satisfies(map));
    }

    {
      // Constraint "X member [foo(3.0),bar(3),jaz(3.00000)]"
      AttributesImpl attr = new AttributesImpl();
      attr.addAttribute("","","var","CDATA","X");
      attr.addAttribute("","","value","CDATA","[foo(3.0),bar(3),jaz(3.00000)]");
      Constraint c = Constraint.create("member",attr,null); 

      HashMap map = new HashMap();

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

      map.put("X",IclTerm.fromString("foo( 3.0000)"));
      assertTrue(c.satisfies(map));

      map.put("X",IclTerm.fromString("  jaz ( 3.0)  "));
      assertTrue(c.satisfies(map));
    }    
  }
    
  public void testRegex1() 
    throws ParseException, RecognitionException, TokenStreamException {
      
    // Constraint matches regex "foo\(.*\)"
    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("","","var","CDATA","X");

⌨️ 快捷键说明

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