Changed tabs to spaces.

Create luceneIndex directory in workspace's cache dir.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1091 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-07-08 23:39:53 +00:00
parent 513283d4d1
commit c041a1966e
24 changed files with 1040 additions and 1037 deletions

View File

@ -12,5 +12,6 @@
<classpathentry kind="lib" path="module/MOD-INF/lib/lucene-core-3.0.1.jar"/> <classpathentry kind="lib" path="module/MOD-INF/lib/lucene-core-3.0.1.jar"/>
<classpathentry kind="lib" path="module/MOD-INF/lib/xercesImpl-2.7.1.jar"/> <classpathentry kind="lib" path="module/MOD-INF/lib/xercesImpl-2.7.1.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/gridworks-server"/> <classpathentry combineaccessrules="false" kind="src" path="/gridworks-server"/>
<classpathentry kind="lib" path="/gridworks/webapp/WEB-INF/lib/butterfly-trunk.jar" sourcepath="/gridworks/webapp/WEB-INF/lib-src/butterfly-trunk.jar"/>
<classpathentry kind="output" path="module/MOD-INF/classes"/> <classpathentry kind="output" path="module/MOD-INF/classes"/>
</classpath> </classpath>

View File

@ -14,26 +14,26 @@ import com.metaweb.gridworks.model.Row;
public class CellBlankNode extends ResourceNode{ public class CellBlankNode extends ResourceNode{
final public int columnIndex; final public int columnIndex;
final public String columnName; final public String columnName;
public CellBlankNode(int i,String columnName){ public CellBlankNode(int i,String columnName){
this.columnIndex = i; this.columnIndex = i;
this.columnName = columnName; this.columnName = columnName;
} }
@Override @Override
public Resource createResource(URI baseUri, Model model, Project project, public Resource createResource(URI baseUri, Model model, Project project,
Row row, int rowIndex,Resource[] blanks) { Row row, int rowIndex,Resource[] blanks) {
return model.createResource(); return model.createResource();
} }
@Override @Override
protected void writeNode(JSONWriter writer, Properties options) protected void writeNode(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.key("nodeType");writer.value("cell-as-blank"); writer.key("nodeType");writer.value("cell-as-blank");
writer.key("columnIndex");writer.value(columnIndex); writer.key("columnIndex");writer.value(columnIndex);
writer.key("columnName");writer.value(columnName); writer.key("columnName");writer.value(columnName);
} }
} }

View File

@ -15,64 +15,64 @@ import com.metaweb.gridworks.model.Row;
public class CellLiteralNode extends CellNode{ public class CellLiteralNode extends CellNode{
private String valueType; private String valueType;
private String lang; private String lang;
public String getValueType() { public String getValueType() {
return valueType; return valueType;
} }
public void setValueType(String valueType) { public void setValueType(String valueType) {
this.valueType = valueType; this.valueType = valueType;
} }
public String getLang() { public String getLang() {
return lang; return lang;
} }
public void setLang(String lang) { public void setLang(String lang) {
this.lang = lang; this.lang = lang;
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("nodeType");writer.value("cell-as-literal"); writer.key("nodeType");writer.value("cell-as-literal");
writer.key("columnIndex");writer.value(columnIndex); writer.key("columnIndex");writer.value(columnIndex);
writer.key("columnName");writer.value(columnName); writer.key("columnName");writer.value(columnName);
if(valueType!=null){ if(valueType!=null){
writer.key("valueType");writer.value(valueType); writer.key("valueType");writer.value(valueType);
} }
if(lang!=null){ if(lang!=null){
writer.key("lang");writer.value(lang); writer.key("lang");writer.value(lang);
} }
writer.endObject(); writer.endObject();
} }
public CellLiteralNode(int index,String columnName){ public CellLiteralNode(int index,String columnName){
super(index,columnName); super(index,columnName);
} }
public CellLiteralNode(int index,String columnName,String valueType,String lang){ public CellLiteralNode(int index,String columnName,String valueType,String lang){
this(index,columnName); this(index,columnName);
this.lang = lang; this.lang = lang;
this.valueType = valueType; this.valueType = valueType;
} }
public RDFNode createNode(URI baseUri, Model model, Project project, public RDFNode createNode(URI baseUri, Model model, Project project,
Row row, int rowIndex,Resource[] blanks) { Row row, int rowIndex,Resource[] blanks) {
String val; String val;
try{ try{
val= row.getCell(this.columnIndex).value.toString(); val= row.getCell(this.columnIndex).value.toString();
}catch(NullPointerException ne){ }catch(NullPointerException ne){
return null; return null;
} }
if(val!=null && val.length()>0){ if(val!=null && val.length()>0){
//TODO language and datatype //TODO language and datatype
Literal l; Literal l;
if(this.valueType!=null){ if(this.valueType!=null){
l = model.createTypedLiteral(val, valueType); l = model.createTypedLiteral(val, valueType);
}else{ }else{
l = model.createLiteral(val); l = model.createLiteral(val);
} }
return l; return l;
}else{ }else{
return null; return null;
} }
} }
} }

View File

@ -1,12 +1,12 @@
package com.metaweb.gridworks.rdf; package com.metaweb.gridworks.rdf;
public abstract class CellNode implements Node{ public abstract class CellNode implements Node{
final public int columnIndex; final public int columnIndex;
final public String columnName; final public String columnName;
public CellNode(int i,String columnName){ public CellNode(int i,String columnName){
this.columnIndex = i; this.columnIndex = i;
this.columnName = columnName; this.columnName = columnName;
} }
} }

View File

@ -19,62 +19,62 @@ import com.metaweb.gridworks.model.Row;
public class CellResourceNode extends ResourceNode{ public class CellResourceNode extends ResourceNode{
private String uriExpression; private String uriExpression;
final public String columnName; final public String columnName;
public String getUriExpression() { public String getUriExpression() {
return uriExpression; return uriExpression;
} }
final public int columnIndex; final public int columnIndex;
public CellResourceNode(int i,String columnName){ public CellResourceNode(int i,String columnName){
this.columnIndex = i; this.columnIndex = i;
this.columnName = columnName; this.columnName = columnName;
} }
public CellResourceNode(int columnIndex,String columnName,String exp) { public CellResourceNode(int columnIndex,String columnName,String exp) {
this(columnIndex,columnName); this(columnIndex,columnName);
this.uriExpression = exp; this.uriExpression = exp;
} }
@Override @Override
public Resource createResource(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks) { public Resource createResource(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks) {
try{ try{
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
Evaluable eval = MetaParser.parse(uriExpression); Evaluable eval = MetaParser.parse(uriExpression);
Cell cell = row.getCell(this.columnIndex); Cell cell = row.getCell(this.columnIndex);
String colName = this.columnIndex>-1?project.columnModel.getColumnByCellIndex(this.columnIndex).getName():""; String colName = this.columnIndex>-1?project.columnModel.getColumnByCellIndex(this.columnIndex).getName():"";
ExpressionUtils.bind(bindings, row, rowIndex,colName , cell); ExpressionUtils.bind(bindings, row, rowIndex,colName , cell);
Object result = eval.evaluate(bindings); Object result = eval.evaluate(bindings);
if(result.toString().length()>0){ if(result.toString().length()>0){
String uri = Util.getUri(baseUri, result.toString()); String uri = Util.getUri(baseUri, result.toString());
Resource r = model.createResource(uri); Resource r = model.createResource(uri);
return r; return r;
}else{ }else{
return null; return null;
} }
}catch(Exception e){ }catch(Exception e){
// e.printStackTrace(); // e.printStackTrace();
//an empty cell might result in an exception out of evaluating URI expression... so it is intended to eat the exception //an empty cell might result in an exception out of evaluating URI expression... so it is intended to eat the exception
return null; return null;
} }
} }
@Override @Override
protected void writeNode(JSONWriter writer, Properties options) protected void writeNode(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.key("nodeType"); writer.value("cell-as-resource"); writer.key("nodeType"); writer.value("cell-as-resource");
writer.key("uriExpression"); writer.value(uriExpression); writer.key("uriExpression"); writer.value(uriExpression);
writer.key("columnIndex"); writer.value(columnIndex); writer.key("columnIndex"); writer.value(columnIndex);
if(columnIndex==-1){ if(columnIndex==-1){
//Row number //Row number
writer.key("isRowNumberCell"); writer.value(true); writer.key("isRowNumberCell"); writer.value(true);
} else { } else {
writer.key("columnName"); writer.value(columnName); writer.key("columnName"); writer.value(columnName);
} }
} }
} }

View File

@ -13,21 +13,21 @@ import com.metaweb.gridworks.model.Row;
public class ConstantBlankNode extends ResourceNode{ public class ConstantBlankNode extends ResourceNode{
private int _id; private int _id;
ConstantBlankNode(int id){ ConstantBlankNode(int id){
this._id = id; this._id = id;
} }
@Override @Override
public Resource createResource(URI baseUri, Model model, Project project, public Resource createResource(URI baseUri, Model model, Project project,
Row row, int rowIndex,Resource[] blanks) { Row row, int rowIndex,Resource[] blanks) {
return blanks[this._id]; return blanks[this._id];
} }
@Override @Override
protected void writeNode(JSONWriter writer, Properties options) protected void writeNode(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.key("nodeType");writer.value("blank"); writer.key("nodeType");writer.value("blank");
} }
} }

View File

@ -15,75 +15,75 @@ import com.metaweb.gridworks.model.Row;
public class ConstantLiteralNode implements Node{ public class ConstantLiteralNode implements Node{
private String valueType; private String valueType;
private String lang; private String lang;
private String value; private String value;
public ConstantLiteralNode(String val,String type,String l){ public ConstantLiteralNode(String val,String type,String l){
this.value = val; this.value = val;
this.valueType = type; this.valueType = type;
this.lang = l; this.lang = l;
} }
public String getValueType() { public String getValueType() {
return valueType; return valueType;
} }
public void setValueType(String valueType) { public void setValueType(String valueType) {
this.valueType = valueType; this.valueType = valueType;
} }
public String getLang() { public String getLang() {
return lang; return lang;
} }
public void setLang(String lang) { public void setLang(String lang) {
this.lang = lang; this.lang = lang;
} }
public String getValue() { public String getValue() {
return value; return value;
} }
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("nodeType"); writer.value("literal"); writer.key("nodeType"); writer.value("literal");
writer.key("value"); writer.value(value); writer.key("value"); writer.value(value);
if(valueType!=null){ if(valueType!=null){
writer.key("valueType"); writer.key("valueType");
writer.value(valueType); writer.value(valueType);
} }
if(lang!=null){ if(lang!=null){
writer.key("lang"); writer.key("lang");
writer.value(lang); writer.value(lang);
} }
writer.endObject(); writer.endObject();
} }
public RDFNode createNode(URI baseUri, Model model, Project project, public RDFNode createNode(URI baseUri, Model model, Project project,
Row row, int rowIndex,Resource[] blanks) { Row row, int rowIndex,Resource[] blanks) {
if(this.value!=null && this.value.length()>0){ if(this.value!=null && this.value.length()>0){
Literal l ; Literal l ;
if(this.valueType!=null){ if(this.valueType!=null){
l = model.createTypedLiteral(this.value, valueType); l = model.createTypedLiteral(this.value, valueType);
}else{ }else{
l = model.createLiteral(this.value); l = model.createLiteral(this.value);
} }
return l; return l;
}else{ }else{
return null; return null;
} }
} }
} }

View File

@ -15,46 +15,46 @@ import com.metaweb.gridworks.model.Row;
public class ConstantResourceNode extends ResourceNode{ public class ConstantResourceNode extends ResourceNode{
private String uri; private String uri;
public String getUri() { public String getUri() {
return uri; return uri;
} }
public void setUri(String uri) { public void setUri(String uri) {
this.uri = uri; this.uri = uri;
} }
public ConstantResourceNode(String uri){ public ConstantResourceNode(String uri){
this.uri = uri; this.uri = uri;
} }
@Override @Override
public Resource createResource(URI baseUri, Model model, Project project, public Resource createResource(URI baseUri, Model model, Project project,
Row row, int rowIndex,Resource[] blanks) { Row row, int rowIndex,Resource[] blanks) {
if(this.uri!=null & this.uri.length()>0){ if(this.uri!=null & this.uri.length()>0){
String tmp; String tmp;
try { try {
tmp = Util.getUri(baseUri, this.uri); tmp = Util.getUri(baseUri, this.uri);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
Resource r = model.createResource(tmp); Resource r = model.createResource(tmp);
return r; return r;
}else{ }else{
return null; return null;
} }
} }
@Override @Override
protected void writeNode(JSONWriter writer, Properties options) protected void writeNode(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.key("nodeType"); writer.value("resource"); writer.key("nodeType"); writer.value("resource");
writer.key("uri"); writer.value(uri); writer.key("uri"); writer.value(uri);
} }
} }

View File

@ -9,28 +9,28 @@ import com.metaweb.gridworks.Jsonizable;
public class Link implements Jsonizable { public class Link implements Jsonizable {
public final String propertyUri; public final String propertyUri;
public final String curie; public final String curie;
public final Node target; public final Node target;
public Link(String uri,String curie,Node t){ public Link(String uri,String curie,Node t){
this.propertyUri = uri; this.propertyUri = uri;
this.target = t; this.target = t;
this.curie = curie; this.curie = curie;
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("uri"); writer.key("uri");
writer.value(propertyUri); writer.value(propertyUri);
writer.key("curie"); writer.key("curie");
writer.value(curie); writer.value(curie);
if (target != null) { if (target != null) {
writer.key("target"); writer.key("target");
target.write(writer, options); target.write(writer, options);
} }
writer.endObject(); writer.endObject();
} }
} }

View File

@ -11,5 +11,5 @@ import com.metaweb.gridworks.model.Row;
public interface Node extends Jsonizable{ public interface Node extends Jsonizable{
RDFNode createNode(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks); RDFNode createNode(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks);
} }

View File

@ -20,114 +20,114 @@ import com.metaweb.gridworks.model.Row;
abstract public class ResourceNode implements Node { abstract public class ResourceNode implements Node {
private List<Link> links = new ArrayList<Link>(); private List<Link> links = new ArrayList<Link>();
private List<RdfType> rdfTypes = new ArrayList<RdfType>(); private List<RdfType> rdfTypes = new ArrayList<RdfType>();
public void addLink(Link link) { public void addLink(Link link) {
this.links.add(link); this.links.add(link);
} }
public void addType(RdfType type) { public void addType(RdfType type) {
this.rdfTypes.add(type); this.rdfTypes.add(type);
} }
public Link getLink(int index) { public Link getLink(int index) {
return this.links.get(index); return this.links.get(index);
} }
public int getLinkCount() { public int getLinkCount() {
return this.links.size(); return this.links.size();
} }
public List<RdfType> getTypes() { public List<RdfType> getTypes() {
return this.rdfTypes; return this.rdfTypes;
} }
protected abstract void writeNode(JSONWriter writer, Properties options) throws JSONException; protected abstract void writeNode(JSONWriter writer, Properties options) throws JSONException;
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
//writer node //writer node
writeNode(writer,options); writeNode(writer,options);
//write types //write types
writer.key("rdfTypes"); writer.key("rdfTypes");
writer.array(); writer.array();
for(RdfType type:this.getTypes()){ for(RdfType type:this.getTypes()){
writer.object(); writer.object();
writer.key("uri");writer.value(type.uri); writer.key("uri");writer.value(type.uri);
writer.key("curie");writer.value(type.curie); writer.key("curie");writer.value(type.curie);
writer.endObject(); writer.endObject();
} }
writer.endArray(); writer.endArray();
//write links //write links
writer.key("links"); writer.key("links");
writer.array(); writer.array();
for(int i=0;i<getLinkCount();i++){ for(int i=0;i<getLinkCount();i++){
Link l = getLink(i); Link l = getLink(i);
/*writer.object(); /*writer.object();
writer.key("uri");writer.value(l.propertyUri); writer.key("uri");writer.value(l.propertyUri);
writer.key("target"); writer.key("target");
l.target.write(writer, options); l.target.write(writer, options);
writer.endObject();*/ writer.endObject();*/
l.write(writer, options); l.write(writer, options);
} }
writer.endArray(); writer.endArray();
writer.endObject(); writer.endObject();
} }
protected void addTypes(Resource r,Model model){ protected void addTypes(Resource r,Model model){
for(RdfType type:this.getTypes()){ for(RdfType type:this.getTypes()){
r.addProperty(RDF.type, model.createResource(type.uri)); r.addProperty(RDF.type, model.createResource(type.uri));
} }
} }
protected Resource addLinks(Resource r,URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks){ protected Resource addLinks(Resource r,URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks){
for(int i=0;i<getLinkCount();i++){ for(int i=0;i<getLinkCount();i++){
Link l = getLink(i); Link l = getLink(i);
String propertyUri; String propertyUri;
try { try {
propertyUri = Util.getUri(baseUri, l.propertyUri); propertyUri = Util.getUri(baseUri, l.propertyUri);
Property p = model.createProperty(propertyUri); Property p = model.createProperty(propertyUri);
RDFNode o = l.target.createNode(baseUri, model, project, row, rowIndex,blanks); RDFNode o = l.target.createNode(baseUri, model, project, row, rowIndex,blanks);
if(o!=null){ if(o!=null){
r.addProperty(p, o); r.addProperty(p, o);
} }
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
return null; return null;
} }
} }
return r; return r;
} }
public void setTypes(List<RdfType> types) { public void setTypes(List<RdfType> types) {
this.rdfTypes = types; this.rdfTypes = types;
} }
public RDFNode createNode(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks) { public RDFNode createNode(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks) {
Resource r = createResource(baseUri, model, project, row, rowIndex,blanks); Resource r = createResource(baseUri, model, project, row, rowIndex,blanks);
if(r==null){ if(r==null){
return null; return null;
} }
addTypes(r, model); addTypes(r, model);
return addLinks(r,baseUri,model,project,row,rowIndex,blanks); return addLinks(r,baseUri,model,project,row,rowIndex,blanks);
} }
public abstract Resource createResource(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks) ; public abstract Resource createResource(URI baseUri,Model model,Project project,Row row,int rowIndex,Resource[] blanks) ;
public static class RdfType{ public static class RdfType{
String uri; String uri;
String curie; String curie;
public RdfType(String uri,String curie){ public RdfType(String uri,String curie){
this.uri = uri; this.uri = uri;
this.curie = curie; this.curie = curie;
} }
} }
} }

View File

@ -16,27 +16,27 @@ import com.metaweb.gridworks.rdf.vocab.VocabularyManager;
public class DeleteVocabularyCommand extends Command{ public class DeleteVocabularyCommand extends Command{
@Override @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
String uri = request.getParameter("uri"); String uri = request.getParameter("uri");
try { try {
VocabularyManager.singleton.deleteVocabulary(uri); VocabularyManager.getSingleton(servlet).deleteVocabulary(uri);
respondJSON(response, new Jsonizable() { respondJSON(response, new Jsonizable() {
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("code"); writer.value("ok"); writer.key("code"); writer.value("ok");
writer.endObject(); writer.endObject();
} }
}); });
} catch (JSONException e) { } catch (JSONException e) {
respondException(response, e); respondException(response, e);
} catch (Exception e){ } catch (Exception e){
respondException(response, e); respondException(response, e);
} }
} }
} }

View File

@ -17,32 +17,32 @@ import com.metaweb.gridworks.rdf.vocab.VocabularyManager;
public class ImportVocabularyCommand extends Command{ public class ImportVocabularyCommand extends Command{
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
String prefix = request.getParameter("prefix"); String prefix = request.getParameter("prefix");
String url = request.getParameter("url"); String url = request.getParameter("url");
String namespace = request.getParameter("namespace"); String namespace = request.getParameter("namespace");
try { try {
VocabularyManager.singleton.addVocabulary(url, prefix, namespace); VocabularyManager.getSingleton(servlet).addVocabulary(url, prefix, namespace);
respondJSON(response, new Jsonizable() { respondJSON(response, new Jsonizable() {
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("code"); writer.value("ok"); writer.key("code"); writer.value("ok");
writer.endObject(); writer.endObject();
} }
}); });
} catch (JSONException e) { } catch (JSONException e) {
respondException(response, e); respondException(response, e);
} catch (VocabularyExistException e) { } catch (VocabularyExistException e) {
respondException(response, e); respondException(response, e);
} catch (Exception e){ } catch (Exception e){
respondException(response, e); respondException(response, e);
} }
} }
} }

View File

@ -14,24 +14,24 @@ import com.metaweb.gridworks.rdf.vocab.Vocabulary;
import com.metaweb.gridworks.rdf.vocab.VocabularyManager; import com.metaweb.gridworks.rdf.vocab.VocabularyManager;
public class ListVocabulariesCommand extends Command{ public class ListVocabulariesCommand extends Command{
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");
try{ try{
JSONWriter writer = new JSONWriter(response.getWriter()); JSONWriter writer = new JSONWriter(response.getWriter());
writer.object(); writer.object();
writer.key("vocabularies"); writer.key("vocabularies");
writer.array(); writer.array();
Properties p = new Properties(); Properties p = new Properties();
for(Vocabulary v:VocabularyManager.singleton.getVocabularies()){ for(Vocabulary v:VocabularyManager.getSingleton(servlet).getVocabularies()){
v.write(writer, p); v.write(writer, p);
} }
writer.endArray(); writer.endArray();
writer.endObject(); writer.endObject();
} catch (Exception e) { } catch (Exception e) {
respondException(response, e); respondException(response, e);
} }
} }
} }

View File

@ -19,7 +19,7 @@ import com.metaweb.gridworks.util.ParsingUtilities;
public class SaveRdfSchemaCommand extends Command{ public class SaveRdfSchemaCommand extends Command{
@Override @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {

View File

@ -19,81 +19,81 @@ import com.metaweb.gridworks.rdf.vocab.VocabularyManager;
public class SuggestTermCommand extends Command{ public class SuggestTermCommand extends Command{
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");
JSONWriter writer = new JSONWriter(response.getWriter()); JSONWriter writer = new JSONWriter(response.getWriter());
String type = request.getParameter("type_strict"); String type = request.getParameter("type_strict");
String prefix = request.getParameter("prefix"); String prefix = request.getParameter("prefix");
try{ try{
writer.object(); writer.object();
writer.key("prefix"); writer.key("prefix");
writer.value(prefix); writer.value(prefix);
writer.key("result"); writer.key("result");
writer.array(); writer.array();
List<RDFNode> nodes; List<RDFNode> nodes;
if(type!=null && type.trim().equals("property")){ if(type!=null && type.trim().equals("property")){
nodes = VocabularyManager.singleton.searchProperties(prefix); nodes = VocabularyManager.getSingleton(servlet).searchProperties(prefix);
}else{ }else{
nodes = VocabularyManager.singleton.searchClasses(prefix); nodes = VocabularyManager.getSingleton(servlet).searchClasses(prefix);
} }
for(RDFNode c:nodes){ for(RDFNode c:nodes){
c.writeAsSearchResult(writer); c.writeAsSearchResult(writer);
} }
writer.endArray(); writer.endArray();
writer.endObject(); writer.endObject();
}catch(Exception e){ }catch(Exception e){
e.printStackTrace(); e.printStackTrace();
throw new ServletException(e); throw new ServletException(e);
} }
} }
} }
class Result implements Jsonizable{ class Result implements Jsonizable{
private List<String[]> results = new ArrayList<String[]>(); private List<String[]> results = new ArrayList<String[]>();
private String prefix; private String prefix;
Result(String p){ Result(String p){
this.prefix = p; this.prefix = p;
} }
void addResult(String id, String name){ void addResult(String id, String name){
String[] res = new String[] {id,name}; String[] res = new String[] {id,name};
results.add(res); results.add(res);
} }
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("prefix"); writer.key("prefix");
writer.value(prefix); writer.value(prefix);
writer.key("result"); writer.key("result");
writer.array(); writer.array();
for(String[] res:results){ for(String[] res:results){
writer.object(); writer.object();
writer.key("id"); writer.key("id");
writer.value(res[0]); writer.value(res[0]);
writer.key("name"); writer.key("name");
writer.value(res[1]); writer.value(res[1]);
writer.endObject(); writer.endObject();
} }
writer.endArray(); writer.endArray();
writer.endObject(); writer.endObject();
} }
} }

View File

@ -15,37 +15,37 @@ import com.metaweb.gridworks.gel.Function;
public class Urlify implements Function { public class Urlify implements Function {
public Object call(Properties bindings, Object[] args) { public Object call(Properties bindings, Object[] args) {
if(args.length==1 || args.length==2){ if(args.length==1 || args.length==2){
String s = args[0].toString(); String s = args[0].toString();
s = s.replaceAll("\\s+", "_"); s = s.replaceAll("\\s+", "_");
if(args.length==2){ if(args.length==2){
String base = args[1].toString(); String base = args[1].toString();
URI base_uri; URI base_uri;
try { try {
base_uri = new URI(base); base_uri = new URI(base);
return base_uri.resolve(s).toString(); return base_uri.resolve(s).toString();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " unable to encode"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " unable to encode");
} }
} }
try { try {
return URLEncoder.encode(s, "UTF-8"); return URLEncoder.encode(s, "UTF-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " unable to encode"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " unable to encode");
} }
} }
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 string"); return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 string");
} }
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("description"); writer.value("replaces spaces with underscore"); writer.key("description"); writer.value("replaces spaces with underscore");
writer.key("params"); writer.value("string s"); writer.key("params"); writer.value("string s");
writer.key("returns"); writer.value("string"); writer.key("returns"); writer.value("string");
writer.endObject(); writer.endObject();
} }
} }

View File

@ -8,123 +8,123 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.Jsonizable;
public abstract class RDFNode implements Jsonizable{ public abstract class RDFNode implements Jsonizable{
private String preferredCURIE; private String preferredCURIE;
private String description; private String description;
private String URI; private String URI;
private String label; private String label;
private String vocabularyPrefix; private String vocabularyPrefix;
private String vocabularyUri; private String vocabularyUri;
public String getVocabularyUri() { public String getVocabularyUri() {
return vocabularyUri; return vocabularyUri;
} }
public void setVocabularyUri(String vocabularyUri) { public void setVocabularyUri(String vocabularyUri) {
this.vocabularyUri = vocabularyUri; this.vocabularyUri = vocabularyUri;
} }
public String getVocabularyPrefix() { public String getVocabularyPrefix() {
return vocabularyPrefix; return vocabularyPrefix;
} }
public void setVocabularyPrefix(String vocabularyPrefix) { public void setVocabularyPrefix(String vocabularyPrefix) {
this.vocabularyPrefix = vocabularyPrefix; this.vocabularyPrefix = vocabularyPrefix;
} }
public String getPreferredCURIE() { public String getPreferredCURIE() {
return preferredCURIE; return preferredCURIE;
} }
public void setPreferredCURIE(String preferredCURIE) { public void setPreferredCURIE(String preferredCURIE) {
this.preferredCURIE = preferredCURIE; this.preferredCURIE = preferredCURIE;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
public String getURI() { public String getURI() {
return URI; return URI;
} }
public void setURI(String uRI) { public void setURI(String uRI) {
URI = uRI; URI = uRI;
} }
public String getLabel() { public String getLabel() {
return label; return label;
} }
public void setLabel(String label) { public void setLabel(String label) {
this.label = label; this.label = label;
} }
public RDFNode(){ public RDFNode(){
} }
public RDFNode(String description, String uRI, public RDFNode(String description, String uRI,
String label,String prefix,String vocabularyUri) { String label,String prefix,String vocabularyUri) {
this.description = description; this.description = description;
URI = uRI; URI = uRI;
this.label = label; this.label = label;
this.vocabularyPrefix = prefix; this.vocabularyPrefix = prefix;
this.preferredCURIE = composePreferredCurie(); this.preferredCURIE = composePreferredCurie();
this.vocabularyUri = vocabularyUri; this.vocabularyUri = vocabularyUri;
} }
private String composePreferredCurie(){ private String composePreferredCurie(){
String qname; String qname;
if(this.URI==null){ if(this.URI==null){
return null; return null;
} }
if(this.URI.indexOf("#")!=-1){ if(this.URI.indexOf("#")!=-1){
qname = this.URI.substring(this.URI.indexOf("#")+1); qname = this.URI.substring(this.URI.indexOf("#")+1);
}else{ }else{
qname = this.URI.substring(this.URI.lastIndexOf("/")+1); qname = this.URI.substring(this.URI.lastIndexOf("/")+1);
} }
return this.vocabularyPrefix + ":" + qname; return this.vocabularyPrefix + ":" + qname;
} }
public abstract String getType(); public abstract String getType();
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("type"); writer.key("type");
writer.value(this.getType()); writer.value(this.getType());
writer.key("prefix"); writer.key("prefix");
writer.value(vocabularyPrefix); writer.value(vocabularyPrefix);
writer.key("preferredCURIE"); writer.key("preferredCURIE");
writer.value(this.preferredCURIE); writer.value(this.preferredCURIE);
writer.key("label"); writer.key("label");
writer.value(label); writer.value(label);
writer.key("description"); writer.key("description");
writer.value(description); writer.value(description);
writer.key("URI"); writer.key("URI");
writer.value(URI); writer.value(URI);
writer.endObject(); writer.endObject();
} }
public void writeAsSearchResult(JSONWriter writer)throws JSONException { public void writeAsSearchResult(JSONWriter writer)throws JSONException {
writer.object(); writer.object();
writer.key("id"); writer.key("id");
writer.value(URI); writer.value(URI);
writer.key("name"); writer.key("name");
writer.value(preferredCURIE); writer.value(preferredCURIE);
writer.key("description"); writer.key("description");
writer.value(description); writer.value(description);
writer.endObject(); writer.endObject();
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if(!(obj instanceof RDFNode)) return false; if(!(obj instanceof RDFNode)) return false;
RDFNode n = (RDFNode) obj; RDFNode n = (RDFNode) obj;
if(n.getURI()==null || this.URI==null){ if(n.getURI()==null || this.URI==null){
return false; return false;
} }
return this.URI.equals(n.getURI()); return this.URI.equals(n.getURI());
} }
@Override @Override
public int hashCode() { public int hashCode() {
return this.URI.hashCode(); return this.URI.hashCode();
} }
} }

View File

@ -3,14 +3,14 @@ package com.metaweb.gridworks.rdf.vocab;
public class RDFSClass extends RDFNode{ public class RDFSClass extends RDFNode{
@Override @Override
public String getType() { public String getType() {
return "class"; return "class";
} }
public RDFSClass( String uRI, public RDFSClass( String uRI,
String label,String description,String prefix,String vocabularyUri) { String label,String description,String prefix,String vocabularyUri) {
super(description,uRI,label,prefix,vocabularyUri); super(description,uRI,label,prefix,vocabularyUri);
} }
} }

View File

@ -3,12 +3,12 @@ package com.metaweb.gridworks.rdf.vocab;
public class RDFSProperty extends RDFNode{ public class RDFSProperty extends RDFNode{
@Override @Override
public String getType() { public String getType() {
return "property"; return "property";
} }
public RDFSProperty(String uRI, public RDFSProperty(String uRI,
String label,String description,String prefix,String vocabularyUri) { String label,String description,String prefix,String vocabularyUri) {
super(description,uRI,label,prefix,vocabularyUri); super(description,uRI,label,prefix,vocabularyUri);
} }
} }

View File

@ -10,56 +10,56 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.Jsonizable;
public class Vocabulary implements Jsonizable{ public class Vocabulary implements Jsonizable{
private final String name; private final String name;
private final String uri; private final String uri;
private List<RDFSClass> classes = new ArrayList<RDFSClass>(); private List<RDFSClass> classes = new ArrayList<RDFSClass>();
private List<RDFSProperty> properties = new ArrayList<RDFSProperty>(); private List<RDFSProperty> properties = new ArrayList<RDFSProperty>();
public Vocabulary(String name,String uri){ public Vocabulary(String name,String uri){
this.name = name; this.name = name;
this.uri = uri; this.uri = uri;
} }
public void addClass(RDFSClass clazz){ public void addClass(RDFSClass clazz){
this.classes.add(clazz); this.classes.add(clazz);
} }
public void addProperty(RDFSProperty prop){ public void addProperty(RDFSProperty prop){
this.properties.add(prop); this.properties.add(prop);
} }
public List<RDFSClass> getClasses() { public List<RDFSClass> getClasses() {
return classes; return classes;
} }
public void setClasses(List<RDFSClass> classes) { public void setClasses(List<RDFSClass> classes) {
this.classes = classes; this.classes = classes;
} }
public List<RDFSProperty> getProperties() { public List<RDFSProperty> getProperties() {
return properties; return properties;
} }
public void setProperties(List<RDFSProperty> properties) { public void setProperties(List<RDFSProperty> properties) {
this.properties = properties; this.properties = properties;
} }
public String getName() { public String getName() {
return name; return name;
} }
public String getUri() { public String getUri() {
return uri; return uri;
} }
@Override @Override
public void write(JSONWriter writer, Properties options) public void write(JSONWriter writer, Properties options)
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("name"); writer.value(name); writer.key("name"); writer.value(name);
writer.key("uri"); writer.value(uri); writer.key("uri"); writer.value(uri);
writer.endObject(); writer.endObject();
} }
} }

View File

@ -4,7 +4,7 @@ public class VocabularyExistException extends Exception {
private static final long serialVersionUID = 1916094460059608851L; private static final long serialVersionUID = 1916094460059608851L;
public VocabularyExistException(String msg){ public VocabularyExistException(String msg){
super(msg); super(msg);
} }
} }

View File

@ -16,104 +16,104 @@ import com.hp.hpl.jena.rdf.model.ModelFactory;
public class VocabularyImporter { public class VocabularyImporter {
private static final String PREFIXES = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> " + private static final String PREFIXES = "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> " +
"PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX skos:<http://www.w3.org/2004/02/skos/core#> "; "PREFIX skos:<http://www.w3.org/2004/02/skos/core#> ";
private static final String CLASSES_QUERY_P1 = PREFIXES + private static final String CLASSES_QUERY_P1 = PREFIXES +
"SELECT ?resource ?label ?en_label ?description ?en_description ?definition ?en_definition " + "SELECT ?resource ?label ?en_label ?description ?en_description ?definition ?en_definition " +
"WHERE { " + "WHERE { " +
"?resource rdf:type rdfs:Class. " + "?resource rdf:type rdfs:Class. " +
"OPTIONAL {?resource rdfs:label ?label.} " + "OPTIONAL {?resource rdfs:label ?label.} " +
"OPTIONAL {?resource rdfs:label ?en_label. FILTER langMatches( lang(?en_label), \"EN\" ) } " + "OPTIONAL {?resource rdfs:label ?en_label. FILTER langMatches( lang(?en_label), \"EN\" ) } " +
"OPTIONAL {?resource rdfs:comment ?description.} " + "OPTIONAL {?resource rdfs:comment ?description.} " +
"OPTIONAL {?resource rdfs:comment ?en_description. FILTER langMatches( lang(?en_description), \"EN\" ) } " + "OPTIONAL {?resource rdfs:comment ?en_description. FILTER langMatches( lang(?en_description), \"EN\" ) } " +
"OPTIONAL {?resource skos:definition ?definition.} " + "OPTIONAL {?resource skos:definition ?definition.} " +
"OPTIONAL {?resource skos:definition ?en_definition. FILTER langMatches( lang(?en_definition), \"EN\" ) } " + "OPTIONAL {?resource skos:definition ?en_definition. FILTER langMatches( lang(?en_definition), \"EN\" ) } " +
"FILTER regex(str(?resource), \"^"; "FILTER regex(str(?resource), \"^";
private static final String CLASSES_QUERY_P2 = "\")}"; private static final String CLASSES_QUERY_P2 = "\")}";
private static final String PROPERTIES_QUERY_P1 = PREFIXES + private static final String PROPERTIES_QUERY_P1 = PREFIXES +
"SELECT ?resource ?label ?en_label ?description ?en_description ?definition ?en_definition " + "SELECT ?resource ?label ?en_label ?description ?en_description ?definition ?en_definition " +
"WHERE { " + "WHERE { " +
"?resource rdf:type rdf:Property. " + "?resource rdf:type rdf:Property. " +
"OPTIONAL {?resource rdfs:label ?label.} " + "OPTIONAL {?resource rdfs:label ?label.} " +
"OPTIONAL {?resource rdfs:label ?en_label. FILTER langMatches( lang(?en_label), \"EN\" ) } " + "OPTIONAL {?resource rdfs:label ?en_label. FILTER langMatches( lang(?en_label), \"EN\" ) } " +
"OPTIONAL {?resource rdfs:comment ?description.} " + "OPTIONAL {?resource rdfs:comment ?description.} " +
"OPTIONAL {?resource rdfs:comment ?en_description. FILTER langMatches( lang(?en_description), \"EN\" ) } " + "OPTIONAL {?resource rdfs:comment ?en_description. FILTER langMatches( lang(?en_description), \"EN\" ) } " +
"OPTIONAL {?resource skos:definition ?definition.} " + "OPTIONAL {?resource skos:definition ?definition.} " +
"OPTIONAL {?resource skos:definition ?en_definition. FILTER langMatches( lang(?en_definition), \"EN\" ) } " + "OPTIONAL {?resource skos:definition ?en_definition. FILTER langMatches( lang(?en_definition), \"EN\" ) } " +
"FILTER regex(str(?resource), \"^"; "FILTER regex(str(?resource), \"^";
private static final String PROPERTIES_QUERY_P2 = "\")}"; private static final String PROPERTIES_QUERY_P2 = "\")}";
public Vocabulary getVocabulary(String url, String prefix, String namespace, String format){ public Vocabulary getVocabulary(String url, String prefix, String namespace, String format){
Model m = getModel(url, format); Model m = getModel(url, format);
return getVocabulary(m,namespace,prefix); return getVocabulary(m,namespace,prefix);
} }
private Model getModel(String url,String format){ private Model getModel(String url,String format){
Model model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF);//ModelFactory.createDefaultModel(); Model model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF);//ModelFactory.createDefaultModel();
model.read(url); model.read(url);
return model; return model;
} }
private Vocabulary getVocabulary(Model m, String namespace, String prefix){ private Vocabulary getVocabulary(Model m, String namespace, String prefix){
Query query = QueryFactory.create(CLASSES_QUERY_P1 + namespace.trim() + CLASSES_QUERY_P2); Query query = QueryFactory.create(CLASSES_QUERY_P1 + namespace.trim() + CLASSES_QUERY_P2);
QueryExecution qe = QueryExecutionFactory.create(query, m); QueryExecution qe = QueryExecutionFactory.create(query, m);
ResultSet res = qe.execSelect(); ResultSet res = qe.execSelect();
Set<String> seen = new HashSet<String>(); Set<String> seen = new HashSet<String>();
Vocabulary vocab = new Vocabulary(prefix, namespace); Vocabulary vocab = new Vocabulary(prefix, namespace);
while(res.hasNext()){ while(res.hasNext()){
QuerySolution qs = res.nextSolution(); QuerySolution qs = res.nextSolution();
String uri = qs.getResource("resource").getURI(); String uri = qs.getResource("resource").getURI();
if(seen.contains(uri)){ if(seen.contains(uri)){
continue; continue;
} }
String label = getFirstNotNull(new Literal[]{qs.getLiteral("en_label"),qs.getLiteral("label")}); String label = getFirstNotNull(new Literal[]{qs.getLiteral("en_label"),qs.getLiteral("label")});
String description = getFirstNotNull(new Literal[]{qs.getLiteral("en_definition"),qs.getLiteral("definition"), String description = getFirstNotNull(new Literal[]{qs.getLiteral("en_definition"),qs.getLiteral("definition"),
qs.getLiteral("en_description"),qs.getLiteral("description")}) ; qs.getLiteral("en_description"),qs.getLiteral("description")}) ;
RDFSClass clazz = new RDFSClass(uri, label, description,prefix,namespace); RDFSClass clazz = new RDFSClass(uri, label, description,prefix,namespace);
vocab.addClass(clazz); vocab.addClass(clazz);
} }
query = QueryFactory.create(PROPERTIES_QUERY_P1 + namespace.trim() + PROPERTIES_QUERY_P2); query = QueryFactory.create(PROPERTIES_QUERY_P1 + namespace.trim() + PROPERTIES_QUERY_P2);
qe = QueryExecutionFactory.create(query, m); qe = QueryExecutionFactory.create(query, m);
res = qe.execSelect(); res = qe.execSelect();
seen = new HashSet<String>(); seen = new HashSet<String>();
while(res.hasNext()){ while(res.hasNext()){
QuerySolution qs = res.nextSolution(); QuerySolution qs = res.nextSolution();
String uri = qs.getResource("resource").getURI(); String uri = qs.getResource("resource").getURI();
if(seen.contains(uri)){ if(seen.contains(uri)){
continue; continue;
} }
String label = getFirstNotNull(new Literal[]{qs.getLiteral("en_label"),qs.getLiteral("label")}); String label = getFirstNotNull(new Literal[]{qs.getLiteral("en_label"),qs.getLiteral("label")});
String description = getFirstNotNull(new Literal[]{qs.getLiteral("en_definition"),qs.getLiteral("definition"), String description = getFirstNotNull(new Literal[]{qs.getLiteral("en_definition"),qs.getLiteral("definition"),
qs.getLiteral("en_description"),qs.getLiteral("description")}) ; qs.getLiteral("en_description"),qs.getLiteral("description")}) ;
RDFSProperty prop = new RDFSProperty(uri, label, description,prefix,namespace); RDFSProperty prop = new RDFSProperty(uri, label, description,prefix,namespace);
vocab.addProperty(prop); vocab.addProperty(prop);
} }
return vocab; return vocab;
} }
private String getFirstNotNull(Literal[] literals){ private String getFirstNotNull(Literal[] literals){
String s = null; String s = null;
for(int i=0;i<literals.length;i++){ for(int i=0;i<literals.length;i++){
s = getString(literals[i]); s = getString(literals[i]);
if(s!=null){ if(s!=null){
break; break;
} }
} }
return s; return s;
} }
private String getString(Literal l){ private String getString(Literal l){
if(l!=null){ if(l!=null){
return l.getString(); return l.getString();
} }
return null; return null;
} }
} }

View File

@ -21,222 +21,224 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory; import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version; import org.apache.lucene.util.Version;
import com.metaweb.gridworks.GridworksServlet;
/** /**
* @author fadmaa * @author fadmaa
* *
*/ */
public class VocabularyManager { public class VocabularyManager {
private static final String INDEX_PATH = "luceneIndex"; private static final String CLASS_TYPE = "class";
private static final String CLASS_TYPE = "class"; private static final String PROPERTY_TYPE = "property";
private static final String PROPERTY_TYPE = "property"; private static final String VOCABULARY_TYPE = "vocabulary";
private static final String VOCABULARY_TYPE = "vocabulary";
private IndexWriter writer;
private IndexWriter writer; private IndexSearcher searcher;
private IndexSearcher searcher;
private Directory _directory;
private Directory _directory;
private static VocabularyManager singleton;
public static VocabularyManager singleton;
private List<Vocabulary> vocabularies = new ArrayList<Vocabulary>();
private List<Vocabulary> vocabularies = new ArrayList<Vocabulary>();
static public VocabularyManager getSingleton(GridworksServlet servlet) {
static{ return singleton != null ? singleton : (singleton = new VocabularyManager(servlet));
singleton = new VocabularyManager(); }
}
private VocabularyManager(GridworksServlet servlet) {
private VocabularyManager(){ try{
try{ synchronized (this) {
synchronized (this) { File dir = servlet.getCacheDir("rdfImporter");
_directory = new SimpleFSDirectory(new File(INDEX_PATH)); _directory = new SimpleFSDirectory(new File(dir, "luceneIndex"));
writer = new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED); writer = new IndexWriter(_directory, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED);
searcher = new IndexSearcher(_directory); searcher = new IndexSearcher(_directory);
updateVocabulariesList(); updateVocabulariesList();
} }
} catch (CorruptIndexException e) { } catch (CorruptIndexException e) {
throw new RuntimeException("Failed initialize vocabulary search",e); throw new RuntimeException("Failed initialize vocabulary search",e);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Failed initialize vocabulary search",e); throw new RuntimeException("Failed initialize vocabulary search",e);
} }
} }
/** /**
* @param url where to get the vocabulary description from * @param url where to get the vocabulary description from
* @param prefix preferred prefix for vocabulary e.g. foaf, dc, skos, dcat * @param prefix preferred prefix for vocabulary e.g. foaf, dc, skos, dcat
* @param namespace the base URI of the vocabulary. usually but not alway the same as url * @param namespace the base URI of the vocabulary. usually but not alway the same as url
* @param format the format of the RDF description of the vocabulary at the end of url (default to RDF/XML) * @param format the format of the RDF description of the vocabulary at the end of url (default to RDF/XML)
* @throws IOException * @throws IOException
* @throws CorruptIndexException * @throws CorruptIndexException
* @throws VocabularyExistException * @throws VocabularyExistException
*/ */
public void addVocabulary(String url, String prefix, String namespace, String format) throws CorruptIndexException, IOException, VocabularyExistException{ public void addVocabulary(String url, String prefix, String namespace, String format) throws CorruptIndexException, IOException, VocabularyExistException{
if (defined(namespace)){ if (defined(namespace)){
throw new VocabularyExistException(namespace + " already exists!"); throw new VocabularyExistException(namespace + " already exists!");
} }
VocabularyImporter importer = new VocabularyImporter(); VocabularyImporter importer = new VocabularyImporter();
Vocabulary vocabulary = importer.getVocabulary(url,prefix,namespace,format); Vocabulary vocabulary = importer.getVocabulary(url,prefix,namespace,format);
indexVocabulary(vocabulary); indexVocabulary(vocabulary);
updateSearcher(); updateSearcher();
// updateVocabulariesList(); // updateVocabulariesList();
this.vocabularies.add(vocabulary); this.vocabularies.add(vocabulary);
} }
public void addVocabulary(String url, String prefix, String namespace) throws CorruptIndexException, IOException, VocabularyExistException{ public void addVocabulary(String url, String prefix, String namespace) throws CorruptIndexException, IOException, VocabularyExistException{
addVocabulary(url, prefix, namespace,"RDF/XML"); addVocabulary(url, prefix, namespace,"RDF/XML");
} }
private void indexVocabulary(Vocabulary v) throws CorruptIndexException, IOException{ private void indexVocabulary(Vocabulary v) throws CorruptIndexException, IOException{
Document doc = new Document(); Document doc = new Document();
doc.add(new Field("type",VOCABULARY_TYPE,Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("type",VOCABULARY_TYPE,Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("name",v.getName(),Field.Store.YES,Field.Index.NO)); doc.add(new Field("name",v.getName(),Field.Store.YES,Field.Index.NO));
doc.add(new Field("uri",v.getUri(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("uri",v.getUri(),Field.Store.YES,Field.Index.NOT_ANALYZED));
writer.addDocument(doc); writer.addDocument(doc);
for(RDFSClass c:v.getClasses()){ for(RDFSClass c:v.getClasses()){
indexRdfNode(c, CLASS_TYPE); indexRdfNode(c, CLASS_TYPE);
} }
for(RDFSProperty p:v.getProperties()){ for(RDFSProperty p:v.getProperties()){
indexRdfNode(p, PROPERTY_TYPE); indexRdfNode(p, PROPERTY_TYPE);
} }
writer.commit(); writer.commit();
} }
public List<RDFNode> searchClasses(String str)throws ParseException, IOException{ public List<RDFNode> searchClasses(String str)throws ParseException, IOException{
List<RDFNode> res = new ArrayList<RDFNode>(); List<RDFNode> res = new ArrayList<RDFNode>();
org.apache.lucene.search.Query query = prepareQuery(str, "class"); org.apache.lucene.search.Query query = prepareQuery(str, "class");
TopDocs docs = searcher.search(query, 1000); TopDocs docs = searcher.search(query, 1000);
for(int i=0;i<docs.totalHits;i++){ for(int i=0;i<docs.totalHits;i++){
Document doc= searcher.doc(docs.scoreDocs[i].doc); Document doc= searcher.doc(docs.scoreDocs[i].doc);
String uri = doc.get("uri"); String uri = doc.get("uri");
String label = doc.get("label"); String label = doc.get("label");
String description = doc.get("description"); String description = doc.get("description");
String namespace = doc.get("namespace"); String namespace = doc.get("namespace");
String prefix = doc.get("prefix"); String prefix = doc.get("prefix");
RDFSClass node = new RDFSClass(uri,label,description,prefix,namespace); RDFSClass node = new RDFSClass(uri,label,description,prefix,namespace);
res.add(node); res.add(node);
} }
return res; return res;
} }
public List<RDFNode> searchProperties(String str)throws ParseException, IOException{ public List<RDFNode> searchProperties(String str)throws ParseException, IOException{
List<RDFNode> res = new ArrayList<RDFNode>(); List<RDFNode> res = new ArrayList<RDFNode>();
org.apache.lucene.search.Query query = prepareQuery(str, "property"); org.apache.lucene.search.Query query = prepareQuery(str, "property");
TopDocs docs = searcher.search(query, 1000); TopDocs docs = searcher.search(query, 1000);
for(int i=0;i<docs.totalHits;i++){ for(int i=0;i<docs.totalHits;i++){
Document doc= searcher.doc(docs.scoreDocs[i].doc); Document doc= searcher.doc(docs.scoreDocs[i].doc);
String uri = doc.get("uri"); String uri = doc.get("uri");
String label = doc.get("label"); String label = doc.get("label");
String description = doc.get("description"); String description = doc.get("description");
String namespace = doc.get("namespace"); String namespace = doc.get("namespace");
String prefix = doc.get("prefix"); String prefix = doc.get("prefix");
RDFNode node = new RDFSProperty(uri,label,description,prefix,namespace); RDFNode node = new RDFSProperty(uri,label,description,prefix,namespace);
res.add(node); res.add(node);
} }
return res; return res;
} }
public List<Vocabulary> getVocabularies(){ public List<Vocabulary> getVocabularies(){
return vocabularies; return vocabularies;
} }
public void deleteVocabulary(String uri) throws IOException{ public void deleteVocabulary(String uri) throws IOException{
Vocabulary vocab = null; Vocabulary vocab = null;
for(Vocabulary v:vocabularies){ for(Vocabulary v:vocabularies){
if(v.getUri().equals(uri)){ if(v.getUri().equals(uri)){
vocab = v; vocab = v;
break; break;
} }
} }
if(vocab==null){ if(vocab==null){
throw new RuntimeException("Vocabulary " + uri + " not found"); throw new RuntimeException("Vocabulary " + uri + " not found");
} }
vocabularies.remove(vocab); vocabularies.remove(vocab);
Term t = new Term("uri",uri); Term t = new Term("uri",uri);
writer.deleteDocuments(t); writer.deleteDocuments(t);
t = new Term("namespace",uri); t = new Term("namespace",uri);
writer.deleteDocuments(t); writer.deleteDocuments(t);
writer.commit(); writer.commit();
updateSearcher(); updateSearcher();
} }
private org.apache.lucene.search.Query prepareQuery(String s,String type)throws ParseException{ private org.apache.lucene.search.Query prepareQuery(String s,String type)throws ParseException{
QueryParser parser = new QueryParser(Version.LUCENE_30,"description",new StandardAnalyzer(Version.LUCENE_30)); QueryParser parser = new QueryParser(Version.LUCENE_30,"description",new StandardAnalyzer(Version.LUCENE_30));
String queryStr = "type:" + type ; String queryStr = "type:" + type ;
if(s!=null && s.trim().length()>0){ if(s!=null && s.trim().length()>0){
s =s.trim(); s =s.trim();
if(s.indexOf("*")==-1){ if(s.indexOf("*")==-1){
s += "*"; s += "*";
} }
if(s.indexOf(":")==-1){ if(s.indexOf(":")==-1){
queryStr += " AND (curie:" + s ; queryStr += " AND (curie:" + s ;
queryStr += " OR description:" + s ; queryStr += " OR description:" + s ;
queryStr += " OR label:" + s + ")"; queryStr += " OR label:" + s + ")";
}else{ }else{
String p1 = s.substring(0,s.indexOf(":")); String p1 = s.substring(0,s.indexOf(":"));
String p2 = s.substring(s.lastIndexOf(":")+1); String p2 = s.substring(s.lastIndexOf(":")+1);
queryStr += " AND prefix:" + p1; queryStr += " AND prefix:" + p1;
if(p2.length()>1){ if(p2.length()>1){
//queryStr += " AND (description:" + p2; //queryStr += " AND (description:" + p2;
queryStr += " AND label:" + p2; queryStr += " AND label:" + p2;
} }
} }
} }
return parser.parse(queryStr); return parser.parse(queryStr);
} }
private void indexRdfNode(RDFNode node, String type) throws CorruptIndexException, IOException{ private void indexRdfNode(RDFNode node, String type) throws CorruptIndexException, IOException{
//TODO weight fields... setBoost //TODO weight fields... setBoost
Document doc = new Document(); Document doc = new Document();
doc.add(new Field("type",type,Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("type",type,Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("prefix",node.getVocabularyPrefix(),Field.Store.YES,Field.Index.NOT_ANALYZED)); doc.add(new Field("prefix",node.getVocabularyPrefix(),Field.Store.YES,Field.Index.NOT_ANALYZED));
String l = node.getLabel()==null?"":node.getLabel(); String l = node.getLabel()==null?"":node.getLabel();
Field labelField = new Field("label",l,Field.Store.NO,Field.Index.ANALYZED); Field labelField = new Field("label",l,Field.Store.NO,Field.Index.ANALYZED);
doc.add(labelField); doc.add(labelField);
String d = node.getDescription()==null?"":node.getDescription(); String d = node.getDescription()==null?"":node.getDescription();
Field descriptionField = new Field("description",d,Field.Store.YES,Field.Index.ANALYZED); Field descriptionField = new Field("description",d,Field.Store.YES,Field.Index.ANALYZED);
doc.add(descriptionField); doc.add(descriptionField);
doc.add(new Field("uri", node.getURI(),Field.Store.YES,Field.Index.NO)); doc.add(new Field("uri", node.getURI(),Field.Store.YES,Field.Index.NO));
Field curieField = new Field("curie", node.getPreferredCURIE(),Field.Store.YES,Field.Index.ANALYZED); Field curieField = new Field("curie", node.getPreferredCURIE(),Field.Store.YES,Field.Index.ANALYZED);
doc.add(curieField); doc.add(curieField);
Field namespaceField = new Field("namespace", node.getVocabularyUri(),Field.Store.YES,Field.Index.NOT_ANALYZED); Field namespaceField = new Field("namespace", node.getVocabularyUri(),Field.Store.YES,Field.Index.NOT_ANALYZED);
doc.add(namespaceField); doc.add(namespaceField);
writer.addDocument(doc); writer.addDocument(doc);
} }
private void updateVocabulariesList() throws IOException{ private void updateVocabulariesList() throws IOException{
Term typeTerm = new Term("type", VOCABULARY_TYPE); Term typeTerm = new Term("type", VOCABULARY_TYPE);
Query query = new TermQuery(typeTerm); Query query = new TermQuery(typeTerm);
//TODO 1000 :O //TODO 1000 :O
TopDocs vocabDocs = searcher.search(query, 1000); TopDocs vocabDocs = searcher.search(query, 1000);
for(int i=0;i<vocabDocs.totalHits;i++){ for(int i=0;i<vocabDocs.totalHits;i++){
Document doc = searcher.doc(vocabDocs.scoreDocs[i].doc); Document doc = searcher.doc(vocabDocs.scoreDocs[i].doc);
String name = doc.get("name"); String name = doc.get("name");
String uri = doc.get("uri"); String uri = doc.get("uri");
Vocabulary vocab = new Vocabulary(name, uri); Vocabulary vocab = new Vocabulary(name, uri);
this.vocabularies.add(vocab); this.vocabularies.add(vocab);
} }
} }
private void updateSearcher() throws IOException{ private void updateSearcher() throws IOException{
this.searcher = new IndexSearcher(_directory); this.searcher = new IndexSearcher(_directory);
} }
private boolean defined(String namespace){ private boolean defined(String namespace){
if(namespace==null){ if(namespace==null){
return false; return false;
} }
namespace = namespace.trim(); namespace = namespace.trim();
for(Vocabulary v:vocabularies){ for(Vocabulary v:vocabularies){
if(v.getUri().equals(namespace)){ if(v.getUri().equals(namespace)){
return true; return true;
} }
} }
return false; return false;
} }
} }