コード例 #1
0
            public void AddRuleToRDF(Rule rule)
            {
                if (rule.OptionalHomeMt == id)
                {
                    if (SelfHostedRdfGraph)
                    {
                        return;
                    }
                }
                if (rule.rdfRuleCache != null)
                {
                    return;
                }
                if (prologEngine.DontRDFSync)
                {
                    Warn("Trying to Add Rule TO rdf: " + rule);
                    return;
                }
                string before   = rule.ToSource(SourceLanguage.Prolog);
                var    rdfRules = GraphWithDef.FromRule(rule, this, rdfGraph);

                rdfRules.AssertTriples(rdfGraph, true, true);
                string after = rule.ToSource(SourceLanguage.Prolog);

                if (before != after)
                {
                    Warn("Manipulated rule: " + before + "->" + after);
                }
            }
コード例 #2
0
            internal void pushGraphToKB(bool clearPrologFirst)
            {
                SaveOffRDF();
                var focus = PrologKB;

                WarnAndClear(focus, ContentBackingStore.RdfMemory, clearPrologFirst, focus.ClearPrologCache,
                             ContentBackingStore.Prolog);
                var trips = Triples;

                if (trips.Count == 0)
                {
                    return;
                }
                lock (trips)
                {
                    focus.pdb.index.Clear();
                    foreach (Triple triple in trips)
                    {
                        RdfRules rules = new RdfRules(rdfGraph);
                        var      term  = MakeTerm(TripleName,
                                                  GraphWithDef.RdfToPart(triple.Subject, rules),
                                                  GraphWithDef.RdfToPart(triple.Predicate, rules),
                                                  GraphWithDef.RdfToPart(triple.Object, rules));
                        var rule = new Rule(term);
                        rule.rdfRuleCache = rules;
                        if (!focus.pdb.rules.Contains(rule))
                        {
                            focus.pdb.rules.Add(rule);
                        }
                    }
                }
            }
コード例 #3
0
        public void refreshRDFGraphOLD()
        {
            var rdfGraphWithDefs = FindOrCreateKB("rdfMT");
            var rdfGraph         = rdfGraphWithDefs.RdfStore.rdfGraph;

            EnsureGraphPrefixes(rdfGraph);
            // Possibly called by the Sparql endpoint before servicing a query
            // Is there anything we want to update rdfGraph with ?
            var    bingingsList = new ListOfBindings();
            string mt           = "spindleMT";

            askQuery(ParseQuery("triple(S,P,O)", mt), mt, true, bingingsList, null);
            RdfRules newTriples = new RdfRules(rdfGraph);

            foreach (var bindings in bingingsList)
            {
                //foreach (string k in bindings.Keys)
                //{
                rdfGraphAssert(rdfGraph,
                               MakeTriple(GraphWithDef.PartToRdf(bindings["S"], newTriples),
                                          GraphWithDef.PartToRdf(bindings["P"], newTriples),
                                          GraphWithDef.PartToRdf(bindings["O"], newTriples)));

                string rdfLine = String.Format(@"<robokind:{0}> <robokind:{1}> <robokind:{2}> .", bindings["S"].ToString(), bindings["P"].ToString(), bindings["O"].ToString());
                StringParser.Parse(rdfGraph, rdfLine);
                // }
            }
            newTriples.AssertTriples(rdfGraphWithDefs.definations, true, true);
        }
コード例 #4
0
 public bool AssertTripleFromGraph(Triple triple, IGraph listResolves, bool allowDuplicates, bool keepOriginalGraphUri)
 {
     lock (CompileLock)
     {
         return(GraphWithDef.InCompiler(this, null, id,
                                        () =>
                                        AssertTriple_ul(triple, listResolves, allowDuplicates,
                                                        keepOriginalGraphUri)));
     }
 }
コード例 #5
0
        public static Triple MakeTriple(INode s, INode p, INode o, bool toplevel)
        {
            IGraph sGraph = s.Graph ?? o.Graph ?? p.Graph;

            p = p.CopyWNode(sGraph);
            o = o.CopyWNode(sGraph);
            s = s.CopyWNode(sGraph);
            Triple          newTriple = new Triple(s, p, o);
            string          warn0     = "";
            Action <string> warn      = s0 =>
            {
                warn0 += s0 + " \n";
            };

            if (p.NodeType != NodeType.Uri)
            {
                warn("pred " + p + " is not URI");
            }
            if (s.NodeType == NodeType.GraphLiteral)
            {
                if (toplevel)
                {
                    IUriNode uri = p as IUriNode;
                    if (uri == null || !uri.Uri.ToString().Contains("log"))
                    {
                        warn("s = not logical (" + s + ")");
                    }
                }
            }
            else
            {
                if (s is ILiteralNode)
                {
                    warn("s = LiteralNode (" + s + ")");
                }
                if (toplevel && !GraphWithDef.CanBeSubjectNode(s))
                {
                    warn("s = !CanBeSubjectNode (" + s + ")");
                }
            }
            if (p is ILiteralNode)
            {
                warn("p = LiteralNode (" + p + ")");
            }
            if (warn0 != "")
            {
                warn("bad triple =" + newTriple);
                Warn(warn0);
            }
            return(newTriple);
        }
コード例 #6
0
            public Rule TripleToRule(Triple triple, IGraph listResolves, bool keepOriginalGraphUri)
            {
                var    s  = triple.Subject;
                var    p  = triple.Predicate;
                var    o  = triple.Object;
                string sp = triple.Predicate.ToString();

                if (!GlobalSharedSettings.TODO1Completed)
                {
                    //RdfRules rules = new RdfRules(rdfGraph);
                    Term t    = MakeTerm(TripleName, Atom.MakeNodeAtom(s), Atom.MakeNodeAtom(p), Atom.MakeNodeAtom(o));
                    var  rule = new Rule(t);
                    //rule.rdfRuleCache = rules;
                    return(rule);
                }
                var pp = Atom.MakeNodeAtom(p);

                if (sp == RdfSpecsHelper.RdfListFirst || sp == RdfSpecsHelper.RdfListRest)
                {
                    var rule =
                        new Rule(new Term(pp.fname, false, new PartListImpl(Atom.MakeNodeAtom(s), Atom.MakeNodeAtom(o))));
                    return(rule);
                }
                int argNum = GraphWithDef.GetInstanceOnArg(sp);

                if (argNum == -1)
                {
                    // assume it's one to one?
                    argNum = 0;
                }
                if (argNum != 0)
                {
                    //1: [a f (b c)]
                    //2: [b f (a c)]
                    //3: [c f (a b)]
                    PartListImpl parts = new PartListImpl();
                    AddRdfList(parts, o, argNum, s, triple, listResolves);
                    var rule = new Rule(new Term(pp.fname, false, parts));
                    return(rule);
                }
                // pred + args are in the list [db1 f (a b c)]
                if (argNum == 0)
                {
                    PartListImpl parts = new PartListImpl();
                    AddRdfList(parts, o, -1, null, triple, listResolves);
                    var rule = new Rule(new Term(pp.fname, false, parts));
                    return(rule);
                }
                return(null);
            }
コード例 #7
0
        public static void DocumentTerm(Term term, bool varnamesOnly)
        {
            bool newlyCreated;
            PredicateProperty pp = GraphWithDef.GetPredDef(term.fname, term.Arity, out newlyCreated);
            int argNum           = 0;

            foreach (Part part in term.ArgList)
            {
                argNum++;
                if (varnamesOnly)
                {
                    if (!(part is Variable))
                    {
                        continue;
                    }
                }
                var argDef = GraphWithDef.GetAdef(pp, argNum, true);
                argDef.AddRangeTypeName(part.Text);
            }
        }
コード例 #8
0
 public void AddRequirement(INode s, string sp, Part o)
 {
     AddRequirement(MakeTriple(s,
                               GraphWithDef.PredicateToProperty(sp),
                               GraphWithDef.PartToRdf(o, this), false));
 }
コード例 #9
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);
            }
        }
コード例 #10
0
        public void pushRulesToGraph(string mt, PNode rdfGraphWithDefs, bool includeInherited)
        {
            // Possibly called by the Sparql endpoint before servicing a query
            // Is there anything we want to update rdfGraph with ?
            PNode focus        = FindOrCreateKB(mt);
            var   bingingsList = new ListOfBindings();

            askQuery(ParseQuery("triple(S,P,O)", mt), mt, includeInherited, bingingsList, null);
            bool useTripeQuery = true;

            if (bingingsList == null || bingingsList.Count <= 0)
            {
                useTripeQuery = false;
                var triples = findVisibleKBRules(mt, new List <string>(), includeInherited);
                foreach (Rule rule in triples)
                {
                    try
                    {
                        rdfGraphWithDefs.AddRuleToRDF(rule);
                    }
                    catch (Exception e)
                    {
                        Warn(e);
                    }
                }
                return;
            }

            var rdfGraph = rdfGraphWithDefs.rdfGraph;

            if (!useTripeQuery)
            {
                return;
            }
            var newTriples = new RdfRules(rdfGraphWithDefs.definations);

            EnsureGraphPrefixes(rdfGraph);
            foreach (Dictionary <string, Part> bindings in bingingsList)
            {
                Part  psubj = bindings["S"];
                Part  ppred = bindings["P"];
                Part  pobj  = bindings["O"];
                INode subj  = GraphWithDef.PartToRdf(psubj, newTriples);
                INode pred  = GraphWithDef.PartToRdf(ppred, newTriples);
                INode obj   = GraphWithDef.PartToRdf(pobj, newTriples);
                if (subj is ILiteralNode)
                {
                    Warn("Subj was a literal (not supported) [{0} {1} {2}]", subj, pred, obj);
                    continue;
                }
                if (!(pred is IUriNode))
                {
                    Warn("Pred was not a uri (not supported) [{0} {1} {2}]", subj, pred, obj);
                    continue;
                }
                //foreach (string k in bindings.Keys)
                //{
                rdfGraphAssert(rdfGraph, MakeTriple(subj, pred, obj));
                //string rdfLine = String.Format(@"<{0}> <{1}> <{2}> .", bindings["S"].ToString(), bindings["P"].ToString(), bindings["O"].ToString());
                //StringParser.Parse(rdfGraph, rdfLine);
                // }
            }
            newTriples.RequirementsMet = true;
            newTriples.AssertTriples(rdfGraphWithDefs.definations, true, true);
        }