Skip to content

Commit

Permalink
First Update For meeting
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofreitas96 committed Feb 6, 2021
1 parent c6de59a commit 6b223a7
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cclasses;

import java.util.List;
import java.util.Map;

public class ResponseClass {

private final String id;

private final String $schema;

private final String description;

private final String type;

private final Map<String,Object> properties;





public ResponseClass( Map<String,Object> properties) {
this.id = "https://example.com/arrays.schema.json";
this.properties = properties;
this.$schema = "http://json-schema.org/draft-07/schema#summaryDoc";
this.description = "The document summary representation.";
this.type = "object";

}

public String getId() {
return id;
}

public Map<String,Object> getProperties() {
return properties;
}

public void addContent(String key,Map<String,String> value) {
properties.put(key,value);
}

public void addList(String key,List<Map<String,String>> value) {
properties.put(key,value);
}

public String get$schema() {
return $schema;
}

public String getDescription() {
return description;
}

public String getType() {
return type;
}
}
103 changes: 103 additions & 0 deletions new_project/backend/JenaTest/src/main/java/queries/Queries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package queries;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;

public class Queries {



public Query getGeneral_query(){
return QueryFactory.create("SELECT * WHERE {\n" +
" ?sub ?pred ?obj .\n" +
"}");
}

public Query getTitle_query(String uuid){
return QueryFactory.create("SELECT ?description\n" +
"WHERE {\n" +
uuid + " <http://erlangen-crm.org/200717/P102_has_title> ?type .\n" +
" ?type <http://www.episa.inesctec.pt/ligacao#hasValue> ?title_type .\n" +
" ?title_type <http://www.episa.inesctec.pt/data_object#stringValue> ?description\n" +
"}");
}

public Query getLevel_of_description_query(String uuid){
return QueryFactory.create("SELECT ?descriptionLevel\n" +
"WHERE {\n" +
uuid + " <http://www.semanticweb.org/dmelo/ontologies/2020/7/untitled-ontology-151#ARP12_has_level_of_description> ?descriptionLevel\n" +
"}");
}

public Query getUuid(String uuid){
return QueryFactory.create("SELECT ?description\n" +
"WHERE {\n" +
uuid + " <http://erlangen-crm.org/200717/has_uuid> ?description\n" +
"}");
}

public Query getIDd(String uuid){
return QueryFactory.create("SELECT ?description\n" +
"WHERE {\n" +
"?description <http://erlangen-crm.org/200717/has_uuid> "+ uuid +"\n" +
"}");
}



public Query getReference_codes_query(String uuid){
return QueryFactory.create("SELECT ?description \n" +
"WHERE {\n" +
uuid + " <http://erlangen-crm.org/200717/P1_is_identified_by> ?cidoc_identifier .\n" +
" ?cidoc_identifier <http://www.episa.inesctec.pt/ligacao#hasValue> ?identifier_value .\n" +
" ?cidoc_identifier <http://erlangen-crm.org/200717/P2_has_type> ?type.\n" +
" ?identifier_value <http://www.episa.inesctec.pt/data_object#stringValue> ?description\n" +
"}");
}

public Query getIdFromReference_codes_query(String refCode){
return QueryFactory.create("SELECT ?description \n" +
"WHERE {\n" +
"?description <http://erlangen-crm.org/200717/P1_is_identified_by> ?cidoc_identifier .\n" +
" ?cidoc_identifier <http://www.episa.inesctec.pt/ligacao#hasValue> ?identifier_value .\n" +
" ?cidoc_identifier <http://erlangen-crm.org/200717/P2_has_type> ?type.\n" +
" ?identifier_value <http://www.episa.inesctec.pt/data_object#stringValue> "+ refCode + "\n" +
"}");
}

public Query getPerson(String uuid){
return QueryFactory.create("SELECT ?name\n" +
"WHERE {\n" +
uuid + "<http://erlangen-crm.org/200717/P1_is_identified_by> ?object.\n" +
" ?object <http://www.episa.inesctec.pt/ligacao#hasValue> ?object2.\n" +
" ?object2 <http://www.episa.inesctec.pt/ligacao#stringValue> ?name \n" +
"}");
}

public Query getPlace(String uuid){
return QueryFactory.create("SELECT ?name \n" +
"WHERE {\n" +
uuid + "<http://erlangen-crm.org/200717/P1_is_identified_by> ?object.\n" +
" ?object <http://www.episa.inesctec.pt/ligacao#hasValue> ?object2.\n" +
" ?object2 <http://www.episa.inesctec.pt/ligacao#stringValue> ?name\n" +
"}");
}

public Query getAllDocs(){
return QueryFactory.create("SELECT ?subject\n" +
"WHERE {\n" +
" ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://erlangen-crm.org/200717/E31_Document>\n" +
"}");
}

public Query insertUuid(String olduuid, String uuid){
return QueryFactory.create("INSERT {\n" +
olduuid + "<has_uuid> \""+ uuid +"\" \n" +
" }");
}





}
Original file line number Diff line number Diff line change
@@ -1,21 +1,77 @@
package restservice;


import java.util.HashMap;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import cclasses.ResponseClass;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import queries.Queries;
import showcase.Connection;

@RestController
@RestController
public class MainController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
private final Queries queries =new Queries();

//@GetMapping("/greeting")
//public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
// return new Greeting(counter.incrementAndGet(), String.format(template, name));
//}
@GetMapping("/doc")
public ResponseClass document(@RequestParam(value = "id", defaultValue = "") String uuid) {
Connection conn = new Connection();
HashMap<String,Object> list = new HashMap<>();

HashMap<String,Object> list0 = new HashMap<>() ;
ResponseClass uuidrep = new ResponseClass(list0);
conn.obtainGeneralResponse(queries.getIDd(uuid),"id",uuidrep);
HashMap<String,String > idmap = (HashMap<String, String>) uuidrep.getProperties().get("id");
String docid = "<" + idmap.get("description") + ">";

ResponseClass rep = new ResponseClass( list);
rep = conn.obtainGeneralResponse(queries.getTitle_query(docid),"title",rep);
rep = conn.obtainGeneralResponse(queries.getUuid(docid),"episaIdentifier",rep);
rep = conn.obtainGeneralResponse(queries.getReference_codes_query(docid),"dglabIdentifier",rep);

return rep;
}

@GetMapping("/search")
public ResponseClass documentSummary(@RequestParam(value = "id", defaultValue = "") String refcode) {
Connection conn = new Connection();
HashMap<String,Object> list0 = new HashMap<>() ;
ResponseClass uuidrep = new ResponseClass(list0);
conn.obtainGeneralResponse(queries.getIdFromReference_codes_query(refcode),"id",uuidrep);
HashMap<String,String > idmap = (HashMap<String, String>) uuidrep.getProperties().get("id");
String uuid = "<" + idmap.get("description") + ">";


HashMap<String,Object> list = new HashMap<>() ;
ResponseClass rep = new ResponseClass( list);
rep = conn.obtainGeneralResponse(queries.getTitle_query(uuid),"title",rep);
rep = conn.obtainGeneralResponse(queries.getUuid(uuid),"episaIdentifier",rep);
rep = conn.obtainGeneralResponse(queries.getReference_codes_query(uuid),"dglabIdentifier",rep);

return rep;
}

@GetMapping("/person")
public ResponseClass person(@RequestParam(value = "id", defaultValue = "") String uuid) {
Connection conn = new Connection();
HashMap<String,Object> list = new HashMap<>() ;
ResponseClass rep = new ResponseClass( list);
rep = conn.obtainGeneralResponse(queries.getPerson(uuid),"person",rep);
return rep;
}

@GetMapping("/place")
public ResponseClass place(@RequestParam(value = "id", defaultValue = "") String uuid) {
Connection conn = new Connection();
HashMap<String,Object> list = new HashMap<>() ;
ResponseClass rep = new ResponseClass( list);
rep = conn.obtainGeneralResponse(queries.getPlace(uuid),"place",rep);
return rep;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package restservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RestServiceApplication {
public static void main(String[] args){
SpringApplication.run(RestServiceApplication.class, args);
}
}
111 changes: 72 additions & 39 deletions new_project/backend/JenaTest/src/main/java/showcase/Connection.java
Original file line number Diff line number Diff line change
@@ -1,60 +1,93 @@
package showcase;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ReadWrite;
import org.apache.jena.query.ResultSetFormatter;
import cclasses.ResponseClass;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.rdfconnection.RDFConnectionFuseki;
import org.apache.jena.rdfconnection.RDFConnectionRemoteBuilder;
import queries.Queries;

import java.util.*;


public class Connection {
Queries querier = new Queries();

public static void main (String args[]) {
public ResponseClass obtainGeneralResponse(Query query,String key,ResponseClass r){
RDFConnectionRemoteBuilder builder = RDFConnectionFuseki.create()
.destination("http://localhost:3030/name/get");
Query query = QueryFactory.create("SELECT * WHERE {\n" +
" ?sub ?pred ?obj .\n" +
"} \n" +
"LIMIT 100");
.destination("http://localhost:3030/name/sparql");


try ( RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build() ) {
//conn.queryResultSet(query, ResultSetFormatter::out);

Model model = conn.fetch();
//conn.query("SELECT * WHERE {\n" +
// " ?sub ?pred ?obj .\n" +
// "} \n" +
// "LIMIT 10").execSelect();
// list the statements in the graph
StmtIterator iter = model.listStatements();

int i = 0;
// print out the predicate, subject and object of each statement
while (iter.hasNext() && i<10) {
Statement stmt = iter.nextStatement(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object

System.out.print(subject.toString());
System.out.print(" " + predicate.toString() + " ");
if (object instanceof Resource) {
System.out.print(object.toString());
} else {
// object is a literal
System.out.print(" \"" + object.toString() + "\"");

ResultSet rs = conn.query(query).execSelect();
int i = 1;
ArrayList<Map<String,String>> complexList = new ArrayList<>();
QuerySolution stmt;
if(rs.hasNext()){
stmt = rs.next();
}else{
return r;
}

//TODO CLEAN INTO 2 FUNCTIONS
if(!rs.hasNext()){
Iterator<String> b = stmt.varNames();
HashMap<String,String> map = new HashMap<>();

while(b.hasNext()){
String current = b.next();
RDFNode res = stmt.get(current);
map.put(current,res.toString());

}
System.out.println(" .");
map.put("type","string");
r.addContent(key,map);
return r;
}

i = 0;
while (rs.hasNext()) {
if (i != 0){
stmt = rs.next();}
Iterator<String> b = stmt.varNames();
HashMap<String, String> map = new HashMap<>();

while (b.hasNext()) {
String current = b.next();
RDFNode res = stmt.get(current);
map.put(current, res.toString());

}
map.put("type","string");
complexList.add(map);
i++;
}
r.addList(key,complexList);
}
return r;
}

public ArrayList<String> getAllUuids(){
RDFConnectionRemoteBuilder builder = RDFConnectionFuseki.create()
.destination("http://localhost:3030/name/sparql");
ArrayList<String> list = new ArrayList<>();

try ( RDFConnectionFuseki conn = (RDFConnectionFuseki)builder.build() ) {
Query query = querier.getAllDocs();
ResultSet rs = conn.query(query).execSelect();
while (rs.hasNext()) {

QuerySolution qs = rs.next();

//model.write(System.out);*/
list.add(qs.get("subject").toString());

}
}
return list;
}



}



Loading

0 comments on commit 6b223a7

Please sign in to comment.