Current section
Files
Jump to
Current section
Files
src/yog_io.erl
-module(yog_io).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yog_io.gleam").
-export([read_graphml/1, write_graphml/2, read_gdf/1, write_gdf/2, default_graphml_options/0, default_gdf_options/0, default_tgf_options/0, default_leda_options/0, default_pajek_options/0, default_pajek_node_attributes/0, default_json_options/0, write_json/2, to_json/1, from_json/1, read_json/1, write_d3_json/2, write_cytoscape_json/2, write_visjs_json/2, read_tgf/2, write_tgf/2, to_tgf/1, read_leda/1, write_leda/2, to_leda/1, read_pajek/1, write_pajek/2, to_pajek/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Yog IO - Graph file format I/O for the yog graph library.\n"
"\n"
" Provides serialization and deserialization support for popular graph file formats:\n"
" - **GraphML** - XML-based format supported by Gephi, yEd, Cytoscape, and NetworkX\n"
" - **GDF** - Simple CSV-like format used by Gephi\n"
" - **TGF** - Trivial Graph Format, a simple text format for quick exchange\n"
" - **LEDA** - Library of Efficient Data types and Algorithms format\n"
" - **Pajek** - Social network analysis standard (.net format)\n"
" - **JSON** - Multiple formats for web visualization libraries (D3.js, Cytoscape.js, vis.js, etc.)\n"
"\n"
" ## Quick Start\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
" import yog_io\n"
"\n"
" // Create a graph\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"friend\")\n"
"\n"
" // Write to GraphML\n"
" let assert Ok(Nil) = yog_io.write_graphml(\"graph.graphml\", graph)\n"
"\n"
" // Read from GraphML\n"
" let assert Ok(loaded) = yog_io.read_graphml(\"graph.graphml\")\n"
"\n"
" // Write to GDF\n"
" let assert Ok(Nil) = yog_io.write_gdf(\"graph.gdf\", graph)\n"
" ```\n"
"\n"
" ## Module Structure\n"
"\n"
" - **`yog_io/graphml`** - Full-featured GraphML support with custom attribute mappers\n"
" - **`yog_io/gdf`** - GDF format support with custom attribute mappers\n"
" - **`yog_io/tgf`** - TGF format support with custom label functions\n"
" - **`yog_io/leda`** - LEDA format support for research tool compatibility\n"
" - **`yog_io/pajek`** - Pajek format support for social network analysis\n"
"\n"
" For more control over serialization, use the submodules directly.\n"
).
-file("src/yog_io.gleam", 148).
?DOC(
" Reads a graph from a GraphML file.\n"
"\n"
" This is a convenience function that reads node and edge data as\n"
" string dictionaries. For custom data types, use `graphml.read_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let assert Ok(graph) = yog_io.read_graphml(\"graph.graphml\")\n"
"\n"
" // Access node data\n"
" import gleam/dict\n"
" let node1_data = dict.get(graph.nodes, 1)\n"
" ```\n"
).
-spec read_graphml(binary()) -> {ok,
yog@model:graph(gleam@dict:dict(binary(), binary()), gleam@dict:dict(binary(), binary()))} |
{error, binary()}.
read_graphml(Path) ->
yog_io@graphml:read(Path).
-file("src/yog_io.gleam", 171).
?DOC(
" Writes a graph to a GraphML file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
" For custom data types, use `graphml.write_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"5\")\n"
"\n"
" let assert Ok(Nil) = yog_io.write_graphml(\"graph.graphml\", graph)\n"
" ```\n"
).
-spec write_graphml(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
write_graphml(Path, Graph) ->
yog_io@graphml:write(Path, Graph).
-file("src/yog_io.gleam", 188).
?DOC(
" Reads a graph from a GDF file.\n"
"\n"
" This is a convenience function that reads node and edge data as\n"
" string dictionaries. For custom data types, use `gdf.read_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let assert Ok(graph) = yog_io.read_gdf(\"graph.gdf\")\n"
" ```\n"
).
-spec read_gdf(binary()) -> {ok,
yog@model:graph(gleam@dict:dict(binary(), binary()), gleam@dict:dict(binary(), binary()))} |
{error, binary()}.
read_gdf(Path) ->
yog_io@gdf:read(Path).
-file("src/yog_io.gleam", 211).
?DOC(
" Writes a graph to a GDF file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
" For custom data types, use `gdf.write_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"5\")\n"
"\n"
" let assert Ok(Nil) = yog_io.write_gdf(\"graph.gdf\", graph)\n"
" ```\n"
).
-spec write_gdf(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
write_gdf(Path, Graph) ->
yog_io@gdf:write(Path, Graph).
-file("src/yog_io.gleam", 223).
?DOC(" Default options for GraphML serialization.\n").
-spec default_graphml_options() -> yog_io@graphml:graph_m_l_options().
default_graphml_options() ->
yog_io@graphml:default_options().
-file("src/yog_io.gleam", 228).
?DOC(" Default options for GDF serialization.\n").
-spec default_gdf_options() -> yog_io@gdf:gdf_options().
default_gdf_options() ->
yog_io@gdf:default_options().
-file("src/yog_io.gleam", 233).
?DOC(" Default options for TGF serialization.\n").
-spec default_tgf_options() -> yog_io@tgf:tgf_options(binary(), binary()).
default_tgf_options() ->
yog_io@tgf:default_options().
-file("src/yog_io.gleam", 238).
?DOC(" Default options for LEDA serialization.\n").
-spec default_leda_options() -> yog_io@leda:leda_options(binary(), binary()).
default_leda_options() ->
yog_io@leda:default_options().
-file("src/yog_io.gleam", 243).
?DOC(" Default options for Pajek serialization.\n").
-spec default_pajek_options() -> yog_io@pajek:pajek_options(binary(), binary()).
default_pajek_options() ->
yog_io@pajek:default_options().
-file("src/yog_io.gleam", 248).
?DOC(" Default node attributes for Pajek serialization.\n").
-spec default_pajek_node_attributes() -> yog_io@pajek:node_attributes().
default_pajek_node_attributes() ->
yog_io@pajek:default_node_attributes().
-file("src/yog_io.gleam", 253).
?DOC(" Default options for JSON export.\n").
-spec default_json_options() -> yog_io@json:json_export_options(binary(), binary()).
default_json_options() ->
yog_io@json:default_export_options().
-file("src/yog_io.gleam", 280).
?DOC(
" Writes a graph to a JSON file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
" For custom data types, use `json.to_json_file` with custom serializers.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"follows\")\n"
"\n"
" let assert Ok(Nil) = yog_io.write_json(\"graph.json\", graph)\n"
" ```\n"
).
-spec write_json(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, yog_io@json:json_error()}.
write_json(Path, Graph) ->
yog_io@json:to_json_file(Graph, Path, yog_io@json:default_export_options()).
-file("src/yog_io.gleam", 307).
?DOC(
" Converts a graph to a JSON string.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let json_string = yog_io.to_json(graph)\n"
"\n"
" // Read back\n"
" import gleam/dynamic/decode\n"
" let assert Ok(loaded) = yog_io.from_json(json_string)\n"
" ```\n"
).
-spec to_json(yog@model:graph(binary(), binary())) -> binary().
to_json(Graph) ->
yog_io@json:to_json(Graph, yog_io@json:default_export_options()).
-file("src/yog_io.gleam", 314).
?DOC(
" Parses a graph from a JSON string.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
).
-spec from_json(binary()) -> {ok, yog@model:graph(binary(), binary())} |
{error, yog_io@json:json_error()}.
from_json(Input) ->
yog_io@json:from_json(Input).
-file("src/yog_io.gleam", 321).
?DOC(
" Reads a graph from a JSON file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
).
-spec read_json(binary()) -> {ok, yog@model:graph(binary(), binary())} |
{error, yog_io@json:json_error()}.
read_json(Path) ->
yog_io@json:read(Path).
-file("src/yog_io.gleam", 332).
?DOC(
" Writes a graph to a JSON file in D3.js force-directed format.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let assert Ok(Nil) = yog_io.write_d3_json(\"graph-d3.json\", graph)\n"
" ```\n"
).
-spec write_d3_json(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, yog_io@json:json_error()}.
write_d3_json(Path, Graph) ->
Options = {json_export_options,
d3_force,
false,
{some, fun gleam@json:string/1},
{some, fun gleam@json:string/1},
true,
none},
yog_io@json:to_json_file(Graph, Path, Options).
-file("src/yog_io.gleam", 355).
?DOC(
" Writes a graph to a JSON file in Cytoscape.js format.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let assert Ok(Nil) = yog_io.write_cytoscape_json(\"graph-cytoscape.json\", graph)\n"
" ```\n"
).
-spec write_cytoscape_json(binary(), yog@model:graph(binary(), binary())) -> {ok,
nil} |
{error, yog_io@json:json_error()}.
write_cytoscape_json(Path, Graph) ->
Options = {json_export_options,
cytoscape,
false,
{some, fun gleam@json:string/1},
{some, fun gleam@json:string/1},
true,
none},
yog_io@json:to_json_file(Graph, Path, Options).
-file("src/yog_io.gleam", 378).
?DOC(
" Writes a graph to a JSON file in vis.js format.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" let assert Ok(Nil) = yog_io.write_visjs_json(\"graph-visjs.json\", graph)\n"
" ```\n"
).
-spec write_visjs_json(binary(), yog@model:graph(binary(), binary())) -> {ok,
nil} |
{error, yog_io@json:json_error()}.
write_visjs_json(Path, Graph) ->
Options = {json_export_options,
vis_js,
false,
{some, fun gleam@json:string/1},
{some, fun gleam@json:string/1},
true,
none},
yog_io@json:to_json_file(Graph, Path, Options).
-file("src/yog_io.gleam", 416).
?DOC(
" Reads a graph from a TGF file.\n"
"\n"
" This is a convenience function that reads node and edge data as strings.\n"
" For custom data types, use `tgf.read_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" case yog_io.read_tgf(\"graph.tgf\", Directed) {\n"
" Ok(tgf.TgfResult(graph, warnings)) -> {\n"
" // Use the graph\n"
" process_graph(graph)\n"
" }\n"
" Error(e) -> handle_error(e)\n"
" }\n"
" ```\n"
).
-spec read_tgf(binary(), yog@model:graph_type()) -> {ok,
yog_io@tgf:tgf_result(binary(), binary())} |
{error, yog_io@tgf:tgf_error()}.
read_tgf(Path, Graph_type) ->
yog_io@tgf:read(Path, Graph_type).
-file("src/yog_io.gleam", 442).
?DOC(
" Writes a graph to a TGF file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
" For custom data types, use `tgf.write_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"follows\")\n"
"\n"
" let assert Ok(Nil) = yog_io.write_tgf(\"graph.tgf\", graph)\n"
" ```\n"
).
-spec write_tgf(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
write_tgf(Path, Graph) ->
yog_io@tgf:write(Path, Graph).
-file("src/yog_io.gleam", 465).
?DOC(
" Converts a graph to a TGF string.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let tgf_string = yog_io.to_tgf(graph)\n"
" ```\n"
).
-spec to_tgf(yog@model:graph(binary(), binary())) -> binary().
to_tgf(Graph) ->
yog_io@tgf:serialize(Graph).
-file("src/yog_io.gleam", 489).
?DOC(
" Reads a graph from a LEDA file.\n"
"\n"
" This is a convenience function that reads node and edge data as strings.\n"
" For custom data types, use `leda.read_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" case yog_io.read_leda(\"graph.gw\") {\n"
" Ok(leda.LedaResult(graph, warnings)) -> {\n"
" // Use the graph\n"
" process_graph(graph)\n"
" }\n"
" Error(e) -> handle_error(e)\n"
" }\n"
" ```\n"
).
-spec read_leda(binary()) -> {ok, yog_io@leda:leda_result(binary(), binary())} |
{error, yog_io@leda:leda_error()}.
read_leda(Path) ->
yog_io@leda:read(Path).
-file("src/yog_io.gleam", 514).
?DOC(
" Writes a graph to a LEDA file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
" For custom data types, use `leda.write_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"follows\")\n"
"\n"
" let assert Ok(Nil) = yog_io.write_leda(\"graph.gw\", graph)\n"
" ```\n"
).
-spec write_leda(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
write_leda(Path, Graph) ->
yog_io@leda:write(Path, Graph).
-file("src/yog_io.gleam", 537).
?DOC(
" Converts a graph to a LEDA string.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let leda_string = yog_io.to_leda(graph)\n"
" ```\n"
).
-spec to_leda(yog@model:graph(binary(), binary())) -> binary().
to_leda(Graph) ->
yog_io@leda:serialize(Graph).
-file("src/yog_io.gleam", 561).
?DOC(
" Reads a graph from a Pajek file.\n"
"\n"
" This is a convenience function that reads node and edge data as strings.\n"
" For custom data types, use `pajek.read_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" case yog_io.read_pajek(\"graph.net\") {\n"
" Ok(pajek.PajekResult(graph, warnings)) -> {\n"
" // Use the graph\n"
" process_graph(graph)\n"
" }\n"
" Error(e) -> handle_error(e)\n"
" }\n"
" ```\n"
).
-spec read_pajek(binary()) -> {ok,
yog_io@pajek:pajek_result(binary(), binary())} |
{error, yog_io@pajek:pajek_error()}.
read_pajek(Path) ->
yog_io@pajek:read(Path).
-file("src/yog_io.gleam", 586).
?DOC(
" Writes a graph to a Pajek file.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
" For custom data types, use `pajek.write_with`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let assert Ok(graph) = model.add_edge(graph, from: 1, to: 2, with: \"follows\")\n"
"\n"
" let assert Ok(Nil) = yog_io.write_pajek(\"graph.net\", graph)\n"
" ```\n"
).
-spec write_pajek(binary(), yog@model:graph(binary(), binary())) -> {ok, nil} |
{error, simplifile:file_error()}.
write_pajek(Path, Graph) ->
yog_io@pajek:write(Path, Graph).
-file("src/yog_io.gleam", 609).
?DOC(
" Converts a graph to a Pajek string.\n"
"\n"
" This is a convenience function for graphs with string data.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" import yog/model.{Directed}\n"
"\n"
" let graph =\n"
" model.new(Directed)\n"
" |> model.add_node(1, \"Alice\")\n"
" |> model.add_node(2, \"Bob\")\n"
"\n"
" let pajek_string = yog_io.to_pajek(graph)\n"
" ```\n"
).
-spec to_pajek(yog@model:graph(binary(), binary())) -> binary().
to_pajek(Graph) ->
yog_io@pajek:serialize(Graph).