SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Idea

Use var(X) and nonvar(X) to determine the search strategy automatically based on the variable binding instead of letting the caller choose the variant.

Although the predicate is harder to write, it is much easier to use and there should be less performance issues caused by using the wrong predicate.

Example

Bad

The user has to select the right predicate for good performance.


/**
 * extends_recursive(+SubClass,?SuperClass)
 */

extends_recursive(SubClass, SuperClass) :-
    extendsT(SubClass, SuperClass).

extends_recursive(SubClass, SuperClass) :-
    extendsT(SubClass, MiddleClass),
    extends_recursive(MiddleClass, SuperClass).

/**
 * extends_recursive_from_super_class(?SubClass,+SuperClass)
 */

extends_recursive_from_super_class(SubClass, SuperClass) :-
    extendsT_In(SuperClass,SubClass).

extends_recursive_from_super_class(SubClass, SuperClass) :-
    extendsT_In(SuperClass,MiddleClass),
    extends_recursive_from_super_class(SubClass, MiddleClass).

Good

The optimal search strategy is choosen automatically based on the binding of the variables. The old strategies renamed and called by the new predicate.


/**
 * extends_recursive(?SubClass,?SuperClass)
 */

% find all superclasses of subclass
extends_recursive(SubClass, SuperClass) :-
    ((nonvar(SubClass),var(SuperClass));(nonvar(SubClass),nonvar(SuperClass))),
    extends_recursive_from_sub_class(SubClass,SuperClass).

% find all subclasses of superclass
extends_recursive(SubClass,SuperClass) :-
    var(SubClass),nonvar(SuperClass),
    extends_recursive_from_super_class(SubClass,SuperClass).

% find all extension pairs    
extends_recursive(SubClass,SuperClass) :-
    var(SubClass),var(SuperClass),
    classDefT(SubClass,_,_,_),
    extends_recursive_from_sub_class(SubClass,SuperClass).

/**
 * extends_recursive_from_sub_class(+SubClass,?SuperClass)
 */
extends_recursive_from_sub_class(SubClass,SuperClass) :-
    extendsT(SubClass,SuperClass).

extends_recursive_from_sub_class(SubClass,SuperClass) :-
    extendsT(SubClass,MiddleClass),
    extends_recursive_from_sub_class(MiddleClass, SuperClass).

/**
 * extends_recursive_from_super_class(?SubClass,+SuperClass)
 */
extends_recursive_from_super_class(SubClass,SuperClass) :-
    extendsT_In(SuperClass,SubClass).

extends_recursive_from_super_class(SubClass,SuperClass) :-
    extendsT_In(SuperClass,MiddleClass),
    extends_recursive_from_super_class(SubClass, MiddleClass).

Warning: Because bugs might only affect the performance, they might be hard to find in the second code block .

research/cultivate/prologsearchstrategies.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2019