<commit_before>#lang racket/base

(provide agt)

(require racket/list)
(require fractalide/modules/rkt/rkt-fbp/agent)
(require fractalide/modules/rkt/rkt-fbp/graph)

; (-> agent graph)
(define (get-graph agent input output)
  (send (output "ask-graph") agent)
  (recv (input "ask-graph")))

; (-> (Listof virtual) (Listof virtual) graph graph)
; TODO : one pass over the edges for optimization
(define (resolve-virtual virtual-in virtual-out actual-graph)
  ; virtual-in resolve
  (define new-edge
    (if (empty? virtual-in)
        (graph-edge actual-graph)
        (for*/fold
            ([acc '()])
            ([edg (graph-edge actual-graph)]
             [virt virtual-in])
          (if (and (string=? (g-edge-in edg) (g-virtual-virtual-agent virt))
                   (string=? (g-edge-port-in edg) (g-virtual-virtual-port virt)))
              (cons (struct-copy g-edge edg [in (g-virtual-agent virt)][port-in (g-virtual-agent-port virt)]) acc)
              (cons edg acc)))))
  ; virtual out-resolve
  (define res-edge
    (if (empty? virtual-out)
        new-edge
        (for*/fold
            ([acc '()])
            ([edg new-edge]
             [virt virtual-out])
          (if (and (string=? (g-edge-out edg) (g-virtual-virtual-agent virt))
                   (string=? (g-edge-port-out edg) (g-virtual-virtual-port virt)))
              (cons (struct-copy g-edge edg [out (g-virtual-agent virt)][port-out (g-virtual-agent-port virt)]) acc)
              (cons edg acc)))))
  ; Iip resolve
  (define res-iip
    (if (empty? virtual-in)
        (graph-iip actual-graph)
        (for*/fold
            ([acc '()])
            ([iip (graph-iip actual-graph)]
             [virt virtual-in])
          (if (and (string=? (g-iip-in iip) (g-virtual-virtual-agent virt))
                   (string=? (g-iip-port-in iip) (g-virtual-virtual-port virt)))
              (cons (struct-copy g-iip iip [in (g-virtual-agent virt)] [port-in (g-virtual-agent-port virt)]) acc)
              (cons iip acc)))))
  (struct-copy graph actual-graph [edge res-edge] [iip res-iip])
  )

; (- (Listof agent) graph String graph)
(define (flat-graph actual-graph input output)
  (define (rec-flat-graph not-visited virtual-in virtual-out actual-graph)
    (if (empty? not-visited)
        (resolve-virtual virtual-in virtual-out actual-graph)
        (let* ([next (car not-visited)]
               [next (begin (send (output "ask-path") next) (recv (input "ask-path")))]
               [is-subnet? (dynamic-require (g-agent-type next) 'g (lambda () #f))])
          (if is-subnet?
              ; It's a sub-graph. Get the new graph, add the nodes in not-visited, save the virtual port and save the rest of the graph
              (let* ([new-graph (get-graph next input output)]
                     ; Add the agents in the not-visited list
                     [new-not-visited (append (graph-agent new-graph) (cdr not-visited))]
                     ; add the virtual port
                     [new-virtual-in (append (graph-virtual-in new-graph) virtual-in)]
                     [new-virtual-out (append (graph-virtual-out new-graph) virtual-out)]
                     ; add the iips
                     [new-iip (append (graph-iip new-graph) (graph-iip actual-graph))]
                     ; add the edges
                     [new-edge (append (graph-edge new-graph) (graph-edge actual-graph))])
                (rec-flat-graph new-not-visited new-virtual-in new-virtual-out
                                (struct-copy graph actual-graph [iip new-iip][edge new-edge])))
              ; It's a normal agent, do nothing and go for the next
              (rec-flat-graph (cdr not-visited) virtual-in virtual-out (struct-copy graph actual-graph [agent (cons next (graph-agent actual-graph))]))))))
  (rec-flat-graph (graph-agent actual-graph) '() '() (struct-copy graph actual-graph [agent '()])))
<commit_msg>Problem : Wrong algorithm in load-graph<commit_after>#lang racket/base

(provide agt)

(require racket/list)
(require fractalide/modules/rkt/rkt-fbp/agent)
(require fractalide/modules/rkt/rkt-fbp/graph)

; (-> agent graph)
(define (get-graph agent input output)
  (send (output "ask-graph") agent)
  (recv (input "ask-graph")))

; (-> (Listof virtual) (Listof virtual) graph graph)
(define (resolve-virtual virtual-in virtual-out actual-graph)
  ; virtual-in resolve
  (define res-edge
    (for/list ([edg (graph-edge actual-graph)])
      (define new-edge (for/fold ([acc edg])
                                 ([virt virtual-in])
                         (if (and (string=? (g-edge-in edg) (g-virtual-virtual-agent virt))
                                  (string=? (g-edge-port-in edg) (g-virtual-virtual-port virt)))
                             (struct-copy g-edge acc [in (g-virtual-agent virt)][port-in (g-virtual-agent-port virt)])
                             acc)))
      (for/fold ([acc new-edge])
                ([virt virtual-out])
        (if (and (string=? (g-edge-out edg) (g-virtual-virtual-agent virt))
                 (string=? (g-edge-port-out edg) (g-virtual-virtual-port virt)))
            (struct-copy g-edge acc [out (g-virtual-agent virt)][port-out (g-virtual-agent-port virt)])
            acc))))
  ; Iip resolve
  (define res-iip
    (for/list ([iip (graph-iip actual-graph)])
      (for*/fold ([acc iip])
                 ([virt virtual-in])
        (if (and (string=? (g-iip-in iip) (g-virtual-virtual-agent virt))
                 (string=? (g-iip-port-in iip) (g-virtual-virtual-port virt)))
            (struct-copy g-iip acc [in (g-virtual-agent virt)] [port-in (g-virtual-agent-port virt)])
            acc))))
  (struct-copy graph actual-graph [edge res-edge] [iip res-iip]))

; (- (Listof agent) graph String graph)
(define (flat-graph actual-graph input output)
  (define (rec-flat-graph not-visited virtual-in virtual-out actual-graph)
    (if (empty? not-visited)
        (resolve-virtual virtual-in virtual-out actual-graph)
        (let* ([next (car not-visited)]
               [next (begin (send (output "ask-path") next) (recv (input "ask-path")))]
               [is-subnet? (dynamic-require (g-agent-type next) 'g (lambda () #f))])
          (if is-subnet?
              ; It's a sub-graph. Get the new graph, add the nodes in not-visited, save the virtual port and save the rest of the graph
              (let* ([new-graph (get-graph next input output)]
                     ; Add the agents in the not-visited list
                     [new-not-visited (append (graph-agent new-graph) (cdr not-visited))]
                     ; add the virtual port
                     [new-virtual-in (append (graph-virtual-in new-graph) virtual-in)]
                     [new-virtual-out (append (graph-virtual-out new-graph) virtual-out)]
                     ; add the iips
                     [new-iip (append (graph-iip new-graph) (graph-iip actual-graph))]
                     ; add the edges
                     [new-edge (append (graph-edge new-graph) (graph-edge actual-graph))])
                (rec-flat-graph new-not-visited new-virtual-in new-virtual-out
                                (struct-copy graph actual-graph [iip new-iip][edge new-edge])))
              ; It's a normal agent, do nothing and go for the next
              (rec-flat-graph (cdr not-visited) virtual-in virtual-out (struct-copy graph actual-graph [agent (cons next (graph-agent actual-graph))]))))))
  (rec-flat-graph (graph-agent actual-graph) '() '() (struct-copy graph actual-graph [agent '()])))

{'fraction_non_alphanumeric': 0.14706663127156888, 'fraction_numerical': 0.0, 'mean_word_length': 2.0667480667480667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2549658', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2335, 'n_words': 635}