|Did you know ...||Search Documentation:|
|Using program resources|
A resource is similar to a file. Resources, however, can be represented in two different formats: on files, as well as part of the resource archive of a saved state (see qsave_program/2) that acts as a virtual file system for the SWI-Prolog I/O predicates (see open/4, register_iri_scheme/3).
A resource has a name. The source data of a
resource is a file. Resources are declared by adding clauses to the
Resources can be accessed from Prolog as files that start with
or they can be opened using
As of SWI-Prolog 7.7.13, resources that are compiled into the program can be accessed using the normal file handling predicates. Currently the following predicates transparently handle resources as read-only files:
In addition, open_shared_object/3, underlying use_foreign_library/1 handles shared objects or DLLs by copying them to a temporary file and opening this file. If the OS allows for it, the copied file is deleted immediately, otherwise it is deleted on program termination.
With the ability to open resources as if they were files we can use them for many tasks without changing the source code as required when using open_resource/2. Below we describe a typical scenario.
:- asserta(user:file_search_path(icon, 'res://app/icon').
Before the system had the ability to open resources as files, resources were opened using the predicates open_resource/2 or open_resource/3. These predicates provide somewhat better dynamic control over resources depending on whether the code is running from files or from a saved state. The main disadvantage is that having a seperate open call requires rewriting code to make it work with resources rather than files.
The predicate open_resource/3 first checks resource/2. When successful it will open the returned resource source file. Otherwise it will look in the program's resource database. When creating a saved state, the system normally saves the resource contents into the resource archive, but does not save the resource clauses.
This way, the development environment uses the files (and modifications) to the resource/3 declarations and/or files containing resource info, thus immediately affecting the running environment, while the runtime system quickly accesses the system resources.
user. Clauses for them may be defined in any module, including the user module. Name is the name of the resource (an atom). A resource name may contain any character, except for $ and :, which are reserved for internal usage by the resource library. FileSpec is a file specification that may exploit file_search_path/2 (see absolute_file_name/2).
Often, resources are defined as unit clauses (facts), but the definition of this predicate also allows for rules. For proper generation of the saved state, it must be possible to enumerate the available resources by calling this predicate with all its arguments unbound.
If FileSpec points at a directory, the content of the
directory is recursively added below Name. If FileSpec
a term of the form
Alias(Name), all directories that match this specification
are enumerated and their content is added to the resource database. If
an file appears in multiple results of this search path only the first
file is added. Note that this is consistent with the normal behaviour
returns the first match. The Options can be used to control
what is saved from a directory.
As of version 7.7.13, SWI-Prolog resource files are zip(1) files. Prolog creates and accesses its resource files using the minizip project. The resource files may be examined and modified using any tool that can process zip files.