1

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

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 查询时,它会抛出以下异常:

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 示例测试数据库似乎是一个很好的例子,但是关于它的文档并不多,而且我无法正确设置它,我构建它成功,但运行时出现同样的错误。

我的测试环境是:

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
4

1 回答 1

0

我自己弄清楚,原因是那里显示了一个实现 IExpressionSerializer 接口的 ExpressionSerializer,我的示例如下:

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 中注册:

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();
于 2018-03-15T01:28:16.770 回答