Logseq freezes when I use rules in a query

Hi,
I want to use rules in queries. But whenever I add a rule, logseq starts to freeze. In principle, I want to do the following:

#+BEGIN_QUERY
  {
   :title [:b "Habit total"]

   :query
   [
    :find (count ?datename) ?datename
    :in $ ?today %
    :where
    [?p :page/journal? true]
    [?b :block/page ?p]

    [?p :block/original-name ?datename]

    [?p :block/journal-day ?date]
    [(+ 2 ?date) ?next_date]
    (consecutive-dates ?next_date ?today)

    [?b :block/properties ?props]
    (property ?b :type "tracker")
    (property ?b :test true)
   ]
   :inputs [:today [[(consecutive-dates ?d1 ?d2) [(== ?d1 ?d2)]]]]
  }
#+END_QUERY

This query freezes logseq. However, when I use the following query which should calculate exactly the same, logseq runs the query and creates an output.


#+BEGIN_QUERY
  {
   :title [:b "Habit total"]

   :query
   [
    :find (count ?datename) ?datename
    :in $ ?today %
    :where
    [?p :page/journal? true]
    [?b :block/page ?p]

    [?p :block/original-name ?datename]

    [?p :block/journal-day ?date]
    [(+ 2 ?date) ?next_date]
    [(== ?next_date ?today)]

    [?b :block/properties ?props]
    (property ?b :type "tracker")
    (property ?b :test true)
   ]
   :inputs [:today [[(consecutive-dates ?d1 ?d2) [(== ?d1 ?d2)]]]]
  }
#+END_QUERY

In the long run, I want to extend the rule to count the days of a current habit streak. Therefore, I want to use a similar approach as is used here in example 3 for the sequels of movies.

PS: could someone tell me, how I can better debug query inputs in logseq?

Hi.

  • Both forms of the query work for me.
    • Though I don’t have your data.
  • Have you tried in a different graph?
    • A new/empty graph would be a good initial test.
  • The logic in the query looks a little strange to me.
    • I don’t think that this is the issue.
  • Have you opened the console (before running and freezing) and seen for any clues there?
    • To open the console, Ctrl + Shift + i

Hi,
I tested both queries in a new graph and there they worked. In the new graph I also see errors in the console when opening it with Ctrl + Shift + i, but in my real graph the console does not generate output to debug. It takes some time and that logseq freezes while the console says DevTools was disconnected from the page....

But I have more questions about using rules, especially with the :rules statement. One of the following queries should in principle work. I do not know how the order of the :inputs and the :rules statements needs to be reflected in the order of the % in the :in line. The rule rule1 is not doing anything useful here, it is just a dummy rule.

#+BEGIN_QUERY
{:title "Count number of blocks in the current page"
 :query [:find (count ?b)
         :in $ % ?current-page
         :where
         [?p :block/name ?current-page]
         [?b :block/page ?p]]
 :inputs [:current-page]}
 :rules [[(rule1 ?r) [(== ?r "test")]]]
#+END_QUERY

#+BEGIN_QUERY
{:title "Count number of blocks in the current page"
 :query [:find (count ?b)
         :in $ % ?current-page
         :where
         [?p :block/name ?current-page]
         [?b :block/page ?p]]
 :rules [[(rule1 ?r) [(== ?r "test")]]]
 :inputs [:current-page]}
#+END_QUERY

#+BEGIN_QUERY
{:title "Count number of blocks in the current page"
 :query [:find (count ?b)
         :in $ ?current-page %
         :where
         [?p :block/name ?current-page]
         [?b :block/page ?p]]
 :inputs [:current-page]}
 :rules [[(rule1 ?r) [(== ?r "test")]]]
#+END_QUERY

The first query produces a Too few inputs passed... in datascript, the second query does not produce a datascript error. The earliest error I can trace back here is importing is not ISeqable from Javascript. The last query produces again a Too few inputs passed ... error. The error logs are very long, so I dont share them here. Also they are very hard to understand for me due to no clear indentation, etc…

When I supply the rule via the :inputs statement, logseq is happy and produces the correct output:

#+BEGIN_QUERY
{:title "Count number of blocks in the current page"
 :query [:find (count ?b)
         :in $ ?current-page %
         :where
         [?p :block/name ?current-page]
         [?b :block/page ?p]]
 :inputs [:current-page [[(rule1 ?r) [(== ?r "test")]]]]}
#+END_QUERY

I can work with supplying rules via the inputs, but I would like to now, why I can not supply them via the :rules statement…

Best,
Niklas

Avoid posting duplicates.

Concerning the freezing, since the empty graph doesn’t freeze, cannot help without having the data of the specific graph. To that effect, try gradually populating the empty graph with any data that may be suspicious. The goal is to identify the type of data that causes the freezing.

Concerning the three queries:

  • in the first query: the % inside :in should be placed last
  • in the second query: the % inside :in should be placed last
  • in the third query: the :rules entry should be placed inside the curly braces {}

Thank you so much for your help. I think I misplaced the ending curly brace } in most of them. Furthermore it seems as if the % in the :in goes always last. Is this correct?

This was intentional, as I wanted to ask specifically about the logical correctness and computational costs of my query in this second post, while in this post here, I only wanted to understand how to insert rules and why they lead to freezing.

I did adapt the first queries to include the correct syntax for using :rules:

#+BEGIN_QUERY
{
   :title [:b "Habit total"]

   :query
   [
    :find (count ?datename) ?datename
    :in $ ?today %
    :where
    [?p :page/journal? true]
    [?b :block/page ?p]

    [?p :block/original-name ?datename]

    [?p :block/journal-day ?date]
    [(+ 2 ?date) ?next_date]
    [(== ?next_date ?today)] ;; do not use the rule

    [?b :block/properties ?props]
    (property ?b :type "tracker")
    (property ?b :test true)
   ]
   :rules [[(consecutive-dates ?d1 ?d2) [(== ?d1 ?d2)]]]  ;; first rules
   :inputs [:today]
  }
#+END_QUERY
#+BEGIN_QUERY
{
   :title [:b "Habit total"]

   :query
   [
    :find (count ?datename) ?datename
    :in $ ?today %
    :where
    [?p :page/journal? true]
    [?b :block/page ?p]

    [?p :block/original-name ?datename]

    [?p :block/journal-day ?date]
    [(+ 2 ?date) ?next_date]
    [(== ?next_date ?today)] ;; do not use the rule

    [?b :block/properties ?props]
    (property ?b :type "tracker")
    (property ?b :test true)
   ]
   :inputs [:today]  ;; first inputs
   :rules [[(consecutive-dates ?d1 ?d2) [(== ?d1 ?d2)]]]
  }
#+END_QUERY

#+BEGIN_QUERY
{
   :title [:b "Habit total"]

   :query
   [
    :find (count ?datename) ?datename
    :in $ ?today %
    :where
    [?p :page/journal? true]
    [?b :block/page ?p]

    [?p :block/original-name ?datename]

    [?p :block/journal-day ?date]
    [(+ 2 ?date) ?next_date]
    (consecutive-dates ?next_date ?today) ;; use the rule

    [?b :block/properties ?props]
    (property ?b :type "tracker")
    (property ?b :test true)
   ]
   :inputs [:today]
   :rules [[(consecutive-dates ?d1 ?d2) [(== ?d1 ?d2)]]]
  }
#+END_QUERY

These three queries give the following error in a completely empty graph (todays Journal page and a second page where I executed the queries).

Error: No protocol method ICollection.-conj defined for type object: :today

Running the next query which is without the rules, I do not get any errors.

#+BEGIN_QUERY
{
   :title [:b "Habit total"]

   :query
   [
    :find (count ?datename) ?datename
    :in $ ?today
    :where
    [?p :page/journal? true]
    [?b :block/page ?p]

    [?p :block/original-name ?datename]

    [?p :block/journal-day ?date]
    [(+ 2 ?date) ?next_date]
    [(== ?next_date ?today)]

    [?b :block/properties ?props]
    (property ?b :type "tracker")
    (property ?b :test true)
   ]
   :inputs [:today]
  }
#+END_QUERY

It seems as if :today is not available when using :rules.

When using :rules, yes. When using :inputs, it can go wherever the rules vector is placed.

:today works for me both with and without :rules and I don’t get any Error with any of your adapted queries. Can you check if your installation of Logseq is up-to-date? Is there anything special in your environment? Any strange plugin or other customization?