background.pl 1.98 KB
Newer Older
1
2
% Types
:- use_module(src/types).
3

4
% Structure of items & persistency
5
:- use_module(src/theory_bg).
6

7
% Error handling
8
9
10
11
:- ensure_loaded(error_handling).

% Item definition
:- ensure_loaded(type_defs).
12

13

Sebastian Seufert's avatar
Sebastian Seufert committed
14
/**
Siebers, Michael's avatar
Siebers, Michael committed
15
 * assert_dict(+Item:dict, -Success:integer) is det.
Sebastian Seufert's avatar
Sebastian Seufert committed
16
 *
Siebers, Michael's avatar
Siebers, Michael committed
17
 * Auxiliary predicate adding an items to the background knowledge if no item with the same absolute path exists there. Success is bound to `1` if the item could be added and to `0` otherwise. 
Sebastian Seufert's avatar
Sebastian Seufert committed
18
19
 *
 */
Siebers, Michael's avatar
Siebers, Michael committed
20
21
22
assert_dict(D, Acc) :-
    del_dict(type, D, Type, Dict),
    is_dict(Dict, Type),
23
    Abs_path = Dict.abs_path,
24
    (
25
        item(Abs_path,_)
26
27
28
    ->  Acc = 0
    ;   add_item(Abs_path, Dict),
        Acc = 1
29
    ).
30

Siebers, Michael's avatar
Siebers, Michael committed
31

Sebastian Seufert's avatar
Sebastian Seufert committed
32
/**
Siebers, Michael's avatar
Siebers, Michael committed
33
 * retract_list(+Items:list(dict), -Failed:list(dict)) is det.
Sebastian Seufert's avatar
Sebastian Seufert committed
34
 *
Siebers, Michael's avatar
Siebers, Michael committed
35
 * Auxiliary predicate removing items from the background knowledge, skipping nonexisting entries.
Sebastian Seufert's avatar
Sebastian Seufert committed
36
 *
Siebers, Michael's avatar
Siebers, Michael committed
37
 * Failed tracks the items that could not be removed, hopefully _only_ because they are not existing.
Sebastian Seufert's avatar
Sebastian Seufert committed
38
39
40
41
 * 
 * @tbd advanced error handling
 */

42
43
retract_list([], []).
retract_list([In|Ins],Outs) :-
44
45
46
    del_dict(type, In, Type, Dict),
    is_dict(Dict, Type),
    rem_item(Dict.abs_path,Dict),!,
47
48
49
50
    retract_list(Ins,Outs).

retract_list([In|Ins],[In|Outs]) :-
    retract_list(Ins,Outs).
51

Siebers, Michael's avatar
Siebers, Michael committed
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
%! all_items_external(-Items:list(dict)) is det.
%
% Generates the list of all items currently defined in theory_bg prepared for externalization. That is, the type of the item is included under the key 'type'.
all_items_external(Items) :-
    all_items(ItemPairs),
    maplist(all_items_external_,ItemPairs, Items),
    item_type(ItemType),
    catch(parse_type(Items, list(ItemType), Items),
        E,
        api_error(implementation_error, [
                    cause(E), predicate(all_items_external/1),
                    message("List of items transformed for externalization does not match expected type.")
        ])       
    ).
    
all_items_external_(_Key-Value,Item) :-
    is_dict(Value, Type),
    parse_type(Value, dict([type:atom=Type]), Item, [extra_keys(add)]).