Skip to content

Instantly share code, notes, and snippets.

@pudo
Created April 7, 2020 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pudo/3ad528beb3b44c97b82f540d99227994 to your computer and use it in GitHub Desktop.
Save pudo/3ad528beb3b44c97b82f540d99227994 to your computer and use it in GitHub Desktop.
from followthemoney import model
from followthemoney.types import registry
from followthemoney.graph import Graph, Node
# Some inspiration:
#
# https://github.com/nchah/freebase-mql#mql-and-graphql
# https://rdflib.readthedocs.io/en/stable/intro_to_graphs.html#basic-triple-matching
# Queries:
# * Node inbound degree query
# * Entity inbound (degree) query
query = {
'subject': 'NODE_ID',
'property': 'QNAME',
'object': 'NODE_ID',
'degree': 'NONE to request DEGREE',
'limit': 'NUMBER TO REQUEST RESULTS',
}
class GraphQuery(object):
def __init__(self, graph, authz=None):
self.graph = graph
self.authz = authz
self.clauses = []
def add(self, node=None, prop=None, value=None, limit=0, count=None):
clause = QueryClause(self, node, prop, value, limit, count)
self.clauses.append(clause)
return clause
def execute(self):
pass
class QueryClause(object):
def __init__(self, query, node=None, prop=None, value=None,
limit=0, count=None):
self.query = query
self.node = node
self.prop = prop
self.value = value
self.limit = limit
self.count = count
@property
def schemata(self):
"""The schema (and hence indexes) affected by this query."""
pass
def demo():
proxy = model.get_proxy({
'id': 'banana',
'schema': 'Person',
'properties': {
'name': ['John Doe'],
'phone': ['+4923239271777']
}
})
# UC 1: Tags query
query = GraphQuery(Graph())
for prop, value in proxy.itervalues():
if not prop.type.matchable:
continue
node = Node(prop.type, value)
query.add(node=node, count=True)
# UC 2: References query
query = GraphQuery(Graph())
for prop in proxy.schema.properties:
if not prop.stub:
continue
node = Node(registry.entity, proxy.id, proxy=proxy)
query.add(prop=prop.reverse, value=node, count=True)
# UC 3: Expand query
graph = Graph()
query = GraphQuery(graph)
for prop in proxy.schema.properties:
if not prop.stub:
continue
node = Node(registry.entity, proxy.id, proxy=proxy)
query.add(prop=prop.reverse, value=node, limit=200, count=True)
if __name__ == '__main__':
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment