Search in sources :

Example 1 with Query

use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.

the class SimpleProgramProverTest method testFill.

@Test
public void testFill() throws IOException, LogicProgramException {
    WamProgram program = WamBaseProgram.load(new File(PROGRAM));
    Query q = new Query(new Goal("coworker", new ConstantArgument("steve"), new ConstantArgument("X")));
    System.out.println("Query: " + q.toString());
    ProofGraph p = new StateProofGraph(q, apr, program);
    Prover prover = new DfsProver(apr);
    Map<State, Double> sols = prover.prove(p, new StatusLogger());
    //		assertEquals(2,sols.size());
    HashMap<String, Integer> expected = new HashMap<String, Integer>();
    expected.put("steve", 0);
    expected.put("sven", 0);
    System.out.println("Query: " + q.toString());
    for (State s : sols.keySet()) {
        if (!s.isCompleted())
            continue;
        System.out.println(s);
        Query a = p.fill(s);
        System.out.println(a);
        String v = a.getRhs()[0].getArg(1).getName();
        System.out.println("Got solution: " + v);
        if (expected.containsKey(v))
            expected.put(v, expected.get(v) + 1);
    }
    for (Map.Entry<String, Integer> e : expected.entrySet()) assertEquals(e.getKey(), 1, e.getValue().intValue());
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) HashMap(java.util.HashMap) DfsProver(edu.cmu.ml.proppr.prove.DfsProver) Prover(edu.cmu.ml.proppr.prove.Prover) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) ConstantArgument(edu.cmu.ml.proppr.prove.wam.ConstantArgument) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) Goal(edu.cmu.ml.proppr.prove.wam.Goal) State(edu.cmu.ml.proppr.prove.wam.State) DfsProver(edu.cmu.ml.proppr.prove.DfsProver) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Query

use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.

the class UngroundedSolutionsTest method test.

@Test
public void test() throws IOException, LogicProgramException {
    WamProgram program = WamBaseProgram.load(new File(PROGRAM));
    APROptions apr = new APROptions("depth=20");
    WamPlugin facts = LightweightGraphPlugin.load(apr, new File(FACTS));
    Query q = new Query(new Goal("grandparent", new ConstantArgument("X"), new ConstantArgument("Y")));
    //		q.variabilize();
    StateProofGraph pg = new StateProofGraph(q, apr, program, facts);
    Prover p = new DfsProver(apr);
    Map<State, Double> ans = p.prove(pg, new StatusLogger());
    //		Map<LogicProgramState,Double> ans = p.proveState(program, new ProPPRLogicProgramState(Goal.decompile("grandparent,-1,-2")));
    System.out.println("===");
    for (State s : ans.keySet()) {
        if (s.isCompleted()) {
            System.out.println(s);
            Map<Argument, String> dict = pg.asDict(s);
            System.out.println(Dictionary.buildString(dict, new StringBuilder(), "\n\t").substring(1));
            for (String a : dict.values()) {
                //					a = a.substring(a.indexOf(":"));
                assertFalse(a.startsWith("X"));
            }
        }
    }
//		System.out.println("===");
//		for (String s : Prover.filterSolutions(ans).keySet()) {
//			System.out.println(s);
//			assertFalse("Filtered solutions contain variables",s.contains("v["));
//		}
}
Also used : StatusLogger(edu.cmu.ml.proppr.util.StatusLogger) Query(edu.cmu.ml.proppr.prove.wam.Query) Argument(edu.cmu.ml.proppr.prove.wam.Argument) ConstantArgument(edu.cmu.ml.proppr.prove.wam.ConstantArgument) DfsProver(edu.cmu.ml.proppr.prove.DfsProver) Prover(edu.cmu.ml.proppr.prove.Prover) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) ConstantArgument(edu.cmu.ml.proppr.prove.wam.ConstantArgument) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) WamPlugin(edu.cmu.ml.proppr.prove.wam.plugins.WamPlugin) Goal(edu.cmu.ml.proppr.prove.wam.Goal) State(edu.cmu.ml.proppr.prove.wam.State) DfsProver(edu.cmu.ml.proppr.prove.DfsProver) APROptions(edu.cmu.ml.proppr.util.APROptions) File(java.io.File) Test(org.junit.Test)

Example 3 with Query

use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.

the class SplitFactsPluginTest method test.

@Test
public void test() throws LogicProgramException {
    int input = 0;
    int output = 0;
    int constants = Configuration.USE_WAM;
    int modules = 0;
    Configuration c = new Configuration(("--programFiles " + GrounderTest.RULES + ":" + GrounderTest.FACTS + ":" + ADDLFACTS).split(" "), input, output, constants, modules);
    assertEquals("# of plugins", c.plugins.length, 1);
    assertEquals("# of members", ((SplitFactsPlugin) c.plugins[0]).plugins.size(), 2);
    assertTrue("claim", c.plugins[0]._claim("validClass/1"));
    Query q = Query.parse("validClass(X)");
    WamInterpreter interp = new WamInterpreter(c.program, c.plugins);
    int queryStartAddr = c.program.size();
    q.variabilize();
    c.program.append(q);
    interp.executeWithoutBranching(queryStartAddr);
    List<Outlink> outs = c.plugins[0].outlinks(interp.saveState(), interp, true);
    assertEquals("# outlinks", 6, outs.size());
}
Also used : Outlink(edu.cmu.ml.proppr.prove.wam.Outlink) Configuration(edu.cmu.ml.proppr.util.Configuration) Query(edu.cmu.ml.proppr.prove.wam.Query) WamInterpreter(edu.cmu.ml.proppr.prove.wam.WamInterpreter) Test(org.junit.Test) GrounderTest(edu.cmu.ml.proppr.GrounderTest)

Example 4 with Query

use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.

the class WeightedEdgeTest method testOne.

public void testOne(APROptions apr, WamPlugin plug) throws IOException, LogicProgramException {
    Prover p = new DprProver(apr);
    WamProgram program = WamBaseProgram.load(RULES);
    WamPlugin[] plugins = new WamPlugin[] { plug };
    Grounder grounder = new Grounder(apr, p, program, plugins);
    assertTrue("Missing weighted functor", plugins[0].claim("hasWord#/3"));
    Query query = Query.parse("words(p1,W)");
    ProofGraph pg = new StateProofGraph(new InferenceExample(query, new Query[] { Query.parse("words(p1,good)") }, new Query[] { Query.parse("words(p1,thing)") }), apr, new SimpleSymbolTable<Feature>(), program, plugins);
    //			Map<String,Double> m = p.solutions(pg);
    //			System.out.println(Dictionary.buildString(m, new StringBuilder(), "\n").toString());
    GroundedExample ex = grounder.groundExample(p, pg);
    String serialized = ex.getGraph().serialize(true).replaceAll("\t", "\n");
    //String serialized = grounder.serializeGroundedExample(pg, ex).replaceAll("\t", "\n");
    System.out.println(serialized);
    assertTrue("Label weights must appear in ground graph (0.9)", serialized.indexOf("0.9") >= 0);
    assertTrue("Label weights must appear in ground graph (0.1)", serialized.indexOf("0.1") >= 0);
    //			Map<String,Double> m = p.solvedQueries(pg);
    //			System.out.println(Dictionary.buildString(m, new StringBuilder(), "\n"));
    Query query2 = Query.parse("words2(p1,W)");
    ProofGraph pg2 = new StateProofGraph(new InferenceExample(query2, new Query[] { Query.parse("words(p1,good)") }, new Query[] { Query.parse("words(p1,thing)") }), apr, new SimpleSymbolTable<Feature>(), program, plugins);
    //			Map<String,Double> m = p.solutions(pg);
    //			System.out.println(Dictionary.buildString(m, new StringBuilder(), "\n").toString());
    GroundedExample ex2 = grounder.groundExample(p, pg2);
    String serialized2 = ex2.getGraph().serialize(true).replaceAll("\t", "\n");
    //String serialized = grounder.serializeGroundedExample(pg, ex).replaceAll("\t", "\n");
    System.out.println(serialized2);
    assertTrue("Label weights must appear in ground graph (0.9)", serialized2.indexOf("0.9") >= 0);
    assertTrue("Label weights must appear in ground graph (0.1)", serialized2.indexOf("0.1") >= 0);
}
Also used : GroundedExample(edu.cmu.ml.proppr.examples.GroundedExample) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) DprProver(edu.cmu.ml.proppr.prove.DprProver) Prover(edu.cmu.ml.proppr.prove.Prover) DprProver(edu.cmu.ml.proppr.prove.DprProver) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) Feature(edu.cmu.ml.proppr.prove.wam.Feature) InferenceExample(edu.cmu.ml.proppr.examples.InferenceExample) Grounder(edu.cmu.ml.proppr.Grounder)

Example 5 with Query

use of edu.cmu.ml.proppr.prove.wam.Query in project ProPPR by TeamCohen.

the class WeightedFeaturesTest method testAsGraph.

@Test
public void testAsGraph() throws IOException, LogicProgramException {
    APROptions apr = new APROptions();
    Prover p = new DprProver(apr);
    WamProgram program = WamBaseProgram.load(RULES);
    WamPlugin[] plugins = new WamPlugin[] { FactsPlugin.load(apr, LABELS, false), LightweightGraphPlugin.load(apr, WORDSGRAPH, -1) };
    Grounder grounder = new Grounder(apr, p, program, plugins);
    assertTrue(plugins[1].claim("hasWord#/3"));
    Query query = Query.parse("predict(p1,Y)");
    ProofGraph pg = new StateProofGraph(new InferenceExample(query, new Query[] { Query.parse("predict(p1,pos)") }, new Query[] { Query.parse("predict(p1,neg)") }), apr, new SimpleSymbolTable<Feature>(), program, plugins);
    GroundedExample ex = grounder.groundExample(p, pg);
    String serialized = grounder.serializeGroundedExample(pg, ex).replaceAll("\t", "\n");
    System.out.println(serialized);
    // hack
    assertTrue("Word weights must appear in ground graph", serialized.indexOf("0.9") > 0);
    assertTrue("Word weights must appear in ground graph", serialized.indexOf("0.1") > 0);
}
Also used : GroundedExample(edu.cmu.ml.proppr.examples.GroundedExample) Query(edu.cmu.ml.proppr.prove.wam.Query) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) ProofGraph(edu.cmu.ml.proppr.prove.wam.ProofGraph) DprProver(edu.cmu.ml.proppr.prove.DprProver) Prover(edu.cmu.ml.proppr.prove.Prover) DprProver(edu.cmu.ml.proppr.prove.DprProver) WamProgram(edu.cmu.ml.proppr.prove.wam.WamProgram) StateProofGraph(edu.cmu.ml.proppr.prove.wam.StateProofGraph) Feature(edu.cmu.ml.proppr.prove.wam.Feature) InferenceExample(edu.cmu.ml.proppr.examples.InferenceExample) APROptions(edu.cmu.ml.proppr.util.APROptions) Grounder(edu.cmu.ml.proppr.Grounder) Test(org.junit.Test)

Aggregations

Query (edu.cmu.ml.proppr.prove.wam.Query)28 Test (org.junit.Test)21 StateProofGraph (edu.cmu.ml.proppr.prove.wam.StateProofGraph)19 WamProgram (edu.cmu.ml.proppr.prove.wam.WamProgram)18 APROptions (edu.cmu.ml.proppr.util.APROptions)15 Prover (edu.cmu.ml.proppr.prove.Prover)14 ProofGraph (edu.cmu.ml.proppr.prove.wam.ProofGraph)12 File (java.io.File)12 StatusLogger (edu.cmu.ml.proppr.util.StatusLogger)11 InferenceExample (edu.cmu.ml.proppr.examples.InferenceExample)10 DprProver (edu.cmu.ml.proppr.prove.DprProver)10 GroundedExample (edu.cmu.ml.proppr.examples.GroundedExample)8 State (edu.cmu.ml.proppr.prove.wam.State)8 ConstantArgument (edu.cmu.ml.proppr.prove.wam.ConstantArgument)7 Feature (edu.cmu.ml.proppr.prove.wam.Feature)7 Map (java.util.Map)7 Grounder (edu.cmu.ml.proppr.Grounder)5 Goal (edu.cmu.ml.proppr.prove.wam.Goal)5 WamPlugin (edu.cmu.ml.proppr.prove.wam.plugins.WamPlugin)5 HashMap (java.util.HashMap)4