From 00c76b0712c606106be4326463e6d99de742a129 Mon Sep 17 00:00:00 2001 From: Thierry Martinez Date: Wed, 21 Oct 2015 18:37:17 +0200 Subject: [PATCH] Graphviz --- Makefile | 6 +- modules/graphviz/Makefile | 20 +++++++ modules/graphviz/graphviz.pl | 13 +++++ modules/graphviz/graphviz.plt | 10 ++++ modules/graphviz/graphviz_swiprolog.c | 84 ++++++++++++++++++++++++--- 5 files changed, 123 insertions(+), 10 deletions(-) create mode 100644 modules/graphviz/Makefile create mode 100644 modules/graphviz/graphviz.pl create mode 100644 modules/graphviz/graphviz.plt diff --git a/Makefile b/Makefile index 6c83aa78..77bbfa67 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ MODULES=$(shell sed -n -E 's/^- (.*\.pl)$$/\1/p' toc.org) # load_test_files/1 should make this useless, but I cannot find how to use it TEST_MODULES=$(wildcard $(MODULES:.pl=.plt)) -all: biocham biocham_debug tests doc +all: biocham biocham_debug test doc -.PHONY: tests doc clean +.PHONY: test doc clean biocham: $(MODULES) toc.org Makefile @echo $(MODULES) @@ -13,7 +13,7 @@ biocham: $(MODULES) toc.org Makefile biocham_debug: $(MODULES) $(TEST_MODULES) toc.org Makefile swipl -o biocham_debug --goal=initialize -c $(MODULES) $(TEST_MODULES) -tests: biocham_tests +test: biocham_tests ./biocham_tests doc: biocham diff --git a/modules/graphviz/Makefile b/modules/graphviz/Makefile new file mode 100644 index 00000000..93a69da7 --- /dev/null +++ b/modules/graphviz/Makefile @@ -0,0 +1,20 @@ +CC=swipl-ld +LDFLAGS=-shared + +all: graphviz_swiprolog test + +.PHONY: clean test + +clean: + rm -f graphviz_swiprolog graphviz_swiprolog.so graphviz_swiprolog.o + +graphviz_swiprolog: graphviz_swiprolog.o + swipl-ld -shared -o graphviz_swiprolog graphviz_swiprolog.o + mv graphviz_swiprolog.so graphviz_swiprolog + +graphviz_swiprolog.o: graphviz_swiprolog.c + +test: + swipl -g "\ + call_cleanup((['graphviz.plt'], run_tests, halt(0)), halt(1))\ + " diff --git a/modules/graphviz/graphviz.pl b/modules/graphviz/graphviz.pl new file mode 100644 index 00000000..287f969f --- /dev/null +++ b/modules/graphviz/graphviz.pl @@ -0,0 +1,13 @@ +:- module( + graphviz, + [ + agclose/1, + agread/2, + gvFreeLayout/1, + gvLayout/2, + gvRender/3 + ] +). + + +:- use_foreign_library(foreign(graphviz_swiprolog)). diff --git a/modules/graphviz/graphviz.plt b/modules/graphviz/graphviz.plt new file mode 100644 index 00000000..7e35acac --- /dev/null +++ b/modules/graphviz/graphviz.plt @@ -0,0 +1,10 @@ +:- use_module(library(plunit)). + +:- use_module(graphviz). + +:- begin_tests(graphviz). + +test(say_hello) :- + say_hello('Hello world.'). + +:- end_tests(graphviz). diff --git a/modules/graphviz/graphviz_swiprolog.c b/modules/graphviz/graphviz_swiprolog.c index 40e34016..02d08510 100644 --- a/modules/graphviz/graphviz_swiprolog.c +++ b/modules/graphviz/graphviz_swiprolog.c @@ -1,17 +1,87 @@ +#include #include +#include + +GVC_t *gvc; + +static foreign_t +pl_agclose(term_t graph_term) { + Agraph_t *graph; + if (!PL_get_pointer(graph_term, &graph)) { + PL_fail; + } + agclose(graph); + PL_succeed; +} + +static foreign_t +pl_agread(term_t filename_term, term_t graph_term) { + char *filename; + FILE *file; + Agraph_t *graph; + if (!PL_get_atom_chars(filename_term, &filename)) { + PL_fail; + } + file = fopen(filename, "r"); + graph = agread(file); + fclose(file); + if (!PL_put_pointer(graph_term, graph)) { + agclose(graph); + PL_fail; + } + PL_succeed; +} static foreign_t -pl_say_hello(term_t to) { - char *a; +pl_gvFreeLayout(term_t graph_term) { + Agraph_t *graph; + if (!PL_get_pointer(graph_term, &graph)) { + PL_fail; + } + gvFreeLayout(gvc, graph); + PL_succeed; +} - if (PL_get_atom_chars(to, &a)) { - PL_succeed; +static foreign_t +pl_gvLayout(term_t graph_term, term_t engine_term) { + Agraph_t *graph; + char *engine; + if (!PL_get_pointer(graph_term, &graph)) { + PL_fail; } + if (!PL_get_atom_chars(engine_term, &engine)) { + PL_fail; + } + gvLayout(gvc, graph, engine); + PL_succeed; +} - PL_fail; +static foreign_t +pl_gvRender(term_t graph_term, term_t format_term, term_t filename) { + Agraph_t *graph; + char *format; + FILE *file; + if (!PL_get_pointer(graph_term, &graph)) { + PL_fail; + } + if (!PL_get_atom_chars(format_term, &format)) { + PL_fail; + } + if (!PL_get_atom_chars(filename_term, &filename)) { + PL_fail; + } + file = fopen(filename, "w"); + gvRender(gvc, graph, format, file); + PL_succeed; } install_t -install_mylib() { - PL_register_foreign("say_hello", 1, pl_say_hello, 0); +install_graphviz_swiprolog() { + gvc = gvContext(); + + PL_register_foreign("agclose", 1, pl_agclose, 0); + PL_register_foreign("agread", 2, pl_agread, 0); + PL_register_foreign("gvFreeLayout", 1, pl_gvFreeLayout, 0); + PL_register_foreign("gvLayout", 2, pl_gvLayout, 0); + PL_register_foreign("gvRender", 3, pl_gvRender, 0); } -- GitLab