feat: implement model functions

This commit is contained in:
Arnaud Vergnet 2022-01-05 09:24:53 +01:00
parent 9776ba727a
commit 69ae083224
4 changed files with 63 additions and 49 deletions

View file

@ -1,7 +1,9 @@
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) {
@ -10,32 +12,51 @@ public class DoItYourselfModel implements IModelFunctions
@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<List<String>> listProperties = model.listProperties(instantURI);
Optional<List<String>> 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<String> instancesURIList = model.getInstancesURI(instantTypeURI);
Optional<String> 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;
}
}

View file

@ -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 <subject, property, object>
* @param subjectURI

View file

@ -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);

View file

@ -2,15 +2,12 @@ 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);
@ -19,8 +16,7 @@ public class TestModelFunctions
}
@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);
@ -32,8 +28,15 @@ public class TestModelFunctions
}
@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");
@ -43,17 +46,7 @@ public class TestModelFunctions
}
@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);