Search in sources :

Example 1 with SewingProverImpl

use of suite.lp.sewing.impl.SewingProverImpl in project suite by stupidsing.

the class CommandDispatcher method dispatchCommand_.

private boolean dispatchCommand_(String input, Writer writer) throws IOException {
    PrintWriter pw = new PrintWriter(writer);
    boolean code = true;
    Pair<InputType, String> pair = new CommandUtil<>(InputType.values()).recognize(input);
    InputType type = pair.t0;
    input = pair.t1.trim();
    if (input.endsWith("#"))
        input = String_.range(input, 0, -1);
    Node node = Suite.parse(input.trim());
    switch(type) {
        case EVALUATE:
            pw.println(Formatter.dump(evaluateFunctional(node)));
            break;
        case EVALUATEDO:
            node = Suite.applyPerform(node, Atom.of("any"));
            pw.println(Formatter.dump(evaluateFunctional(node)));
            break;
        case EVALUATEDOCHARS:
            node = Suite.applyPerform(node, Suite.parse("[n^Chars]"));
            printEvaluated(writer, node);
            break;
        case EVALUATEDOSTR:
            node = Suite.applyPerform(node, Atom.of("string"));
            printEvaluated(writer, Suite.applyWriter(node));
            break;
        case EVALUATEEFI:
            InterpretFunEager efi = new InterpretFunEager();
            efi.setLazyify(opt.isLazy());
            pw.println(efi.eager(node));
            break;
        case EVALUATELFI0:
            pw.println(new InterpretFunLazy0().lazy(node).get());
            break;
        case EVALUATELFI:
            pw.println(new InterpretFunLazy().lazy(node).get());
            break;
        case EVALUATESTR:
            node = Suite.substitute("string of .0", node);
            printEvaluated(writer, Suite.applyWriter(node));
            break;
        case EVALUATETYPE:
            pw.println(Formatter.dump(Suite.evaluateFunType(opt.fcc(node))));
            break;
        case FACT:
            ruleSet.addRule(Rule.of(node));
            break;
        case OPTION:
            Source<String> source = To.source(("-" + input).split(" "));
            String option;
            while ((option = source.source()) != null) opt.processOption(option, source);
            break;
        case PRETTYPRINT:
            pw.println(new PrettyPrinter().prettyPrint(node));
            break;
        case QUERY:
            code = query(new InterpretedProverBuilder(opt.pc(ruleSet)), ruleSet, node);
            break;
        case QUERYCOMPILED:
            code = query(CompiledProverBuilder.level1(opt.pc(ruleSet)), ruleSet, node);
            break;
        case QUERYCOMPILED2:
            if (builderLevel2 == null)
                builderLevel2 = CompiledProverBuilder.level2(opt.pc(ruleSet));
            code = query(builderLevel2, ruleSet, node);
            break;
        case QUERYELABORATE:
            elaborate(node, new Prover(opt.pc(ruleSet))::prove);
            break;
        case QUERYSEWING:
            code = query(new SewingProverBuilder(opt.pc(ruleSet)), ruleSet, node);
            break;
        case QUERYSEWINGELAB:
            elaborate(node, n -> new SewingProverImpl(ruleSet).prover(n).test(new ProverConfig(ruleSet)));
            break;
        case RESET:
            ruleSet = Suite.newRuleSet();
            importFiles(List.of());
    }
    pw.flush();
    return code;
}
Also used : InterpretFunLazy(suite.fp.InterpretFunLazy) SewingProverBuilder(suite.lp.search.SewingProverBuilder) InterpretFunLazy0(suite.fp.InterpretFunLazy0) Node(suite.node.Node) Prover(suite.lp.doer.Prover) InterpretFunEager(suite.fp.InterpretFunEager) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) PrettyPrinter(suite.node.pp.PrettyPrinter) InterpretedProverBuilder(suite.lp.search.InterpretedProverBuilder) ProverConfig(suite.lp.Configuration.ProverConfig) PrintWriter(java.io.PrintWriter)

Example 2 with SewingProverImpl

use of suite.lp.sewing.impl.SewingProverImpl in project suite by stupidsing.

the class SewingProverBuilder method build.

@Override
public Fun<Node, Finder> build(RuleSet ruleSet) {
    ProverFactory sewingProver = new SewingProverImpl(ruleSet);
    return goal -> {
        Node goal1 = SewingGeneralizerImpl.generalize(goal);
        Prove_ pred = sewingProver.prover(goal1);
        return (source, sink) -> {
            ProverConfig proverConfig1 = new ProverConfig(ruleSet, proverConfig);
            proverConfig1.setSource(source);
            proverConfig1.setSink(sink);
            pred.test(proverConfig1);
        };
    };
}
Also used : SewingGeneralizerImpl(suite.lp.sewing.impl.SewingGeneralizerImpl) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) Finder(suite.lp.search.ProverBuilder.Finder) ProverFactory(suite.lp.doer.ProverFactory) Builder(suite.lp.search.ProverBuilder.Builder) Prove_(suite.lp.doer.ProverFactory.Prove_) RuleSet(suite.lp.kb.RuleSet) Fun(suite.util.FunUtil.Fun) ProverConfig(suite.lp.Configuration.ProverConfig) Node(suite.node.Node) Node(suite.node.Node) ProverConfig(suite.lp.Configuration.ProverConfig) Prove_(suite.lp.doer.ProverFactory.Prove_) ProverFactory(suite.lp.doer.ProverFactory) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl)

Example 3 with SewingProverImpl

use of suite.lp.sewing.impl.SewingProverImpl in project suite by stupidsing.

the class SewingProverTest method testPerformance.

@Test
public void testPerformance() {
    RuleSet rs = Suite.newRuleSet();
    Atom pred = Atom.of("q");
    Atom tail = Atom.NIL;
    for (int i = 0; i < 65536; i++) rs.addRule(Rule.of(Tree.of(TermOp.IS____, Tree.of(TermOp.TUPLE_, pred, Int.of(i)), tail)));
    ProverFactory sp = new SewingProverImpl(rs);
    ProverConfig pc = new ProverConfig(rs);
    Prove_ test = sp.prover(Suite.parse("q 32768"));
    Source<Stopwatch<Boolean>> trial = () -> Stopwatch.of(() -> {
        boolean isOk = true;
        for (int i = 0; i < 65536; i++) isOk &= test.test(pc);
        assertTrue(isOk);
        return isOk;
    });
    for (int i = 0; i < 8; i++) trial.source();
    Stopwatch<Boolean> sw = trial.source();
    System.out.println(sw.duration);
    assertTrue(sw.duration < 300);
}
Also used : RuleSet(suite.lp.kb.RuleSet) ProverConfig(suite.lp.Configuration.ProverConfig) Stopwatch(suite.os.Stopwatch) Prove_(suite.lp.doer.ProverFactory.Prove_) Atom(suite.node.Atom) ProverFactory(suite.lp.doer.ProverFactory) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) Test(org.junit.Test)

Example 4 with SewingProverImpl

use of suite.lp.sewing.impl.SewingProverImpl in project suite by stupidsing.

the class SewingProverTest method testBacktrack.

@Test
public void testBacktrack() {
    RuleSet rs = Suite.newRuleSet();
    Suite.addRule(rs, "mem (.e, _) .e");
    Suite.addRule(rs, "mem (_, .tail) .e :- mem .tail .e");
    Suite.addRule(rs, "q .c .v :- once (mem (0,) .v), .a/.b/.c = 0/0/0; mem (1,) .v, .a/.b/.c = 1/1/1");
    Suite.addRule(rs, "r .c :- q .c .v, .v = 1");
    ProverFactory sp = new SewingProverImpl(rs);
    ProverConfig pc = new ProverConfig(rs);
    assertTrue(sp.prover(new Generalizer().generalize(Suite.parse("r .c"))).test(pc));
}
Also used : RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) ProverConfig(suite.lp.Configuration.ProverConfig) ProverFactory(suite.lp.doer.ProverFactory) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl) Test(org.junit.Test)

Example 5 with SewingProverImpl

use of suite.lp.sewing.impl.SewingProverImpl in project suite by stupidsing.

the class ProverFactoryTest method test.

private void test(String query, boolean result) {
    for (ProverFactory pf : new ProverFactory[] { // 
    new CompileProverImpl(), new SewingProverImpl(Suite.newRuleSet(List.of("auto.sl"))) }) {
        Prove_ p = pf.prover(Suite.parse(query));
        assertEquals(result, p.test(new ProverConfig()));
    }
}
Also used : ProverConfig(suite.lp.Configuration.ProverConfig) CompileProverImpl(suite.lp.compile.impl.CompileProverImpl) Prove_(suite.lp.doer.ProverFactory.Prove_) SewingProverImpl(suite.lp.sewing.impl.SewingProverImpl)

Aggregations

ProverConfig (suite.lp.Configuration.ProverConfig)9 SewingProverImpl (suite.lp.sewing.impl.SewingProverImpl)9 ProverFactory (suite.lp.doer.ProverFactory)7 RuleSet (suite.lp.kb.RuleSet)7 Test (org.junit.Test)6 Prove_ (suite.lp.doer.ProverFactory.Prove_)3 Generalizer (suite.lp.doer.Generalizer)2 Node (suite.node.Node)2 PrintWriter (java.io.PrintWriter)1 InterpretFunEager (suite.fp.InterpretFunEager)1 InterpretFunLazy (suite.fp.InterpretFunLazy)1 InterpretFunLazy0 (suite.fp.InterpretFunLazy0)1 CompileProverImpl (suite.lp.compile.impl.CompileProverImpl)1 Prover (suite.lp.doer.Prover)1 InterpretedProverBuilder (suite.lp.search.InterpretedProverBuilder)1 Builder (suite.lp.search.ProverBuilder.Builder)1 Finder (suite.lp.search.ProverBuilder.Finder)1 SewingProverBuilder (suite.lp.search.SewingProverBuilder)1 SewingGeneralizerImpl (suite.lp.sewing.impl.SewingGeneralizerImpl)1 Atom (suite.node.Atom)1