コード例 #1
0
 public PNode(string plMt, SIProlog prolog, Graph data)
 {
     this.prologEngine = prolog;
     this.id           = plMt;
     prolog.KBGraph.AddNode(this);
     prologEngine.GraphForMT[plMt] = this;
     prologEngine.RegisterHomeGraph(data.BaseUri.AbsoluteUri, data, true);
     pdb = new PDB(true);
     pdb.RegisterRuleList(this);
     pdb.startMt        = plMt;
     pdb.followedGenlMt = false;
     PrologKB.id        = plMt;
     _rdfGraph          = data;
     EnsureGraphPrefixes(rdfGraph);
 }
コード例 #2
0
        public void GenRulesFromMt(SIProlog pEngine, string sourceMt, string destMt, string targetAttribute)
        {
            MtDataSource samples = new MtDataSource(pEngine, sourceMt);

            TreeAttributeCollection attributes = samples.GetValidAttributeCollection(targetAttribute);

            DecisionTree id3  = new DecisionTree();
            TreeNode     root = id3.mountTree(samples, targetAttribute, attributes);

            string prologCode = PrologPrintNode(root, "", targetAttribute);

            pEngine.insertKB(prologCode, destMt);
            string codeSummary = PrintNode(root, "") + Environment.NewLine + PrologPrintNode(root, "", "result");

            Console.WriteLine(codeSummary);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: hackerlank/trunk-chatbot
        public static void RunAllTests(SIProlog testIt)
        {
            if (runningTests != null)
            {
                SIProlog.Warn("Already started tests");
                return;
            }
            runningTests = testIt;
            prologEngine = testIt;
            //testProlog4();
            testPrologBuiltins();
            //testRDFServer();
            testCema();
            testSat();
            //return;
            testProlog1();
            testProlog2();
            testKBload();
            testProlog3();
            testPrologBuiltins();
            prologEngine.mtest();
            if (SIProlog.RdfDeveloperSanityChecks > 2)
            {
                prologEngine.askQuery("triple(SUBJECT,PRED,OBJ)", "testRDF");
                if (prologEngine.HasKBNamed("dbpediaRdfMemory"))
                {
                    prologEngine.askQuery("triple(SUBJECT,PRED,OBJ)", "dbpediaRdfMemory");
                }
            }
            testProlog4();
            //p1();
            p2();
            p3();

            ourFilter.defMeanParticle();
            //ourFilter.dump();
            Console.WriteLine("meanP raw:{0}", ourFilter.meanParticle.ToString());
            ourFilter.meanParticle.normalize(ourFilter.constraintSet);
            Console.WriteLine("meanP norm:{0}", ourFilter.meanParticle.ToString());
            Console.WriteLine("done");
        }
コード例 #4
0
        public void GenRulesFromMt(SIProlog pEngine, string sourceMt, string destMt)
        {
            string targetAttr = "result";

            List <Dictionary <string, string> > bingingsList = new List <Dictionary <string, string> >();
            //Get collection of instances ID's and Attribute Names
            string query = "targetAttribute(ATTRIBUTE)";

            pEngine.askQuery(query, sourceMt, out bingingsList);
            foreach (Dictionary <string, string> bindings in bingingsList)
            {
                foreach (string k in bindings.Keys)
                {
                    if (k == "ATTRIBUTE")
                    {
                        targetAttr = bindings[k];
                    }
                }
            }

            GenRulesFromMt(pEngine, sourceMt, destMt, targetAttr);
        }
コード例 #5
0
            static public Part RdfToPart(INode node, RdfRules triples)
            {
                SIProlog.checkNode(node);
                if (node is IVariableNode)
                {
                    var vnode = (IVariableNode)node;
                    return(new Variable(vnode.VariableName));
                }
                if (node is IGraphLiteralNode)
                {
                    var vnode = (IGraphLiteralNode)node;
                    throw ErrorBadOp("RDFToPart: on " + vnode);
                }
                if (node is IBlankNode)
                {
                    var vnode = (IBlankNode)node;
                    var atom  = Atom.MakeNodeAtom(vnode);
                    return(atom);

                    node = triples.def.CreateUriNode(UriFactory.Create("_:" + vnode.InternalID));
                }

                if (node is IUriNode)
                {
                    var vnode = (IUriNode)node;
                    var atom  = Atom.MakeNodeAtom(vnode);
                    return(atom);
                }

                // all the below are now Literal Nodes of some type  (we divide into  "strings", numbers and "strings with"^"meaning" and
                ILiteralNode litnode = node as ILiteralNode;

                if (litnode == null)
                {
                    throw ErrorBadOp("Cant find the nodetype on  " + node);
                }
                return(Atom.MakeNodeAtomFixme(litnode));
            }
コード例 #6
0
 public MtDataSource(SIProlog pengine, string sourceMt)
 {
     _sourceMt    = sourceMt;
     prologEngine = pengine;
     load();
 }
コード例 #7
0
        public void commitSolution(GoapState cState, string solutionMt, string nowMt, string backgroundMt)
        {
            prologEngine.markKBScratchpad(solutionMt);
            planNode = cState;
            // Modules/Actions are in reverse order from now to goal so flip them
            cState.modList.Reverse();

            // Make final connections
            if (backgroundMt != null)
            {
                prologEngine.connectMT(solutionMt, backgroundMt);
            }
            foreach (string moduleMt in cState.modList)
            {
                prologEngine.connectMT(solutionMt, moduleMt);
            }

            // Post stats and planner state
            string postScript = "";

            postScript += String.Format("g({0}).\n", cState.costSoFar());
            postScript += String.Format("h({0}).\n", cState.distToGoal());
            postScript += String.Format("f({0}).\n", cState.costSoFar() + cState.distToGoal() * problemWorstCost);
            postScript += String.Format("worst({0}).\n", problemWorstCost);
            postScript += String.Format("openedNodes({0}).\n", openSet.Count);
            postScript += String.Format("closedNodes({0}).\n", closedSet.Count);
            postScript += String.Format("totalNodes({0}).\n", openSet.Count + closedSet.Count);

            if (cState.distToGoal() == 0)
            {
                postScript += "planstate(solved).\n";
            }
            else
            {
                postScript += "planstate(unsolved).\n";
            }

            prologEngine.appendKB(postScript, solutionMt);


            // post the modules used
            string modString = "";

            if (cState.modList.Count > 0)
            {
                foreach (string m in cState.modList)
                {
                    modString += m + " ";
                }
                prologEngine.appendListPredToMt("modlist", modString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("modlist([]).\n", solutionMt);
            }

            string planSequence = "";
            int    planCount    = 0;

            if (cState.modList.Count > 0)
            {
                foreach (string m in cState.modList)
                {
                    planSequence += String.Format("planraw({0}).\n", m);
                }
                foreach (string m in cState.modList)
                {
                    planSequence += String.Format("planseq({0},{1}).\n", planCount, m);
                    planCount++;
                }
                prologEngine.appendKB(planSequence, solutionMt);
            }
            else
            {
                prologEngine.appendKB("planraw(nop).\n planseq(0,nop).\n", solutionMt);
            }
            //post anything missing.

            if (cState.missingList.Count > 0)
            {
                string missingString = "";
                foreach (string m in cState.missingList)
                {
                    missingString += " " + m;
                }
                prologEngine.appendListPredToMt("missing", missingString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("missing([]).\n", solutionMt);
            }
            tickEnd = Environment.TickCount;
            int elapsed    = tickEnd - tickBegin;
            int totalNodes = openSet.Count + closedSet.Count;

            SIProlog.ConsoleWriteLine("Planning time = {0}", elapsed);
            SIProlog.ConsoleWriteLine("Planning list = '{0}'", modString);

            SIProlog.ConsoleWriteLine("Planning tials = {0}", trials);
            SIProlog.ConsoleWriteLine("TotalNodes = {0}", totalNodes);
            if (trials > 0)
            {
                SIProlog.ConsoleWriteLine("Planning ms/trials = {0}", ((double)elapsed / (double)trials));
            }
            if (totalNodes > 0)
            {
                double mspn = ((double)elapsed / (double)totalNodes);
                SIProlog.ConsoleWriteLine("Planning ms/nodes = {0}", mspn);
                if (mspn > 0)
                {
                    SIProlog.ConsoleWriteLine("Planning @ nodes/sec = {0}", 1000 / mspn);
                }
            }
            if (elapsed > 0)
            {
                SIProlog.ConsoleWriteLine("Planning trials/ms = {0}", ((double)trials / (double)elapsed));
                SIProlog.ConsoleWriteLine("Planning nodes/ms = {0}", ((double)totalNodes / (double)elapsed));
            }

            SIProlog.ConsoleWriteLine(postScript);
            prologEngine.markKBNonScratchPad(solutionMt);
        }
コード例 #8
0
 public GOAPSolver(SIProlog prologEng)
 {
     prologEngine = prologEng;
 }
コード例 #9
0
ファイル: Cema.cs プロジェクト: hackerlank/trunk-chatbot
        public void commitSolution(CemaState cState, string solutionMt, string problemMt)
        {
            planNode = cState;
            // Post stats and planner state
            string postScript = "";

            postScript += String.Format("g({0}).\n", cState.costSoFar());
            postScript += String.Format("h({0}).\n", cState.distToGoal());
            postScript += String.Format("f({0}).\n", cState.costSoFar() + cState.distToGoal() * problemWorstCost);
            postScript += String.Format("worst({0}).\n", problemWorstCost);
            postScript += String.Format("openedNodes({0}).\n", openSet.Count);
            postScript += String.Format("closedNodes({0}).\n", closedSet.Count);
            postScript += String.Format("totalNodes({0}).\n", openSet.Count + closedSet.Count);

            if (cState.distToGoal() == 0)
            {
                postScript += "planstate(solved).\n";
            }
            else
            {
                postScript += "planstate(unsolved).\n";
            }

            prologEngine.appendKB(postScript, solutionMt);

            // post the modules used
            string modString = "";

            if (cState.modList.Count > 0)
            {
                foreach (string m in cState.modList)
                {
                    modString += " " + m;
                }
                prologEngine.appendListPredToMt("modlist", modString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("modlist([]).\n", solutionMt);
            }
            //post anything missing.

            if (cState.missingList.Count > 0)
            {
                string missingString = "";
                foreach (string m in cState.missingList)
                {
                    missingString += " " + m;
                }
                prologEngine.appendListPredToMt("missing", missingString, solutionMt);
            }
            else
            {
                prologEngine.appendKB("missing([]).\n", solutionMt);
            }
            tickEnd = Environment.TickCount;
            int elapsed    = tickEnd - tickBegin;
            int totalNodes = openSet.Count + closedSet.Count;

            SIProlog.ConsoleWriteLine("Inventing time = {0}", elapsed);
            SIProlog.ConsoleWriteLine("Inventing list = {0}", modString);

            SIProlog.ConsoleWriteLine("Inventing tials = {0}", trials);
            SIProlog.ConsoleWriteLine("TotalNodes = {0}", totalNodes);
            if (trials > 0)
            {
                SIProlog.ConsoleWriteLine("Inventing ms/trials = {0}", ((double)elapsed / (double)trials));
            }
            if (totalNodes > 0)
            {
                double mspn = ((double)elapsed / (double)totalNodes);
                SIProlog.ConsoleWriteLine("Inventing ms/nodes = {0}", mspn);
                if (mspn > 0)
                {
                    SIProlog.ConsoleWriteLine("Inventing @ nodes/sec = {0}", 1000 / mspn);
                }
            }
            if (elapsed > 0)
            {
                SIProlog.ConsoleWriteLine("Inventing trials/ms = {0}", ((double)trials / (double)elapsed));
                SIProlog.ConsoleWriteLine("Inventing nodes/ms = {0}", ((double)totalNodes / (double)elapsed));
            }

            SIProlog.ConsoleWriteLine(postScript);
        }
コード例 #10
0
ファイル: Cema.cs プロジェクト: hackerlank/trunk-chatbot
 public CemaSolver(SIProlog prologEng)
 {
     prologEngine = prologEng;
 }
コード例 #11
0
        private void GetMiniMt(object results, string assertTemplate, string graphKBName, PNode repo, bool show, ICollection <Rule> newRules, TextWriter ruleSources, RdfRules ruleDefs)
        {
            assertTemplate = assertTemplate ?? "triple($?s$,$?p$,$?o$).\n";
            bool MakeRules = newRules != null && assertTemplate.Trim().EndsWith(".");
            var  outMap    = new Dictionary <string, string>();

            outMap["s"]  = "unknownSubject";
            outMap["p"]  = "unknownPredicate";
            outMap["o"]  = "unknownObject";
            outMap["mt"] = Atom.aq(repo.Id);

            if (results is SparqlResultSet)
            {
                //SELECT/ASK queries give a SparqlResultSet
                SparqlResultSet rset = (SparqlResultSet)results;
                if (show)
                {
                    ConsoleWriteLine("SparqlResultSet.Count = {0}", rset.Count);
                    ConsoleWriteLine("SparqlResultSet:{0}", rset.ToString());
                }
                foreach (SparqlResult r in rset)
                {
                    //Do whatever you want with each Result
                    if (show)
                    {
                        ConsoleWriteLine("SparqlResult.Count = {0}", r.Count);
                        ConsoleWriteLine("SparqlResult:{0}", r.ToString());
                    }

                    var assertIt = assertTemplate;
                    //Do whatever you want with each Triple
                    foreach (string vname in r.Variables)
                    {
                        INode value0 = r[vname];
                        SIProlog.checkNode(value0);
                        //Graph into = FindGraph(baseURI.AbsoluteUri);
                        INode  value  = value0.CopyWNode(repo.rdfGraph);
                        string strVal = GraphWithDef.PlReadble(value, ruleDefs);
                        assertIt = assertIt.Replace("$?" + vname + "$", strVal);
                        if (show)
                        {
                            ConsoleWriteLine("BIND: {0} = {1}", vname, strVal);
                        }
                    }
                    if (assertIt.Contains("$?s$"))
                    {
                        foreach (KeyValuePair <string, string> map in outMap)
                        {
                            assertIt = assertIt.Replace("$?" + map.Key + "$", map.Value);
                        }
                    }
                    if (MakeRules)
                    {
                        Rule rule = ParseRule(new Tokeniser(assertIt), graphKBName);
                        if (show)
                        {
                            ConsoleWriteLine("RULE_IG: {0}", rule);
                        }
                        newRules.Add(rule);
                    }
                    else
                    {
                        if (show)
                        {
                            ConsoleWriteLine("TRIPLE_IG: {0}", assertIt);
                        }
                    }
                    if (ruleSources != null)
                    {
                        ruleSources.WriteLine(assertIt);
                    }
                }
            }
            else if (results is IGraph)
            {
                //CONSTRUCT/DESCRIBE queries give a IGraph
                IGraph resGraph = (IGraph)results;
                var    rset     = resGraph.Triples;
                outMap["mt"] = "<" + resGraph.BaseUri.AbsoluteUri + ">";
                if (show)
                {
                    ConsoleWriteLine("IGraphResultSet.Count = {0}", rset.Count);
                    ConsoleWriteLine("IGraphResultSet:{0}", rset.ToString());
                }
                foreach (Triple t in rset)
                {
                    var assertIt = assertTemplate;
                    //Do whatever you want with each Triple
                    outMap["s"] = GraphWithDef.PlReadble(t.Subject, ruleDefs);
                    outMap["p"] = GraphWithDef.PlReadble(t.Predicate, ruleDefs);
                    outMap["o"] = GraphWithDef.PlReadble(t.Object, ruleDefs);
                    foreach (KeyValuePair <string, string> map in outMap)
                    {
                        assertIt = assertIt.Replace("$?" + map.Key + "$", map.Value);
                    }
                    if (MakeRules)
                    {
                        Rule rule = ParseRule(new Tokeniser(assertIt), graphKBName);
                        if (show)
                        {
                            ConsoleWriteLine("RULE_IG: {0}", rule);
                        }
                        newRules.Add(rule);
                    }
                    else
                    {
                        if (show)
                        {
                            ConsoleWriteLine("TRIPLE_IG: {0}", assertIt);
                        }
                    }
                    if (ruleSources != null)
                    {
                        ruleSources.WriteLine(assertIt);
                    }
                }
            }
            else
            {
                //If you don't get a SparqlResutlSet or IGraph something went wrong
                //but didn't throw an exception so you should handle it here
                if (results == null)
                {
                    throw ErrorBadOp("ERROR: no Results From NULL Query Object for " + graphKBName);
                }
                throw ErrorBadOp("ERROR: Cant how understand " + results.GetType() + " " + results + " to import to " +
                                 graphKBName);
            }
        }