简体   繁体   English

从Json生成谓词

[英]Generate predicates from Json

I have a WEB API done in Asp.Net that returns some points of interest in JSON. 我在Asp.Net中完成了一个WEB API,该API返回了JSON的一些兴趣点。 In prolog I obtain it by doing this: 在序言中,我通过执行以下操作获取它:

poi(X):- http_client:http_get('http://localhost:XXXXX/api/PontosInteresse',X,[]).

This returns something of the sort: 这将返回某种信息:

X = '[{"ID":1,"nome":"teste","descricao":"gfds","tempoEstimado":21,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":2,"nome":"teste2","descricao":"qweqwe","tempoEstimado":13,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":3,"nome":"teste3","descricao":"fgdfg","tempoEstimado":0,"acessibilidade":0,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null}]'.

How can I transform this to a bunch of predicates, in the form of: 我如何将其转换为以下形式的谓词:

poi(ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId).
local(ID, Nome, Latitude, Longitude).

an example of how to access untyped content: 如何访问未键入内容的示例:

:- use_module(library(http/json)).

tj :-   X = '[{"ID":1,"nome":"teste","descricao":"gfds","tempoEstimado":21,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":2,"nome":"teste2","descricao":"qweqwe","tempoEstimado":13,"acessibilidade":1,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null},{"ID":3,"nome":"teste3","descricao":"fgdfg","tempoEstimado":0,"acessibilidade":0,"localID":1,"local":{"ID":1,"nome":"porto","latitude":"21","longitude":"-21","ApplicationUserID":null},"categoriaID":1,"categoria":{"ID":1,"nome":"cultura","ApplicationUserID":null},"hashtags":null,"ApplicationUserID":null}]',
    open_any(string(X), read, S, Close, []),
    json_read(S, T),
    Close,
    maplist(convert, T, C),
    maplist(writeln, C).

convert(json(L),
    poi(ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId)
    / local(IDLoc, NomeLoc, Latitude, Longitude)
) :-
    maplist({L}/[F,V]>>memberchk(F=V, L),
        ['ID', nome, descricao, tempoEstimado, acessibilidade, localID],
        [ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId]),
    memberchk(local=json(LL), L),
    maplist({LL}/[F,V]>>memberchk(F=V, LL),
        ['ID', nome, latitude, longitude],
        [IDLoc, NomeLoc, Latitude, Longitude]).

that yields 产生

poi(1,teste,gfds,21,1,1)/local(1,porto,21,-21)
poi(2,teste2,qweqwe,13,1,1)/local(1,porto,21,-21)
poi(3,teste3,fgdfg,0,0,1)/local(1,porto,21,-21)

edit 编辑

here is the equivalent code that avoids the powerful library(yall) 这是避免强大的等效代码(yall)

convert(json(L),
    poi(ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId)
    / local(IDLoc, NomeLoc, Latitude, Longitude)
) :-
    maplist(field_value(L),
        ['ID', nome, descricao, tempoEstimado, acessibilidade, localID],
        [ID, Nome, Descricao, TempoEstimado, Acessibilidade, LocalId]),
    memberchk(local=json(LL), L),
    maplist(field_value(LL),
        ['ID', nome, latitude, longitude],
        [IDLoc, NomeLoc, Latitude, Longitude]).

field_value(Fields, Field, Value) :-
    memberchk(Field=Value, Fields).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM