private GraphNet LoadSparqlGraphNet()
        {
            var gn = new SparqlSyntaxNet();

            //gn.MaxNumberOfPaths = 5;
            //gn.MaxPathLenght = 10;
            gn.RegisterDynamic("parse", (node, graph) => {
                node.ShortId = node.ShortId.Replace(SparqlSyntaxNet.IRI_PREFIX, "");
                var q        = new QueryGraphNet("x")
                {
                    QueryText = node.ShortId
                };
                var current = q.LastWordAsNode();
                if (current != null)
                {
                    node.AddEdge(graph.Node("current"), graph.Node(current.ShortId));
                }

                Node.BaseOnAdd(node, graph);
            });
            gn.DefaultInput = "parse";

            gn.TrainFromQueries(
                "select * where { ?s ?p ?o }",
                "select ?p where { ?s ?p ?o }",
                "select * where { ?s <http://test.com/name> ?name }",
                "select * where { ?s <http://test.com/other> ?other }"
                );


            return(gn);
        }
        private GraphNet LoadQueryNet()
        {
            var gn = new QueryGraphNet("Test_Query");

            gn.LimitNumberOfPaths = true;
            gn.PrefixLoader       = (uri, graph) =>
            {
                //TODO: I think this loader should just return the nodes should this
                var net = graph as PredicateNet;
                if (net != null)
                {
                    net.Add("p:name", "http://www.w3.org/2000/01/rdf-schema#domain", "p:Place");
                    net.Add("p:code", "http://www.w3.org/2000/01/rdf-schema#domain", "p:Place");
                    net.Add(net.GetPrefixNamespaceNode(), net.Node("has"), graph.Node("p:name"));
                    net.Add(net.GetPrefixNamespaceNode(), net.Node("has"), graph.Node("p:code"));
                }
            };

            gn.RegisterDynamic("parse", (node, graph) => {
                gn.QueryText = node.ShortId.ToString().Replace("http://graph.network.com/ld/", "");
                node.ShortId = gn.QueryText;
                var lastWord = gn.LastWordAsNode();
                if (lastWord != null && node.Edges.Count == 0)
                {
                    node.AddEdge(graph.Node("current"), lastWord);
                }
                Node.BaseOnAdd(node, graph);
            });
            gn.DefaultInput = "parse";

            gn.TrainFromQueries(
                "PREFIX p: <http://test.com/places/> select * where { ?s a p:City . ?s p:mayor ?mayor }",
                "PREFIX p: <http://test.com/places/> select * where { ?s a p:Country . ?s p:flag ?flag }",
                "select * where { ?s p:name ?name }",
                "select * where { ?s ?p ?o }"
                );

            return(gn);
        }
示例#3
0
        public void TestSimpleQuery()
        {
            var gn = new QueryGraphNet("Test_Query", maxNumberOfPaths: 10, maxPathLenght: 20);

            gn.LimitNumberOfPaths = true;
            gn.PrefixLoader       = (uri, graph) =>
            {
                graph.Add("p:name", "http://www.w3.org/2000/01/rdf-schema#domain", "p:Place");
                graph.Add("p:code", "http://www.w3.org/2000/01/rdf-schema#domain", "p:Place");
                var net = graph as PredicateNet;
                net.Add(net.GetPrefixNamespaceNode(), net.Node("has"), graph.Node("p:name"));
                net.Add(net.GetPrefixNamespaceNode(), net.Node("has"), graph.Node("p:code"));
            };

            //TODO: move this code into the QueryGraphNet as really it is core to how it works
            gn.RegisterDynamic("parse", (node, graph) => {
                gn.QueryText = node.ShortId.ToString().Replace("http://graph.network.com/ld/", "");
                var lastWord = gn.LastWordAsNode();
                if (lastWord != null && node.Edges.Count == 0)
                {
                    node.AddEdge(graph.Node("current"), lastWord);
                }
                Node.BaseOnAdd(node, graph);
            });
            gn.DefaultInput = "parse";

            gn.TrainFromQueries(
                "PREFIX p: <http://test.com/places/> select * where { ?s a p:City . ?s p:name ?mayor }",
                "PREFIX p: <http://test.com/places/> select * where { ?s a p:Country . ?s p:name ?flag }"
                );

            Assert.AreEqual("where", gn.Predict("PREFIX p: <http://test.com/places/> SELECT *"));
            Assert.AreEqual("{", gn.Predict("PREFIX p: <http://test.com/places/> SELECT * WHERE"));
            Assert.AreEqual("name", gn.Predict("PREFIX p: <http://test.com/places/> SELECT * WHERE {?s p:}"));
            //TODO: Assert.AreEqual("p:name", gn.Predict("PREFIX p: <http://test.com/places/> SELECT * WHERE {?s }"));
        }