API Documentation

Factbase

class clingraph.orm.Factbase(prefix: str = '', default_graph: str = 'default')

Stores facts that are accepted by clingraphs syntax. It performs a preprocessing of the facts to unify them, and uses clorm as ORM to store and query the facts.

__init__(prefix: str = '', default_graph: str = 'default')

Defines the factbase behavior based on the prefix for the predicates and the name of the deafult graph

Parameters
  • prefix (str) – The prefix to all predicate names

  • default_graph (str) – Name of the default graph, all elements with arity 1 will be assigned to this graph

add_fact_file(file)

Adds a file containing facts to the Factbase

Parameters

file (str) – The path to the file

Raises

InvalidSyntax – If the input are not facts

add_fact_string(program)

Adds a string containing facts to the Factbase

Parameters

program (str) – A string consisting of only facts, divided by a .

Raises

InvalidSyntax – If the input are not facts

add_fb(fb)

Adds a clorm fact base to the Factbase

add_model(model)

Adds a clingo model to the Factbase

Parameters

model (clingo.Model) – A model returned by clingo

classmethod from_model(model, prefix: str = '', default_graph: str = 'default')

Creates a Factbase from a clingo model

Parameters
  • model (clingo.Model) – A model returned by clingo

  • prefix (str) – The prefix to all predicate names

  • default_graph (str) – Name of the default graph, all elements with arity 1 will be assigned to this graph

classmethod from_string(string, prefix: str = '', default_graph: str = 'default')

Creates a Factbase from a string

Parameters
  • string (str) – A string consisting of only facts, divided by a .

  • prefix (str) – The prefix to all predicate names

  • default_graph (str) – Name of the default graph, all elements with arity 1 will be assigned to this graph

Raises

InvalidSyntax – If the input are not facts

get_all_graphs()

Gets a list if the identifiers for all the graphs

Returns

(list) A list with the identifiers for all the graphs

get_element_attr(element_type, element_id)

Gets the attributes a specific element Returns a dictionary where the keys are attribute name and values are attribute values.

Parameters
  • element_type (str) – The element type: graph, edge or node

  • element_id – Identifier of the element

Returns

(dic) A dictionary with attribute names as key and attribute values as values.

get_facts()

Gets the facts in the factbase after preprocessing as a string

Returns

(str) A string with the facts

get_graph_elements(element_type, graph_id)

Gets the list of elements for a graph

Parameters
  • element_type (str) – The element type: edge or node

  • graph_id – Identifier of the graph

Returns

(list) The list of elements that belong to the graph

get_graph_global_element_attr(element_type, graph_id)

Gets the attributes for a global element: graph_nodes or graph_edges.

Parameters
  • element_type (str) – The element type: edge or node

  • graph_id – Identifier of the graph

Returns

(dic) A dictionary with attribute names as key and attribute values as values.

get_parent_graph(graph_id)

Gets the parent graph for a given graph_id.

Parameters

graph_id – Identifier of the subgraph

Returns

The identifier of the parent graph or None if there is no parent

Graphviz

Graphviz functionality

clingraph.graphviz.compute_graphs(fb, graphviz_type='graph', seed=None)

Computes graphs using Graphviz for the given factbase

Parameters
  • fb (list[Factbase] | Factbase) – A Factbase to generate the graphviz objects. If a list is passed, each element is cosidered as the Factbase for a stable model.

  • graphviz_type (str) – The type of graph graph for graphviz.Graph and digraph for graphviz.Digraph

  • seed (int) – A number used as seed

clingraph.graphviz.dot(graphs)

Gets the source string in dot language for the graphs

Parameters

graphs (dic|list[dic]) – A dictionary of graphviz objects where the keys are the graph names. Or a list of such dictionaries, each element corresponding to a model.

clingraph.graphviz.render(graphs, directory='out', format='pdf', name_format=None, engine='dot', view=False)

Render the given graphviz graphs that where computed using compute_graphs().

Parameters
  • graphs (dic|list[dic]) – A dictionary of graphviz objects where the keys are the graph names. Or a list of such dictionaries, each element corresponding to a model.

  • directory (str) – Where to save the object

  • format (str) – The format for the output pdf, png or svg

  • name_format (str) – The format for the name.

  • engine (str) – Engine used for the layout

  • view (bool) – If the rendered files will be oppend

Returns

A dictionary with the paths where the images where saved as values for each graph.

Or a list of such dictionaries, each element corresponding to a model.

Return type

[dic | list[dic]]

Graphviz gif

Graphviz integration to generate gifs

clingraph.gif.save_gif(graphs, directory='out', name_format='movie', engine='dot', fps=1, sort='asc-str')

Generates a gif for the given graphs

Parameters
  • graphs (dic|list[dic]) – A dictionary of graphviz objects where the keys are the graph names. Or a list of such dictionaries, each element corresponding to a model.

  • directory (str) – Path to the directory where to write

  • name_format (str) – The file name for the gif can include {model_number}

  • engine (str) – The engine used for rendering

  • fps (int) – The number of frames per second

  • sort (str) – How to sort the images used to generate the gif asc-str Sort ascendent based on the graph name, desc-str Sort descendent based on the graph name, asc-int Sort descendent based on the graph name converted to an interger, desc-int Sort descendent based on the graph name converted to an integer, name1,...,namex A string with the order of the graph names separated by ,

Returns

A dictionary with the paths where the gifss where saved as values for each graph.

Or a list of such dictionaries, each element corresponding to a model.

Return type

[dic | list[dic]]

Graphviz tex

Graphviz integration to generate latex

clingraph.tex.tex(graphs, **kwargs)

Generates the latex code for the graphs

Parameters
  • graphs (dic|list[dic]) – A dictionary of graphviz objects where the keys are the graph names. Or a list of such dictionaries, each element corresponding to a model.

  • **kwargs – Any additional arguments passed to dot2tex dot2tex() function

Returns: (dic|list[dic]) The dictionary or list with a string containing the latex code instead of the graphviz objects.

Utils

Utils functions used across the project

clingraph.utils.apply(elements, function, **kwargs)

Applies a given function to the elements.

Parameters
  • elements (list[dic]|dic) – A dictionary where the keys are considered the graph names and the function is applied to the values. Can also be a list of such dictionaries, where each element is considered a model. Any None values in the list will be skiped.

  • function (callable) – The function to be applied

  • **kwargs – Any arguments passed to the function. If an argument name_format is passed, {graph_name} and {model_number} will be substituted in this string by the corresponding values during the iteration.

Returns: (list[dic]|[dic]) The elements after appling the function to the values

clingraph.utils.write(elements, directory, format, name_format=None)

Writes all the elements info using apply()

Parameters
  • elements (list|dic) – A dictionary where the keys are considered the graph names and the write is applied to the values. Can also be a list of such dictionaries, where each element is considered a model.

  • directory (str) – Path to the directory where to write

  • format (str) – Output format

  • name_format (str) – The file name

Returns

A dictionary with the paths where the files where saved as values for each graph.

Or a list of such dictionaries, each element corresponding to a model.

Return type

[dic | list[dic]]

Clingo Utils

Functions used for the clingo integration

class clingraph.clingo_utils.ClingraphContext

Provides avaliable python functions to be used in a visualization encoding passed in the command line via option –viz-encoding

concat(*args)

Concatenates the given symbols as a string :param args: All symbols

Returns

(clingo.Symbol.String) The string concatenating all symbols

pos(x, y)
Parameters
  • x (clingo.Symbol.Number) – Number for the X coordinate

  • y (clingo.Symbol.Number) – Number for the Y coordinate

Returns

(clingo.Symbol.String) position as a string of form (x,y)!

clingraph.clingo_utils.parse_clingo_json(json_str)

Parses a json string from the output of clingo obtained using the option --outf=2. Expects a SATISFIABLE answer.

Parameters

json_str (str) – A string with the json

Returns

(list[str]) A list with the programs as strings

Raises

InvalidSyntax – if the json format is invalid or is not a SAT result.

Exceptions

Exceptions

exception clingraph.exceptions.InvalidSyntax(*args)

Exception returned when the input syntax is not expected

exception clingraph.exceptions.InvalidSyntaxJSON(*args)

Exception returned when the input syntax is not expected