以图形表示形式获取 Prolog 查询结果
Get Prolog query results as a graph representation
我有以下代码:
edge(a,e).
edge(e,d).
edge(d,c).
edge(c,b).
edge(b,a).
edge(d,a).
edge(e,c).
edge(f,b).
graph(Graph):-findall(A-B, edge(A,B), L),
findall(B-A, edge(A,B), L1),
append(L, L1, Graph).
执行查询以获得结果图后,我得到以下信息:
?-rbacGraph(Graph);true.
Graph=[a-e,b-a,c-b,d-a,d-c,e-c,e-d,f-b,a-b,a-d,b-c,b-f,c-d,c-e,d-e,e-a].
我只想问这个,有什么方法可以从 Prolog 中获取或将结果转换为图形表示(获取节点和连接边)?
您可以考虑将此 'program' 生成的输出粘贴到以下 HTML5 片段(由 cytoscape.js 提供):
graph_so :-
setof(N, M^(edge(N,M);edge(M,N)), Ns),
maplist([N] >> format('{data:{id:"~w"}},~n', [N]), Ns),
forall(edge(S,T), format('{data:{id:"~w-~w",source:"~w",target:"~w"}},~n', [S,T,S,T])).
edge(a,e).
edge(e,d).
edge(d,c).
edge(c,b).
edge(b,a).
edge(d,a).
edge(e,c).
edge(f,b).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/2.7.16/cytoscape.min.js"></script>
</head>
<div id='cy'></div>
<style>
#cy {
width: 500px;
height: 500px;
}
</style>
<script>
(function() {
var elements = [
// generated by Prolog
{data:{id:"a"}},
{data:{id:"b"}},
{data:{id:"c"}},
{data:{id:"d"}},
{data:{id:"e"}},
{data:{id:"f"}},
{data:{id:"a-e",source:"a",target:"e"}},
{data:{id:"e-d",source:"e",target:"d"}},
{data:{id:"d-c",source:"d",target:"c"}},
{data:{id:"c-b",source:"c",target:"b"}},
{data:{id:"b-a",source:"b",target:"a"}},
{data:{id:"d-a",source:"d",target:"a"}},
{data:{id:"e-c",source:"e",target:"c"}},
{data:{id:"f-b",source:"f",target:"b"}},
];
var cy = cytoscape({
container: document.getElementById('cy'),
elements: elements,
style: [
{ selector: 'node',
style: {
'background-color': '#666',
'label': 'data(id)'
}
},
{ selector: 'edge',
style: {
'target-arrow-shape': 'triangle',
'width': 3,
'line-color': '#ddd',
'target-arrow-color': '#ddd',
'curve-style': 'bezier'
}
}
],
})
var layout = cy.makeLayout({ name: 'cose' });
layout.run();
})()
</script>
</html>
或者这个更有效的模式(从 Prolog 的角度来看),它生成一个有向图,就像你从你的原始代码中得到的那样:
graph_opt :-
setof(N, node_(N), Ns),
maplist([N] >> format('{data:{id:"~w"}},~n', [N]), Ns).
node_(S) :-
( edge(S,T) ; edge(T,S) ),
format('{data:{id:"~w-~w",source:"~w",target:"~w"}},~n', [S,T,S,T]).
编辑
从 HTML 代码段中删除了 jQuery 依赖项
我有以下代码:
edge(a,e).
edge(e,d).
edge(d,c).
edge(c,b).
edge(b,a).
edge(d,a).
edge(e,c).
edge(f,b).
graph(Graph):-findall(A-B, edge(A,B), L),
findall(B-A, edge(A,B), L1),
append(L, L1, Graph).
执行查询以获得结果图后,我得到以下信息:
?-rbacGraph(Graph);true.
Graph=[a-e,b-a,c-b,d-a,d-c,e-c,e-d,f-b,a-b,a-d,b-c,b-f,c-d,c-e,d-e,e-a].
我只想问这个,有什么方法可以从 Prolog 中获取或将结果转换为图形表示(获取节点和连接边)?
您可以考虑将此 'program' 生成的输出粘贴到以下 HTML5 片段(由 cytoscape.js 提供):
graph_so :-
setof(N, M^(edge(N,M);edge(M,N)), Ns),
maplist([N] >> format('{data:{id:"~w"}},~n', [N]), Ns),
forall(edge(S,T), format('{data:{id:"~w-~w",source:"~w",target:"~w"}},~n', [S,T,S,T])).
edge(a,e).
edge(e,d).
edge(d,c).
edge(c,b).
edge(b,a).
edge(d,a).
edge(e,c).
edge(f,b).
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/2.7.16/cytoscape.min.js"></script>
</head>
<div id='cy'></div>
<style>
#cy {
width: 500px;
height: 500px;
}
</style>
<script>
(function() {
var elements = [
// generated by Prolog
{data:{id:"a"}},
{data:{id:"b"}},
{data:{id:"c"}},
{data:{id:"d"}},
{data:{id:"e"}},
{data:{id:"f"}},
{data:{id:"a-e",source:"a",target:"e"}},
{data:{id:"e-d",source:"e",target:"d"}},
{data:{id:"d-c",source:"d",target:"c"}},
{data:{id:"c-b",source:"c",target:"b"}},
{data:{id:"b-a",source:"b",target:"a"}},
{data:{id:"d-a",source:"d",target:"a"}},
{data:{id:"e-c",source:"e",target:"c"}},
{data:{id:"f-b",source:"f",target:"b"}},
];
var cy = cytoscape({
container: document.getElementById('cy'),
elements: elements,
style: [
{ selector: 'node',
style: {
'background-color': '#666',
'label': 'data(id)'
}
},
{ selector: 'edge',
style: {
'target-arrow-shape': 'triangle',
'width': 3,
'line-color': '#ddd',
'target-arrow-color': '#ddd',
'curve-style': 'bezier'
}
}
],
})
var layout = cy.makeLayout({ name: 'cose' });
layout.run();
})()
</script>
</html>
或者这个更有效的模式(从 Prolog 的角度来看),它生成一个有向图,就像你从你的原始代码中得到的那样:
graph_opt :-
setof(N, node_(N), Ns),
maplist([N] >> format('{data:{id:"~w"}},~n', [N]), Ns).
node_(S) :-
( edge(S,T) ; edge(T,S) ),
format('{data:{id:"~w-~w",source:"~w",target:"~w"}},~n', [S,T,S,T]).
编辑
从 HTML 代码段中删除了 jQuery 依赖项