首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphEngine LIKQ查询抛出System.NullReferenceException

GraphEngine LIKQ查询抛出System.NullReferenceException
EN

Stack Overflow用户
提问于 2018-03-13 15:22:26
回答 1查看 104关注 0票数 1

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

TSL代码:

代码语言:javascript
复制
cell People
{
   [Index]
   string Name;
   optional int Age;
   [GraphEdge]
   optional List<CellId> Friends;
}

主程序:

代码语言:javascript
复制
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查询时,它抛出以下异常:

代码语言:javascript
复制
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

freebase-likq example测试数据库似乎是一个很好的例子,但是关于它的文档并不多,而且我不能正确地设置它,我成功地构建了它,但是运行时出现了相同的错误。

我的测试环境是:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2018-03-15 09:28:17

我自己想,原因是有一个显示注册实现IExpressionSerializer接口的ExpressionSerializer,我的例子如下:

代码语言:javascript
复制
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中注册:

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49250406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档