From 69ae0832243c038ca750cb1ac9b33db1a7cb8c9f Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Wed, 5 Jan 2022 09:24:53 +0100 Subject: [PATCH] feat: implement model functions --- .../semantic/model/DoItYourselfModel.java | 57 +++++++++++++------ .../semantic/model/IConvenienceInterface.java | 8 +-- .../java/semantic/model/SemanticModel.java | 2 +- .../java/semantic/TestModelFunctions.java | 45 +++++++-------- 4 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/main/java/semantic/model/DoItYourselfModel.java b/src/main/java/semantic/model/DoItYourselfModel.java index 6050dd8..23558dd 100644 --- a/src/main/java/semantic/model/DoItYourselfModel.java +++ b/src/main/java/semantic/model/DoItYourselfModel.java @@ -1,41 +1,62 @@ package semantic.model; -public class DoItYourselfModel implements IModelFunctions -{ +import java.util.List; +import java.util.Optional; + +public class DoItYourselfModel implements IModelFunctions { IConvenienceInterface model; - + public DoItYourselfModel(IConvenienceInterface m) { this.model = m; } @Override public String createPlace(String name) { - // TODO Auto-generated method stub - return null; + String placeURI = model.getEntityURI("Place").get(0); + return model.createInstance(name, placeURI); } @Override public String createInstant(TimestampEntity instant) { - // TODO Auto-generated method stub - return null; + String instantURI = model.getEntityURI("Instant").get(0); + String propertyURI = model.getEntityURI("a pour timestamp").get(0); + String instantInstanceURI = model.createInstance(instant.getTimeStamp(), instantURI); + model.addDataPropertyToIndividual(instantInstanceURI, propertyURI, instant.getTimeStamp()); + return instantInstanceURI; + } + + @Override + public String getInstantTimestamp(String instantURI) { + String propertyURI = model.getEntityURI("a pour timestamp").get(0); + List> listProperties = model.listProperties(instantURI); + Optional> timestampProperty = listProperties.stream().filter(property -> + property.get(0).equals(propertyURI) + ).findFirst(); + return timestampProperty.map(strings -> strings.get(1)).orElse(null); } @Override public String getInstantURI(TimestampEntity instant) { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getInstantTimestamp(String instantURI) - { - // TODO Auto-generated method stub - return null; + String instantTypeURI = model.getEntityURI("Instant").get(0); + String propertyURI = model.getEntityURI("a pour timestamp").get(0); + List instancesURIList = model.getInstancesURI(instantTypeURI); + Optional instantURI = instancesURIList.stream().filter(instanceURI -> + model.hasDataPropertyValue( + instanceURI, propertyURI, instant.getTimeStamp()) + ).findFirst(); + return instantURI.orElse(null); } @Override public String createObs(String value, String paramURI, String instantURI) { - // TODO Auto-generated method stub - return null; + String obsClassURI = model.getEntityURI("Observation").get(0); + String obsInstanceURI = model.createInstance(instantURI + "_" + paramURI + "_" + value, obsClassURI); + String dataValueURI = model.getEntityURI("a pour valeur").get(0); + String datePropertyURI = model.getEntityURI("a pour date").get(0); + String sensorURI = model.whichSensorDidIt(getInstantTimestamp(instantURI), paramURI); + model.addDataPropertyToIndividual(obsInstanceURI, dataValueURI, value); + model.addObjectPropertyToIndividual(obsInstanceURI, datePropertyURI, instantURI); + model.addObservationToSensor(obsInstanceURI, sensorURI); + return obsInstanceURI; } } diff --git a/src/main/java/semantic/model/IConvenienceInterface.java b/src/main/java/semantic/model/IConvenienceInterface.java index ec94433..17b912f 100644 --- a/src/main/java/semantic/model/IConvenienceInterface.java +++ b/src/main/java/semantic/model/IConvenienceInterface.java @@ -49,12 +49,12 @@ public interface IConvenienceInterface */ public boolean isOfType(String instanceURI, String typeURI); /** - * Creates an instance of the provided type, with the provided label. - * @param label - * @param type the URI of the type + * Creates an instance of the provided typeURI, with the provided label. + * @param label the label of the instance + * @param typeURI the URI of the type * @return the URI of the created individual */ - public String createInstance(String label, String type); + public String createInstance(String label, String typeURI); /** * Adds a triple in the knowledge base * @param subjectURI diff --git a/src/main/java/semantic/model/SemanticModel.java b/src/main/java/semantic/model/SemanticModel.java index 11cc856..7f3fabc 100644 --- a/src/main/java/semantic/model/SemanticModel.java +++ b/src/main/java/semantic/model/SemanticModel.java @@ -126,7 +126,7 @@ public class SemanticModel implements IConvenienceInterface this.model = this.dataset.getDefaultModel(); this.loadFromFile("./tp2_sensors.rdf", "", "RDF/XML", true); this.loadFromFile(ontologyPath, "", "TURTLE", true); - // Computation of URI that will be needed often, to limit frequent requests + // Computation of URI that will often be needed, to limit frequent requests this.temperatureSensor1URI = this.getEntityURI("TemperatureSensor_1").get(0); this.temperatureSensor2URI = this.getEntityURI("TemperatureSensor_2").get(0); this.temperatureSensor3URI = this.getEntityURI("TemperatureSensor_3").get(0); diff --git a/src/test/java/semantic/TestModelFunctions.java b/src/test/java/semantic/TestModelFunctions.java index 4ba52c1..7ad001f 100644 --- a/src/test/java/semantic/TestModelFunctions.java +++ b/src/test/java/semantic/TestModelFunctions.java @@ -2,38 +2,41 @@ package semantic; import org.junit.Assert; import org.junit.Test; - import semantic.controler.Controller; import semantic.model.TimestampEntity; -public class TestModelFunctions -{ +public class TestModelFunctions { @Test - public void testPlaceCreation() - { + public void testPlaceCreation() { Controller c = new Controller(); String jurassicParkURI = c.getCustomModel().createPlace("Jurassic park"); String placeClassURI = c.getModel().getEntityURI("Lieu").get(0); Assert.assertTrue("L'entité créée n'est pas de classe Lieu", c.getModel().isOfType(jurassicParkURI, placeClassURI)); Assert.assertTrue("Le lieu créé n'a pas de label pour indiquer son nom", c.getModel().listLabels(jurassicParkURI).contains("Jurassic park")); } - + @Test - public void testInstantCreation() - { + public void testInstantCreation() { Controller c = new Controller(); TimestampEntity t = new TimestampEntity("2014-02-13T06:20:00"); String instantURI = c.getCustomModel().createInstant(t); String instantClassURI = c.getModel().getEntityURI("Instant").get(0); - String propertyURI = c.getModel().getEntityURI("a pour timestamp").get(0); + String propertyURI = c.getModel().getEntityURI("a pour timestamp").get(0); Assert.assertTrue("L'entité créée n'est pas de la classe Instant", c.getModel().isOfType(instantURI, instantClassURI)); Assert.assertTrue("L'instant créé n'a pas le bon timestamp", c.getModel().hasDataPropertyValue( instantURI, propertyURI, "2014-02-13T06:20:00")); } - + @Test - public void testInstantRetrieval() - { + public void testTimestampRetrieval() { + Controller c = new Controller(); + TimestampEntity t = new TimestampEntity("2014-02-13T06:20:00"); + String instantURI = c.getCustomModel().createInstant(t); + Assert.assertTrue("Le timestamp ne correspond pas", c.getCustomModel().getInstantTimestamp(instantURI).equals("2014-02-13T06:20:00")); + } + + @Test + public void testInstantRetrieval() { Controller c = new Controller(); TimestampEntity t = new TimestampEntity("2014-02-13T06:20:00"); TimestampEntity t2 = new TimestampEntity("2015-02-13T06:20:00"); @@ -41,19 +44,9 @@ public class TestModelFunctions Assert.assertTrue("La recherche d'un instant par timestamp ne retourne rien pour un instant sensé exister", c.getCustomModel().getInstantURI(t).equals(instantURI)); Assert.assertNull("La recherche d'un instant par timestamp inexistant ne retourne pas un résultat null", c.getCustomModel().getInstantURI(t2)); } - + @Test - public void testTimestampRetrieval() - { - Controller c = new Controller(); - TimestampEntity t = new TimestampEntity("2014-02-13T06:20:00"); - String instantURI = c.getCustomModel().createInstant(t); - Assert.assertTrue("Le timestamp ne correspond pas", c.getCustomModel().getInstantTimestamp(instantURI).equals("2014-02-13T06:20:00")); - } - - @Test - public void testObservationCreation() - { + public void testObservationCreation() { Controller c = new Controller(); TimestampEntity t = new TimestampEntity("2014-02-13T06:20:00"); String instantURI = c.getCustomModel().createInstant(t); @@ -66,8 +59,8 @@ public class TestModelFunctions String datePropertyURI = c.getModel().getEntityURI("a pour date").get(0); String sensorURI = c.getModel().whichSensorDidIt("2014-02-13T06:20:00", paramURI); Assert.assertTrue("L'observation n'est pas une instance de la bonne classe", c.getModel().isOfType(obsURI, obsClassURI)); - Assert.assertTrue("L'observation n'a pas la bonne valeur",c.getModel().hasDataPropertyValue(obsURI, hasDataValueURI, value)); - Assert.assertTrue("L'observation n'a pas la bonne date",c.getModel().hasObjectProperty(obsURI, datePropertyURI, instantURI)); + Assert.assertTrue("L'observation n'a pas la bonne valeur", c.getModel().hasDataPropertyValue(obsURI, hasDataValueURI, value)); + Assert.assertTrue("L'observation n'a pas la bonne date", c.getModel().hasObjectProperty(obsURI, datePropertyURI, instantURI)); Assert.assertTrue("L'observation n'est pas rattachée au bon capteur", c.getModel().hasSensorDoneIt(obsURI, sensorURI)); } }