Fork me on GitHub
#asami
<
2021-02-24
>
rickmoynihan10:02:36

@quoll: So I’m toying with reasoning about RDF triples with naga, starting with rdfs ranges. I have the following pabu rule: rdf:type(Z,X) :- rdfs:range(A, X), A(Y, Z). However to make that inference work, I also need the axiomatic triples loaded into naga. Obviously the easiest thing to do is to just load all my data as axioms into naga, which works, however most (if not all) of my axioms will be defined in user supplied data. e.g. for the above range rule to be inferred I also need at least two more axiomatics triple e.g. [:skos/inScheme :rdfs/range :skos/ConceptScheme] (which is trivially provided by loading vocabularies as axioms), however another triple is also required as an axiom e.g. something like [:ref/female :skos/inScheme :scheme/genders]. This final class of axiomatic triples will more commonly be found in my user supplied data, so I need to have a way for any given graph of user data to find and load them into naga. I can see I have all the information to do this. Presumably I just need to look at the graph of user data, and filter it to the set of predicates that are grounded in the antecedents of my rules?

rickmoynihan10:02:35

One other thing… I can see when building the naga program that the axioms are defined defined as a lazy sequence, that then appears to get indexed by the storage layer. This makes sense as even though it’s forward chaining you presumably don’t want to do a linear search across axioms in rules that reference multiple axioms. My question is presumably in code like this:

(def program (rules/create-program (:rules rdfs-rules)
                                   (concat user-data
                                           (:axioms rdfs-rules))))

(defn apply-rules [db]
  (engine/run db program))

(-> index/empty-graph
    (graph/graph-transact 0 [user-data] nil)
    asami/as-connection
    apply-rules
    first
    :connection
    asami/db
    asami/graph
    (graph-q '[:find ?a ?v :where [:scheme/genders ?a ?v]])
    )
If I use engine/run I’ll presumably index those axioms twice. I’m guessing to avoid this I’ll want to reimplement engine/run to take the axioms from my graph of user-data?

rickmoynihan10:02:26

(the above is obviously scratch code btw)

quoll14:02:46

The idea of having “axioms” in a program is for statements that are necessary and the rest of the program won’t run without them, hence you want to ensure that they are present every time. But there is nothing particularly special about them, and they can be inserted whenever and wherever you like… so long as it’s before the program runs. Assuming that a used may want to insert data once was why I took the approach of loading the data separately in the CLI program: https://github.com/threatgrid/naga/blob/main/cli/src/naga/cli.clj#L76 I apologize that this code uses the Naga APIs for storage and not the transact function. That’s historical.

quoll14:02:57

You could try something like this instead:

(def program (rules/create-program (:rules rdfs-rules) []))

(-> index/empty-graph
    (graph/graph-transact 0 user-data nil)             ;; load user data
    (graph/graph-transact 0 (:axioms rdfs-rules) nil)  ;; load axioms explicitly
    asami/as-connection
    (engine/run program)                               ;; program does not contain axioms
    first
    :connection
    asami/db
    asami/graph
    (graph-q '[:find ?a ?v :where [:scheme/genders ?a ?v]])
    )

rickmoynihan23:02:39

@quoll: Thanks that’s essentially what I wrote first of all, but it doesn’t seem to work, it returns an empty seq:

quoll00:02:09

OK, so this is a bug. Either the connection is not returning the modified graph, or the as-connection is not properly wrapping the graph.

rickmoynihan09:02:42

Pretty sure the error is happening between lines 34 and 32. As inspecting the data returned by line 34 you can see the connection contains only the explicit triples and none of the inferred ones:

rickmoynihan09:02:23

Inspecting the data at line 32 looks like this:

rickmoynihan09:02:16

which yeah you’re right — I think it looks a bit garbled.

rickmoynihan09:02:11

ok looks like the issue might be with asami’s graph-transact?! Inspecting this :

(-> index/empty-graph
    (graph/graph-transact 0 [user-data] nil)
    )

rickmoynihan09:02:21

{:spo
 {[:ref/female :skos/inScheme :scheme/genders]
  {[:skos/inScheme :rdfs/range :skos/ConceptScheme] #{nil}}},
 :pos
 {[:skos/inScheme :rdfs/range :skos/ConceptScheme]
  {nil #{[:ref/female :skos/inScheme :scheme/genders]}}},
 :osp
 {nil
  {[:ref/female :skos/inScheme :scheme/genders]
   #{[:skos/inScheme :rdfs/range :skos/ConceptScheme]}}}}

rickmoynihan09:02:34

ok I think I just spotted the issue…

rickmoynihan09:02:36

Ok @quoll you’ll be relieved that this is a bug in my code, not in asami/naga 🙂

(def user-data [[:ref/female :skos/inScheme :scheme/genders]
                [:skos/inScheme :rdfs/range :skos/ConceptScheme]
                ])
,,,

    (graph/graph-transact 0 [user-data] nil)
that graph-transact line should be: (graph/graph-transact 0 user-data nil)

rickmoynihan09:02:37

With that correction, asami does indeed return the correct / expected results!! partywombat

quoll15:02:31

OK… but I should probably have some better data checking in there. Silently failing while building invalid data structures isn’t particularly user friendly!

rickmoynihan15:02:26

Perhaps… I did wonder if your plumatic schema stuff might have caught it, if I’d turned it on in dev with s/set-fn-validation! but I’ve just tried and it seems it still gets through

quoll15:02:08

There are diminishing returns with checking for everything, and the cost can be high

quoll15:02:48

My main reason for using plumatic schema has been documenting APIs. It really helps a lot! It’s also nice when it catches a bug 🙂

rickmoynihan15:02:59

agreed — but it can work well to do it in development/repl contexts

rickmoynihan16:02:18

For example I’ve just added

(comment
  (require '[schema.core :as s])
  (s/set-fn-validation! true))
At the end of my file, so when I hack on this next I can evaluate that and hopefully reduce any other mistakes like this.

quoll16:02:58

I was making shortcuts last night by NOT using schema in my code. You’re going to guilt me into putting it back in 🙂

rickmoynihan16:02:10

Well problems like this come with the territory of using a dynamic language. If I thought type systems were more important than everything else I’d be using Haskell or Idris day to day. You’ve clearly pulled in plumatic schema because you feel it benefits cases like this though, so it probably makes sense to keep leveraging it; but please don’t let me guilt you into anything. What you have already is great! 🙇

quoll16:02:43

99% of why I use Schema is to document what a function does. This helps me write and debug code, since I know what is supposed to go in and out. It also documents it for someone else who wants to look at it, but really, I do it for me 🙂

👍 3
quoll16:02:00

catching bugs is just a side effect

rickmoynihan16:02:27

Anyway now I’m over this hurdle I shall look forward to playing with naga in my vanishingly little spare time

quoll16:02:11

I have 3 children, so I can relate to this 🙂

rickmoynihan16:02:04

I have 2 under 2 😂

❤️ 3
rickmoynihan23:02:53

However moving the axioms from the user-data into the naga program does: