annotate service/reasoning/rdflibtrig.py @ 934:3bb18b7d21df

reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now Ignore-this: 3ab80fee5836817dcd54ce6678a6089d darcs-hash:20131009044224-312f9-b47b461e8872e4ef5c4951703ab8f13d5b848ea3
author drewp <drewp@bigasterisk.com>
date Tue, 08 Oct 2013 21:42:24 -0700
parents 4ae49c6adecb
children 5ad229334a88
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
1 import re, time
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
2 import restkit
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
3 from rdflib import URIRef
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
4 try:
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
5 from rdflib import StringInputSource
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
6 from rdflib.Graph import Graph
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
7 except ImportError:
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
8 from rdflib.parser import StringInputSource
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
9 from rdflib import Graph
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
10
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
11 def parseTrig(trig):
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
12 """
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
13 yields quads
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
14 """
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
15 m = re.match(r"<([^>]+)> \{(.*)\}\s*$", trig, re.DOTALL)
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
16 if m is None:
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
17 raise NotImplementedError("trig format was too tricky: %r..." % trig[:200])
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
18
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
19 ctx = URIRef(m.group(1))
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
20 n3 = m.group(2)
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
21 g = Graph()
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
22 g.parse(StringInputSource(n3), format="n3")
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
23 for stmt in g:
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
24 yield stmt + (ctx,)
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
25
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
26
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
27 def addTrig(graph, url, timeout=2):
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
28 t1 = time.time()
934
3bb18b7d21df reasoning actions: generalize them a bit but then add a bunch of special cases for mpd for now
drewp <drewp@bigasterisk.com>
parents: 923
diff changeset
29 response = restkit.request(url, timeout=timeout)
923
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 919
diff changeset
30 if response.status_int != 200:
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 919
diff changeset
31 raise ValueError("status %s from %s" % (response.status, url))
4ae49c6adecb errors and logging in reasoning
drewp <drewp@bigasterisk.com>
parents: 919
diff changeset
32 trig = response.body_string()
919
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
33 fetchTime = time.time() - t1
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
34 graph.addN(parseTrig(trig))
6ee2a90fc816 bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp <drewp@bigasterisk.com>
parents:
diff changeset
35 return fetchTime