Created
March 30, 2019 14:36
-
-
Save mwatts15/4d88968771d4a40266d7f0f118b19c8e to your computer and use it in GitHub Desktop.
Partial answer to openworm/owmeta#254
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/PyOpenWorm/context_store.py b/PyOpenWorm/context_store.py | |
index c278af9..c753eed 100644 | |
--- a/PyOpenWorm/context_store.py | |
+++ b/PyOpenWorm/context_store.py | |
@@ -151,7 +151,15 @@ class RDFContextStore(Store): | |
self.__init_contexts() | |
for t in self.__store.triples(pattern, context): | |
contexts = set(getattr(c, 'identifier', c) for c in t[1]) | |
- inter = self.__context_transitive_imports & contexts | |
+ if self.__context_transitive_imports: | |
+ inter = self.__context_transitive_imports & contexts | |
+ else: | |
+ # Note that our own identifier is also included in the | |
+ # transitive imports, so if we don't have *any* imports then we | |
+ # fall back to querying across all contexts => we don't filter | |
+ # based on contexts. This is in line with rdflib ConjuctiveGraph | |
+ # semantics | |
+ inter = contexts | |
if inter: | |
yield t[0], inter | |
diff --git a/PyOpenWorm/evidence.py b/PyOpenWorm/evidence.py | |
index 5a5d97c..3572f58 100644 | |
--- a/PyOpenWorm/evidence.py | |
+++ b/PyOpenWorm/evidence.py | |
@@ -7,7 +7,6 @@ from PyOpenWorm.context import Context | |
logger = logging.getLogger(__name__) | |
- | |
class EvidenceError(Exception): | |
pass | |
@@ -87,23 +86,43 @@ class Evidence(DataObject): | |
return self.make_identifier(s) | |
-def evidence_for(qctx, conn): | |
+def evidence_for(qctx, ctx, evctx=None): | |
""" | |
Returns an iterable of Evidence | |
+ | |
+ Parameters | |
+ ---------- | |
+ qctx : object | |
+ an object supported by evidence. If the object is a | |
+ :class:`~PyOpenWorm.context.Context` with no identifier, then the query | |
+ considers statements 'staged' (rather than stored) in the context | |
+ ctx : Context | |
+ Context that bounds where we look for statements about `qctx`. The | |
+ contexts for statements found in this context are the actual targets of | |
+ Evidence.supports statements. | |
+ evctx : Context | |
+ if the Evidence.supports statements should be looked for somewhere other | |
+ than `ctx`, that can be specified in evctx. optional | |
""" | |
- ctxs = query_context(conn.conf['rdf.graph'], qctx) | |
+ if not evctx: | |
+ evctx = ctx | |
+ ctxs = query_context(ctx.rdf_graph(), qctx) | |
ev_objs = [] | |
for c in ctxs: | |
- mqctx = Context(conf=conn.conf) | |
- print('CONTEXT', c.identifier) | |
- ev = mqctx.stored(Evidence)() | |
- ev.supports(Context(ident=c.identifier, conf=conn.conf).rdf_object) | |
+ ev = evctx(Evidence)() | |
+ ev.supports(Context(ident=c.identifier).rdf_object) | |
for x in ev.load(): | |
ev_objs.append(x) | |
return ev_objs | |
def query_context(graph, qctx): | |
+ ''' | |
+ graph : rdflib.graph.Graph | |
+ Graph where we can find the contexts for statements in `qctx` | |
+ qctx : PyOpenWorm.context.Context | |
+ Container for statements | |
+ ''' | |
trips = qctx.contents_triples() | |
lctx = None | |
for t in trips: | |
diff --git a/tests/DataTestTemplate.py b/tests/DataTestTemplate.py | |
index 7235d0a..3c64d89 100644 | |
--- a/tests/DataTestTemplate.py | |
+++ b/tests/DataTestTemplate.py | |
@@ -56,3 +56,4 @@ class _DataTest(unittest.TestCase): | |
@property | |
def config(self): | |
return self.TestConfig | |
+ conf = config | |
diff --git a/tests/EvidenceForTest.py b/tests/EvidenceForTest.py | |
index 693083a..02c5267 100644 | |
--- a/tests/EvidenceForTest.py | |
+++ b/tests/EvidenceForTest.py | |
@@ -8,26 +8,92 @@ from .TestUtilities import xfail_without_db | |
import PyOpenWorm | |
from PyOpenWorm.context import Context | |
from PyOpenWorm.neuron import Neuron | |
-from PyOpenWorm.worm import Worm | |
from PyOpenWorm.evidence import Evidence | |
from PyOpenWorm.evidence import evidence_for | |
from PyOpenWorm import connect, disconnect | |
from .DataTestTemplate import _DataTest | |
+ | |
class EvidenceForTest(_DataTest): | |
''' Tests for statements having an associated Evidence object ''' | |
+ | |
def setUp(self): | |
- xfail_without_db() | |
- self.conn = PyOpenWorm.connect('readme.conf') | |
- self.g = self.conn.conf["rdf.graph"] | |
- self.context = Context() | |
- self.qctx = self.context.stored | |
+ # Make the statements and evidence we will query for in the test | |
+ super(EvidenceForTest, self).setUp() | |
+ c1 = Context(ident='http://example.org/statements', conf=self.conf) | |
+ c1(Neuron)('AVAL').innexin('UNC-7') | |
- def tearDown(self): | |
- PyOpenWorm.disconnect(self.conn) | |
+ evc = Context(ident='http://example.org/metadata', conf=self.conf) | |
+ ev1 = evc(Evidence)(key='js2019') | |
+ ev1.supports(c1.rdf_object) | |
- def test_evidence_for(self): | |
- c1 = Context() | |
- c1(Neuron)('AVAL').innexin('UNC-7') | |
- ev_iterable = evidence_for(c1, self.conn) | |
- self.assertTrue((len(ev_iterable) != 0)) | |
+ # Save them | |
+ c1.save_context() | |
+ evc.save_context() | |
+ | |
+ def test_retrieve(self): | |
+ # Make the context that holds the statements. The identifier and whether | |
+ # it's connected to a database doesn't matter here: it's just a | |
+ # container for statements | |
+ qctx = Context() | |
+ qctx(Neuron)('AVAL').innexin('UNC-7') | |
+ | |
+ # Make the context we query statements from. This could be a 'staged' | |
+ # context, but in this case we use what we've written to the IOMemory | |
+ # store provided by _DataTest in self.conf['rdf.graph'] | |
+ ctx = Context(conf=self.conf).stored | |
+ | |
+ # Actually do the query | |
+ ev_iterable = evidence_for(qctx, ctx) | |
+ | |
+ self.assertEqual(len(ev_iterable), 1) | |
+ | |
+ def test_statements_with_no_evidence(self): | |
+ # Make the context that holds the statements. | |
+ # These statements were not made in the setUp | |
+ qctx = Context() | |
+ qctx(Neuron)('AVAR').innexin('UNC-7') | |
+ | |
+ # Make the context we query statements from. This could be a 'staged' | |
+ # context, but in this case we use what we've written to the IOMemory | |
+ # store provided by _DataTest in self.conf['rdf.graph'] | |
+ ctx = Context(conf=self.conf).stored | |
+ | |
+ # Actually do the query | |
+ ev_iterable = evidence_for(qctx, ctx) | |
+ | |
+ self.assertEqual(len(ev_iterable), 0) | |
+ | |
+ def test_distinct_evidence_context(self): | |
+ # Make the context that holds the statements. | |
+ qctx = Context() | |
+ qctx(Neuron)('AVAL').innexin('UNC-7') | |
+ | |
+ # Make the context we query statements from. This could be a 'staged' | |
+ # context, but in this case we use what we've written to the IOMemory | |
+ # store provided by _DataTest in self.conf['rdf.graph'] | |
+ ctx = Context(ident='http://example.org/statements', conf=self.conf).stored | |
+ # Make the context that we query Evidence from | |
+ evctx = Context(ident='http://example.org/metadata', conf=self.conf).stored | |
+ | |
+ # Actually do the query | |
+ ev_iterable = evidence_for(qctx, ctx, evctx) | |
+ | |
+ self.assertEqual(len(ev_iterable), 1) | |
+ | |
+ def test_statements_but_no_evidence(self): | |
+ # Make the context that holds the statements. | |
+ qctx = Context() | |
+ qctx(Neuron)('AVAL').innexin('UNC-7') | |
+ | |
+ # Make the context we query statements from. This could be a 'staged' | |
+ # context, but in this case we use what we've written to the IOMemory | |
+ # store provided by _DataTest in self.conf['rdf.graph'] | |
+ ctx = Context(ident='http://example.org/statements', conf=self.conf).stored | |
+ evctx = Context(ident='http://example.org/somerandomcontext', conf=self.conf).stored | |
+ | |
+ # Actually do the query | |
+ ev_iterable = evidence_for(qctx, ctx, evctx) | |
+ | |
+ # Verify that there is at least one evidence object returned | |
+ self.assertEqual(len(ev_iterable), 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment