我有以下代码:
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的结果转换为图形表示(获取节点和连接边缘)?
答案 0 :(得分:1)
您可以考虑粘贴到以下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依赖项