Commit 463e1d78c066c8dde3cd2a0ebccca936d264a443

Thomas de Grivel 2024-11-01T13:56:23

fix env_facts_first_with

diff --git a/lib/kc3/0.1/facts.kc3 b/lib/kc3/0.1/facts.kc3
index 886d031..838c1b4 100644
--- a/lib/kc3/0.1/facts.kc3
+++ b/lib/kc3/0.1/facts.kc3
@@ -27,8 +27,10 @@ defmodule Facts do
   # env_db() -> facts
   def env_db = cfn Ptr "kc3_env_db" (Result)
 
-  # with_tags(facts, subject, predicate, object,
-  #           fn (fact) {result}) -> result
+  def first_with = cfn Tag "kc3_facts_first_with" (Facts, List, Fn, Result)
+
+  # first_with_tags(facts, subject, predicate, object,
+  #                 fn (fact) {result}) -> result
   def first_with_tags = cfn Tag "kc3_facts_first_with_tags" (Facts, Tag,
     Tag, Tag, Fn, Result)
 
diff --git a/libkc3/env.c b/libkc3/env.c
index 29e41e3..6fac5e9 100644
--- a/libkc3/env.c
+++ b/libkc3/env.c
@@ -2184,6 +2184,57 @@ s_tag * env_facts_collect_with_tags (s_env *env, s_facts *facts,
   return NULL;
 }
 
+s_tag * env_facts_first_with (s_env *env, s_facts *facts,
+                              s_list **spec,
+                              s_fn *callback, s_tag *dest)
+{
+  s_list *arguments;
+  s_facts_with_cursor cursor = {0};
+  const s_fact *fact = NULL;
+  s_fact_w *fact_w = NULL;
+  s_tag tmp = {0};
+  assert(env);
+  assert(facts);
+  assert(spec);
+  assert(callback);
+  assert(dest);
+  if (! (arguments = list_new_struct(&g_sym_FactW, NULL)))
+    return NULL;
+  if (! struct_allocate(&arguments->tag.data.struct_)) {
+    list_delete_all(arguments);
+    return NULL;
+  }
+  fact_w = arguments->tag.data.struct_.data;
+  if (! facts_with_list(facts, &cursor, *spec))
+    return NULL;
+  if (! facts_with_cursor_next(&cursor, &fact))
+    goto clean;
+  if (! fact)
+    goto ok;
+  if (! fact_w_init_fact(fact_w, fact))
+    goto clean;
+  if (! env_eval_call_fn_args(env, callback, arguments, &tmp)) {
+    goto clean;
+  }
+  fact_w_clean(fact_w);
+  fact_w_init(fact_w);
+ ok:
+  list_delete_all(arguments);
+  if (true) {
+    err_write_1("env_facts_first_with: ");
+    err_inspect_tag(&tmp);
+    err_write_1("\n");
+  }
+  *dest = tmp;
+  return dest;
+ clean:
+  facts_with_cursor_clean(&cursor);
+  tag_clean(&tmp);
+  fact_w_clean(fact_w);
+  list_delete_all(arguments);
+  return NULL;  
+}
+
 s_tag * env_facts_first_with_tags (s_env *env, s_facts *facts,
                                    s_tag *subject, s_tag *predicate,
                                    s_tag *object, s_fn *callback,
diff --git a/libkc3/env.h b/libkc3/env.h
index 8757ef3..1373b4b 100644
--- a/libkc3/env.h
+++ b/libkc3/env.h
@@ -67,6 +67,9 @@ s_tag *        env_facts_collect_with_tags (s_env *env, s_facts *facts,
                                             s_tag *object,
                                             s_fn *callback,
                                             s_tag *dest);
+s_tag *        env_facts_first_with (s_env *env, s_facts *facts,
+                                     s_list **spec, s_fn *callback,
+                                     s_tag *dest);
 s_tag *        env_facts_first_with_tags (s_env *env, s_facts *facts,
                                           s_tag *subject,
                                           s_tag *predicate,
diff --git a/libkc3/kc3.c b/libkc3/kc3.c
index 776c8f1..9e4415a 100644
--- a/libkc3/kc3.c
+++ b/libkc3/kc3.c
@@ -259,12 +259,18 @@ s_tag * kc3_facts_collect_with_tags (s_facts *facts,
                                      predicate, object, callback, dest);
 }
 
+s_tag * kc3_facts_first_with (s_facts *facts, s_list **spec,
+                              s_fn *callback, s_tag *dest)
+{
+  return env_facts_first_with(&g_kc3_env, facts, spec, callback, dest);
+}
+
 s_tag * kc3_facts_first_with_tags (s_facts *facts, s_tag *subject,
                                    s_tag *predicate, s_tag *object,
                                    s_fn *callback, s_tag *dest)
 {
-  return env_facts_with_tags(&g_kc3_env, facts, subject, predicate,
-                             object, callback, dest);
+  return env_facts_first_with_tags(&g_kc3_env, facts, subject,
+                                   predicate, object, callback, dest);
 }
 
 uw * kc3_facts_next_id (uw *dest)
diff --git a/libkc3/kc3_main.h b/libkc3/kc3_main.h
index a3269aa..c3c2972 100644
--- a/libkc3/kc3_main.h
+++ b/libkc3/kc3_main.h
@@ -83,6 +83,11 @@ s_tag *      kc3_facts_collect_with_tags (s_facts *facts,
                                           s_tag *object,
                                           s_fn *callback,
                                           s_tag *dest);
+s_tag *      kc3_facts_first_with (s_facts *facts, s_list **spec,
+                                   s_fn *callback, s_tag *dest);
+s_tag *      kc3_facts_first_with_tags (s_facts *facts, s_tag *subject,
+                                        s_tag *predicate, s_tag *object,
+                                        s_fn *callback, s_tag *dest);
 bool *       kc3_facts_remove_tags (s_facts *facts,
                                     const s_tag *subject,
                                     const s_tag *predicate,