A back arrow icon.
RDFox Blog
A back arrow icon.
RDFox Videos

How to calculate maths and functions in semantic reasoning for a Knowledge Graph

How to calculate maths and functions in semantic reasoning for a Knowledge Graph
Thomas Vout

Next episode: click here

Hello and welcome to another episode of the RDFox introductory series.

In this episode, we're going to be looking at calculating complex mathematical expressions with BINDs. If you haven't already, please do check out our previous videos where we cover the foundations of reasoning and the other functionality that Datalog has to offer.  

BINDs are incredibly versatile and powerful features because they enable us to encapsulate complex mathematical expressions and even use some of RDFox’s inbuilt functions.

This can be incredibly useful across the board but some examples include compatibility calculations, which can range from anything from article recommendations to machine part compatibility in configuration, to furniture, and whether 2 pieces fit together.  

So let's have a look at our example here. What will be looking at is calculating our win percentage for our drivers, which of course needs us to divide the total win count by the total race count. If you've been following along with our workshop series, you will have known that we've calculated these properties previously in other rules, so we can now make use of them here. This rule is also an exercise, so if you'd like to give this a go for yourself, please do pause the video and have a go. We’ll be sharing the answer in just a moment.  

The body for this rule is actually quite simple, we just need to describe the pattern that we're interested in, as we have done with any other rule, which in this case just includes the race count and the race wins for a given driver. Then we use our BIND function to bind the result of race win count divided by race count and we can save the output of this function as the new variable ‘percentage’. From there we can use these variables in the head of the rule to infer our new triple ‘driver hasWinPercentage percentage’.  

If you’ve seen the aggregate video where we covered counting races in rules, you would have known that we can gain some performance from query performance by encoding this logic in rules and absolutely the same applies to BINDs where we can precompute the results, all these calculations, in advance meaning that at query time we can see potentially orders of magnitude faster queries.  

Let's have a look at how we actually do this in RDFox. We can see here our completed rule where we describe the race count and race win count for each of our drivers, binding the win count divided by race count as percentage. So, let's import this rule. And from there we can verify this rule by querying for our newly created relationship ‘hasWinPercentage’.  

But I'm actually going to head back to the RDFox console to visualise these results and explain them as well.  

Here I'm looking at that same query, and if I run these results, we can see a list of ten of our drivers who have the highest win percentage. If I explore these results we can see them graphically but here I want to point out an additional feature of the console where we can highlight reasoning.  

By clicking this I can distinguish my reasoned facts from my explicit facts. So, if we look at Hamilton over here you can see we have two explicit facts which are his first and second name and a reasoned fact that has been added by our new rule shown in blue.  

Because this is an inferred fact, we can ask RDFox to explain it. So if we right click on this fact, we can choose ‘explain’ and RDFox will give us a proof tree of why this fact exist, giving us an auditable trail all the way back down to the explicit data. So here we can see our fact and we can see the two additional facts that were used to create it and of course the BIND calculation. We can also see the rule that was used to perform this BIND.  

For each of the additional facts, if we add them to our view, we can even see the rules that created them as well. In this case we're looking at the ‘hasRaceCount’ property where you will recognise our aggregate rule that we went through in a previous video. This enabled us to go right back to the grounded facts that really do exist in our data so that you have a complete audit trail, an explanation as to why all of your inferred facts really exist.  

If we like to see how we did these aggregates or any other function within Datalog or RDFox in general, please do check out the other videos in this series.

Take your first steps towards a solution.

Start with a free RDFox demo!

Take your first steps towards a solution.

Get started with RDFox for free!

Team and Resources

The team behind Oxford Semantic Technologies started working on RDFox in 2011 at the Computer Science Department of the University of Oxford with the conviction that flexible and high-performance reasoning was a possibility for data-intensive applications without jeopardising the correctness of the results. RDFox is the first market-ready knowledge graph designed from the ground up with reasoning in mind. Oxford Semantic Technologies is a spin-out of the University of Oxford and is backed by leading investors including Samsung Venture Investment Corporation (SVIC), Oxford Sciences Enterprises (OSE) and Oxford University Innovation (OUI).