InferredAxiomLoader extends AbstractAxiomLoader { @Override List<Axiom<?>> find(AxiomDescriptor descriptor, Map<String, Assertion> assertions) { final List<Axiom<?>> result = new ArrayList<>(); final Resource subject = createResource(descriptor.getSubject().getIdentifier().toString()); for (Assertion a : assertions.values()) { final Property property = createProperty(a.getIdentifier().toString()); final Collection<Statement> statements = findStatements(subject, property, descriptor.getAssertionContext(a)); statements.forEach(s -> { final Optional<Value<?>> value = resolveValue(a, s.getObject()); value.ifPresent(v -> result.add(new AxiomImpl<>(descriptor.getSubject(), a, v))); }); } return result; } InferredAxiomLoader(InferredStorageConnector connector);   }
@Test void findLoadsValuesOfSpecifiedAssertions() { final Assertion aOne = Assertion.createClassAssertion(true); final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true); final Resource cls = createResource(Generator.generateUri().toString()); final Property property = createProperty(aTwo.getIdentifier().toString()); final Resource object = createResource(Generator.generateUri().toString()); when(connectorMock.findWithInference(SUBJECT_RES, RDF.type, null, null)) .thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, RDF.type, cls))); when(connectorMock.findWithInference(SUBJECT_RES, property, null, null)) .thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, property, object))); descriptor.addAssertion(aOne); descriptor.addAssertion(aTwo); final List<Axiom<?>> result = axiomLoader.find(descriptor, mapAssertions(descriptor)); assertEquals(2, result.size()); final Optional<Axiom<?>> clsAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny(); assertTrue(clsAxiom.isPresent()); assertEquals(NamedResource.create(cls.getURI()), clsAxiom.get().getValue().getValue()); final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny(); assertTrue(refAxiom.isPresent()); assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue()); verify(connectorMock).findWithInference(SUBJECT_RES, RDF.type, null, null); verify(connectorMock).findWithInference(SUBJECT_RES, property, null, null); }
@Test void findLoadsValuesOfAssertionsInContext() { final Assertion aOne = Assertion.createClassAssertion(true); final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true); final Resource cls = createResource(Generator.generateUri().toString()); final Property property = createProperty(aTwo.getIdentifier().toString()); final Resource object = createResource(Generator.generateUri().toString()); when(connectorMock.findWithInference(SUBJECT_RES, RDF.type, null, CONTEXT.toString())) .thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, RDF.type, cls))); when(connectorMock.findWithInference(SUBJECT_RES, property, null, CONTEXT.toString())) .thenReturn(Collections.singletonList(createStatement(SUBJECT_RES, property, object))); descriptor.addAssertion(aOne); descriptor.setAssertionContext(aOne, CONTEXT); descriptor.addAssertion(aTwo); descriptor.setSubjectContext(CONTEXT); final List<Axiom<?>> result = axiomLoader.find(descriptor, mapAssertions(descriptor)); assertEquals(2, result.size()); final Optional<Axiom<?>> clsAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny(); assertTrue(clsAxiom.isPresent()); assertEquals(NamedResource.create(cls.getURI()), clsAxiom.get().getValue().getValue()); final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny(); assertTrue(refAxiom.isPresent()); assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue()); verify(connectorMock).findWithInference(SUBJECT_RES, RDF.type, null, CONTEXT.toString()); verify(connectorMock).findWithInference(SUBJECT_RES, property, null, CONTEXT.toString()); }
@Test void findBySubjectLoadsAllPropertyAxiomsRelatedToSpecifiedSubject() { final Assertion aOne = Assertion.createDataPropertyAssertion(Generator.generateUri(), true); final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true); final Literal literal = createTypedLiteral(117); final Property propertyOne = createProperty(aOne.getIdentifier().toString()); final Property propertyTwo = createProperty(aTwo.getIdentifier().toString()); final Resource object = createResource(Generator.generateUri().toString()); when(connectorMock.findWithInference(SUBJECT_RES, null, null, null)).thenReturn(Arrays.asList( createStatement(SUBJECT_RES, propertyOne, literal), createStatement(SUBJECT_RES, propertyTwo, object) )); final Collection<Axiom<?>> result = axiomLoader.find(SUBJECT, null); assertEquals(2, result.size()); final Optional<Axiom<?>> dpAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny(); assertTrue(dpAxiom.isPresent()); assertEquals(117, dpAxiom.get().getValue().getValue()); final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny(); assertTrue(refAxiom.isPresent()); assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue()); verify(connectorMock).findWithInference(SUBJECT_RES, null, null, null); }
@Test void findBySubjectSkipsClassAssertions() { final Literal literal = createTypedLiteral(117); final Property propertyOne = createProperty(Generator.generateUri().toString()); final Resource object = createResource(Generator.generateUri().toString()); when(connectorMock.findWithInference(SUBJECT_RES, null, null, null)).thenReturn(Arrays.asList( createStatement(SUBJECT_RES, propertyOne, literal), createStatement(SUBJECT_RES, RDF.type, object) )); final Collection<Axiom<?>> result = axiomLoader.find(SUBJECT, null); assertEquals(1, result.size()); assertFalse(result.stream().anyMatch(a -> a.getAssertion().isClassAssertion())); }
@Test void findBySubjectWorksInContext() { final Assertion aOne = Assertion.createDataPropertyAssertion(Generator.generateUri(), true); final Assertion aTwo = Assertion.createObjectPropertyAssertion(Generator.generateUri(), true); final Literal literal = createTypedLiteral(117); final Property propertyOne = createProperty(aOne.getIdentifier().toString()); final Property propertyTwo = createProperty(aTwo.getIdentifier().toString()); final Resource object = createResource(Generator.generateUri().toString()); when(connectorMock.findWithInference(SUBJECT_RES, null, null, CONTEXT.toString())).thenReturn(Arrays.asList( createStatement(SUBJECT_RES, propertyOne, literal), createStatement(SUBJECT_RES, propertyTwo, object) )); final Collection<Axiom<?>> result = axiomLoader.find(SUBJECT, CONTEXT); assertEquals(2, result.size()); final Optional<Axiom<?>> dpAxiom = result.stream().filter(a -> a.getAssertion().equals(aOne)).findAny(); assertTrue(dpAxiom.isPresent()); assertEquals(117, dpAxiom.get().getValue().getValue()); final Optional<Axiom<?>> refAxiom = result.stream().filter(a -> a.getAssertion().equals(aTwo)).findAny(); assertTrue(refAxiom.isPresent()); assertEquals(NamedResource.create(object.getURI()), refAxiom.get().getValue().getValue()); verify(connectorMock).findWithInference(SUBJECT_RES, null, null, CONTEXT.toString()); }