Mercurial > code > home > repos > homeauto
annotate service/reasoning/inference.py @ 281:9728288c7f2f
refactor
Ignore-this: 61a7c93a71fba5feea956ef99c84a30e
author | drewp@bigasterisk.com |
---|---|
date | Fri, 06 May 2016 17:34:26 -0700 |
parents | 5ad229334a88 |
children | 95f72a22965d |
rev | line source |
---|---|
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
1 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
2 see ./reasoning for usage |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
3 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
4 |
179
0f2ac014d9ea
reasoning: -v verbose setting, more oneshot support, some custom stuff for mpd commands
drewp@bigasterisk.com
parents:
114
diff
changeset
|
5 import sys |
114
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
45
diff
changeset
|
6 try: |
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
45
diff
changeset
|
7 from rdflib.Graph import Graph |
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
45
diff
changeset
|
8 except ImportError: |
4cd065b97fa1
bugs in async http client. move trig helpers to rdflibtrig, which can work with rdflib 4
drewp@bigasterisk.com
parents:
45
diff
changeset
|
9 from rdflib import Graph |
20
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
10 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
11 sys.path.append("/my/proj/room/fuxi/build/lib.linux-x86_64-2.6") |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
12 from FuXi.Rete.Util import generateTokenSet |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
13 from FuXi.Rete import ReteNetwork |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
14 from rdflib import plugin |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
15 from rdflib.store import Store |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
16 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
17 def infer(graph, rules): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
18 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
19 returns new graph of inferred statements |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
20 """ |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
21 # based on fuxi/tools/rdfpipe.py |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
22 store = plugin.get('IOMemory',Store)() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
23 store.open('') |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
24 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
25 target = Graph() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
26 tokenSet = generateTokenSet(graph) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
27 network = ReteNetwork(rules, inferredTarget=target) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
28 network.feedFactsToAdd(tokenSet) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
29 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
30 store.rollback() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
31 return target |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
33 import time, logging |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
34 log = logging.getLogger() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
35 def logTime(func): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
36 def inner(*args, **kw): |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
37 t1 = time.time() |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
38 try: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
39 ret = func(*args, **kw) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
40 finally: |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
41 log.info("Call to %s took %.1f ms" % ( |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
42 func.__name__, 1000 * (time.time() - t1))) |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
43 return ret |
3f0dd03112b5
move reasoning from /my/proj/room, new integration with magma
drewp@bigasterisk.com
parents:
diff
changeset
|
44 return inner |