Did you know ... Search Documentation:
Pack logtalk -- logtalk-3.86.0/examples/complements/allow/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.

% start by loading the non-patched object "employee":

| ?- logtalk_load(complements_allow(employee)). ...

% ask "employee" its name:

| ?- employee::name(Name).

Name = john yes

% ask "employee" its salary:

| ?- employee::salary(Salary).

Salary = 23500 yes

% check the protocol of the object "employee":

| ?- findall(Predicate, employee::current_predicate(Predicate), Predicates).

Predicates = [age/1, name/1, salary/1] yes

% now load the "add_on" complementing category:

| ?- logtalk_load(complements_allow(add_on)). ...

% load the "dynamic.lgt" source file that creates a dynamic complementing % category, also patching the "employee" object:

| ?- logtalk_load(complements_allow(dynamic)). ...

% find categories that complement objects:

| ?- complements_object(Category, Object).

Category = dynamic_patch, Object = employee ; Category = add_on, Object = employee yes

% use the event handler defined in the "add_on" category for the object "employee":

| ?- employee::name(Name). Received message name(_16) from user

Name = john yes

% check the consequences of the runtime patch of the salary/1 predicate:

| ?- employee::salary(Salary).

Received message salary(_G192) from user Salary = 42000 yes

% check the new protocol of the object "employee":

| ?- employee::predicates(Predicates). Received message predicates(_G180) from user

Predicates = [after/3, age/1, before/3, income/1, name/1, predicates/1, salary/1] yes

| ?- employee::predicate_property(predicates(_), Property).

Property = logtalk ; Property = scope(public) ; Property = (public) ; Property = static ; Property = declared_in(add_on) ; Property = declared_in(add_on, 29) ; Property = defined_in(add_on) ; Property = defined_in(add_on, 31) ; Property = number_of_clauses(1) yes

| ?- employee::predicate_property(income(_), Property).

Property = logtalk ; Property = scope(public) ; Property = (public) ; Property = static ; Property = declared_in(employee) ; Property = alias_of(salary(_G3724)) ; Property = defined_in(dynamic_patch) ; Property = number_of_clauses(0) yes

% later, the boss finds out about the employee hacked salary:

| ?- abolish_category(dynamic_patch).

yes

| ?- employee::salary(Salary).

Received message salary(_G192) from user Salary = 23500 yes