示例#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 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);
  }
示例#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"));
  }
示例#4
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);
  }
示例#5
0
  static void testParseMultipleProg() {
    String query =
        "Foo(int f,int b).\n"
            + "Bar(int b,int c).\n"
            + "Zoo(int z,int a).\n"
            + "Foo(a,b) :- Bar(a,c), Zoo(b,c).\n"
            + "Bar(a,b) :- Bar(a,c), Zoo(b,c).\n";

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

    String query2 = "Foo(a,b) :- Foo(a,c), Zoo(b,c).\n";
    p.parse(query2);
    Assert.true_(p.getRules().get(0).name().startsWith("Foo"));

    String query3 = "Zoo(a,b) :- Foo(a,b).\n";
    p.parse(query3);
    Assert.true_(p.getRules().get(0).name().startsWith("Zoo"));
  }
示例#6
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"));
  }
示例#7
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");
  }
示例#8
0
  static void testNestedTable() {
    String query = "Triangle(int h,(int x, int y, int z)).\n" + "Edge(int s,int t).\n";

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

    Analysis an = new Analysis(p);
    an.run();

    Table triangle = an.getTableMap().get("Triangle");
    Column col = triangle.getColumn(0);
    Assert.true_(col.isPrimaryShard());
    col = triangle.getColumn(1);
    Assert.not_true(col.isPrimaryShard());
    List<ColumnGroup> cg = triangle.getColumnGroups();
    Assert.true_(cg.size() == 2);

    Table edge = an.getTableMap().get("Edge");
    cg = edge.getColumnGroups();
    Assert.true_(cg.size() == 1);
  }
示例#9
0
  static void testTableColumns() {
    String query =
        "PageRank(int n,(int _iter, double r)) sortby _iter desc.\n"
            + "Edge(int s:0..10,int t).\n"
            + "Foo(int a:0..10, double d).\n";

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

    Analysis an = new Analysis(p);
    an.run();

    Table pagerank = an.getTableMap().get("PageRank");
    Assert.true_(pagerank.getColumn(1).isSortedDesc());

    Table edge = an.getTableMap().get("Edge");
    Assert.true_(edge.getColumn(0).hasRange());

    Table foo = an.getTableMap().get("Edge");
    Assert.true_(foo.getColumn(0).hasRange());
  }
示例#10
0
  static void testTableOptions() {
    String query =
        "Edge(int s,int t) predefined.\n" + "B(int a,int b, (int i, double c)) sortby i.\n";
    Parser p = new Parser();
    p.parse(query);

    List<Table> tables = p.getNewTables();
    Table edge = tables.get(0);
    Table b = tables.get(1);

    Assert.true_(edge.isPredefined());
    Assert.true_(b.sortbyCols().length == 1);
    Assert.true_(b.sortbyCols()[0] == 2);

    query = "Foo(int i:0..100, (int s, String t)) sortby s, sortby t.\n";
    p.parse(query);
    Analysis an = new Analysis(p);
    try {
      an.run();
      throw new SociaLiteException("should throw analysis exception");
    } catch (AnalysisException e) {
      /* expected */
    }
  }
示例#11
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");
  }
示例#12
0
  static void testDistRule() {
    String query =
        "Edge(int a:0..100,(int b)).\n"
            + "InEdge(int a:0..100,(int b)).\n"
            + "Edge(s,t) :- s=1, t=99.\n"
            + "InEdge(t,s) :- Edge(s,t).\n"
            + "?-InEdge(s,t).\n";
    Parser p = new Parser();
    p.parse(query);
    TestAnalysis an = new TestAnalysis(p);
    an.setDistributed();
    an.run();

    for (Table t : an.getRemoteTables()) {
      Assert.true_(t instanceof RemoteHeadTable);
      Column c = t.getColumn(0);
      Assert.not_true(c.hasRange(), "RemoteTables are not array-tables");
    }
  }
示例#13
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);
  }
示例#14
0
 static void testEpochs2() {
   String query =
       "Edge(int s:0..410235, (int t)).\n"
           + "InEdge(int s:0..410235,(int t)).\n"
           + "EdgeCnt(int s:0..410235, int cnt).\n"
           + "PR0(int p:0..410235,double r).\n"
           + "PR1(int p:0..410235,double r).\n"
           + "PR2(int p:0..410235,double r).\n"
           + "PR3(int p:0..410235,double r).\n"
           + "PR4(int p:0..410235,double r).\n"
           + "PR0(_,r) :- r= 1/410236.0.\n"
           + "PR1(p,$sum(r)) :- InEdge(p,n), PR0(n,r1), EdgeCnt(n, cnt), r=0.85*r1/cnt.\n"
           + "PR2(p,$sum(r)) :- InEdge(p,n), PR1(n,r1), EdgeCnt(n, cnt), r=0.85*r1/cnt.\n"
           + "PR3(p,$sum(r)) :- InEdge(p,n), PR2(n,r1), EdgeCnt(n, cnt), r=0.85*r1/cnt.\n"
           + "PR4(p,$sum(r)) :- InEdge(p,n), PR3(n,r1), EdgeCnt(n, cnt), r=0.85*r1/cnt.\n";
   Parser p = new Parser();
   p.parse(query);
   Analysis an = new Analysis(p);
   an.run();
   List<Epoch> epoch = an.getEpochs();
   Assert.true_(epoch.size() == 4, "Epoch #:" + epoch.size());
 }