Sign in to follow this  
Followers 0
JRowe

Optional Parameters and general style?

8 posts in this topic

I'm wrapping openCyc, which is an expert system/AI type software system. The functions are sent and data returned via TCP connection on port 3601 on your machine. I'll be posting a much more detailed writeup after I finish the UDF, but...

The way I want this to work is simple.

#include "openCycAPI.au3"
_cycConnect(@IPAddress1, "3601")
$test = _constantP("aConstant")
msgBox(0,"Test", $test)oÝ÷ ØÜç^_ð­ªiz»azy쨺»®÷«rçyËb¢|"¶!jׯz²jwi¢»ZÛaz{¬y©ò¡ûayÌ~éܶ*'°ý½è(¶Ø^~æj×hÂv§vØ^µë-³
+Êj¶î´³
'uêâ!jÒ,¥uÚ0ØfÉû§rبÂ!j÷·ß%w¬¡û§~)â²~éܶ*'±©Ýmçè­â,µªí§©ò}ªíêÈÁ©íyÛ(触ëm¢ÇûaȬæjR7öY[y§]x¢iÖ«Ë÷hréÖ­Ú~׫!ø§ÈZ²Øb²+=öÇ+`zw«jײڬý½èî²×è®f­µçmè«ò¡Ë¦z{Z¶*'{hëh"×îËb¢{,¡ö«jëh×6;~openCycAPI.au3
global $cycConnection
global $cycData

Func _cycConnect($IPAddress, $cycPort)
    $cycConnection = TCPConnect($IPAddress, $cycPort)
    If @error Then
        MsgBox(0x20,"Connection error", "Unable to connect to " & $IPAddress & ":" & $cycPort)
    EndIf

EndFunc

Func _cycDisconnect($cycConnection)
        TCPCloseSocket($cycConnection)
        TCPShutdown()
EndFunc

Func _constantP($object)
    ;Fires off a correctly formatted TCP message to the Cyc server
    TCPSend($cycConnection,'(CONSTANT-P "'& $object & '")]')
        While 1
            ;Listens for the response, and when it gets one, writes the data to $cycData
            $cycData = TCPRecv($cycConnection,3601)
                If $cycData Then 
                    ExitLoop
                EndIf
        WEnd
    ;Returns $cycData
    Return $cycData
;~Return T iff the argument is a CycL constant
;~Single value returned satisfies BOOLEANP.
EndFuncoÝ÷ Ù8Z·lë-jƦ¦W¨Z´ay&mmæ ¢pk+hv­Z«xýéÜ¢éíz¸§ènW¦²+0Øh¦Ø¨©ij¶¦z׫°:«Þ¶+^iƬzì¢g¬¢»h}ºÚÈ_¢·¯z¼¨¦Ø¨©lqéÚ®*(®+-êÞk²¶h¶¬éÞ®'¢·±jjey«­¢+ÙÕ¹}åÍÍÉÐ ÀÌØí͹ѹ°ÀÌØíµÐôÀ°ÀÌØíÁɽÁÉÑ¥ÌôÀ¤(í¥É̽½ÉÉѱä½ÉµÑÑQ
@µÍÍѼѡ
åÍÉÙÈ(%Q
AM¹ ÀÌØíå
½¹¹Ñ¥½¸°Ìäì¡
eµMMIPÅÕ½ÐìÌäìµÀìÀÌØí͹ѹµÀìÌäìÅÕ½Ðì¥tÌäì¤($%]¡¥±Ä($$$í1¥Íѹ̽ÈÑ¡ÉÍÁ½¹Í°¹Ý¡¸¥ÐÑ̽¹°ÝÉ¥ÑÌÑ¡ÑѼÀÌØíåÑ($$$ÀÌØíåÑôQ
AIØ ÀÌØíå
½¹¹Ñ¥½¸°ÌØÀĤ($$$%%ÀÌØíåÑQ¡¸($$$$%á¥Ñ1½½À($$$%¹%($%]¹($íIÑÕɹÌÀÌØíåÑ(%IÑÕɸÀÌØíåÑ)¹Õ¹oÝ÷ Øz0v&¡ØÊØb²Ê-«b¶Ç§vËazmÚ«jg­z»"~Ø^Éìb²Ö§våÊ'íìÆ+-!ûay×jém¥ªÚë^®Æ«zǧ´ý
"h«Þ¶+y«^iºÚÈ_¢·¯z¼©¢Ë"nW¯jX¢b¢v¥±Ç§j¸¨jwm«p¢é]¶¸©ì!jÒ7ö÷ ¢Ö¡y§h~gºÚ"µÍÔÙ[
    ÌÍØÞXÐÛÛXÝ[Û   ÌÎNÊÖPËPTÔÑT ][ÝÉÌÎNÉ[È    ÌÍÜÙ[[ÙH   [È ÌÎNÉ][ÝÊWIÌÎNÊ

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like:

Func SelectFunc($Par)
    Select
        Case $Par = 1
            MsgBox(0, "", "1")
        Case $Par = 2
            MsgBox(0, "", "2")
        Case $Par = 3
            MsgBox(0, "", "3")
        Case $Par = "a"
            MsgBox(0, "", "a")
        Case $Par = "b"
            MsgBox(0, "", "b")
        Case $Par = "c"
            MsgBox(0, "", "c")
        EndSelect
EndFunc

?

Edited by Jiim

Share this post


Link to post
Share on other sites

Yes, but that would mean I'd have to go through and create a case for every optional scenario, which would be about 3x the amount of work.

What I'm looking for is some way of creating a generic template, with which I can paste into every function, and then modify only the TCPSend portion.

@NUMPARAMS is one macro that I've been looking at, trying to figure out how I can cycle through parameters and append them to a TCP message if they are not 0. But what if I need to pass 0 as a parameter?

Anyway, I was just thinking that there might be a way to handle it generically, rather than specific to each function I want to wrap.

Share this post


Link to post
Share on other sites

Yes, but that would mean I'd have to go through and create a case for every optional scenario, which would be about 3x the amount of work.

What I'm looking for is some way of creating a generic template, with which I can paste into every function, and then modify only the TCPSend portion.

@NUMPARAMS is one macro that I've been looking at, trying to figure out how I can cycle through parameters and append them to a TCP message if they are not 0. But what if I need to pass 0 as a parameter?

Anyway, I was just thinking that there might be a way to handle it generically, rather than specific to each function I want to wrap.

I expect there is a way to do what you want but I don't understand what you want. (Common problem of mine.)

What do you want to do with the optional parameters, and what form do the optional parametrs take? I mean, are the parameters always itegers, or always strings, or can they be anything?

The problem you imagine about having a parameter of zero doesn't exist because it will still count as a parameter.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The parameters are always strings. There are a variable number of parameters for each function, and some have a variable number of optional parameters. I've set each optional parameter to equal 0.

Every function has the same essential format, it simply sends the command and the parameters, if any, to the server, and then listens for the response, and returns the response. (I suppose I should put some sort of timeout in there.)

What I have is some huge number of functions, all formatted for autoit, and the original function definitions. Basically, I just have 3300 lines of

Func _someCycFuncName($parameter1, $parameter2=0)
;comments
;comments
EndFunc

What I want to do is append a generic function body to each one, to have some automatic way of placing a variable number of optional and required parameters within the TCPSend() so that I don't have to code individual snippets for each function.

Edited by Jrowe

Share this post


Link to post
Share on other sites

The parameters are always strings. There are a variable number of parameters for each function, and some have a variable number of optional parameters. I've set each optional parameter to equal 0.

Every function has the same essential format, it simply sends the command and the parameters, if any, to the server, and then listens for the response, and returns the response. (I suppose I should put some sort of timeout in there.)

What I have is some huge number of functions, all formatted for autoit, and the original function definitions. Basically, I just have 3300 lines of

Func _someCycFuncName($parameter1, $parameter2=0)
;comments
;comments
EndFunc

What I want to do is append a generic function body to each one, to have some automatic way of placing a variable number of optional and required parameters within the TCPSend() so that I don't have to code individual snippets for each function.

Assuming I understand now, then suppose the most parameters any functions needs is 7. One way is to write the functions so they all take 7 parameters, then each function can use the para,meters it needs.

But it would be better if you just had one function which could work out for itself what to do.

Can you give me an example of 3 different functions because the problem still isn't clear to me.

Do you mean that this will happen:-

You start the script by calling it with 10 parameters for example.

When functionA is called it needs to be passed param1 and param6,

when FunctionB is called it needs param3, param5 and para1

when functionC is called it needs no parameters.

Is it something like that? If it is then I can suggest something, if it isn't then I'm still lost. (I'll post a thread in general help to see if someone can explain it to me :) )


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

The most parameters needed by a single function is 6, of which 3 are optional. There are 178 different variations of parameters, and some 60 unique parameter names. I wanted to leave the parameter names intact, for easier learning and transitioning between the actual API and the AutoIt UDF I'm building.

I'll write a script that reads each function, and then appends a set of cases for each optional, returning a string which is the command and whatever parameters were passed. So the functions themselves will only be translating between something like _cycCommand($param1, $param2, $param3) to $command = 'Cyc-Command ' & '"hello"' & '"cruel"' & '"world"', and then I can just use TCPSend to fire off $command, instead of bloating my script with a ton of repetitive TCPSends and formatting.

It's amazing what sleep can do to clarify your thoughts. :)

I should have a working version within a few hours. I apologize for not making sense before, but this is probably a really specialized problem. You'll understand after I release it.

Share this post


Link to post
Share on other sites

K, a few hours = 8 or 9.

I'm having to go through by hand and flesh out the functions, because I can't find a generic enough pattern to replicate on all the functions.

So, I've got 3840 lines of code left, 260 functions of 449 done, although some don't accept parameters. I figure that since the structure is there, once I or other people encounter specific needs for the extended versions of the functions, they can easily add in the parameter Switch cases.

:)

To see a simple but really neat thing that OpenCyc can be used for in conjunction with AutoIt, check this out:

http://freshmeat.net/articles/view/1910/

Considering that UDFs are a structured format of well defined algorithmic knowledge, it should be a simple extension of that experiment to utilize the UDF packages documented and included with the standard AutoIt release.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0