How to add the inference rule to query?


(Martin) #1

For the tutorial:

https://www.stardog.com/blog/finding-fraud-with-stardog/

To add this rule, the tutorial says that " To define the :hasAffiliation relationship, we’ll add a couple of rules to the Knowledge Graph. To add the rules, you simply insert a triple with a reserved stardog:rule:content predicate. Stardog will detect the rule and apply it to any relevant query that has reasoning enabled."

IF {
    ?x :hasAddress ?a .
    ?y :hasAddress ?a .
    filter (?x != ?y)
}
THEN {
    ?x :hasAffiliation ?y .
}

IF {
    ?x :holds ?holding .
    ?holding :company ?c .
    ?holding :share ?share .
    filter (?share >= 50)
}
THEN {
    ?x :hasAffiliation ?c .
    ?c :hasAffiliation ?x .
}

I have this query typed in the Studio below. How to make the rule above to work for this query in the Studio? How to understand this "stardog:rule:content` predicate". Thanks,

# Fraud score as Money * (Paths Count)^2
select ?org ?name1 ?ben ?name2 ?score {
    {
        # Count distinct paths
        select ?org ?name1 ?ben ?name2 ?s (count(*) as ?c) {
            {
                # Group by intermediaries
                select ?org ?name1 ?ben ?name2 ?t1 ?t2 ?s {
                    ?org :lastName ?lname1 ;
                        :firstName ?fname1 ;
                        :hasAffiliation* ?t1 .
                    ?t1 :hasBankAccount ?a1 .
                    ?a1 :originated ?tx .
                    ?tx :beneficiary ?a2 .
                    ?t2 :hasBankAccount ?a2 ;
                        :hasAffiliation* ?ben .
                    ?ben :lastName ?lname2 ;
                        :firstName ?fname2 .
                    {
                        # Find highest sum of Tx for all paths
                        # between ?org and ?ben
                        select ?org ?ben (sum(?m) as ?s) {
                            ?org a :Person ;
                                :hasAffiliation* ?t1 .
                            ?t1 :hasBankAccount ?a1 .
                            ?a1 :originated ?tx .
                            ?tx :beneficiary ?a2 ;
                                :amount ?m .
                            ?t2 :hasBankAccount ?a2 ;
                                :hasAffiliation* ?ben .
                            ?ben a :Person .
                        }
                        group by ?org ?ben
                        order by desc(?s) ?org ?ben
                        limit 1000
                    }
                    bind(concat(?fname1, ' - ', ?lname1) as ?name1)
                    bind(concat(?fname2, ' - ', ?lname2) as ?name2)
                }
                group by ?org ?name1 ?ben ?name2 ?t1 ?t2 ?s
            }
        }
        group by ?org ?ben ?name1 ?name2 ?s
        order by desc(?s) ?org ?ben
    }
    # Calculate score as total money transacted times
    # the square of unique path count between actors
    bind(?s * ?c * ?c as ?score)
}
order by desc(?score) ?org ?ben

(Evren Sirin) #2

This is explained in the docs but actually you don't need to worry about this predicate. Here is the easiest method:

  1. Copy/paste the methods into an empty editor in Studio
  2. Click the button on the lower left corner that says SPARQL and choose Turtle
  3. Choose the database on the toolbar
  4. Click the Add button

Syntax highlighting in Studio will look as if there is a syntax error but the rules will be loaded and saved as triples that use the special rule predicate.

Best,
Evren


(Martin) #3

Hi, Evern: I did what you instructed and then re-run the code, but the result is very different from the tutorial. As you can see, the top records don't show a fraud ring is detected.

I copied both the inference rule and the query into the editor. Other queries in the tutorial seem to be the same as in the tutorial. But the last one for the inference example doesn't.

So in my Studio workspace, I have to files, which holds the main query and the other holds the inference rule. And as instructed, I stored it as a turtle format, and clicked the 'Add' button, and the Studio shows that the turtles are added to the database successfully. But why doesn't the query result show the same as in the tutorial?


(Evren Sirin) #4

You need to enable reasoning (see the toggle button on the toolbar) for the rules to take effect.

Best,
Evren


(Martin) #5

Evren, After I enabled the reasoning button, it takes about 1 minute to run the query, and at the end, it displays a red message "something went wrong", without any clue.

Could you re-run the example on your local computer to see whether the result can be re-produced? Thanks.