Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sernadela committed Jun 18, 2013
2 parents 2aaf0f8 + 31a92ce commit fe4de01
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package pt.ua.bioinformatics.coeus.actions;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -94,56 +95,35 @@ public Resolution handle() {
if (sub.indexOf(":") > 1) {
try {
if (pred.indexOf(":") > 1) {
if (obj.contains(":")) {
//verify if the obj is a Resource or a Literal
if (obj.indexOf(":") > 1) {
// obj is a Resource
Statement s = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(obj)));
//verify if exists in the kb
if (Boot.getAPI().containsStatement(s)) {
//Exists so try to remove
Boot.getAPI().removeStatement(s);
//verify if has been removed
if (!Boot.getAPI().containsStatement(s)) {
result.put("status", 100);
result.put("message", "[COEUS][API][Delete] Triples removed from the knowledge base.");

} else {
result.put("status", 200);
result.put("message", "[COEUS][API][Delete] Triples not removed from knowledge base.");
}

} else {
result.put("status", 203);
result.put("message", "[COEUS][API][Delete] Unknown triples in the knowledge base.");
}
} else {

result.put("status", 203);
result.put("message", "[COEUS][API][Delete] " + obj + " is an invalid object.");
}

// test obj
String xsd = "http://www.w3.org/2001/XMLSchema#";
Statement statToRemove = null;
//verify if is a xsd type
if (obj.startsWith("xsd:")) {
String[] old = obj.split(":", 3);
Literal l = Boot.getAPI().getModel().createTypedLiteral(old[2], xsd + old[1]);
statToRemove = Boot.getAPI().getModel().createLiteralStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), l);
} else if (obj.indexOf(":") > 1) {
//is a Resource
statToRemove = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(obj)));
} else {
//obj is a literal
Statement s = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), obj);
//verify if exists in the kb
if (Boot.getAPI().containsStatement(s)) {
//Exists so try to remove
Boot.getAPI().removeStatement(s);
//verify if has been removed
if (!Boot.getAPI().containsStatement(s)) {
result.put("status", 100);
result.put("message", "[COEUS][API][Delete] Triples removed from the knowledge base.");

} else {
result.put("status", 200);
result.put("message", "[COEUS][API][Delete] Triples not removed from knowledge base.");
}

} else {
result.put("status", 203);
result.put("message", "[COEUS][API][Delete] Unknown triples in the knowledge base.");
}
//is a Literal
statToRemove = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), obj);
}

if (Boot.getAPI().containsStatement(statToRemove)) {

Boot.getAPI().removeStatement(statToRemove);

result.put("status", 100);
result.put("message", "[COEUS][API][Delete] Triples removed from the knowledge base.");

} else {
result.put("status", 201);
result.put("message", "[COEUS][API][Delete] Triple do not existe in this model in order to remove it.");
}

} else {
result.put("status", 202);
result.put("message", "[COEUS][API][Delete] " + pred + " is an invalid predicate.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
*/
package pt.ua.bioinformatics.coeus.actions;

import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -110,59 +108,63 @@ public Resolution handle() {
if (sub.indexOf(":") > 1) {
try {
if (pred.indexOf(":") > 1) {
if (old_obj.contains(":")) {
//verify if the obj is a Resource or a Literal
if (old_obj.indexOf(":") > 1 && new_obj.indexOf(":") > 1) {
// obj is a Resource

Statement s = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(old_obj)));
if (Boot.getAPI().containsStatement(s)) {

Boot.getAPI().removeStatement(s);
Boot.getAPI().addStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(new_obj)));

result.put("status", 100);
result.put("message", "[COEUS][API][Update] Triples updated in the knowledge base.");

} else {
result.put("status", 201);
result.put("message", "[COEUS][API][Update] Triple do not existe in this model in order to update it.");
}
} else {

result.put("status", 203);
result.put("message", "[COEUS][API][Update] " + old_obj + " or " + new_obj + " is an invalid object.");
}
// test old_obj
String xsd = "http://www.w3.org/2001/XMLSchema#";
Statement statToRemove = null;
//verify if is a xsd type
if (old_obj.startsWith("xsd:")) {
String[] old = old_obj.split(":", 3);
Literal l = Boot.getAPI().getModel().createTypedLiteral(old[2], xsd + old[1]);
statToRemove = Boot.getAPI().getModel().createLiteralStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), l);
} else if (old_obj.indexOf(":") > 1) {
//is a Resource
statToRemove = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(old_obj)));
} else {
//obj is a literal
//is a Literal
statToRemove = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), old_obj);
}

Statement s = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), old_obj);
if (Boot.getAPI().containsStatement(s)) {
// test new_obj
Statement statToAdd = null;
//verify if is a xsd type
if (new_obj.startsWith("xsd:")) {
String[] old = new_obj.split(":", 3);
Literal l = Boot.getAPI().getModel().createTypedLiteral(old[2], xsd + old[1]);
statToAdd = Boot.getAPI().getModel().createLiteralStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), l);
} else if (new_obj.indexOf(":") > 1) {
//is a Resource
statToAdd = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(new_obj)));
} else {
//is a Literal
statToAdd = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), new_obj);
}

Boot.getAPI().removeStatement(s);
Boot.getAPI().addStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), new_obj);
if (Boot.getAPI().containsStatement(statToRemove)) {

result.put("status", 100);
result.put("message", "[COEUS][API][Update] Triples updated in the knowledge base.");
Boot.getAPI().addStatement(statToAdd);
Boot.getAPI().removeStatement(statToRemove);

} else {
result.put("status", 201);
result.put("message", "[COEUS][API][Update] Triple do not existe in this model in order to update it.");
}
result.put("status", 100);
result.put("message", "[COEUS][API][Update] Triples updated in the knowledge base.");

} else {
result.put("status", 201);
result.put("message", "[COEUS][API][Update] Triple do not existe in this model in order to update it.");
}

} else {
result.put("status", 202);
result.put("message", "[COEUS][API][Update] " + pred + " is an invalid predicate.");
}

} catch (Exception ex) {
if (Config.isDebug()) {
System.out.println("[COEUS][UpdateActionBean] Unable to add triple to knowledge base. Invalid request.");
System.out.println("[COEUS][UpdateActionBean] Unable to update triple in the knowledge base. Invalid request.");
Logger.getLogger(UpdateActionBean.class.getName()).log(Level.SEVERE, null, ex);
}
result.put("status", 200);
result.put("message", "[COEUS][API][Update] Unable to remove triples to knowledge base, check exception.");
result.put("message", "[COEUS][API][Update] Unable to update triples in the knowledge base, check exception.");
result.put("exception", ex.toString());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pt.ua.bioinformatics.coeus.actions;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.stripes.action.ActionBean;
Expand Down Expand Up @@ -79,29 +81,38 @@ public ActionBeanContext getContext() {
@DefaultHandler
public Resolution handle() {
Boot.start();
if (sub != null && pred != null && obj != null ) {
if (sub != null && pred != null && obj != null) {
if (Boot.getAPI().validateKey(apikey)) {
if (sub.indexOf(":") > 1) {
try {

if (pred.contains(":")) {
if (obj.contains(":")) {
if (obj.indexOf(":") > 1) {
Boot.getAPI().addStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(obj)));
} else {
result.put("status", 203);
result.put("message", "[COEUS][API][Write] " + obj + " is an invalid object.");
}
String xsd = "http://www.w3.org/2001/XMLSchema#";
Statement statToAdd = null;
//verify if is a xsd type
if (obj.startsWith("xsd:")) {
String[] old = obj.split(":", 3);
Literal l = Boot.getAPI().getModel().createTypedLiteral(old[2], xsd + old[1]);
statToAdd = Boot.getAPI().getModel().createLiteralStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), l);
} else if (obj.indexOf(":") > 1) {
//is a Resource
statToAdd = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), Boot.getAPI().createResource(PrefixFactory.decode(obj)));
} else {
Boot.getAPI().addStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), obj);
//is a Literal
statToAdd = Boot.getAPI().createStatement(Boot.getAPI().createResource(PrefixFactory.decode(sub)), Predicate.get(pred), obj);
}

Boot.getAPI().addStatement(statToAdd);

result.put("status", 100);
result.put("message", "[COEUS][API][Write] Triples added to knowledge base.");

} else {
result.put("status", 202);
result.put("message", "[COEUS][API][Write] " + pred + " is an invalid predicate.");
}

result.put("status", 100);
result.put("message", "[COEUS][API][Write] Triples added to knowledge base.");

} catch (Exception ex) {
if (Config.isDebug()) {
System.out.println("[COEUS][WriteActionBean] Unable to add triple to knowledge base. Invalid request.");
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/pt/ua/bioinformatics/coeus/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ public void removeStatement(Statement statement) {
}

}

/**
/**
* Verify if the statement exists in the model
*
* @param statement
Expand All @@ -479,6 +478,24 @@ public boolean containsStatement(Statement statement) {
return false;
}

}
/**
* Add statement to the model
*
* @param statement
* @return
*/
public void addStatement(Statement statement) {
try {
this.model.add(statement);
} catch (Exception ex) {
if (Config.isDebug()) {
String stat=statement.getSubject()+" "+statement.getPredicate()+" "+statement.getObject();
System.out.println("[COEUS][API] Unable add statement in the model: "+stat);
Logger.getLogger(API.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
/**
* Create a statement from the model.
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/pt/ua/bioinformatics/coeus/common/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ public static boolean readData(Resource r) {
ResourceFactory factory;
try {
if (!r.isBuilt()) {
if (r.getPublisher().equals("plugin")) {
if (r.getPublisher().equalsIgnoreCase("plugin")) {
factory = new PluginFactory(r);
} else if (r.getPublisher().equals("csv")) {
} else if (r.getPublisher().equalsIgnoreCase("csv")) {
factory = new CSVFactory(r);
} else if (r.getPublisher().equals("xml")) {
} else if (r.getPublisher().equalsIgnoreCase("xml")) {
factory = new XMLFactory(r);
} else if (r.getPublisher().equals("sql")) {
} else if (r.getPublisher().equalsIgnoreCase("sql")) {
factory = new SQLFactory(r);
} else if (r.getPublisher().equals("sparql")) {
} else if (r.getPublisher().equalsIgnoreCase("sparql")) {
factory = new SPARQLFactory(r);
} else if (r.getPublisher().equals("json")) {
} else if (r.getPublisher().equalsIgnoreCase("json")) {
factory = new JSONFactory(r);
} else if (r.getPublisher().equals("ld")) {
} else if (r.getPublisher().equalsIgnoreCase("ld") | r.getPublisher().equalsIgnoreCase("linkeddata")) {
factory = new LinkedDataFactory(r);
} else if (r.getPublisher().equals("rdf") | r.getPublisher().equals("ttl")) {
} else if (r.getPublisher().equalsIgnoreCase("rdf") | r.getPublisher().equalsIgnoreCase("ttl")) {
factory = new RDFFactory(r);
} else {
factory = null;
Expand Down
Loading

0 comments on commit fe4de01

Please sign in to comment.