コード例 #1
0
  static void testVarResolution() {
    String simpleQuery =
        "Edge(int s,(int t)).\n"
            + "Foaf(int a,int b).\n"
            + "Foo(int a,int b).\n"
            + "Foaf(n1,n3) :- Foo(n1,n2), Edge(n2,n3), n4=n1*(n2+1).\n";
    Parser p = new Parser(simpleQuery);
    p.parse();
    Analysis an = new Analysis(p);
    an.run();

    Rule r = p.getRules().get(0);
    Set<Variable> resolvedVars[] = Analysis.getResolvedVars(r);
    Assert.equals(resolvedVars.length, 4);
    Assert.true_(resolvedVars[0].isEmpty());
    Assert.true_(resolvedVars[1].contains(new Variable("n1", int.class)));
    Assert.true_(resolvedVars[1].contains(new Variable("n2", int.class)));
    Assert.true_(!resolvedVars[1].contains(new Variable("n3", int.class)));
    Assert.true_(!resolvedVars[1].contains(new Variable("n3", int.class)));

    Assert.true_(!resolvedVars[2].contains(new Variable("n4", int.class)));

    Assert.true_(resolvedVars[3].contains(new Variable("n1", int.class)));
    Assert.true_(resolvedVars[3].contains(new Variable("n2", int.class)));
    Assert.true_(resolvedVars[3].contains(new Variable("n3", int.class)));
    Assert.true_(resolvedVars[3].contains(new Variable("n4", int.class)));
  }
コード例 #2
0
  static void testFunction() {
    String prQuery =
        "Edge(int s,int t).\n"
            + "Foo(int a, String b, int c).\n"
            + "Bar(int a, String b).\n"
            + "Edge(s,t) :- s=1, t=2.\n"
            + "Foo(a, b, d) :- Bar(a, b), c=$toInt(\"11\"), d=$toInt(\"12\")+3*$toInt(\"12\").\n";

    Parser p = new Parser(prQuery);
    p.parse();

    Analysis analysis = new Analysis(p);
    analysis.run();
    Rule r = p.getRules().get(1);
    Expr expr = (Expr) r.getBody().get(1);
    AssignOp op = (AssignOp) expr.root;
    Function f = (Function) op.arg2;
    Assert.equals(f.name(), "Builtin.toInt");
    Assert.equals(f.getReturns().get(0).name, "c");
    Assert.equals(f.getReturns().get(0).type, int.class);
  }
コード例 #3
0
  static void testQuery() {
    String prQuery =
        "PageRank(int n,int i, double r).\n"
            + "Edge(int s,int t).\n"
            + "PageRank(t, 0, r) :- Edge(p,t), r = 1.0/100.\n"
            + "?-PageRank(1,0, r).\n";
    Parser p = new Parser(prQuery);
    p.parse();
    Analysis an = new Analysis(p);
    an.run();

    Query q = p.getQuery();
    Assert.true_(q != null);
    Assert.equals(q.getP().name(), "PageRank");
  }