Menu

Routers to save state ( save) ?

Help
Arya
2024-10-04
2024-10-05
  • Arya

    Arya - 2024-10-04

    Hello there !

    I am using the CLIPS V 6.31 through the .NET core wrapper provided.
    So far , I have been able to

    1. Assert a number of facts within the environment using the assert string function
    2. Run the engine
    3. Invoke (eval) a CLIPS native function (not a UDF) which returns a json representation of a CLIPS instance which is indeed the produced result of the run and deserialize it inside the C# application.

    At this point, I do indeed to save into the database ( or .net app memory ) every fact or instance available within the environment before i dispose of the object. I will then load into a new environment the last persisted set of facts and instances where i left earlier.

    the problem is , as I have learned ( and correct me if I'm wrong ) , functions (save) , (bsave) , (save-facts ) do not use a logical name and i therefore cannot capture their output ( as i would with wdisplay & (facts) function ) through custom routers .

    I am aware that i can write the files into a hard disk and read from there or perhaps write other clips native functions to pass to c# string representations of every fact and instance within the env ( i believe there is no readily available function to do this )
    The two of the approaches would negatively affect the performance of the application .

    so what would be the correct approach for the problem at hand ? the performance is a matter of concern .

    Many thanks in advance

     
  • Gary Riley

    Gary Riley - 2024-10-04

    The ppfact function allows you to specify a logical name, so if you want to use a custom router you can iterate over the facts and use ppfact to direct the output to a logical name recognized by the router.

         CLIPS (6.31 6/12/19)
    CLIPS> (assert (a) (b) (c))
    <Fact-3>
    CLIPS> (open "facts.out" out "w")
    TRUE
    CLIPS> (foreach ?f (get-fact-list) (ppfact ?f out))
    CLIPS> (close out)
    TRUE
    CLIPS> 
    
     
  • Arya

    Arya - 2024-10-04

    Thanks for the response. Also is there an equivalent to retrieve the instances ready to be loaded ? or do i need to write a function ? I understand (ppinstance) works in a different manner

    update : how about :

    (deffunction get-instance-list ()
        (find-all-instances ((?ins USER)) TRUE))
    
    
    (deffunction route-print-instance (?instance_address ?logical_name)
    
        (bind ?class (class ?instance_address)) ; e.g., PROFILE
        (bind ?instance_name (instance-name ?instance_address)) ; e.g., gen1
        (bind ?instance_slots_string "")
    
    
        (bind ?class_slot_names (class-slots ?class))
    
    
        (loop-for-count  (?i 1 (length$ ?class_slot_names) )    do  
    
            (bind ?slot_name    (nth$ ?i ?class_slot_names))                        ; slot_name_2
            (bind ?slot_value   (send ?instance_name (sym-cat get- ?slot_name)))    ; slot_value_2
    
            (if (and (multifieldp ?slot_value) (eq 0 (length$ ?slot_value)))
                then (bind ?slot_value ""))
    
            (bind ?stringified (str-cat "(" ?slot_name " " ?slot_value ")") )       ; "(slot_name_2 slot_value_2)"
    
            ; concat to previous slots e.g.,  "(slot_name_1 slot_value_1) (slot_name_2 slot_value_2)"
            (bind ?instance_slots_string (str-cat ?instance_slots_string  ?stringified))   
        )
    
    
        ; result = "gen2 of PROFILE (slot_name_1 slot_value_1) (slot_name_2 slot_value_2) "
        (bind ?result (str-cat  ?instance_name " "  "of" " " ?class  " " ?instance_slots_string )) 
    
        (printout ?logical_name ?result)
    )
    

    Is there a more efficient way or perhaps a native function for the purpose that i have missed ?

     

    Last edit: Arya 2024-10-05
    • Gary Riley

      Gary Riley - 2024-10-05

      In CLIPS 6.3, there are two C functions, EnvGetInstancePPForm and EnvGetFactPPForm, that will return the printed representation of an instance or a fact. You can iterate over the facts/instances and save that representation to later recreate the facts/instances. The drawback is that the buffer passed to the functions is fixed to whatever it's initially set. In CLIPS 6.4, the equivalent functions are InstancePPForm and FactPPForm and these use a data structure that will expand the string buffer needed to represent the entire fact/instance.

       
  • Arya

    Arya - 2024-10-05

    Thanks for the prompt reply . I sure will check them out

     

Log in to post a comment.

MongoDB Logo MongoDB