A very simple example to get familiar with the syntax #6
-
Hi and thank you for opening up the discussion section. I'm trying to learn pyreason, executed the hello world example successfully, and saw the csv files in the output folder. But I need to know the API, the syntax, by which a problem must be encoded. Thus decided to start with a very smaller example. Let's say we have just Mary and John, the only fact we know is that Mary owns a cat, and the only rule we know is that John will be friends with whoever owns a pet, i.e: Here is my graph file:
The first question is: Should I add another The second question is how to encode the only rule we have. Here is my rules file:
It's not clear how to define: if there is an |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @HRezaei, apologies for the late reply. For your first question:There are two ways of doing this:
For your second question:Your graphml file is not entirely optimal, based on your graphml, Solution walkthroughI'll walk you through how to make this example work:
import networkx as nx
g = nx.DiGraph()
# Two people with attribute `person = [1,1]`
g.add_node('Mary', person=1)
g.add_node('John', person=1)
# Create a node Cat with attribute pet=1 (in PyReason this attribute will act as a fact setting `pet = [1,1]`)
g.add_node('Cat', pet=1)
# Create an edge between Mary and Cat, with the edge attribute `owns = [1,1]`
g.add_edge('Mary', 'Cat', owns=1)
nx.write_graphml_lxml(g, 'graph.graphml', named_key_ids=True) This will create the desired graph, I'm attaching the graph file below.
# All Rules come under here
rule_1:
target: # You do not need a target label since you are adding a new edge and not modifying an existing label
target_criteria: # The Target node has to be a person
- [person, 1, 1]
delta_t: 1 # Delta t, time when this rule is applicable
neigh_criteria: # List of all neighbour criteria in the form [criteria on node/edge, variable, label, [lower_bound, upper_bound], [equal/greater/less/greater_equal/less_equal, number/[percent, total/available], value]]
- [node, [x1], pet, [1,1]] # you can omit [greater_equal, number, 1] because this is the default
- [edge, [target, x1], owns, [1,1]] # you can omit [greater_equal, number, 1] because this is the default
edges: [John, target, friends] # To create an edge or edges, specify the node or subset of nodes and the label to add/modify
ann_fn: [1,1] # Annotation function name or bound. See annotation_functions.py for list of available functions. The name of that function comes here
# Could be func_name or [l, u] Please take a look at the comments inside the file.
import pyreason as pr
pr.settings.verbose = True
pr.settings.atom_trace = True
pr.load_graph('graph.graphml')
pr.load_rules('rules.yaml')
interpretation = pr.reason()
pr.save_rule_trace(interpretation) You should be able to see the change in friends in the edge rule-trace. Graphml file generated in step 1:<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<key id="owns" for="edge" attr.name="owns" attr.type="long" />
<key id="pet" for="node" attr.name="pet" attr.type="long" />
<key id="person" for="node" attr.name="person" attr.type="long" />
<graph edgedefault="directed">
<node id="Mary">
<data key="person">1</data>
</node>
<node id="John">
<data key="person">1</data>
</node>
<node id="Cat">
<data key="pet">1</data>
</node>
<edge source="Mary" target="Cat">
<data key="owns">1</data>
</edge>
</graph>
</graphml> |
Beta Was this translation helpful? Give feedback.
Hi @HRezaei, apologies for the late reply.
For your first question:
There are two ways of doing this:
edge
option inrules.yaml
) if the conditions are satisfied. In this case it would create a node betweenMary
andJohn
with afriends
attribute set to[1,1]
. This is recommended since it will be simpler. I will show you how to make this specific case work below.friends
attribute from[0,0]
to[1,1]
. In which case, you would need to have a pre-defined edge betweenMary
andJohn
withfriends
set to[0,0]
in the graphml file.For your second question:
Your graphml file is not ent…