本文介绍了GraphEngine LIKQ查询引发System.NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用并尝试构建一个LIKQ示例,我的代码如下:

I started to use Microsoft GraphEngine and try to build a LIKQ example, my code below:

TSL代码:

cell People
{
   [Index]
   string Name;
   optional int Age;
   [GraphEdge]
   optional List<CellId> Friends;
}

主程序:

using System;
using System.Collections.Generic;
using System.Linq;
using FanoutSearch;
using FanoutSearch.LIKQ;
using PeopleModel;
using Trinity;
using Trinity.Network;
using Action = FanoutSearch.Action;

namespace People_LIKQ
{
class Program
{
    static void Main(string[] args)
    {
        //GenerateData();
        LikqQurety();
    }

    /// <summary>
    /// Try to query with LIKQ
    /// </summary>
    static void LikqQurety()
    {
        TrinityServer server=new TrinityServer();
        server.RegisterCommunicationModule<FanoutSearchModule>();
        server.Start();
        Global.LocalStorage.LoadStorage();

        var startNodes = from node in Global.LocalStorage.People_Selector()
            where node.Name == "张三"
            select node;
        var startNode = startNodes.FirstOrDefault();

        //System.NullReferenceException here
        var datas=KnowledgeGraph.StartFrom(startNode)
            .FollowEdge("Friends").VisitNode(_=>Action.Continue)
            .FollowEdge("Friends").VisitNode(_=>Action.Continue)
            .FollowEdge("Friends").VisitNode(_=>Action.Return).ToList();

        foreach (PathDescriptor path in datas)
        {
            var ids = path.Select(ToReachableIDs).ToList();
        }
    }

    private static object ToReachableIDs(NodeDescriptor arg)
    {
        using (var cell=Global.LocalStorage.UsePeople(arg.id))
        {
            Console.WriteLine(cell.Name);
            return cell.CellID.Value;
        }
    }



    /// <summary>
    /// Generate somet testing data
    /// </summary>
    static void GenerateData()
    {
        Console.WriteLine("开始生成测试数据");
        People zhangsan=new People(Name:"张三",Age:34,Friends:new List<long>());
        People lisi=new People(Name:"李四",Age:33,Friends:new List<long>());
        People wangwu=new People(Name:"王五",Age:26,Friends:new List<long>());
        People xiaoming=new People(Name:"小明",Age:15,Friends:new List<long>());
        People xiaohua=new People(Name:"小花",Age:16,Friends:new List<long>());
        People jack=new People(Name:"Jack",Age:30,Friends:new List<long>());
        People rose=new People(Name:"Rose",Age:20,Friends:new List<long>());

        zhangsan.Friends.AddRange(new []{lisi.CellID,wangwu.CellID});
        lisi.Friends.AddRange(new []{zhangsan.CellID,xiaoming.CellID,jack.CellID});
        wangwu.Friends.AddRange(new []{zhangsan.CellID,xiaoming.CellID,jack.CellID});
        xiaoming.Friends.AddRange(new []{lisi.CellID,xiaohua.CellID,wangwu.CellID});
        xiaohua.Friends.AddRange(new []{xiaoming.CellID,rose.CellID});
        jack.Friends.AddRange(new []{lisi.CellID,wangwu.CellID,rose.CellID});
        rose.Friends.AddRange(new []{jack.CellID,xiaohua.CellID});

        Global.LocalStorage.SavePeople(zhangsan);
        Global.LocalStorage.SavePeople(lisi);
        Global.LocalStorage.SavePeople(wangwu);
        Global.LocalStorage.SavePeople(xiaoming);
        Global.LocalStorage.SavePeople(xiaohua);
        Global.LocalStorage.SavePeople(jack);
        Global.LocalStorage.SavePeople(rose);

        Global.LocalStorage.SaveStorage();
        Console.WriteLine("数据生成并保存成功");

    }
}

}

当我生成测试数据并尝试运行LIKQ查询时,它将引发以下异常:

When I generated the testing data and try to run LIKQ query, It throws the exception below:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at FanoutSearch.ExpressionSerializer.EnsureSerializer()
at FanoutSearch.ExpressionSerializer.Serialize(Expression pred)
at FanoutSearch.FanoutSearchDescriptor.<>c. <Serialize>b__22_0(Expression pred)
at System.Linq.Enumerable.SelectListIterator`2.ToList()
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at FanoutSearch.FanoutSearchDescriptor.Serialize()
at FanoutSearch.FanoutSearchDescriptor._ExecuteQuery()
at FanoutSearch.FanoutSearchDescriptor.GetEnumerator()
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at People_LIKQ.Program.LikqQurety() in /home/coder/Documents/NetCore/LIKQ-Examples/People-LIKQ/Program.cs:line 32
at People_LIKQ.Program.Main(String[] args) in /home/coder/Documents/NetCore/LIKQ-Examples/People-LIKQ/Program.cs:line 18

测试da tabase似乎是一个很好的例子,但是关于它的文档并不多,而且我无法正确设置它,构建成功,但会出现相同的错误。

The freebase-likq example test database seems to be a good example, but there is not much documents about it, and I can't set it up correctly, I build it successfully,but run with the same error.

我的测试环境是:

Ubuntu16.04 x64
.NetCore 2.1.4
GraphEngine.Core 1.0.9083  (I build it myself,The NuGet package outdated.)
GraphEngine.LIKQ 1.0.9083


推荐答案

我自己弄清楚,原因是这里显示了一个实现IExpressionSerializer接口的ExpressionSerializer寄存器,下面是我的示例:

I figure out with myself, the reason is there show register a ExpressionSerializer which implement the IExpressionSerializer interface, my example below:

public class ExpressionSerializer:IExpressionSerializer
{
    private static XmlSerializer m_serializer = null;
    private static NodeFactory m_factory = null;

    public ExpressionSerializer()
    {
        m_serializer=new XmlSerializer();
        m_serializer.AddKnownType(typeof(FanoutSearch.Action));
        m_factory=new NodeFactory();
    }

    public string Serialize(Expression pred)
    {
        return pred.ToXml(m_factory,m_serializer);
    }

    public Func<ICellAccessor, Action> DeserializeTraverseAction(string pred)
    {
        var func_exp = m_serializer.Deserialize<LambdaExpressionNode>(pred)
            .ToExpression<Func<ICellAccessor, FanoutSearch.Action>>();
        return func_exp.Compile();
    }

    public Func<ICellAccessor, bool> DeserializeOriginPredicate(string pred)
    {
        var fun_exp = m_serializer.Deserialize<LambdaExpressionNode>(pred)
            .ToExpression<Func<ICellAccessor, bool>>();
        return fun_exp.Compile();
    }
}

并在Program.cs中注册:

And register in Program.cs:

Global.LocalStorage.LoadStorage();
TrinityConfig.HttpPort = 80;
FanoutSearchModule.EnableExternalQuery(true);
FanoutSearchModule.SetQueryTimeout(3000);
FanoutSearchModule.RegisterIndexService(Indexer);
FanoutSearchModule.RegisterExpressionSerializerFactory(()=>new ExpressionSerializer());
TrinityServer server=new TrinityServer();
server.RegisterCommunicationModule<FanoutSearchModule>();
server.Start();

这篇关于GraphEngine LIKQ查询引发System.NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 09:31