Re: [eclipse-clp-users] R: How to create a EC_word/string from an array?

From: Kish Shen <kisshen_at_cisco.com>
Date: Fri, 17 Oct 2008 12:06:13 +0100
Hi Giuseppe,

Your code first checks if X is a list -- if X is supposed to be your 
array (i.e. the structure), then it would not be a list -- lists are 
special structures with the functor ./2.

EC_word is essentially an ECLiPSe variable, i.e. it can be instantiated 
to any term, but if you assign an array (which is a structure) to the 
term, it is not an atomic term (and specifically not an atom or string, 
which is what ec_get_string() requires the EC_word type to be. This is 
the same as an array will not unify with a string.

In general, you should try to do as much of the manipulating of ECliPSe 
data structure with ECLiPSe code, rather than in your C/C++ code. It is 
much easier. In the C/C++ interface, you are essentially manipulating 
the raw representation of the ECLiPSe data structures, and you should 
really know something about the implementation of these things to use 
them (although the EC_word does provide some protection). It is also 
easy to get things wrong, without necessarily getting any immediate 
errors, but which may then cause what seems to be mysterious problems 
later on.

So if what you want to do is to convert an array, say [](1,2,3) to the 
string "[](1,2,3)", the easiest way to do this is with ECLiPSe, e.g. 
using term_string/2, and then passing the string to your C++ code. The 
other way is to open a stream to

If you want to do this entirely in C++, you will need to first convert 
the functor to a string, and then access each argument of your array to 
convert the arguments to a string (and you may need to do this 
recursively, if your arguments can be compound terms_. This is much more 
difficult to write.

If you have any more questions, I think it may be best if you just send 
them to me, rather than the whole list, unless you think they are of 
general interest.

Cheers,

Kish

Giuseppe Di Guglielmo wrote:
> Dear Kish,
> in the Section Decomposing ECLiPSe terms in C++ of the Embedding and
> Interfacing Manual:
> http://87.230.22.228/doc/embedding/embroot067.html
> there are some function to convert a EC_word to string, long, double types.
> 
> I'd like to assign the array to a EC_word and then if possible convert it to
> a C++ string.
> 
> At the moment the following C++ code fails, I assume it is due to the
> conversion to C++ which never succeeds:
> 
> EC_word head;
> EC_word tail;
> char**  as_string;
> long    as_long;
> 
> if ( (X.is_list( head, tail ) == EC_succeed) ) {
> 
> 	std::cout << "X!" std::endl;
> 
> } if ( (X.is_long( &as_long ) == EC_succeed) ) {
> 
>  	std::cout << "X = " << as_long << std::endl;
> 
> } if ( (X.is_long( as_string ) == EC_succeed) ) {
> 
>  	std::cout << "X = " << std::string( as_string ) << std::endl;
> 
> } else {
> 
> 	std::cout << "FAIL!" << std::endl;
> 	exit(1);
> 
> }
> 
> So I thought to use something like
> print_obj(Obj) :- dim(Obj, [N]), ( for(J,1,N), param(Obj) do Ob is Obj[J],
> printf("%1d", Ob) ), nl.
> to create a EC_word (as string) and the retrieve it as a C++-string.
> 
> I hope the problem is clear now,
> thanks,
> Giuseppe
> 
>> -----Messaggio originale-----
>> Da: Kish Shen [mailto:kisshen_at_cisco.com]
>> Inviato: venerd́ 17 ottobre 2008 4.48
>> A: Giuseppe Di Guglielmo
>> Cc: eclipse-clp-users_at_lists.sf.net
>> Oggetto: Re: [eclipse-clp-users] How to create a EC_word/string from an
>> array?
>>
>> Giuseppe Di Guglielmo wrote:
>>> Dear all,
>>> the function
>>>
>>> print_obj(Obj) :- dim(Obj, [N]), ( for(J,1,N), param(Obj) do Ob is
>> Obj[J],
>>> printf("%1d", Ob) ), nl.
>>>
>>> permits to print on console the values of an array. How can I append all
>>> this value to create a EC_word?
>>>
>>> Best regards,
>>> Giuseppe
>>>
>>> ------------------------------------------------------------------------
>> ----
>>> Giuseppe Di Guglielmo       Dept. of Computer Science - University of
>>> Verona
>>>                                Strada le Grazie, 15 - 37134 Verona -
>> Italy
>>> Phone Office:   +39 045 8027049         Fax:    +39 045 8027068
>>> Phone Home:     +39 045 6152251         Mobile: +39 347 0493371
>>>
>>> WWW: http://profs.sci.univr.it/~diguglielmo
>>>
>>> email: giuseppe.diguglielmo_at_univr.it
>>> email: diguglielmo_at_certess.com          Skype: giuseppe.diguglielmo
>>>
>> Hi Giuseppe,
>>
>> I am not quite sure what you are asking. An array is simply a structure
>> (the array notation just allow structures to be used more like
>> conventional array syntax), and you can pass a structure to your C++ as
>> an EC_word. You don't need to construct it if the structure already
>> exist in ECLiPSe.
>>
>> 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.
>>
>> -------------------------------------------------------------------------
>> This SF.Net email is sponsored by the Moblin Your Move Developer's
>> challenge
>> Build the coolest Linux based applications with Moblin SDK & win great
>> prizes
>> Grand prize is a trip for two to an Open Source event anywhere in the
>> world
>> http://moblin-contest.org/redirect.php?banner_id=100&url=/
>> _______________________________________________
>> ECLiPSe-CLP-Users mailing list
>> ECLiPSe-CLP-Users_at_lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/eclipse-clp-users
> 


-- 
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 Fri Oct 17 2008 - 11:06:46 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET