user-doc.mkd 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

# User Documentation
{:.no_toc}

1. TOC
{:toc}


# installation
First, clone (`git clone git@gitlab.inria.fr:lbourneu/biseau.git`)
or [download](https://gitlab.inria.fr/lbourneu/biseau/repository/master/archive.zip)
the repository (NB: packaging is currently not operationnal).

All Python requirements are detailed in [`requirements.txt`](requirements.txt),
therefore a `pip install -Ur requirements.txt` will be enough to install them
on a properly configurated environnement.

(beware: it's `pip3` on some systems ; just be sure to use Python 3)


Also, you will need [clingo](https://potassco.org/clingo/)
(from [potassco labs](https://potassco.org/)) and
Lucas Bourneuf's avatar
Lucas Bourneuf committed
23
[graphviz](http://graphviz.org/) to be installed in your \$PATH.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

Unless they are on standard repositories (graphviz is probably, clingo likely is not),
you can get them from [graphviz.org](http://graphviz.org/Download..php)
and [github.com/potassco](https://github.com/potassco/clingo/releases/).


Biseau is tested with Python 3.5, Clingo 5.2.1 and Graphviz 2.38.
(but any 3.3+, 5.2.1+ and 2.38+ versions, respectively, should work properly)


## windows
You will also need to manually:

- setup your PATH, to both *clingo.exe* and *dot.exe* (which is in *bin/* subdirectory of graphviz install directory).
- restart your command line prompt, in order to take the changes into account (or look at [this](https://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w)).



# basic usage
See [`Makefile`](Makefile) for various recipes.
Running biseau can be done using the following commands:

    python -m biseau gui     # run the Graphical User Interface
    python -m biseau         # run the Command Line Interface
    python -m biseau --help  # get available options (both GUI and CLI)

See also script [`doc/run.bat`](doc/run.bat) (windows) or
[`doc/run.sh`](doc/run.sh) (unix).


### Scripts modifications
You may want to modify the run scripts, for instance,
in order to provide the path to clingo
for instance, which is possible using the `--clingo` option.

You better copy the script to modify, and modify/use the copy instead,
so that an update of the script will not avoid you to update the entire software.


## Update
If you have cloned the repository, you can update it using `git pull`.

Note that, if you change anything in the repository, update may fail,
expecting you to do something with your changes.

See also script [`doc/gitpull.bat`](doc/gitpull.bat) (windows) or
[`doc/gitpull.sh`](doc/gitpull.sh) (unix)
if you want to update by just running a file.



Lucas Bourneuf's avatar
Lucas Bourneuf committed
75 76 77 78
# API
Biseau is a regular python package, importable with `import biseau`.
See [the example](../examples/api_basics.py) for usage, and [the main](../biseau/__main__.py) for implementation of the CLI, that can be useful.

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282


# Program
The Program is managed in the right part, by first choosing a context,
and then the set of scripts/rules to apply to it.

These scripts are written in python/ASP, (or JSON/ASP in case of rules),
and will, from top to bottom, build a program that will finally
be run by clingo.

The result of that run will determine the visualization.

(NB: the context choice is itself a script, that takes nothing in input and yield `rel/2`
atoms describing the context content. These atoms are used by others context.)





# Visualization principles
The drawing on the left is done with the [dot language](https://en.wikipedia.org/wiki/DOT%20(graph%20description%20language)),
itself generated following ASP atoms generated by the program.
Therefore, user do not have to know the DOT language to generate a visualization.

For instance, `link(a,b).` will generate a graph of two nodes, a and b,
linked together. Adding `label(a,"hello, i'm a").` will show
a greeting label on node `a`.

These simple mappings in ASP provides an elegant way to build mathematical objects,
for instance the standard representation of a [Galois Lattice](https://en.wikipedia.org/wiki/Formal%20concept%20analysis)
in reduced form (AOC poset instead of all intents/extents on each node),
as shown by FCA-related scripts.

You can find [here](doc/user_doc.mkd#asp-to-dot) a complete list of mappings from ASP to DOT.


## Colors
Currently, for pragmatic reasons, only red, green and blue are supported
as colors.

You should note that, when a node receive 2 or 3 colors,
it will get a color based on [additive color](https://en.wikipedia.org/wiki/Additive%20color).
For instance, a node marked with blue and green will be drawn yellow.

It should be in theory possible to get a much more complete color system
with parametrable color merge. It's not a priority.


## Ranks
Ranks in DOT are explained [here](http://graphs.grevian.org/example#example-7). They are handlable using the `rank/2` atoms.

Follows an example, showing how to place some nodes at the same rank (lower rank is higher in visualization):

    viz(1,link(a,(b;c;d))).
    viz(1,rank(same,(a;b))).
    viz(2,rank(same,(c;d))).

It is also possible to tune the rankdir global property:

    link(a,b).
    obj_property(graph,rankdir,"LR").



# ASP to DOT
Once the ASP program generated by scripts, have been solved by clingo,
the output atoms are studied, filtered and posttreated in order to produce dot file(s).


## visualization atoms
The ASP atoms that have an effect on dot file generation are:

- `link(A,B)`: two nodes, A and B, are linked in the graph (A is source, B target).
- `color(N,C)`: the node N, if exists, is colored in color C (see [colors](https://github.com/Aluriak/biseau#colors)).
- `color(A,B,C)`: the edge A->B, if exists, is colored in color C.
- `shape(N,S)`: the node N, if exists, will be shaped as indicated (rectangle, ellipse, circle,…).
- `annot(upper,N,T)`: the node N, if exists, will get the text T shown *above* it.
- `annot(lower,N,T)`: the node N, if exists, will get the text T shown *below* it.
- `annot(label,N,T)`: the node N, if exists, will get the label T.
- `label(N,T)`: shortcut for the previous.
- `annot(upper,N,F,V)`: upper annotation of node N, if exists, will have field F set to V (example: `annot(upper,3,labelfontcolor,red)`).
- `annot(lower,N,F,V)`: same for lower annotation.
- `textwrap(I)`: all texts will be wrapped to I characters (default: no wrap).
- `textwrap(N,I)`: texts related to node N will be wrapped to I characters.
- `textwrap(A,B,I)`: texts related to edge linking A to B will be wrapped to I characters.
- `dot_property(N,F,V)`: set for node N the value V for the field F in dot representation. In essence, label and color atoms are just shortcuts to this rule.
- `dot_property(S,T,F,V)`: set for link from node S to node T the value V for the field F in dot representation.
- `obj_property(O,F,V)`: set for the dot object O (*graph*, *edge* or *node*) the value V for global field F.

## Usage example
The script *Show Galois Lattice* is setting
most of these elements, notabily label as concepts uid,
upper annotation for objects in AOC poset,
lower ones for attributes in AOC poset, *link* as the *under* relation,
and also `obj_property(graph,dpi,500)` and `obj_property(edge,arrowhead,none)` in order to get a non-pixellized view and a lattice without arrows.
in the Galois Lattice.
See #11, #6.

## explanation of the dot_to_asp routine


## viz/2
Atoms may also be encapsulated in viz/2 atoms, for instance : `viz(color(a,b,red)).` or `viz(1,color(a,b,red)).` ;
those two will have the very same effect as `color(a,b,red)`, but with some variation on corner cases.

Let's build a node `a` with two reflexive links of different colors, `blue` and `red`.
The first obvious way to achieve that is:

    link(a,a).
    color(a,a,red).
    link(a,a).
    color(a,a,blue).

However, in ASP, an atom is either true or false, therefore `link(a,a)` is true, and telling this two times to the solver will not change anything.
Consequently, the resulting graph is a node `a` with *one* reflexive link, colored in *magenta* (blue + red).

What we want is to get *two* reflexive edges, i.e. apply rules multiple times. This can be achieved using levels in viz/2 atoms:

    viz(1,link(a,a)).
    viz(1,color(a,a,red)).
    viz(2,link(a,a)).
    viz(2,color(a,a,blue)).

These atoms are now pushed into a *level*, which is the first argument of viz/2 atom.
Each *level* is separately interpreted, allowing user to define multiple times the same values, but at different
levels so the output will consider multiple times the same atom.

Note that atoms that are not in a level, like `link(a,a)` or `viz(link(a,a))` are pushed into a *base level*,
i.e. a level that will be added to all levels.

Knowing that, we can improve a little our code:

    link(a,a).  % this one is common to all levels
    viz(1,color(a,a,red)).  % in the first case it's red
    viz(2,color(a,a,blue)).  % in the second, it's blue

In our example, the first atom is pushed into base level, and the two lasts atoms are manually pushed into level one and two.
Using this code, you should get a node with two reflexive edges of different colors.

Now, if in the final code, we:

- remove the levels: all atoms are in base level, it's equivalent to the first attempt we made.
- make the level identical: we got only one version of the link, and it's magenta. Again, equivalent to the first attempt.




# Scripting
Scripting is the most powerful feature of Biseau,
allowing user to code a program block that will be addable to the GUI.

Module [`core.scripts`](core/scripts.py) is in charge to find,
load and validate all the scripts.

Scripts comes in three flavors, three levels of complexity and control:

- *python modules*, which constitute the main interface.
- *rules*, which provides a simpler interface for only a subset of the features.
- *containers*, that are the most powerful/complex way to write a script.


## Rules
These scripts are written in JSON, but do not allow any mean of customization:
no option, just raw code that is added to the context.
It's a special case of scripts, where the context is modified
by appending some text.

You will find the current one in [`scripts/rules.json`](scripts/rules.json).

Note that rules are JSON dict, and all json files in [`scripts/`](scripts/) are read.
A JSON file can either contains a list of rules, or a single rule.


## Python modules
Python scripts are the most generalized way to add a treatment.

A script should be placed in [`scripts/`](scripts/), and the minimal working code is the following:

    NAME = "Name of my script"
    def run_on(context): return context

Follows a script that add some atoms in the context, depending of various options tunable by user:

```python
"""Build a random graph"""
import random
import itertools
NAME = "Random graph builder"
OUTPUTS = {'link/2'}
def run_on(context, *, nb_node:int=10, density:float=0.2):
    """
    nb_node -- number of node in the graph
    density -- targeted density. Real density may differ.
    """
    yield context  # do not shallow the context
    nodes = tuple(range(1, int(nb_node) + 1))
    for a, b in itertools.combinations(nodes, 2):
        if random.random() < float(density):
            yield 'link({},{}).'.format(a, b)
```

Note that:

- a `run_on` function can be a generator of string
Lucas Bourneuf's avatar
Lucas Bourneuf committed
283 284
- script have the responsibility to preserve (or not) the input context, unless `ERASE_CONTEXT = False` is given in global scope. This allow for scripts that hide or modify it, for instance.
- `INPUTS` and `OUTPUTS` are used when sorting the scripts: biseau tries to order scripts according to their I/O.
285 286 287 288 289 290
- the docstrings are used in the GUI, as script doc or to populate tooltips.


You will find much more explanations in the [example script](scripts/example.py).