Skip to content

Commit

Permalink
Changes on helpers URI
Browse files Browse the repository at this point in the history
  • Loading branch information
vfrico committed Sep 1, 2018
1 parent e94b866 commit f077d8b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 4 deletions.
60 changes: 59 additions & 1 deletion src/main/java/es/upm/oeg/tools/mappings/SparqlReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.apache.jena.graph.Triple;
import org.apache.jena.query.ParameterizedSparqlString;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.impl.PropertyImpl;
import org.apache.jena.util.SplitIRI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -64,14 +66,21 @@ public class SparqlReader {
" <http://dbpedia.org/x-template> ?templateB;" +
" <http://dbpedia.org/x-attribute> ?attributeB ." +
" }" +
"} limit 10";
"} limit 20";

private static final String SPARQL_GET_RELATED_ENTITY =
"PREFIX dbo: <http://dbpedia.org/ontology/> \n" +
"select ?object " +
"where {" +
" ?subject ?predicate ?object ." +
"}";

private static final String SPARQL_GET_SUBJECT_AND_OBJECT =
"select ?subject ?object " +
"where {" +
" ?subject ?predicate ?object ." +
"} limit 10";

public SparqlReader(String endpoint) {
this.endpoint = endpoint;
}
Expand All @@ -94,6 +103,55 @@ public long getCountTemplateUsage(String template, String lang) {
return count;
}


public List<Triple> getTriplesFromB(Annotation annotation) {
String fullUri = "http://dbpedia.org/ontology/"+SplitIRI.localname(annotation.getPropB());
RDFNode predicate = new PropertyImpl(fullUri);
ParameterizedSparqlString parametrizedQuery = new ParameterizedSparqlString();
parametrizedQuery.setCommandText(SPARQL_GET_SUBJECT_AND_OBJECT);
parametrizedQuery.setNsPrefix("dbo", "http://dbpedia.org/ontology/");
parametrizedQuery.setParam("predicate", predicate);
String query = parametrizedQuery.toString();
logger.info("SPARQL: "+query);

List<Map<String, RDFNode>> results;
results = SPARQLBackend.executeQueryForList(query, SPARQLUtils.getDBpediaEndpointByLang(annotation.getLangB()),
Sets.newHashSet("subject", "object"));

List<Triple> helperTriples = new ArrayList<>();

for (Map<String, RDFNode> result : results) {
RDFNode subject = result.get("subject");
RDFNode object = result.get("object");
helperTriples.add(new Triple(subject.asNode(), predicate.asNode(), object.asNode()));
}
return helperTriples;
}

public List<Triple> getTriplesFromA(Annotation annotation) {
String fullUri = "http://dbpedia.org/ontology/"+SplitIRI.localname(annotation.getPropA());
RDFNode predicate = new PropertyImpl(fullUri);
ParameterizedSparqlString parametrizedQuery = new ParameterizedSparqlString();
parametrizedQuery.setCommandText(SPARQL_GET_SUBJECT_AND_OBJECT);
parametrizedQuery.setNsPrefix("dbo", "http://dbpedia.org/ontology/");
parametrizedQuery.setParam("predicate", predicate);
String query = parametrizedQuery.toString();
logger.info("SPARQL: "+query);

List<Map<String, RDFNode>> results;
results = SPARQLBackend.executeQueryForList(query, SPARQLUtils.getDBpediaEndpointByLang(annotation.getLangA()),
Sets.newHashSet("subject", "object"));

List<Triple> helperTriples = new ArrayList<>();

for (Map<String, RDFNode> result : results) {
RDFNode subject = result.get("subject");
RDFNode object = result.get("object");
helperTriples.add(new Triple(subject.asNode(), predicate.asNode(), object.asNode()));
}
return helperTriples;
}

public List<Triple> getAnnotationHelp(Annotation annotation) {
ParameterizedSparqlString parametrizedQuery = new ParameterizedSparqlString();
parametrizedQuery.setCommandText(SPARQL_GET_RELATED_SUBJECTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,17 @@ public Response getHelpers(@PathParam("id") int id) {
AnnotationDAO resp = sqlService.getAnnotation(id);
if (resp != null) {
SparqlReader reader = new SparqlReader(Utils.getSPARQLEndpoint());
List<Triple> triplesJena = reader.getAnnotationHelp(resp);
List<TripleDAO> triples = triplesJena.stream().map(TripleDAO::new).collect(Collectors.toList());
//List<Triple> triplesJena = reader.getAnnotationHelp(resp);
List<Triple> triplesJenaForA = reader.getTriplesFromA(resp);
List<Triple> triplesJenaForB = reader.getTriplesFromB(resp);
List<TripleDAO> triplesA = triplesJenaForA.stream().map(TripleDAO::new).collect(Collectors.toList());
List<TripleDAO> triplesB = triplesJenaForB.stream().map(TripleDAO::new).collect(Collectors.toList());
AnnotationHelperDAO helper = new AnnotationHelperDAO();
helper.setRelatedTriples(triples);
helper.setRelatedTriplesA(triplesA);
helper.setRelatedTriplesB(triplesB);
List<TripleDAO> allTriples = new ArrayList<>(triplesA);
allTriples.addAll(triplesB);
helper.setRelatedTriples(allTriples);
return Response.status(200).entity(helper).build();
} else {
ApiError err = new ApiError("Annotation id "+id+" not found", 404);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
public class AnnotationHelperDAO {
List<TripleDAO> relatedTriples;
List<TripleDAO> relatedTriplesA;
List<TripleDAO> relatedTriplesB;

public List<TripleDAO> getRelatedTriples() {
return relatedTriples;
Expand All @@ -31,4 +33,22 @@ public AnnotationHelperDAO setRelatedTriples(List<TripleDAO> relatedTriples) {
this.relatedTriples = relatedTriples;
return this;
}

public List<TripleDAO> getRelatedTriplesA() {
return relatedTriplesA;
}

public AnnotationHelperDAO setRelatedTriplesA(List<TripleDAO> relatedTriplesA) {
this.relatedTriplesA = relatedTriplesA;
return this;
}

public List<TripleDAO> getRelatedTriplesB() {
return relatedTriplesB;
}

public AnnotationHelperDAO setRelatedTriplesB(List<TripleDAO> relatedTriplesB) {
this.relatedTriplesB = relatedTriplesB;
return this;
}
}

0 comments on commit f077d8b

Please sign in to comment.