graph_editor.pl 7.57 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3 4 5 6 7 8 9 10
:- module(
  graph_editor,
  [
    new_graph/0,
    delete_graph/1,
    set_graph_name/1,
    list_graphs/0,
    select_graph/1,
    add_vertex/1,
    delete_vertex/1,
Thierry Martinez's avatar
Thierry Martinez committed
11
    edge/1,
Thierry Martinez's avatar
Thierry Martinez committed
12 13
    add_edge/1,
    delete_edge/1,
Thierry Martinez's avatar
Thierry Martinez committed
14
    list_edges/0,
Thierry Martinez's avatar
Thierry Martinez committed
15 16
    list_isolated_vertices/0,
    list_graph_objects/0,
Thierry Martinez's avatar
Thierry Martinez committed
17
    graph_object/1,
Thierry Martinez's avatar
Thierry Martinez committed
18
    attribute/1,
Thierry Martinez's avatar
Thierry Martinez committed
19 20 21 22 23 24
    set_attribute/2,
    delete_attribute/2,
    list_attributes/1,
    place/1,
    transition/1,
    get_current_graph/1,
Thierry Martinez's avatar
Thierry Martinez committed
25
    set_current_graph/1,
Thierry Martinez's avatar
Thierry Martinez committed
26
    get_graph_name/2,
Thierry Martinez's avatar
Thierry Martinez committed
27
    get_attribute/2
Thierry Martinez's avatar
Thierry Martinez committed
28 29 30
  ]
).

Thierry Martinez's avatar
Thierry Martinez committed
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

:- devdoc('\\section{Commands}').


new_graph :-
  biocham_command,
  doc('Creates a new graph.'),
  add_item([kind: graph, key: new_graph, id: Id]),
  set_current_graph(Id).


delete_graph(Name) :-
  biocham_command,
  type(Name, name),
  doc('Deletes a graph.'),
  delete_items([kind: graph, key: Name]).


set_graph_name(Name) :-
  biocham_command,
  type(Name, name),
  doc('Sets the name of the current graph.'),
  get_current_graph(Id),
  replace_item(Id, graph, Name, Name).


list_graphs :-
  biocham_command,
  doc('Lists the graph of the current model.'),
  list_items([kind: graph]).


select_graph(Name) :-
  biocham_command,
  type(Name, name),
  doc('Selects a graph'),
  find_item([kind: graph, key: Name, id: Id]),
  set_current_graph(Id).


Thierry Martinez's avatar
Thierry Martinez committed
71 72 73 74 75 76 77 78 79
add_vertex(NameList) :-
  biocham_command(*),
  type(NameList, '*'(name)),
  doc('Adds new vertices to the current graph.'),
  \+ (
    member(Name, NameList),
    \+ (
      add_vertex(Name, _VertexId)
    )
Thierry Martinez's avatar
Thierry Martinez committed
80 81 82
  ).


Thierry Martinez's avatar
Thierry Martinez committed
83 84 85 86
delete_vertex(NameList) :-
  biocham_command(*),
  type(NameList, '*'(name)),
  doc('Deletes a set of vertices from the current graph.'),
Thierry Martinez's avatar
Thierry Martinez committed
87
  get_current_graph(GraphId),
Thierry Martinez's avatar
Thierry Martinez committed
88 89 90 91 92 93
  \+ (
    member(Name, NameList),
    \+ (
      delete_item([parent: GraphId, kind: vertex, key: Name])
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
94 95 96 97 98 99 100 101 102 103


:- grammar(edge).


edge(From -> To) :-
  name(From),
  name(To).


Thierry Martinez's avatar
Thierry Martinez committed
104 105 106 107 108 109 110 111 112 113 114 115
add_edge(EdgeList) :-
  biocham_command(*),
  type(EdgeList, '*'(edge)),
  doc('
    Adds the given set of edges to the current graph.
    The vertices are added if needed.
  '),
  \+ (
    member(Edge, EdgeList),
    \+ (
      add_edge(Edge, _EdgeId)
    )
Thierry Martinez's avatar
Thierry Martinez committed
116
  ).
Thierry Martinez's avatar
Thierry Martinez committed
117 118


Thierry Martinez's avatar
Thierry Martinez committed
119 120 121 122
delete_edge(EdgeList) :-
  biocham_command(*),
  type(EdgeList, '*'(edge)),
  doc('Deletes a set of edges from the current graph.'),
Thierry Martinez's avatar
Thierry Martinez committed
123
  get_current_graph(GraphId),
Thierry Martinez's avatar
Thierry Martinez committed
124 125 126 127 128 129
  \+ (
    member(Edge, EdgeList),
    \+ (
      delete_item([parent: GraphId, kind: edge, key: Edge])
    )
  ).
Thierry Martinez's avatar
Thierry Martinez committed
130 131 132 133


list_edges :-
  biocham_command,
Thierry Martinez's avatar
Thierry Martinez committed
134
  doc('Lists the edges of the current graph.'),
Thierry Martinez's avatar
Thierry Martinez committed
135 136 137 138
  get_current_graph(GraphId),
  list_items([parent: GraphId, kind: edge]).


Thierry Martinez's avatar
Thierry Martinez committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
list_isolated_vertices :-
  biocham_command,
  doc('Lists the isolated vertices of the current graph.'),
  get_current_graph(GraphId),
  isolated_vertices(GraphId, VerticeIds),
  list_ids(VerticeIds).


list_graph_objects :-
  biocham_command,
  doc('
    Lists the edges and the isolated vertices of the current graph,
    and their attributes if any.
  '),
  get_current_graph(GraphId),
  all_ids([parent: GraphId, kind: edge], EdgeIds),
  isolated_or_attributed_vertices(GraphId, VerticeIds),
  append(EdgeIds, VerticeIds, AllIds),
  list_ids([recursive, kind: attribute], AllIds).


Thierry Martinez's avatar
Thierry Martinez committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
:- grammar(graph_object).


graph_object(Edge) :-
  edge(Edge).

graph_object(Name) :-
  name(Name).


:- grammar(attribute).


attribute(Key: Value) :-
  name(Key),
Thierry Martinez's avatar
Thierry Martinez committed
175
  term(Value).
Thierry Martinez's avatar
Thierry Martinez committed
176 177 178 179 180

attribute(Name) :-
  name(Name).


Thierry Martinez's avatar
Thierry Martinez committed
181
set_attribute(GraphObjectSet, Attribute) :-
Thierry Martinez's avatar
Thierry Martinez committed
182
  biocham_command,
Thierry Martinez's avatar
Thierry Martinez committed
183
  type(GraphObjectSet, {graph_object}),
Thierry Martinez's avatar
Thierry Martinez committed
184
  type(Attribute, attribute),
Thierry Martinez's avatar
Thierry Martinez committed
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
  doc('
    Adds an attribute to every vertex or edge in the given set.
    The vertices and the edges are added if needed.
  '),
  \+ (
    member(GraphObject, GraphObjectSet),
    \+ (
      add_graph_object(GraphObject, Id),
      (
        Attribute = Key: _Value
      ->
        true
      ;
        Key = Attribute
      ),
      (
        item(
          [parent: Id, kind: attribute, key: Key, item: Item, id: AttributeId]
        )
      ->
        (
          Item = Attribute
        ->
          true
        ;
          replace_item(AttributeId, attribute, Key, Attribute)
        )
      ;
        add_item([parent: Id, kind: attribute, key: Key, item: Attribute])
      )
Thierry Martinez's avatar
Thierry Martinez committed
215 216 217 218
    )
  ).


Thierry Martinez's avatar
Thierry Martinez committed
219 220 221
place(NameList) :-
  biocham_command(*),
  type(NameList, '*'(name)),
Thierry Martinez's avatar
Thierry Martinez committed
222
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
223
    Sets that the vertices \\argument{NameList} are places.
Thierry Martinez's avatar
Thierry Martinez committed
224
  '),
Thierry Martinez's avatar
Thierry Martinez committed
225 226
  add_vertex(NameList),
  set_attribute(NameList, kind: place).
Thierry Martinez's avatar
Thierry Martinez committed
227 228 229



Thierry Martinez's avatar
Thierry Martinez committed
230 231 232
transition(NameList) :-
  biocham_command(*),
  type(NameList, '*'(name)),
Thierry Martinez's avatar
Thierry Martinez committed
233
  doc('
Thierry Martinez's avatar
Thierry Martinez committed
234
    Sets that the vertices \\argument{NameList} are transitions.
Thierry Martinez's avatar
Thierry Martinez committed
235
  '),
Thierry Martinez's avatar
Thierry Martinez committed
236 237
  add_vertex(NameList),
  set_attribute(NameList, kind: transition).
Thierry Martinez's avatar
Thierry Martinez committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256


delete_attribute(GraphObject, Attribute) :-
  biocham_command,
  type(GraphObject, graph_object),
  type(Attribute, name),
  doc('Removes an attribute from \\argument{GraphObject}.'),
  find_graph_object(GraphObject, Id),
  delete_item([parent: Id, kind: attribute, key: Attribute]).


list_attributes(GraphObject) :-
  biocham_command,
  type(GraphObject, graph_object),
  doc('List the attributes of \\argument{GraphObject}.'),
  find_graph_object(GraphObject, Id),
  list_items([parent: Id, kind: attribute]).


Thierry Martinez's avatar
Thierry Martinez committed
257 258 259 260 261 262 263 264 265 266 267 268 269
:- devdoc('\\section{Public API}').


get_current_graph(Id) :-
  find_item([kind: current_graph, item: Id]).


set_current_graph(Id) :-
  deselect_graph,
  add_item([kind: current_graph, item: Id, id: CurrentGraphId]),
  add_dependency(CurrentGraphId, Id).


Thierry Martinez's avatar
Thierry Martinez committed
270 271 272 273
get_graph_name(Id, Name) :-
  find_item([id: Id, item: Name]).


Thierry Martinez's avatar
Thierry Martinez committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
get_attribute(Id, Attribute) :-
  integer(Id),
  !,
  (
    Attribute = Key: _Value
  ->
    true
  ;
    Key = Attribute
  ),
  item([parent: Id, kind: attribute, key: Key, item: Attribute]).


get_attribute(GraphObject, Attribute) :-
  find_graph_object(GraphObject, Id),
  get_attribute(Id, Attribute).


:- devdoc('\\section{Private predicates}').
Thierry Martinez's avatar
Thierry Martinez committed
293 294 295 296 297 298 299


find_vertex(Name, Id) :-
  get_current_graph(GraphId),
  find_item([parent: GraphId, kind: vertex, key: Name, id: Id]).


Thierry Martinez's avatar
Thierry Martinez committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
find_edge(Edge, Id) :-
  get_current_graph(GraphId),
  find_item([parent: GraphId, kind: edge, key: Edge, id: Id]).


find_graph_object(GraphObject, Id) :-
  (
    GraphObject = (_From->_To)
  ->
    find_edge(GraphObject, Id)
  ;
    find_vertex(GraphObject, Id)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
315 316 317 318 319 320 321 322 323 324
add_graph_object(GraphObject, Id) :-
  (
    GraphObject = (_From->_To)
  ->
    add_edge(GraphObject, Id)
  ;
    add_vertex(GraphObject, Id)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
325
deselect_graph :-
Thierry Martinez's avatar
Thierry Martinez committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  delete_items([kind: current_graph]).


isolated_vertices(GraphId, VerticesId) :-
  findall(
    VertexId,
    (
      item([parent: GraphId, kind: vertex, id: VertexId, item: Vertex]),
      \+ item([parent: GraphId, kind: edge, item: (Vertex -> _)]),
      \+ item([parent: GraphId, kind: edge, item: (_ -> Vertex)])
    ),
    VerticesId
  ).

isolated_or_attributed_vertices(GraphId, VerticesId) :-
  findall(
    VertexId,
    (
      item([parent: GraphId, kind: vertex, id: VertexId, item: Vertex]),
Thierry Martinez's avatar
Thierry Martinez committed
345
      (
Thierry Martinez's avatar
Thierry Martinez committed
346 347
        \+ item([parent: GraphId, kind: edge, item: (Vertex -> _)]),
        \+ item([parent: GraphId, kind: edge, item: (_ -> Vertex)])
Thierry Martinez's avatar
Thierry Martinez committed
348 349 350
      ->
        true
      ;
Thierry Martinez's avatar
Thierry Martinez committed
351
        once(item([parent: VertexId, kind: attribute]))
Thierry Martinez's avatar
Thierry Martinez committed
352
      )
Thierry Martinez's avatar
Thierry Martinez committed
353 354
    ),
    VerticesId
Thierry Martinez's avatar
Thierry Martinez committed
355 356 357
  ).


Thierry Martinez's avatar
Thierry Martinez committed
358 359
add_vertex(Name, VertexId) :-
  get_current_graph(GraphId),
Thierry Martinez's avatar
Thierry Martinez committed
360
  (
Thierry Martinez's avatar
Thierry Martinez committed
361
    item([parent: GraphId, kind: vertex, key: Name, id: VertexId])
Thierry Martinez's avatar
Thierry Martinez committed
362 363 364
  ->
    true
  ;
Thierry Martinez's avatar
Thierry Martinez committed
365
    add_item([parent: GraphId, kind: vertex, key: Name, id: VertexId])
Thierry Martinez's avatar
Thierry Martinez committed
366
  ).
Thierry Martinez's avatar
Thierry Martinez committed
367 368


Thierry Martinez's avatar
Thierry Martinez committed
369 370 371 372 373
add_edge(Edge, EdgeId) :-
  get_current_graph(GraphId),
  Edge = (From -> To),
  add_vertex(From, FromId),
  add_vertex(To, ToId),
Thierry Martinez's avatar
Thierry Martinez committed
374
  (
Thierry Martinez's avatar
Thierry Martinez committed
375
    item([parent: GraphId, kind: edge, key: Edge, id: EdgeId])
Thierry Martinez's avatar
Thierry Martinez committed
376 377 378
  ->
    true
  ;
Thierry Martinez's avatar
Thierry Martinez committed
379 380 381 382
    add_item([parent: GraphId, kind: edge, key: Edge, id: EdgeId]),
    add_dependency(EdgeId, FromId),
    add_dependency(EdgeId, ToId)
  ).