Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 67 more general query methods #71

Merged
merged 6 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import uk.gov.gchq.magmacore.database.query.QueryResultList;
import uk.gov.gchq.magmacore.hqdm.model.Thing;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.HqdmIri;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IRI;
import uk.gov.gchq.magmacore.service.transformation.DbCreateOperation;
import uk.gov.gchq.magmacore.service.transformation.DbDeleteOperation;
Expand Down Expand Up @@ -112,16 +111,16 @@ public interface MagmaCoreDatabase {
* @param predicateIri IRI of the HQDM relationship type being queried.
* @return The {@link Thing}(s) found.
*/
List<Thing> findByPredicateIriOnly(HqdmIri predicateIri);
List<Thing> findByPredicateIriOnly(IRI predicateIri);

/**
* Find object(s) that have a specific case-sensitive string-value attribute associated with them.
* Find object(s) that have a specific value attribute associated with them.
*
* @param predicateIri IRI of the predicate being queried.
* @param value Case-sensitive string to match.
* @param value Object to match.
* @return The {@link Thing}(s) found.
*/
List<Thing> findByPredicateIriAndStringValue(IRI predicateIri, String value);
List<Thing> findByPredicateIriAndValue(IRI predicateIri, Object value);

/**
* Find object(s) that have a specific string-value attribute associated with them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@
import uk.gov.gchq.magmacore.database.query.QueryResultList;
import uk.gov.gchq.magmacore.hqdm.model.Thing;
import uk.gov.gchq.magmacore.hqdm.rdf.HqdmObjectFactory;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.HqdmIri;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IRI;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IriBase;
import uk.gov.gchq.magmacore.hqdm.rdf.util.Pair;
import uk.gov.gchq.magmacore.service.transformation.DbCreateOperation;
import uk.gov.gchq.magmacore.service.transformation.DbDeleteOperation;

/**
* Apache Jena triplestore to store HQDM objects as RDF triples either as an in-memory Jena dataset
* or persistent TDB triplestore.
* Apache Jena triplestore to store HQDM objects as RDF triples either as an
* in-memory Jena dataset or persistent TDB triplestore.
*/
public class MagmaCoreJenaDatabase implements MagmaCoreDatabase {

Expand Down Expand Up @@ -101,8 +100,8 @@ public final Dataset getDataset() {
}

/**
* Register a new prefix/namespace mapping which will be used to shorten the print strings for
* resources in known namespaces.
* Register a new prefix/namespace mapping which will be used to shorten the
* print strings for resources in known namespaces.
*
* @param base {@link IriBase} to register.
*/
Expand Down Expand Up @@ -270,7 +269,7 @@ public List<Thing> findByPredicateIri(final IRI predicateIri, final IRI objectIr
* {@inheritDoc}
*/
@Override
public List<Thing> findByPredicateIriOnly(final HqdmIri predicateIri) {
public List<Thing> findByPredicateIriOnly(final IRI predicateIri) {
final String query = "SELECT ?s ?p ?o WHERE {{select ?s ?p ?o where { ?s ?p ?o.}}{select ?s where {?s <"
+ predicateIri.toString() + "> ?o.}}}";
final QueryResultList list = executeQuery(query);
Expand All @@ -281,9 +280,16 @@ public List<Thing> findByPredicateIriOnly(final HqdmIri predicateIri) {
* {@inheritDoc}
*/
@Override
public List<Thing> findByPredicateIriAndStringValue(final IRI predicateIri, final String value) {
final String query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s <" + predicateIri.toString() + "> \"\"\"" + value
+ "\"\"\".}";
public List<Thing> findByPredicateIriAndValue(final IRI predicateIri, final Object value) {
final String query;

if (value instanceof IRI) {
query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s <" + predicateIri.toString() + "> <" + value
+ ">.}";
} else {
query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s <" + predicateIri.toString() + "> \"\"\"" + value
+ "\"\"\".}";
}
final QueryResultList list = executeQuery(query);
return toTopObjects(list);
}
Expand Down Expand Up @@ -340,7 +346,8 @@ public QueryResultList executeQuery(final String sparqlQueryString) {
}

/**
* Execute a SPARQL query and construct a list of HQDM objects from the resulting RDF triples.
* Execute a SPARQL query and construct a list of HQDM objects from the
* resulting RDF triples.
*
* @param queryExec SPARQL query to execute.
* @return Results of the query.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import uk.gov.gchq.magmacore.database.query.QueryResultList;
import uk.gov.gchq.magmacore.hqdm.model.Thing;
import uk.gov.gchq.magmacore.hqdm.rdf.HqdmObjectFactory;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.HqdmIri;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IRI;
import uk.gov.gchq.magmacore.hqdm.rdf.util.Pair;
import uk.gov.gchq.magmacore.service.transformation.DbCreateOperation;
Expand Down Expand Up @@ -252,7 +251,7 @@ public List<Thing> findByPredicateIri(final IRI predicateIri, final IRI objectIr
* {@inheritDoc}
*/
@Override
public List<Thing> findByPredicateIriOnly(final HqdmIri predicateIri) {
public List<Thing> findByPredicateIriOnly(final IRI predicateIri) {
final String query = "SELECT ?s ?p ?o WHERE {{select ?s ?p ?o where { ?s ?p ?o.}}{select ?s where {?s <"
+ predicateIri.toString() + "> ?o.}}}";
final QueryResultList list = executeQuery(query);
Expand All @@ -263,9 +262,16 @@ public List<Thing> findByPredicateIriOnly(final HqdmIri predicateIri) {
* {@inheritDoc}
*/
@Override
public List<Thing> findByPredicateIriAndStringValue(final IRI predicateIri, final String value) {
final String query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s <" + predicateIri.toString() + "> \"\"\"" + value
+ "\"\"\".}";
public List<Thing> findByPredicateIriAndValue(final IRI predicateIri, final Object value) {
final String query;

if (value instanceof IRI) {
query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s <" + predicateIri.toString() + "> <" + value
+ ">.}";
} else {
query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s <" + predicateIri.toString() + "> \"\"\"" + value
+ "\"\"\".}";
}
final QueryResultList list = executeQuery(query);
return toTopObjects(list);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ private static SignPatternDto toSignPatternDto(final QueryResult qr) {
* @throws RuntimeException If no or multiple results were found.
*/
public <T extends Thing> T findByEntityName(final String entityName) {
final List<Thing> searchResult = database.findByPredicateIriAndStringValue(HQDM.ENTITY_NAME, entityName);
final List<Thing> searchResult = findByPredicateIriAndValue(HQDM.ENTITY_NAME, entityName);

if (searchResult.size() == 1) {
return (T) searchResult.get(0);
Expand All @@ -431,6 +431,29 @@ public <T extends Thing> T findByEntityName(final String entityName) {
}
}

/**
* Find objects by a predicate.
*
* @param <T> HQDM entity type.
* @param predicate the predicate {@link IRI}
* @return a List of {@link Thing} that were found.
*/
public <T extends Thing> List<T> findByPredicateIriOnly(final IRI predicate) {
return (List<T>) database.findByPredicateIriOnly(predicate);
}

/**
* Find objects by a predicate value.
*
* @param <T> HQDM entity type.
* @param predicate the predicate {@link IRI}
* @param value The value of the predicate.
* @return a List of {@link Thing} that were found.
*/
public <T extends Thing> List<T> findByPredicateIriAndValue(final IRI predicate, final Object value) {
return (List<T>) database.findByPredicateIriAndValue(predicate, value);
}

/**
* Find members of a given class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import uk.gov.gchq.magmacore.hqdm.model.Thing;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.HQDM;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IRI;
import uk.gov.gchq.magmacore.hqdm.rdf.iri.RDFS;
import uk.gov.gchq.magmacore.hqdm.services.SpatioTemporalExtentServices;

/**
Expand Down Expand Up @@ -72,9 +73,11 @@ public void test() {
}

/**
* Test that findBySignValue can be used to find the right Things represented by a sign value for
* Test that findBySignValue can be used to find the right Things represented by
* a sign value for
* the given {@link uk.gov.gchq.magmacore.hqdm.model.Pattern} and
* {@link uk.gov.gchq.magmacore.hqdm.model.RecognizingLanguageCommunity} at the given
* {@link uk.gov.gchq.magmacore.hqdm.model.RecognizingLanguageCommunity} at the
* given
* {@link uk.gov.gchq.magmacore.hqdm.model.PointInTime}.
*/
@Test
Expand Down Expand Up @@ -150,7 +153,8 @@ public void testFindBySignWithNullSignValue() throws MagmaCoreException {
}

/**
* Check that we get an empty result if the pointInTime does not have an ENTITY_NAME.
* Check that we get an empty result if the pointInTime does not have an
* ENTITY_NAME.
*/
@Test
public void testFindBySignWithBadPointInTime() throws MagmaCoreException {
Expand All @@ -174,4 +178,60 @@ public void testFindBySignWithBadPointInTime() throws MagmaCoreException {
assertNotNull(found);
assertTrue(found.isEmpty());
}

/**
* Test that entities can be found by predicate only or predicate and value.
*/
@Test
public void testFindByPredicateOnly() {
final MagmaCoreService svc = MagmaCoreServiceFactory.createWithJenaDatabase();

final IRI individual1Iri = new IRI(SignPatternTestData.TEST_BASE, "individual1");
final IRI individual2Iri = new IRI(SignPatternTestData.TEST_BASE, "individual2");
final Individual individual1 = SpatioTemporalExtentServices.createIndividual(individual1Iri.getIri());
final Individual individual2 = SpatioTemporalExtentServices.createIndividual(individual2Iri.getIri());

individual1.addValue(HQDM.MEMBER_OF, "classOfIndividual");
individual2.addValue(HQDM.MEMBER_OF_KIND, "kindOfIndividual");
individual1.addValue(RDFS.RDF_TYPE, HQDM.INDIVIDUAL);
individual2.addValue(RDFS.RDF_TYPE, HQDM.INDIVIDUAL);

// Create two objects.
svc.runInTransaction(mc -> {
mc.create(individual1);
mc.create(individual2);
return mc;
});

// Find individual2 since it's the only one with MEMBER_OF_KIND
svc.runInTransaction(mc -> {
final List<Thing> result = mc.findByPredicateIriOnly(HQDM.MEMBER_OF_KIND);

assertEquals(1, result.size());
assertTrue(result.contains(individual2));
return mc;
});

// Find both individuals by RDF_TYPE IRI object.
svc.runInTransaction(mc -> {
final List<Thing> result = mc.findByPredicateIriAndValue(RDFS.RDF_TYPE, HQDM.INDIVIDUAL);

assertEquals(2, result.size());
assertTrue(result.contains(individual1));
assertTrue(result.contains(individual2));

return mc;
});

// Find individual1 by a String value
svc.runInTransaction(mc -> {
final List<Thing> result = mc.findByPredicateIriAndValue(HQDM.MEMBER_OF, "classOfIndividual");

assertEquals(1, result.size());
assertTrue(result.contains(individual1));

return mc;
});
}

}