External file for calculations

External file for calculations

Postby Jeff Barnes » Fri Jul 27, 2012 4:02 pm

Is there a way to have an external file (txt or maybe ini) where I could put code and have my app run that code and return a result?

I want to be able to give my users a way to change a formula that uses data in a dbf file.

Example of external file:

IF TEST->Age > 40
nResult := (TEST->Data1 * TEST->Data2) - (TEST->Data3 / TEST->Data4)
ELSE
nResult := (TEST->Data1 * TEST->Data2) - (TEST->Data3 / TEST->Data4) + 500
ENDIF
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: External file for calculations

Postby ukoenig » Fri Jul 27, 2012 4:24 pm

Jeff,

a Solution without using a extra File :

1. add a new Value-field to the DBF.
2. save the Value to the 1. physical Record. )
3. Before working with the DBF, go to the 1. Record ( Ordkeyno() because of maybe defined Index)
-- to read the Value for Your calculations.

Best Regards
Uwe :lol:
Since 1995 ( the first release of FW 1.9 )
i work with FW.
If you have any questions about special functions, maybe i can help.
User avatar
ukoenig
 
Posts: 4043
Joined: Wed Dec 19, 2007 6:40 pm
Location: Germany

Re: External file for calculations

Postby Jeff Barnes » Fri Jul 27, 2012 4:35 pm

Thanks for the reply Uwe but what I am looking to do is give the user the ability to create their own formulas.
Thanks,
Jeff Barnes

(FWH 16.11, xHarbour 1.2.3, Bcc730)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: External file for calculations

Postby Enrico Maria Giordano » Fri Jul 27, 2012 5:56 pm

Try this:

nResult := &( "(TEST->Data1 * TEST->Data2) - (TEST->Data3 / TEST->Data4)" )

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: External file for calculations

Postby Armando » Fri Jul 27, 2012 8:17 pm

Jeff:

Perhaps the Antonio's script.prg can help you.

viewtopic.php?f=6&t=21557&hilit=script&start=0

Or This one

http://www.xharbour.com/products/xbscript.php

Regards
SOI, s.a. de c.v.
estbucarm@gmail.com
http://www.soisa.mex.tl/
http://sqlcmd.blogspot.com/
Tel. (722) 174 44 45
Carpe diem quam minimum credula postero
User avatar
Armando
 
Posts: 3222
Joined: Fri Oct 07, 2005 8:20 pm
Location: Toluca, México

Re: External file for calculations

Postby kok joek hoa » Sat Jul 28, 2012 4:57 am

Hi Jeff,

Here sample the function that I use to calculate using outside string.

Regards,

kok

Code: Select all  Expand view
*-- FUNCTION -----------------------------------------------------------------
* Name........: HITUNG_BERAT
* Description.:
* Parameters..:
* Return value: none
* Author......:
*-----------------------------------------------------------------------------
STATIC FUNCTION HITUNG_BERAT(cstring)

    LOCAL CRUMUS    :=ALLTRIM(cstring)
    LOCAL CTEMP     :=''
    LOCAL ARUMUS    :={}
    LOCAL NCTL      :=0

    LOCAL NTEBAL    :=0.000
    LOCAL NLEBAR    :=0.000
    LOCAL NPANJANG  :=0.000
    LOCAL NOD       :=0.000
    LOCAL NID       :=0.000
    LOCAL NILAI_KG  :=0.000

   
    if alltrim(crumus)==''
       return 0
    endif  
   
    
    FOR NCTL=1 TO LEN(CRUMUS)

        IF  SUBSTR(CRUMUS,NCTL,1)=='('  .OR. ;
            SUBSTR(CRUMUS,NCTL,1)==')'  .OR. ;
            SUBSTR(CRUMUS,NCTL,1)=='+'   .OR. ;
            SUBSTR(CRUMUS,NCTL,1)=='-'   .OR. ;
            SUBSTR(CRUMUS,NCTL,1)=='/'   .OR. ;
            SUBSTR(CRUMUS,NCTL,1)=='*'
            AADD(ARUMUS,SUBSTR(CRUMUS,NCTL,1) )
        elseIF ISALPHA(SUBSTR(CRUMUS,NCTL,1))
            IF ISALPHA(SUBSTR(CRUMUS,NCTL+1,1))
              AADD(ARUMUS, SUBSTR(CRUMUS,NCTL,2) )
              NCTL :=NCTL+1
            ELSE
              AADD(ARUMUS, SUBSTR(CRUMUS,NCTL,1) )
            ENDIF  
        else
             AADD(ARUMUS, SUBSTR(CRUMUS,NCTL,1) )      
        ENDIF
   
    NEXT
   

    FOR NCTL=1 TO LEN(ARUMUS)

        DO CASE
            CASE ARUMUS[NCTL] =='T'
               
                IF (AALIAS[DB_SATUAN])->(DBSEEK(ODATABASE[DB_UKURAN]:UTEBAL))     
                    IF    (AALIAS[DB_SATUAN])->TIPE
                        NTEBAL := ODATABASE[DB_UKURAN]:TEBAL * (AALIAS[DB_SATUAN])->KONVERSI
                    ENDIF
                ENDIF
               
                ARUMUS[NCTL] := NTEBAL
           
            CASE ARUMUS[NCTL] =='L'        
                IF (AALIAS[DB_SATUAN])->(DBSEEK(ODATABASE[DB_UKURAN]:ULEBAR))     
                    IF    (AALIAS[DB_SATUAN])->TIPE
                        NLEBAR := ODATABASE[DB_UKURAN]:LEBAR * (AALIAS[DB_SATUAN])->KONVERSI
                    ENDIF
                ENDIF        
                ARUMUS[NCTL] :=NLEBAR
               
            CASE ARUMUS[NCTL] =='P'        
                IF (AALIAS[DB_SATUAN])->(DBSEEK(ODATABASE[DB_UKURAN]:UPANJANG))   
                    IF    (AALIAS[DB_SATUAN])->TIPE
                        NPANJANG := ODATABASE[DB_UKURAN]:PANJANG * (AALIAS[DB_SATUAN])->KONVERSI
                    ENDIF
                ENDIF          
                ARUMUS[NCTL] := NPANJANG
               
            CASE ARUMUS[NCTL] =='OD'           
                IF (AALIAS[DB_SATUAN])->(DBSEEK(ODATABASE[DB_UKURAN]:UOD))     
                    IF    (AALIAS[DB_SATUAN])->TIPE
                        NOD := ODATABASE[DB_UKURAN]:OD * (AALIAS[DB_SATUAN])->KONVERSI
                    ENDIF
                ENDIF      
                ARUMUS[NCTL] :=NOD
               
            CASE ARUMUS[NCTL] =='ID'           
                IF (AALIAS[DB_SATUAN])->(DBSEEK(ODATABASE[DB_UKURAN]:UID))     
                    IF    (AALIAS[DB_SATUAN])->TIPE
                        NID := ODATABASE[DB_UKURAN]:ID * (AALIAS[DB_SATUAN])->KONVERSI
                    ENDIF
                ENDIF   
                ARUMUS[NCTL] :=NID
               
            CASE ARUMUS[NCTL] =='BJ'
                ARUMUS[NCTL] := VGET[VAR_VALUE_BERAT_JENIS]
            CASE ARUMUS[NCTL] =='PR'
                ARUMUS[NCTL] := VGET[VAR_VALUE_PARAMETER]
           
               
        ENDCASE    
     
    NEXT  

    FOR NCTL =1 TO LEN(ARUMUS)
         
        DO CASE
            CASE VALTYPE(ARUMUS[NCTL])<>'N'
                CTEMP := CTEMP + ARUMUS[NCTL]
            CASE VALTYPE(ARUMUS[NCTL])=='N'
                CTEMP := CTEMP+ALLTRIM(STR(ARUMUS[NCTL]))
        ENDCASE    
    NEXT

   
   
    NILAI_KG :=&(CTEMP)
   

   
    nilai_kg := nilai_kg    

RETURN NILAI_KG
kok joek hoa
 
Posts: 117
Joined: Tue Jan 03, 2006 6:18 pm

Re: External file for calculations

Postby FiveWiDi » Sat Jul 28, 2012 9:37 am

Jeff Barnes wrote:Is there a way to have an external file (txt or maybe ini) where I could put code and have my app run that code and return a result?

I want to be able to give my users a way to change a formula that uses data in a dbf file.

Example of external file:

IF TEST->Age > 40
nResult := (TEST->Data1 * TEST->Data2) - (TEST->Data3 / TEST->Data4)
ELSE
nResult := (TEST->Data1 * TEST->Data2) - (TEST->Data3 / TEST->Data4) + 500
ENDIF


Jefff try tihis:

In your application:
/* ***************************************** */
FUNCTION ExeFile( )
Local cFile := "MyAMPA32.prg"
Local nRespuesta := 0

Local ureturn := Nil

If File (cFile )

If File ( "MyAMPA32.hrb" )
HB_HRBRun( "MyAMPA32.hrb", @ureturn )
Else
If (nRespuesta := HB_Compile( "", cFile, "-n", "-gh" ) ) == 0
HB_HRBRun( "MyAMPA32.hrb", @ureturn )
EndIf
EndIf

EndIf

msgalert( ureturn, "ssss" )

Return nRespuesta
/* ***************************************** */

Your external PRG:
/* ***************************************** */
FUNCTION MyAMPA32( uReturn )

uReturn := Date() + 5

RETURN uReturn - 3
/* ***************************************** */

If you changes the external file, you must delete the "MyAMPA32.hrb" file.

For this you must add the lib "hbcplr.lib"

Regards,
Un Saludo
Carlos G.

FiveWin 24.02 + Harbour 3.2.0dev (r2403071241), BCC 7.7 Windows 10
FiveWiDi
 
Posts: 1189
Joined: Mon Oct 10, 2005 2:38 pm

Re: External file for calculations

Postby Antonio Linares » Sat Jul 28, 2012 10:40 am

Jeff,

Please review FWH\samples\scripts.prg, it uses the powerful Harbour capability to have the full Harbour compiler inside your own apps so you can compile and run any external file with code or dinamically generated code from your app :-)

In FWH 12.07 we provide a new function Execute( cCode, params ) --> uResult that simplifies the process,
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42089
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], Silvio.Falconi and 117 guests