Exemplo n.º 1
0
  static void test1() {
    // not really a PageRank algorithm
    String prQuery2 =
        "Edge(int s,int t).\n"
            + "PageRank(int i:0..10,(int n:0..100, double d)).\n"
            + "PageRank(1, n, $sum(r)) :- PageRank(0, p, r1),"
            + "  Edge(p,n), r = r1/100.\n";
    Parser p = new Parser(prQuery2);
    try {
      p.parse();
    } catch (ParseException pe) {
      System.out.println(pe.compileErrorMsg());
    }

    Assert.true_(p.getRules().size() == 1, "rules:" + p.getRules());
    Analysis analysis = new Analysis(p);
    analysis.run();

    FunctionLoader.loadAll(p.getRules());
    analysis.requireTypeChecking();

    Rule r = p.getRules().get(0);

    Set<Variable> vars = r.getBodyVariables();
    vars.addAll(r.getHeadVariables());
    Assert.true_(vars.contains(new Variable("n", int.class)));
    Assert.true_(vars.contains(new Variable("r", double.class)));
    Assert.true_(vars.contains(new Variable("r1", double.class)));
    Assert.true_(vars.size() == 4);
  }
Exemplo n.º 2
0
  static void testRuleDependency() {
    String prQuery =
        "Edge(int s,int t).\n"
            + "Foo(int a,int b).\n"
            + "Bar(int a,int b).\n"
            + "Zoo(int a,int b).\n"
            + "Foo(a,b) :- Bar(a,c), Zoo(b,c).\n"
            + "Bar(a,b) :- Bar(a,c), Zoo(b,c).\n"
            + "Edge(s,t) :- s=1, t=2 .\n";

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

    Analysis an = new Analysis(p);
    an.run();
    List<Epoch> strata = an.getEpochs();
    Assert.true_(strata.size() == 2, "Strata #:" + strata.size());

    List<Rule> rules = an.getRules();
    Rule fooRule = rules.get(0);
    Assert.true_(fooRule.name().startsWith("Foo"));
    Assert.true_(
        fooRule.getDependingRules().size() == 0, // no deps in the same strata
        "Foo deps #:" + fooRule.getDependingRules().size());

    Rule barRule = rules.get(1);
    Assert.true_(barRule.name().startsWith("Bar"));
    Assert.true_(barRule.getDependingRules().size() == 2);

    Rule edgeRule = rules.get(2);
    Assert.true_(edgeRule.name().startsWith("Edge"));
    Assert.true_(edgeRule.getDependingRules().size() == 0);
  }
Exemplo n.º 3
0
  static void testEpochs() {
    String query =
        "SP(int n:0..2000000,int d).\n"
            + "Edge(int s,int t).\n"
            + "Foo(int n, int a).\n"
            + "SP(t,d) :- Edge(1,t), d=1 .\n"
            + "SP(t,$min(d)) :- SP(p,d1), Edge(p,t), d=d1+1 .\n"
            + "Foo(a, b) :- SP(a,b).\n"
            + "Edge(n1,n2) :- n1=2, n2=20.\n";
    Parser p = new Parser();
    p.parse(query);
    Analysis an = new Analysis(p);
    an.run();
    List<Epoch> epochs = an.getEpochs();

    Assert.true_(epochs.size() == 3);
    Epoch e = epochs.get(0);
    Assert.true_(e.getRuleCompNum() == 1);
    Rule edgeLoad = e.getRuleComps().get(0).get(0);
    Assert.true_(edgeLoad.getHead().name().equals("Edge"));

    e = epochs.get(1);
    Assert.true_(e.getRuleCompNum() == 1);
    Rule sp = e.getRuleComps().get(0).get(0);
    Assert.true_(sp.getHead().name().equals("SP") && sp.getHead().hasFunctionParam());
    e = epochs.get(2);
    Assert.true_(e.getRuleCompNum() == 1);
    Rule foo = e.getRuleComps().get(0).get(0);
    Assert.true_(foo.getHead().name().equals("Foo"));
  }
Exemplo n.º 4
0
  static void testEpochRecursive() {
    String query =
        "Edge(int s:1..35000, (int t)).\n"
            + "SP(int x:1..35000, int dist).\n"
            + "Edge(s, t) :- s=1,t=2.\n"
            + "SP(s, d) :- s=1, d=1.\n"
            + "SP(t, $min(d)) :- SP(s, d1), Edge(s, t), d=d1+1.\n"
            + "?- SP(5, d).\n";
    Parser p = new Parser();
    p.parse(query);
    Analysis an = new Analysis(p);
    an.run();
    List<Epoch> epoch = an.getEpochs();

    Assert.true_(epoch.size() == 2);
    Epoch s = epoch.get(0);
    Assert.true_(s.getRuleCompNum() == 1);
    Rule edgeLoad = s.getRuleComps().get(0).get(0);
    Assert.true_(edgeLoad.getHead().name().equals("Edge"));
  }
Exemplo n.º 5
0
  static void testVariables() {
    String query =
        "Edge(int s,int t).\n"
            + "Foo(int f,int a, int b).\n"
            + "Foo(a, b,d) :- Foo(a,b,c), Edge(b,d).\n";
    Parser p = new Parser(query);
    p.parse();

    Assert.true_(p.getRules().size() == 1);
    Analysis analysis = new Analysis(p);
    analysis.run();

    Rule r = p.getRules().get(0);
    Predicate p0 = r.getBodyP().get(0);
    Predicate p1 = r.getBodyP().get(1);
    Variable b1 = (Variable) p0.params.get(0);
    Variable b2 = (Variable) p1.first();
    Assert.true_(b1 == b2, "expecting the same instance");
    Variable b3 = (Variable) r.getHead().params.get(0);
    Assert.true_(b1 == b3, "expecting the same instance");
  }
Exemplo n.º 6
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);
  }
Exemplo n.º 7
0
  static void testPrivatization() {
    String query =
        "Edge(int s:0..100000, (int t)).\n"
            + "Clique(int x:10, int y, int z) indexby x.\n"
            + "Clique(x,y,z):-Edge(x,y), Edge(y, z), Edge(z, s).\n";

    Parser p = new Parser();
    p.parse(query);
    Analysis an = new Analysis(p);
    an.run();
    List<Epoch> epochs = an.getEpochs();
    Epoch e = epochs.get(0);

    List<Rule> rules = e.getRules();
    boolean hasPrivateTable = false;
    for (Rule r : rules) {
      Map<String, Table> tableMap = an.getTableMap();
      Table t = tableMap.get(r.getHead().name());
      if (t instanceof PrivateTable) hasPrivateTable = true;
    }
    Assert.true_(hasPrivateTable);
  }
Exemplo n.º 8
0
 public void removeRules(CodeGenMain code, List<Rule> rules) {
   errorLog.remove(code);
   for (Rule r : rules) {
     ruleToCodeMap.remove(r.id());
   }
 }
Exemplo n.º 9
0
 public void registerRules(List<Rule> rules, CodeGenMain code) {
   for (Rule r : rules) {
     ruleToCodeMap.put(r.id(), code);
   }
 }
Exemplo n.º 10
0
 public boolean equals(Object o) {
   if (!(o instanceof Rule)) return false;
   Rule r = (Rule) o;
   return r.ruleDecl.equals(ruleDecl) && getClass().equals(r.getClass());
 }