Calling clause from new-clause

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
mike
Posts: 6
Joined: Tue Jun 16, 2009 2:44 pm

Calling clause from new-clause

Post by mike » Thu Jun 18, 2009 8:21 am

Hello,

I try to call a clause when instatiating an object, so that a variable/attribute is evaluated right at the
beginning (instatiation):

Code: Select all

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	   The constructor 
	   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
	:- public(new/3).
	:- mode(new(-object_identifier, +atom, +atom), one).
	new(Domain, LowBorder, HighBorder) :- self(Self),
	                                      create_object(Domain, 
										                [extends(Self)], 
														[], 
														[
														    lowBorder(LowBorder), 
														  	highBorder(HighBorder),
														  	counter(0),
                                              // HERE SHOULD BE CALLED THE evalInDomain
														]
									                   ).
	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	   Evaluations of attributes -> saved in boolean attributes 
	   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
	:- public(evalInDomain/0).
	:- mode(evalInDomain, one). 
    evalInDomain :- ::lowBorder(LowBorder),
    				::highBorder(HighBorder),
    				::counter(Counter),
    				    				
    				( number(LowBorder), number(HighBorder) 		-> ::lowBorder(LB), ::highBorder(HB); true ), 
    				( \+number(LowBorder), number(HighBorder) 		-> LowBorder::lowBorder(LB), ::highBorder(HB); true ),
    				( number(LowBorder), \+number(HighBorder) 		-> ::lowBorder(LB), HighBorder::highBorder(HB); true ),
    				( \+number(LowBorder), \+number(HighBorder) 	-> LowBorder::lowBorder(LB), HighBorder::highBorder(HB); true ),
    				   				
					( LB =< Counter, Counter =< HB -> ::asserta(inDomain(true)); ::asserta(inDomain(false)) ).

But when I put the evalInDomain/0 into the new-clause it does not work.


I tried it over a second clause init/3 where new/3 is called and afterwards evalInDomain/0.

Code: Select all

:- public(init/3).
	:- mode(init(-object_identifier, +atom, +atom), one).
	init(Domain, LowBorder, HighBorder) :- ::new(Domain, LowBorder, HighBorder), ::evalInDomain.
but that does not work either.

Thanx for any help,

Mike

Paulo Moura
Logtalk developer
Posts: 474
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Calling clause from new-clause

Post by Paulo Moura » Thu Jun 18, 2009 12:08 pm

In order to call a goal at object creation, you must use an initialization/1 directive. In your case, try:

Code: Select all

new(Domain, LowBorder, HighBorder) :-
    self(Self),
    create_object(
        Domain,
        [extends(Self)],
        [initialization(evalInDomain)],
        [lowBorder(LowBorder), highBorder(HighBorder), counter(0)]
    ).
Let me know...
Paulo Moura
Logtalk developer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest