Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/db selection #11

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
5efcbbb
Correct grammar and interface for parser
twist900 Nov 27, 2013
e6bf73f
Merge branch 'feature/server-test#types' of https://github.com/ilucin…
twist900 Nov 27, 2013
e31e463
Merge pull request #4 from ilucin/feature/server-test#methods
supervitis Nov 27, 2013
b7200a2
Merge pull request #6 from ilucin/parser
supervitis Nov 27, 2013
deb431f
Code maintenance: removed unused variables, fixed indentation, fixed
CastMi Dec 1, 2013
161b333
Added an helper class to send message and to encapsulate the
CastMi Dec 3, 2013
20c436e
Updated client with simple menu
supervitis Dec 4, 2013
59ee21c
Parser added
supervitis Dec 4, 2013
5976257
Parser connected, error on tokens.
supervitis Dec 4, 2013
e0292a7
A better handling of exceptions
CastMi Dec 4, 2013
4db87b8
Avoid stupid crashes of the client.
CastMi Dec 4, 2013
91699c7
Example interface for the parser (needs to be completed)
CastMi Dec 4, 2013
6a0d923
Added a method to send an error message to the client. Added Javadocs
Espanico Dec 4, 2013
e57ad25
FIX: sendCommandCompleteMessage method
Espanico Dec 4, 2013
e2fdfb3
FIX: now the server perfectly receive the query from the client
Espanico Dec 4, 2013
98e3f86
Connected DBMS, fails on parser. Needs interface
supervitis Dec 5, 2013
9bc71e6
Parser integration #1 test
twist900 Dec 7, 2013
8ead8d2
selection clauses and expressions
mladens Dec 8, 2013
64ec01c
moved negation from expressions to clauses
mladens Dec 8, 2013
f2689ec
init queryExec, moved init,offset,label to properties class
mladens Dec 8, 2013
11cbe38
"Grammar enlarged, interface changed"
twist900 Dec 8, 2013
9c19cdc
"Add getters to ProjectionCell"
twist900 Dec 8, 2013
6d0167f
Merge branch 'parser' into feature/db-selection
mladens Dec 8, 2013
152a5e2
"Able to fill ProjectionCell with data"
twist900 Dec 8, 2013
a10690a
Merge branch 'parser' into feature/db-selection
mladens Dec 8, 2013
c568e44
New approach in semanticLayer
w-is-h Dec 11, 2013
ff5818e
Merge remote-tracking branch 'origin/feature/db-selection' into featu…
w-is-h Dec 11, 2013
2616c4f
midwork
mladens Dec 11, 2013
942608b
New implementation of sparql queries
w-is-h Dec 15, 2013
bb44a2b
Merge remote-tracking branch 'origin' into feature/semanticLayer
w-is-h Dec 15, 2013
5b326cd
pre merge
mladens Dec 15, 2013
43d6864
Merge branch 'feature/semanticLayer' into feature/db-selection
mladens Dec 15, 2013
011d198
Fix for RemoteSourceRDF, now it can be compiled
w-is-h Dec 15, 2013
4adbde6
disi
mladens Dec 15, 2013
202cdce
Merge branch 'feature/semanticLayer' of https://github.com/ilucin/rel…
mladens Dec 15, 2013
144506f
demoable query executor
mladens Dec 15, 2013
4fecff7
mock schema object
mladens Dec 16, 2013
cbce4f3
random comment
mladens Dec 16, 2013
cbcd289
merging hell
mladens Dec 16, 2013
212d8b7
Update of HierarchyRDF
w-is-h Dec 17, 2013
df31abf
dasdasd
mladens Dec 17, 2013
e2f66f8
gitignore
mladens Dec 17, 2013
9a8c216
Merge branch 'feature/semanticLayer' into feature/db-selection
mladens Dec 17, 2013
56db387
added mocked SelectObject for driver testing
mladens Dec 17, 2013
cd0954f
added sort flag to properties
mladens Dec 17, 2013
b53309a
dsa
mladens Dec 18, 2013
0439796
physically merged for beta
mladens Dec 18, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ tmp/**/*
out/*
bin/**

src/META-INF
src/META-INF/*

.idea/*
.project
.metadata
Expand Down
11 changes: 6 additions & 5 deletions src/com/etk/data/DataSource.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.etk.data;

import com.etk.data.query.Expression;
import com.etk.data.query.Properties;
import com.etk.manager.schema.Attribute;
import com.etk.manager.schema.Type;
import com.etk.manager.schema.mappings.AttributeMapping;
Expand All @@ -15,10 +17,9 @@
* To change this template use File | Settings | File Templates.
*/
public interface DataSource {

public List<Object> getEntityCandidates(int limit, int offset, boolean label);
public List<Object> getAttributes(String entity, int limit, int offset, boolean label);
public List<Class> getType(String entity, String attribute);
public List<Object> getValues(String entity, String attributes[], int limit, int offset);
public List<Object> getEntityCandidates(Properties queryProperties);
public List<Object> getAttributes(String entity, Properties queryProperties);
public String getType(String entity, String attribute);
public List<Object> getValues(String entity, String attributes[], Properties queryProperties, Expression whereExpression);
public List<String> getAvailableEndpoints();
}
280 changes: 280 additions & 0 deletions src/com/etk/data/RemoteSourceHierarchyRDF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
package com.etk.data;

import java.util.ArrayList;
import java.util.List;



import com.etk.data.query.Expression;
import com.etk.data.query.Operator;
import com.etk.data.query.OperatorMapping;
import com.etk.data.query.Properties;
import com.etk.data.query.SelectionClause;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.reasoner.rulesys.builtins.Regex;

public class RemoteSourceHierarchyRDF implements DataSource{
String service;
String queryStringTemplate;
String defaultDataSetName;

public RemoteSourceHierarchyRDF( String service ){
this.service = service;
this.defaultDataSetName = null;
queryStringTemplate = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> ";
}

public RemoteSourceHierarchyRDF( String service, String defaultDataSetName ){
this(service);
this.defaultDataSetName = defaultDataSetName;
}


@Override
public List<Object> getEntityCandidates(Properties queryProperties) {
String queryString = queryStringTemplate;
queryString += "SELECT DISTINCT (count(?o) as ?num) ?o ";
if(defaultDataSetName != null){
queryString += "FROM <" + defaultDataSetName + "> ";
}
queryString += "WHERE { ";

if( queryProperties.getEntitySuperClass() != null ){
queryString += "?o rdfs:subClassOf{" + queryProperties.getDepthFrom() + "," +
queryProperties.getDepthTo() + "} " +
"<" + queryProperties.getEntitySuperClass() + ">. }";
}
else{
queryString += "?s rdfs:subClassOf ?o. " +
"FILTER NOT EXISTS { ?o rdfs:subClassOf ?o2 }. }";
}

//Add order by and sort by
queryString += " GROUP BY ?o ORDER BY DESC(?num)";

if( queryProperties.getLimit() != 0 ){
queryString += " LIMIT " + Integer.toString( queryProperties.getLimit() );
}
if( queryProperties.getOffset() != 0 ){
queryString += " OFFSET " + Integer.toString( queryProperties.getOffset() );
}
System.out.println(queryString);
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryExecution queryExecution = QueryExecutionFactory.createServiceRequest(
service, query );
ResultSet resultSet = queryExecution.execSelect();

return entityCandidatesFromRS(resultSet, queryProperties.isLabel());
}


private List<Object> entityCandidatesFromRS(ResultSet resultSet, boolean label){
List<Object> entityCandidates = new ArrayList<Object>();

QuerySolution qs;
while( resultSet.hasNext() ){
qs = resultSet.next();
entityCandidates.add( new EntityCandidate(qs.get("o").toString()) );
}
return entityCandidates;
}

@Override
public List<Object> getAttributes(String entity, Properties queryProperties) {
String queryString = queryStringTemplate;
queryString += "Select (count(?p) as ?num) ?p ";
if(defaultDataSetName != null){
queryString += "FROM <" + defaultDataSetName + "> ";
}
queryString += " WHERE { ?s rdf:type/rdfs:subClassOf* <" + entity + ">." +
" ?s ?p ?o." +
" ?p a rdf:Property.}";

//Add group by and order by
queryString += "group by ?p order by desc(?num) ";

if( queryProperties.getLimit() != 0 ){
queryString += " LIMIT " + Integer.toString( queryProperties.getLimit() );
}
if( queryProperties.getOffset() != 0 ){
queryString += " OFFSET " + Integer.toString( queryProperties.getOffset() );
}
System.out.println(queryString);
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryExecution queryExecution = QueryExecutionFactory.createServiceRequest(
service, query );
ResultSet resultSet = queryExecution.execSelect();

return atributesFromRS(resultSet, queryProperties.isLabel());
}

private List<Object> atributesFromRS(ResultSet resultSet, boolean label){
List<Object> atributeCandidates = new ArrayList<Object>();

QuerySolution qs;
while( resultSet.hasNext() ){
qs = resultSet.next();

atributeCandidates.add( new AttributeCandidate(qs.get("p").toString()) );
}
return atributeCandidates;
}

@Override
public String getType(String entity, String attribute) {
String type = "string";
String attributes[] = {attribute};
Properties queryProperties = new Properties();
queryProperties.setLimit(1);

List<Object> values = getValues(entity, attributes, queryProperties, null);
ValueCandidate vCan = (ValueCandidate) values.get(0);
String toTest = vCan.getValues().get(attribute);

try{
Integer.parseInt(toTest);
type = "integer";
}
catch(Exception e){
//Do nothing
}

try{
Double.parseDouble(toTest);
type = "real";
}
catch(Exception e){
//Do nothing
}

// TODO Auto-generated method stub
return type;
}

@Override
public List<Object> getValues(String entity, String attributes[], Properties queryProperties, Expression whereExpression) {
String queryString = queryStringTemplate;
queryString += "Select distinct ?s " + repeateString("?o", attributes.length) +
" WHERE { ?s rdf:type/rdfs:subClassOf* <" + entity + ">.";

for(int i = 0; i < attributes.length; i++){
queryString += "?s" + " <" + attributes[i] + "> " + "?o" + i + ".";
}

if(whereExpression != null){
String operatorOuter = OperatorMapping.getOperator(whereExpression.getOperator());
String operatorInner;

List<SelectionClause> clauses = whereExpression.getClauses();

String toAppend = "FILTER( ";
for(SelectionClause one : clauses){
if( one.isNegated() ){
toAppend += "!";
}
toAppend += "( ";

if( one.getOperator() != Operator.LIKE ){
operatorInner = OperatorMapping.getOperator(one.getOperator());
toAppend += "xsd:double(" + variableForAttribute(attributes, one.getAttribute()) +
")" + operatorInner + one.getValue() + ") ";
}
else{
toAppend += "regex(" + variableForAttribute(attributes, one.getAttribute()) +
"," + regexFromLike(one.getValue().toString()) + "))";

}

toAppend += operatorOuter;
}
toAppend = toAppend.substring(0, toAppend.length() - operatorOuter.length()) + ")";
System.out.println(toAppend);
queryString += toAppend;
}


queryString += "}";
/* For now I will disable order by, because it is very slow
if( limit != 0 && offset != 0 ){
queryString += " ORDER BY ?o ";
}
*/


if( queryProperties.getLimit() != 0 ){
queryString += " LIMIT " + Integer.toString( queryProperties.getLimit() );
}
if( queryProperties.getOffset() != 0 ){
queryString += " OFFSET " + Integer.toString( queryProperties.getOffset() );
}
System.out.println(queryString);
Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryExecution queryExecution = QueryExecutionFactory.createServiceRequest(
service, query );
ResultSet resultSet = queryExecution.execSelect();

return valuesFromRS(resultSet, attributes);
}

private String regexFromLike(String sqlString){
String tmp = sqlString;

tmp = tmp.replaceAll("%", ".*");
tmp = tmp.replaceAll("_", ".");

return "'^" + tmp + "$'";
}

private String variableForAttribute(String attributes[], String attribute){
int index = java.util.Arrays.asList(attributes).indexOf(attribute);
return "?o" + index;
}

private List<Object> valuesFromRS(ResultSet resultSet, String attributes[] ){
List<Object> valueCandidates = new ArrayList<Object>();
int numOfAttributes = attributes.length;

QuerySolution qs;
ValueCandidate valueCandidate;
RDFNode node;
while( resultSet.hasNext() ){
qs = resultSet.next();
valueCandidate = new ValueCandidate(qs.get("s").toString());

for(int i = 0; i < numOfAttributes; i++){
node = qs.get(String.format("?oo%d", i));
valueCandidate.addOneValue(node.toString(), attributes[i]);
}

valueCandidates.add(valueCandidate);
}


return valueCandidates;
}

private String repeateString(String s, int n){
String out = " ";

for(int i = 0; i < n; i++){
out += "(str(" + s + i + ") AS " + s + 'o' + i + ") ";
}

return out;
}

public List<String> getAvailableEndpoints(){
return null;
}


}
Loading