コード例 #1
0
        public void TestQueryByMultipleIds()
        {
            var ids = new List <string> {
                "Q26", "Q27", "Q29"
            };
            const string indexPath = "Resources/IndexMultiple";

            Assert.True(Directory.Exists(indexPath));

            var entities = new BatchIdEntityQuery(indexPath, ids).Query().ToList();

            Assert.Equal(3, entities.Count);

            //Q26, Q27, Q29
            var doc = entities.ElementAt(0);

            Assert.NotNull(doc);
            Assert.Equal("Q26", doc.Id);
            Assert.Equal("Northern Ireland", doc.Label);

            doc = entities.ElementAt(1);
            Assert.NotNull(doc);
            Assert.Equal("Q27", doc.Id);
            Assert.Equal("Ireland", doc.Label);

            doc = entities.ElementAt(2);
            Assert.NotNull(doc);
            Assert.Equal("Q29", doc.Id);
            Assert.Equal("Spain", doc.Label);
        }
コード例 #2
0
        private void RunEdgeQueries(QueryGraph graph)
        {
            foreach (var edge in graph.Edges.Select(x => x.Value).Where(x => !x.AvoidQuery))
            {
                var sourceNode = edge.GetSourceNode(graph);
                var targetNode = edge.GetTargetNode(graph);

                var possibleProperties = new List <string>();

                if (sourceNode.IsConstant || targetNode.IsConstant)
                {
                    var sourceGivenPropertiesIds = new BatchIdEntityQuery(graph.EntitiesIndexPath, sourceNode.Types)
                                                   .Query().SelectMany(x => x.Properties).Select(x => x.Id);

                    var targetGivenPropertiesIds = new BatchIdEntityQuery(graph.EntitiesIndexPath, targetNode.Types)
                                                   .Query().SelectMany(x => x.ReverseProperties).Select(x => x.Id);

                    possibleProperties = possibleProperties.IntersectIfAny(sourceGivenPropertiesIds)
                                         .IntersectIfAny(targetGivenPropertiesIds).ToList();
                }
                else
                {
                    if (sourceNode.IsInstanceOf || targetNode.IsInstanceOf)
                    {
                        var instanceOfSourceProperties =
                            InMemoryQueryEngine.BatchEntityIdOutgoingPropertiesQuery(sourceNode.ParentTypes);
                        var instanceOfTargetProperties =
                            InMemoryQueryEngine.BatchEntityIdIncomingPropertiesQuery(targetNode.ParentTypes);
                        possibleProperties = possibleProperties.IntersectIfAny(instanceOfSourceProperties)
                                             .IntersectIfAny(instanceOfTargetProperties).ToList();
                    }

                    var sourceGivenOutgoingEdges = sourceNode.GetOutgoingEdges(graph).Where(x => x.IsConstant)
                                                   .Where(x => !x.IsInstanceOf).ToArray();
                    var sourceGivenIncomingEdges = sourceNode.GetIncomingEdges(graph).Where(x => x.IsConstant)
                                                   .Where(x => !x.IsInstanceOf).ToArray();
                    var targetGivenOutgoingEdges = targetNode.GetOutgoingEdges(graph).Where(x => x.IsConstant)
                                                   .Where(x => !x.IsInstanceOf).ToArray();
                    var targetGivenIncomingEdges = targetNode.GetIncomingEdges(graph).Where(x => x.IsConstant)
                                                   .Where(x => !x.IsInstanceOf).ToArray();

                    foreach (var givenOutgoingEdge in sourceGivenOutgoingEdges)
                    {
                        foreach (var uri in givenOutgoingEdge.uris)
                        {
                            var sourceOutgoing = InMemoryQueryEngine.PropertyDomainOutgoingPropertiesQuery(uri);
                            possibleProperties = possibleProperties.IntersectIfAny(sourceOutgoing).ToList();
                        }
                    }

                    foreach (var givenIncomingEdge in sourceGivenIncomingEdges)
                    {
                        foreach (var uri in givenIncomingEdge.uris)
                        {
                            var sourceIncoming = InMemoryQueryEngine.PropertyRangeOutgoingPropertiesQuery(uri);
                            possibleProperties = possibleProperties.IntersectIfAny(sourceIncoming).ToList();
                        }
                    }

                    foreach (var givenOutgoingEdge in targetGivenOutgoingEdges)
                    {
                        foreach (var uri in givenOutgoingEdge.uris)
                        {
                            var targetOutgoing = InMemoryQueryEngine.PropertyDomainIncomingPropertiesQuery(uri);
                            possibleProperties = possibleProperties.IntersectIfAny(targetOutgoing).ToList();
                        }
                    }

                    foreach (var givenIncomingEdge in targetGivenIncomingEdges)
                    {
                        foreach (var uri in givenIncomingEdge.uris)
                        {
                            var targetIncoming = InMemoryQueryEngine.PropertyRangeIncomingPropertiesQuery(uri);
                            possibleProperties = possibleProperties.IntersectIfAny(targetIncoming).ToList();
                        }
                    }
                }

                edge.Results = !possibleProperties.Any()
                    ? new MultiLabelPropertyQuery(graph.PropertiesIndexPath, "*", 100000).Query(1000).ToList()
                    : new BatchIdPropertyQuery(graph.PropertiesIndexPath, possibleProperties).Query(10000).ToList();
            }
        }