Commit dd2fdc98 authored by akucera's avatar akucera
Browse files

Addedd support for relations on the server side

parent dd1c2804
......@@ -227,7 +227,7 @@
<ObjectProperty rdf:about="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#isUsedBy">
<inverseOf rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#uses"/>
<rdfs:domain rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#DataPoint"/>
<rdfs:domain rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#Address"/>
<rdfs:range rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#Algorithm"/>
</ObjectProperty>
......@@ -322,7 +322,7 @@
<ObjectProperty rdf:about="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#uses">
<rdfs:domain rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#Algorithm"/>
<rdfs:range rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#DataPoint"/>
<rdfs:range rdf:resource="http://is.muni.cz/www/255658/sbms/v1_0/SemanticBMS#Address"/>
</ObjectProperty>
......
......@@ -5,9 +5,11 @@ import java.net.URI;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
......@@ -19,10 +21,12 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import cz.muni.fi.lasaris.sbms.api.entities.Address;
import cz.muni.fi.lasaris.sbms.api.entities.Algorithm;
import cz.muni.fi.lasaris.sbms.api.entities.Influence;
import cz.muni.fi.lasaris.sbms.api.entities.ObservedProperty;
import cz.muni.fi.lasaris.sbms.api.entities.Scope;
import cz.muni.fi.lasaris.sbms.api.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.api.request.AlgorithmRequest;
import cz.muni.fi.lasaris.sbms.api.request.Insert;
import cz.muni.fi.lasaris.sbms.api.response.AddressResponse;
import cz.muni.fi.lasaris.sbms.api.response.GroupedResponse;
......@@ -31,10 +35,10 @@ import cz.muni.fi.lasaris.sbms.model.Fields;
import cz.muni.fi.lasaris.sbms.model.ModelUpdater;
import cz.muni.fi.lasaris.sbms.model.QueryParser;
@Path("influence")
@Path("relations")
@PermitAll
public class InfluencesEndpoint {
final static Logger logger = Logger.getLogger(InfluencesEndpoint.class);
public class RelationsEndpoint {
final static Logger logger = Logger.getLogger(RelationsEndpoint.class);
@GET
@Path("/influencing")
......@@ -87,6 +91,35 @@ public class InfluencesEndpoint {
return QueryParser.getInfluencingResponse(r);
}
@GET
@Path("/used")
@Produces(MediaType.APPLICATION_JSON)
public AddressResponse getUsed(
@QueryParam("bmsId") String bmsIdP) {
if(StringUtils.isBlank(bmsIdP)) {
GroupedResponse.getErrorResponse("bmsId must be set");
}
AlgorithmRequest r = new AlgorithmRequest(new Algorithm(bmsIdP));
r.getResponseFields().addAll(Fields.INFLUENCE_FIELDS);
return QueryParser.getUsedResponse(r);
}
@GET
@Path("/using")
@Produces(MediaType.APPLICATION_JSON)
public AddressResponse getUsing(
@QueryParam("bmsId") String bmsIdP) {
if(StringUtils.isBlank(bmsIdP)) {
GroupedResponse.getErrorResponse("bmsId must be set");
}
AlgorithmRequest r = new AlgorithmRequest(new Algorithm());
r.getAlgorithm().setUsedAddress(new Address(bmsIdP));
r.getResponseFields().addAll(Fields.INFLUENCE_FIELDS);
return QueryParser.getUsingResponse(r);
}
@GET
@Path("/influenced")
@Produces(MediaType.APPLICATION_JSON)
......@@ -101,6 +134,8 @@ public class InfluencesEndpoint {
return QueryParser.getInfluencedResponse(r);
}
@Context
private UriInfo uriInfo;
......@@ -112,7 +147,11 @@ public class InfluencesEndpoint {
if(insert.getAddress() != null && ModelUpdater.insertAddress(insert)) {
URI createdUri = new URI(uriInfo.getAbsolutePath().toString() + insert.getAddress().getResourceId());
return Response.created(createdUri).build();
} else {
} else if (insert.getAlgorithm() != null && ModelUpdater.insertAlgorithm(insert)) {
URI createdUri = new URI(uriInfo.getAbsolutePath().toString() + insert.getAlgorithm().getResourceId());
return Response.created(createdUri).build();
}
else {
return Response.status(Response.Status.BAD_REQUEST).entity("Invalid JSON data - see server log").build();
}
} catch (Exception e) {
......@@ -121,4 +160,21 @@ public class InfluencesEndpoint {
}
}
@RolesAllowed("admin")
@DELETE
@Path("/{bmsId}")
@Consumes(MediaType.APPLICATION_JSON)
public Response RemoveRelationInfo(@PathParam("bmsId") String bmsId) {
System.out.println(bmsId);
try {
if(ModelUpdater.removeRelationAssertions(bmsId)) {
return Response.ok().build();
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("Invalid JSON data - see server log").build();
}
} catch (Exception e) {
logger.error(e);
return Response.serverError().build();
}
}
}
......@@ -5,9 +5,11 @@ import java.net.URI;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
......@@ -139,4 +141,22 @@ final static Logger logger = Logger.getLogger(TrendsEndpoint.class);
}
}
@RolesAllowed("admin")
@DELETE
@Path("/{bmsId}")
@Consumes(MediaType.APPLICATION_JSON)
public Response RemoveTrendInfo(@PathParam("bmsId") String bmsId) {
System.out.println(bmsId);
try {
if(ModelUpdater.removeTrendAssertions(bmsId)) {
return Response.ok().build();
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("Invalid JSON data - see server log").build();
}
} catch (Exception e) {
logger.error(e);
return Response.serverError().build();
}
}
}
......@@ -22,8 +22,8 @@ public class Address {
public String getType() {
return type;
}
public void setType(String dataPointType) {
this.type = dataPointType;
public void setType(String type) {
this.type = type;
}
@JsonIgnore
......@@ -67,9 +67,9 @@ public class Address {
this.bmsId = bmsId;
}
public Address(String bmsId, String dataPointType) {
public Address(String bmsId, String type) {
this.bmsId = bmsId;
this.type = dataPointType;
this.type = type;
}
@Override
......
......@@ -5,23 +5,23 @@ import java.util.TreeSet;
public class Algorithm extends Address {
public SortedSet<DataPoint> usedDataPointList;
public SortedSet<Address> usedAddressList;
public SortedSet<DataPoint> getUsedDataPointList() {
return usedDataPointList;
public SortedSet<Address> getUsedAddressList() {
return usedAddressList;
}
public void setUsedDataPointList(SortedSet<DataPoint> usedDataPointList) {
this.usedDataPointList = usedDataPointList;
public void setUsedAddressList(SortedSet<Address> usedDataPointList) {
this.usedAddressList = usedDataPointList;
}
public DataPoint getUsedDataPoint() {
return usedDataPointList.first();
public Address getUsedAddress() {
return usedAddressList.first();
}
public void setUsedDataPoint(DataPoint dp) {
usedDataPointList = new TreeSet<DataPoint>();
usedDataPointList.add(dp);
public void setUsedAddress(Address a) {
usedAddressList = new TreeSet<Address>();
usedAddressList.add(a);
}
public Algorithm() {
......@@ -31,8 +31,8 @@ public class Algorithm extends Address {
super(bmsId);
}
public Algorithm(String bmsId, String dataPointType) {
super(bmsId, dataPointType);
public Algorithm(String bmsId, String type) {
super(bmsId, type);
}
......
......@@ -6,9 +6,11 @@ public class AddressRequest extends Request {
private Address address;
public AddressRequest() {
super();
}
public AddressRequest(Address address) {
super();
this.address = address;
}
......
package cz.muni.fi.lasaris.sbms.api.request;
import cz.muni.fi.lasaris.sbms.api.entities.Algorithm;
public class AlgorithmRequest extends Request {
private Algorithm algorithm;
public Algorithm getAlgorithm() {
return algorithm;
}
public AlgorithmRequest(Algorithm algorithm) {
super();
this.algorithm = algorithm;
}
public void setAlgorithm(Algorithm algorithm) {
this.algorithm = algorithm;
}
@Override
public String getIdentityField() {
return "bmsId";
}
}
......@@ -16,8 +16,10 @@ import org.apache.jena.sparql.syntax.ElementOptional;
import org.apache.jena.sparql.syntax.ElementTriplesBlock;
import cz.muni.fi.lasaris.sbms.api.entities.Address;
import cz.muni.fi.lasaris.sbms.api.entities.Algorithm;
import cz.muni.fi.lasaris.sbms.api.entities.DataPoint;
import cz.muni.fi.lasaris.sbms.api.request.AddressRequest;
import cz.muni.fi.lasaris.sbms.api.request.AlgorithmRequest;
import cz.muni.fi.lasaris.sbms.api.request.DataPointsRequest;
import cz.muni.fi.lasaris.sbms.api.request.GroupableRequest;
import cz.muni.fi.lasaris.sbms.api.request.TrendsRequest;
......@@ -342,7 +344,11 @@ public class BodyBuilders {
NodeFactory.createURI(NS.sbms + "hasBMSId"),
NodeFactory.createLiteral(a.getBmsId())));
}
if(fields.contains("type")) {
setTypeClause(body, "address", gsv("type"), NS.sbms + "Address", NS.sbms + "X");
}
if(a.getPublisher() != null || fields.contains(gsv("publisher.bimId"))) {
body.addTriplePattern(Triple.create(Var.alloc("address"),
......@@ -410,6 +416,64 @@ public class BodyBuilders {
Var.alloc(gsv("influenced.property.quality"))));
}
}
return body;
}
public static ElementGroup getQueryBody(AlgorithmRequest request) {
ElementGroup body = new ElementGroup();
Algorithm a = request.getAlgorithm();
List<String> fields = request.getResponseFields();
body.addTriplePattern(Triple.create(Var.alloc("algorithm"),
NodeFactory.createURI(NS.sbms + "hasBMSId"),
Var.alloc(gsv("bmsId"))));
if(a.getBmsId() != null) {
body.addTriplePattern(Triple.create(Var.alloc("algorithm"),
NodeFactory.createURI(NS.sbms + "hasBMSId"),
NodeFactory.createLiteral(a.getBmsId())));
}
/*
if(fields.contains("type")) {
setTypeClause(body, "algorithm", gsv("type"), NS.sbms + "Algorithm", NS.sbms + "Algorithm");
}
*/
if(a.getUsedAddress() != null || fields.contains("used.bmsId") || fields.contains("used.type")) {
body.addTriplePattern(Triple.create(Var.alloc("algorithm"),
NodeFactory.createURI(NS.sbms + "uses"),
Var.alloc(gsv("used"))));
if(a.getUsedAddress() != null && a.getUsedAddress().getBmsId() != null) {
body.addTriplePattern(Triple.create(Var.alloc("used"),
NodeFactory.createURI(NS.sbms + "hasBMSId"),
NodeFactory.createLiteral(a.getUsedAddress().getBmsId())));
}
if(fields.contains("used.bmsId")) {
body.addTriplePattern(Triple.create(Var.alloc("used"),
NodeFactory.createURI(NS.sbms + "hasBMSId"),
Var.alloc(gsv("used.bmsId"))));
}
if(fields.contains("used.type")) {
setTypeClause(body, "used", gsv("type"), NS.sbms + "Address", NS.sbms + "Address");
}
if(a.getUsedAddress() != null && a.getUsedAddress().getType() !=null) {
body.addTriplePattern(Triple.create(Var.alloc("used"),
NodeFactory.createURI(NS.rdf + "type"),
NodeFactory.createURI(NS.sbms + a.getUsedAddress().getType())));
}
}
return body;
}
......
......@@ -9,6 +9,7 @@ import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.apache.log4j.Logger;
import cz.muni.fi.lasaris.sbms.api.entities.Address;
import cz.muni.fi.lasaris.sbms.api.entities.DataPoint;
import cz.muni.fi.lasaris.sbms.api.entities.Influence;
import cz.muni.fi.lasaris.sbms.api.request.Insert;
......@@ -40,9 +41,12 @@ public class ModelUpdater {
// ?dataPoint rdf:type ?dataPointClass.
// ?observation rdf:type sbms:Observation.
dp.addProperty(RDF.type, m.createResource(NS.sbms + d.getType()));
obs.addProperty(RDF.type, m.createResource(NS.sbms + "Observation"));
if(!dp.hasProperty(RDF.type)) {
dp.addProperty(RDF.type, m.createResource(NS.sbms + d.getType()));
}
if(!obs.hasProperty(RDF.type)) {
obs.addProperty(RDF.type, m.createResource(NS.sbms + "Observation"));
}
// ?dataPoint sbms:hasBMSId ?bmsId.
Property p = null;
p = m.createProperty(NS.sbms + "hasBMSId");
......@@ -140,34 +144,74 @@ public class ModelUpdater {
}
}
// ?dataPoint sbms:influences ?property.
if(d.getInfluenceList() != null) {
for(Influence inf : d.getInfluenceList()) {
if(inf.getProperty() != null
&& inf.getProperty().getDomain() != null
&& inf.getProperty().getQuality() != null && inf.getScope() != null &&
inf.getScope().getBimId() != null) {
insertInfluence(d, m, dp);
TdbConnector.commit();
logger.debug("Data commited to the TDB");
return true;
} catch (Exception e) {
return failInsert(e);
}
}
Resource ip = m.createResource(NS.sbmsd + inf.getScope().getResourceId()
+ inf.getProperty().getDomain()
+ inf.getProperty().getQuality() + "ObsProp");
ip.addProperty(RDF.type, NS.sbms + m.createResource(NS.sbms + "ObservedProperty"));
private static void insertInfluence(Address entity, Model m, Resource res) {
Property p;
// ?dataPoint sbms:influences ?property.
if(entity.getInfluenceList() != null) {
for(Influence inf : entity.getInfluenceList()) {
if(inf.getProperty() != null
&& inf.getProperty().getDomain() != null
&& inf.getProperty().getQuality() != null && inf.getScope() != null &&
inf.getScope().getBimId() != null) {
Resource ip = m.createResource(NS.sbmsd + inf.getScope().getResourceId()
+ inf.getProperty().getDomain()
+ inf.getProperty().getQuality() + "ObsProp");
ip.addProperty(RDF.type, NS.sbms + m.createResource(NS.sbms + "ObservedProperty"));
// ?datapoint sbms:influences ?property.
p = m.createProperty(NS.sbms + "influences");
dp.addProperty(p, ip);
// ?property sbms:hasPhysicalQuality ?quality.
ip.addProperty(m.createProperty(NS.sbms + "hasPhysicalQuality"),
m.createResource(NS.ucum + inf.getProperty().getQuality()));
// ?property sbms:hasPropertyDomain ?propDomain.
ip.addProperty(m.createProperty(NS.sbms + "hasPropertyDomain"),
m.createResource(NS.sbms + inf.getProperty().getDomain()));
// ?property sbms:isPropertyOf ?scope.
ip.addProperty(m.createProperty(NS.sbms + "isPropertyOf"),
m.createResource(NS.sbmsd +inf.getScope().getResourceId()));
}
// ?datapoint sbms:influences ?property.
p = m.createProperty(NS.sbms + "influences");
res.addProperty(p, ip);
// ?property sbms:hasPhysicalQuality ?quality.
ip.addProperty(m.createProperty(NS.sbms + "hasPhysicalQuality"),
m.createResource(NS.ucum + inf.getProperty().getQuality()));
// ?property sbms:hasPropertyDomain ?propDomain.
ip.addProperty(m.createProperty(NS.sbms + "hasPropertyDomain"),
m.createResource(NS.sbms + inf.getProperty().getDomain()));
// ?property sbms:isPropertyOf ?scope.
ip.addProperty(m.createProperty(NS.sbms + "isPropertyOf"),
m.createResource(NS.sbmsd +inf.getScope().getResourceId()));
}
}
}
}
public static boolean insertTrend(Insert insert) {
if(insert.getTrend() == null || insert.getTrend().getBmsId() == null) {
return failInsert("no TL to insert");
}
try {
Model m = TdbConnector.getWritableModel();
Resource trend = m.createResource(NS.sbmsd + insert.getTrend().getResourceId());
if(!trend.hasProperty(RDF.type)) {
trend.addProperty(RDF.type, m.createResource(NS.sbms + "Trend"));
}
Property p = null;
p = m.createProperty(NS.sbms + "hasBMSId");
if(!trend.hasProperty(p)) {
trend.addLiteral(p, insert.getTrend().getBmsId());
}
if(insert.getTrend().getDataPoint() != null && insert.getTrend().getDataPoint().getResourceId() != null) {
Resource dp = m.createResource(NS.sbmsd + insert.getTrend().getDataPoint().getResourceId());
p = m.createProperty(NS.sbms + "trends");
if(!trend.hasProperty(p)) {
trend.addProperty(p, dp);
}
}
TdbConnector.commit();
......@@ -176,23 +220,65 @@ public class ModelUpdater {
} catch (Exception e) {
return failInsert(e);
}
}
public static boolean insertTrend(Insert insert) {
// TODO Implement insertTrend
return false;
}
public static boolean insertInfluence(Insert insert) {
// TODO Implement insertInfluence
return false;
public static boolean insertAddress(Insert insert) {
if(insert.getAddress() == null || insert.getAddress().getBmsId() == null) {
return failInsert("no TL to insert");
}
try {
Model m = TdbConnector.getWritableModel();
Resource address = m.createResource(NS.sbmsd + insert.getTrend().getResourceId());
if(!address.hasProperty(RDF.type)) {
address.addProperty(RDF.type, m.createResource(NS.sbms + insert.getAddress().getType()));
}
Property p = null;
p = m.createProperty(NS.sbms + "hasBMSId");
if(!address.hasProperty(p)) {
address.addLiteral(p, insert.getAddress().getBmsId());
}
insertInfluence(insert.getAddress(), m, address);
TdbConnector.commit();
logger.debug("Data commited to the TDB");
return true;
} catch (Exception e) {
return failInsert(e);
}
}
public static boolean insertAddress(Insert insert) {
// TODO Implement program insert
return false;
public static boolean insertAlgorithm(Insert insert) {
if(insert.getAlgorithm() == null || insert.getAlgorithm().getBmsId() == null) {
return failInsert("no algorithm to insert");
}
try {
Model m = TdbConnector.getWritableModel();
Resource alg = m.createResource(NS.sbmsd + insert.getAlgorithm().getResourceId());
Property p = null;
if(!alg.hasProperty(RDF.type)) {
alg.addProperty(RDF.type, m.createResource(NS.sbms + "Algorithm"));
}
p = m.createProperty(NS.sbms + "hasBMSId");
if(!alg.hasProperty(p)) {
alg.addLiteral(p, insert.getAlgorithm().getBmsId());
}
if(insert.getAlgorithm().getUsedAddressList() != null) {
for(Address a : insert.getAlgorithm().getUsedAddressList()) {
Resource address = m.createResource(NS.sbmsd + a.getResourceId());
p = m.createProperty(NS.sbms + "uses");
alg.addProperty(p, address);
}
}
TdbConnector.commit();
logger.debug("Data commited to the TDB");
return true;
} catch (Exception e) {
return failInsert(e);
}
}
private static boolean failInsert(Object msg) {
......@@ -208,7 +