-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 169-improve-the-magmacoreservice
- Loading branch information
Showing
26 changed files
with
476 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
71 changes: 71 additions & 0 deletions
71
examples/src/main/java/uk/gov/gchq/magmacore/examples/extensions/ModelExtensionTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package uk.gov.gchq.magmacore.examples.extensions; | ||
|
||
import java.util.UUID; | ||
|
||
import uk.gov.gchq.magmacore.examples.extensions.model.Constants; | ||
import uk.gov.gchq.magmacore.examples.extensions.model.UkLimitedCompany; | ||
import uk.gov.gchq.magmacore.examples.extensions.model.UkLimitedCompanyImpl; | ||
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.IriBase; | ||
import uk.gov.gchq.magmacore.hqdm.rdf.iri.RDFS; | ||
import uk.gov.gchq.magmacore.service.MagmaCoreServiceFactory; | ||
|
||
/** | ||
* Test the model extension provided by the model-extension-example module. | ||
* | ||
* <p> | ||
* Run this using mvn exec:java | ||
* -Dexec.mainClass=uk.gov.gchq.magmacore.examples.extensions.ModelExtensionTest | ||
* </p> | ||
*/ | ||
public class ModelExtensionTest { | ||
|
||
// Declare an IRI base for the data to be created. | ||
private static final IriBase TEST_BASE = new IriBase("test", "http://example.com/test#"); | ||
|
||
/** | ||
* Main entry point. | ||
* | ||
* @param args a String array | ||
*/ | ||
public static void main(final String[] args) { | ||
// Create a MagmaCoreService with an in-memory Apache Jena database. | ||
final var mcs = MagmaCoreServiceFactory.createWithJenaDatabase(); | ||
|
||
// The entity will be a part of a dummy possible_world, we just use the IRI | ||
// rather than creating the possible_world for this example. | ||
final var possibleWorldIri = new IRI(TEST_BASE, UUID.randomUUID().toString()); | ||
|
||
// Create an IRI for the entity we want to create, then create the entity. | ||
final var entityIri = new IRI(TEST_BASE, UUID.randomUUID().toString()); | ||
final Thing entity = new UkLimitedCompanyImpl(entityIri); | ||
|
||
// Set the RDF_TYPE and add the entity as a `part_of_possible_world`. | ||
entity.addValue(RDFS.RDF_TYPE, Constants.UK_LIMITED_COMPANY_IRI); | ||
entity.addValue(HQDM.PART_OF_POSSIBLE_WORLD, possibleWorldIri); | ||
|
||
// Persist the entity in the database. | ||
mcs.runInWriteTransaction(svc -> { | ||
svc.create(entity); | ||
return svc; | ||
}); | ||
|
||
// Read the entity back and assert that it matches the original. | ||
mcs.runInReadTransaction(svc -> { | ||
final var restoredEntity = svc.get(entityIri); | ||
|
||
if (restoredEntity == null) { | ||
System.err.println("restoredEntity should not be null."); | ||
} else if (!(restoredEntity instanceof UkLimitedCompany)) { | ||
System.err.println("restoredEntity should be an instanceof UkLimitedCompany."); | ||
} else if (!entity.equals(restoredEntity)) { | ||
System.err.println("restoredEntity should be equal to the original entity."); | ||
} else { | ||
System.out.println("Success."); | ||
} | ||
return svc; | ||
}); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
26 changes: 26 additions & 0 deletions
26
hqdm/src/main/java/uk/gov/gchq/magmacore/hqdm/extensions/ExtensionService.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package uk.gov.gchq.magmacore.hqdm.extensions; | ||
|
||
import uk.gov.gchq.magmacore.hqdm.model.Thing; | ||
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IRI; | ||
|
||
/** | ||
* An SPI interface for extending HQDM. | ||
* | ||
* <p> | ||
* See: <a href="https://github.com/twalmsley/hqdm_model_extension_example">HQDM Model Extension Example</a> for | ||
* an example of how to write an extension module. | ||
* </p> | ||
*/ | ||
public interface ExtensionService { | ||
|
||
/** | ||
* Create and entity with the given typeName, or return null if the typeName is not recognised | ||
* by the ExtensionService. | ||
* | ||
* @param typeName A String with the required type to be created. | ||
* @param iri The IRI to use when creating the instance. | ||
* @return A Thing if the typeName is recognised, null otherwise. | ||
*/ | ||
Thing createEntity(final String typeName, final IRI iri); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
hqdm/src/main/java/uk/gov/gchq/magmacore/hqdm/extensions/ExtensionServiceProvider.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package uk.gov.gchq.magmacore.hqdm.extensions; | ||
|
||
import java.util.Map; | ||
|
||
import uk.gov.gchq.magmacore.hqdm.model.Thing; | ||
import uk.gov.gchq.magmacore.hqdm.rdf.iri.IRI; | ||
|
||
/** | ||
* An SPI interface for extending HQDM. | ||
* | ||
* <p> | ||
* See: <a href="https://github.com/twalmsley/hqdm_model_extension_example">HQDM Model Extension Example</a> for | ||
* an example of how to write an extension module. | ||
* </p> | ||
*/ | ||
public interface ExtensionServiceProvider { | ||
|
||
/** | ||
* Create an instance of the extension service and ask it to register any new HQDM types in the Map. | ||
* | ||
* @param map a Map of IRI to Class so that MagmaCore can dynamically create Entities when found in a database. | ||
* @return ExtensionService instance. | ||
*/ | ||
ExtensionService createService(final Map<IRI, Class<? extends Thing>> map); | ||
|
||
} |
This file contains 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
Oops, something went wrong.