Did you know ... Search Documentation:
Pack logtalk -- logtalk-3.77.0/examples/serialization/SCRIPT.txt

This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

% load the example:

| ?- logtalk_load(serialization(loader)). ... yes

% create a protocol declaring some predicates:

| ?- create_protocol(abc, [], [public([a/1,b/1,c/1])]).

yes

% create some dynamic objects implementing the protocol:

| ?- create_object(Object1, [implements(abc)], [], [a(1),b(1),c(1)]), create_object(Object2, [implements(abc)], [], [a(2),b(2),c(2)]), create_object(Object3, [implements(abc)], [], [a(3),b(3),c(3)]).

Object1 = o1, Object2 = o2 Object3 = o3 yes

% save the objects to a file:

| ?- serializer::save(abc, abc_objects).

yes

% abolish all objects:

| ?- forall(conforms_to_protocol(Object,abc), abolish_object(Object)).

yes

% restore the serialized objects from the file:

| ?- serializer::restore(abc_objects).

yes

% confirm the restoring process worked as expected:

| ?- conforms_to_protocol(Object, abc), Object::(a(A), b(B), c(C)).

Object = o3, A = B, B = C, C = 1 ; Object = o4, A = B, B = C, C = 2 ; Object = o5, A = B, B = C, C = 3 yes