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

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

7
8
% Error handling
:- consult(error_handling).
9

10

Sebastian Seufert's avatar
Sebastian Seufert committed
11
/**
Siebers, Michael's avatar
Siebers, Michael committed
12
 * assert_dict(+Item:dict, -Success:integer) is det.
Sebastian Seufert's avatar
Sebastian Seufert committed
13
 *
Siebers, Michael's avatar
Siebers, Michael committed
14
 * 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
15
16
 *
 */
Siebers, Michael's avatar
Siebers, Michael committed
17
18
19
assert_dict(D, Acc) :-
    del_dict(type, D, Type, Dict),
    is_dict(Dict, Type),
20
    Abs_path = Dict.abs_path,
21
    (
22
        item(Abs_path,_)
23
24
25
    ->  Acc = 0
    ;   add_item(Abs_path, Dict),
        Acc = 1
26
    ).
27

Siebers, Michael's avatar
Siebers, Michael committed
28

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

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

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

Siebers, Michael's avatar
Siebers, Michael committed
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%! 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)]).
68
    
69
70
71
    
%! item_type(-Type) is det.
%
Siebers, Michael's avatar
Siebers, Michael committed
72
% Generates the type definition for an item (that is a file or a directory). Type is a valid type as defined by types:check_type_def/1.
73
item_type(anyof([DirectoryType,FileType])) :-
74
    abs_path_type(AbsPathType),
75
    CommonKeys=[
76
        abs_path: AbsPathType,
77
78
79
        creation_time: integer
    ],
    FileType = dict([
Siebers, Michael's avatar
Siebers, Michael committed
80
        type: atom('^file$'),
81
82
83
84
85
86
87
88
        file_size: positive_integer,
        media_type: atom,
        filename_extension: atom,
        modification_time: integer,
        access_time: integer,
        change_time: integer
        | CommonKeys
    ]),
Siebers, Michael's avatar
Siebers, Michael committed
89
    DirectoryType = dict([type:atom('^directory$')|CommonKeys]).