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/xercesImpl-2.7.1.jar"/>
<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"/>
</classpath>

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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