Re: [eclipse-clp-users] Retract/asserta in C++

From: Kish Shen <kisshen_at_cisco.com>
Date: Wed, 25 Apr 2012 16:20:39 +0100
Hi Christian,

On 23/04/2012 10:17, Christian Giffhorn wrote:
> I have problems, to edit my knowledge base from c++ side. The first
> addition with "asserta" still works, but it behave like an "assert"
> because even when the retract didn't work, it should use the newest
> entry from the "asserta" like when i use asserta in the eclipse tk.
>

There should be no difference in behaviour when a built-in like assert 
or asserta is run from ECLiPSe directly (what you call eclipse tk), or 
from C++. I suspect the difference is due to the way you are posting the 
goals in C++, i.e. it does not correspond to what you are doing from 
ECLiPSe.

I don't understand what you mean by 'even when retract didn't work, it 
should use the newest entry from the asserta' -- what do you mean by 
'retract didn't work', and what do you mean by 'use the newest entry'? 
If by 'didn't work' you mean the assert failed (or aborted), then no 
clause is retracted, so nothing is 'used' -- assuming by 'used' you mean 
the clause is removed.


> eclipse:
> :- discontiguous(tool/3).
> :- dynamic tool/3.
>
> % rules set etc.
>
> C++:
> EC_word asserta(EC_word fact)
> {
> 	return term(EC_functor("asserta", 1), fact);
> }
>
> EC_word removefact(EC_word fact)
> {
> 	return term(EC_functor("retract", 1), fact);
> } // you should be concrte with your fact here, for the case that you
> only more then one target fitting this criteria, it will take the
> first
>

I don't understand the intention of these two functions -- they just 
return an ECLiPSe term (assert(...) and retract(...)), without going to 
ECLiPSe. From you following code, are you thinking these functions will 
be executed when you call post_goal()? This is not what post_goal() does 
-- it posts a goal to the ECLiPSe side, which would be executed when 
control is passed to ECLiPSe (by calling ec_resume(), which you don't do 
in the code you show).

Are you trying to define ECLiPSe clauses from C++? This is not what you 
are doing, and I don't think it is a good idea to try to do this -- the 
ECLiPSe C++ interface is not designed to be an API for you to program 
ECLiPSe code from C++.

If you must define the code from C++, then you need to post a goal to 
compile the clauses you want to compile (you can also assert the 
clauses, but this is also not recommended). You also don't need to (and 
should not) define asserta, as this is already a built-in.

>
> for (int i = 0; i<  3; i++)
> {
>     sprintf (randomv, "%i", rand() % 8 + 1);
>
>     post_goal(asserta(term(EC_functor("tool", 3), EC_atom("hammer"),
> EC_atom(randomv), EC_atom("s0"))));
>
>     post_goal(run(EC_atom("control"), EC_atom("s0"), p));
>
>     post_goal(removefact(term(EC_functor("tool", 3), EC_atom("hammer"),
> EC_atom(randomv), EC_atom("s0"))));
>
> }

What you seem to be doing here is posting the same 3 goals 3 times, and 
this is equivalent to the following query in ECLiPSe:

asserta(...),
run(control(...)),
removefact(...),
asserta(...),
run(...),
removefact(...),
asserta(...),
run(...),
removefact(...)


asserta is a built-in, but run/1 and removefact/1 are not, and unless 
they are defined in your ECLiPSe code, executing these goals will lead 
to an abort on the ECLiPSe side, and the following goals will not be 
executed.

And repeating what I said already, you need to call ec_resume() from C++ 
for the posted goals to be executed.

In addition, in some of your arguments, you use EC_atom(randomv) where 
randomv is a string representation of an integer. What this does is 
create an atom with a name that looks like an integer (e.g. '1' in 
ECLiPSe syntax) but is not an integer (1 in ECLiPSe syntax), which I 
assume is not what you want. Such an atom is not the same as the 
corresponding integer, will not unify with it, and cannot be used as a 
number (e.g. in expressions).


Cheers,

Kish


-- 
This e-mail may contain confidential and privileged material for the
sole use of the intended recipient. Any review, use, distribution or
disclosure by others is strictly prohibited. If you are not the intended
recipient (or authorized to receive for the recipient), please contact
the sender by reply e-mail and delete all copies of this message.
Cisco Systems Limited (Company Number: 02558939), is registered in
England and Wales with its registered office at 1 Callaghan Square,
Cardiff, South Glamorgan CF10 5BT.
Received on Wed Apr 25 2012 - 15:20:47 CEST

This archive was generated by hypermail 2.2.0 : Tue May 01 2012 - 06:14:57 CEST