Skip to content

Commit cf763c6

Browse files
committed
upload as bytes
1 parent c2d5e82 commit cf763c6

4 files changed

Lines changed: 100 additions & 29 deletions

File tree

server/src/main/java/org/diskproject/server/repository/DiskRepository.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ public Hypothesis addHypothesis(String username, Hypothesis hypothesis) {
658658
String desc = hypothesis.getDescription();
659659
String question = hypothesis.getQuestion();
660660
String dateCreated = hypothesis.getDateCreated();
661+
System.out.println(dateCreated);
661662
if (dateCreated == null || dateCreated.equals("")) {
662663
// SET DATE
663664
hypothesis.setDateCreated(dateformatter.format(new Date()));
@@ -1263,8 +1264,8 @@ public Map<LineOfInquiry, List<Map<String, String>>> getLOIByHypothesisId(String
12631264
if (allSolutions != null) {
12641265
if (allSolutions.size() == 0) {
12651266
System.out.println("No solutions for " + loi.getId());
1266-
String errorMesString = "No solutions found for the query: \n" + query;
1267-
System.out.println(errorMesString);
1267+
//String errorMesString = "No solutions found for the query: \n" + query;
1268+
//System.out.println(errorMesString);
12681269
// throw new NotFoundException(errorMesString);
12691270
} else
12701271
for (List<SparqlQuerySolution> row : allSolutions) {
@@ -1514,9 +1515,6 @@ private List<WorkflowBindings> getTLOIBindings(String username, List<WorkflowBin
15141515
if (sparqlvar == null)
15151516
continue;
15161517

1517-
System.out.println("1 " + sparqlvar);
1518-
System.out.println(dataVarBindings);
1519-
15201518
// Get the data bindings for the sparql variable
15211519
List<String> dsurls = dataVarBindings.get(sparqlvar);
15221520

@@ -1538,8 +1536,9 @@ private List<WorkflowBindings> getTLOIBindings(String username, List<WorkflowBin
15381536
for (Variable v: allVars) {
15391537
if (varName.equals(v.getName())) {
15401538
List<String> classes = v.getType();
1541-
if (classes != null && classes.size() > 0)
1542-
dType = classes.get(0); //TODO: This should be a selection of some type
1539+
if (classes != null && classes.size() > 0) {
1540+
dType = classes.contains(vbinding.getType()) ? vbinding.getType() : classes.get(0);
1541+
}
15431542
}
15441543
}
15451544
// TODO: this should be async
@@ -1653,7 +1652,7 @@ private Map<String, String> addData(List<String> dsurls, MethodAdapter methodAda
16531652
// upload the files
16541653
for (String newFilename : names) {
16551654
String newFile = nameToUrl.get(newFilename);
1656-
System.out.println("Uploading to " + methodAdapter.getName() + ": " + newFile + " as " + newFilename);
1655+
System.out.println("Uploading to " + methodAdapter.getName() + ": " + newFile + " as " + newFilename + "(" + dType + ")");
16571656
methodAdapter.addData(newFile, newFilename, dType);
16581657
}
16591658

@@ -1779,7 +1778,6 @@ public Map<String, String> getNarratives(String username, String tloid) {
17791778
dataset += "<li>" + anchor + "</li>";
17801779
}
17811780
}
1782-
System.out.println("binding: " + binding);
17831781
}
17841782
}
17851783
Double confidence = tloi.getConfidenceValue();

server/src/main/java/org/diskproject/server/repository/WingsAdapter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.diskproject.server.repository;
22

3+
import java.io.ByteArrayOutputStream;
34
import java.io.File;
45
import java.io.IOException;
56
import java.lang.reflect.Type;
@@ -202,7 +203,6 @@ private List<String> getSubClasses(String superClass) {
202203
List<NameValuePair> formdata = new ArrayList<NameValuePair>();
203204
formdata.add(new BasicNameValuePair("query", query));
204205
formdata.add(new BasicNameValuePair("format", "json"));
205-
System.out.println("q> " + query);
206206
String resultjson = get(pageid, formdata);
207207
if (resultjson != null && !resultjson.equals("")) {
208208
JsonObject result = jsonParser.parse(resultjson).getAsJsonObject();
@@ -213,7 +213,6 @@ private List<String> getSubClasses(String superClass) {
213213
if (bindingJson.get("sc") == null)
214214
continue;
215215
String subClass = bindingJson.get("sc").getAsJsonObject().get("value").getAsString();
216-
System.out.println("1> " + subClass);
217216
subClasses.add(subClass);
218217
}
219218
}
@@ -765,9 +764,9 @@ public String addDataToWings(String id, String type, String contents) {
765764
}
766765

767766

768-
public String addDataToWingsAsFile(String id, String contents, String type) {
767+
public String addDataToWingsAsFile(String id, byte[] contents, String type) {
769768
// Compute the hash of the contents and check it agains filename.
770-
String sha = DigestUtils.sha1Hex(contents.getBytes());
769+
String sha = DigestUtils.sha1Hex(contents);
771770
String correctName = "SHA" + sha.substring(0, 6);
772771
if (!id.contains(correctName))
773772
System.out.println("File " + id + " does not have the same hash: " + sha);
@@ -782,7 +781,7 @@ public String addDataToWingsAsFile(String id, String contents, String type) {
782781
return null;
783782
}
784783
File f = new File(dir.getAbsolutePath() + "/" + id);
785-
FileUtils.write(f, contents);
784+
FileUtils.writeByteArrayToFile(f, contents);
786785
String uploadResponse = this.upload(uploadPage, "data", f);
787786

788787
// Decode response
@@ -831,15 +830,17 @@ public String addRemoteDataToWings(String url, String name, String dType) throws
831830
/* FIXME: Wings rename does not rename the file, only the id
832831
* thus we cannot upload two files with the same name and then rename them. */
833832
// Get the file.
834-
String fileContents = null;
835833
CloseableHttpClient client = HttpClientBuilder.create().build();
834+
byte[] bytes = null;
836835
try {
837836
HttpGet securedResource = new HttpGet(url);
838837
CloseableHttpResponse httpResponse = client.execute(securedResource);
839838

840839
try {
841840
HttpEntity responseEntity = httpResponse.getEntity();
842-
fileContents = EntityUtils.toString(responseEntity);
841+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
842+
responseEntity.writeTo(baos);
843+
bytes = baos.toByteArray();
843844
EntityUtils.consume(responseEntity);
844845
httpResponse.close();
845846
} catch (Exception e) {
@@ -856,8 +857,8 @@ public String addRemoteDataToWings(String url, String name, String dType) throws
856857
}
857858
}
858859

859-
System.out.println("Content downloaded [" + fileContents.length() + "] " + fileContents.substring(0, fileContents.length() > 10 ? 10 : fileContents.length()-1) + "...");
860-
String dataid = addDataToWingsAsFile(name, fileContents, dType);
860+
System.out.println("Content downloaded [" + bytes.length + "] ");
861+
String dataid = addDataToWingsAsFile(name, bytes, dType);
861862
System.out.println("Data ID generated: " + dataid);
862863
return dataid;
863864
}

server/src/main/java/org/diskproject/server/repository/WriteKBRepository.java

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,22 @@ private KBAPI getKB(String url) {
7474
}
7575

7676
private KBObject getKBValue(Value v, KBAPI kb) {
77+
String dataType = v.getDatatype();
78+
Object value = v.getValue();
7779
if (v.getType() == Value.Type.LITERAL) {
78-
if (v.getDatatype() != null)
79-
return kb.createXSDLiteral(v.getValue().toString(), v.getDatatype());
80+
if (dataType != null)
81+
return kb.createXSDLiteral(value.toString(), v.getDatatype());
8082
else
81-
return kb.createLiteral(v.getValue());
83+
return kb.createLiteral(value.toString());
8284
} else {
83-
return kb.getResource(v.getValue().toString());
85+
if (dataType != null) {
86+
//FIXME: if we assing a new datatype to V we must ensure is an URI
87+
//KBObject cls = kb.createClass(dataType);
88+
//return kb.createObjectOfClass(value.toString(), cls);
89+
return kb.getResource(value.toString());
90+
} else {
91+
return kb.getResource(value.toString());
92+
}
8493
}
8594
}
8695

@@ -543,6 +552,24 @@ protected LineOfInquiry loadLOI(String username, String id) {
543552
return null;
544553

545554
this.start_read();
555+
/*
556+
KBObject typeprop = userKB.getProperty(KBConstants.RDF_NS + "type");
557+
for (KBTriple t : userKB.genericTripleQuery(null, typeprop, null)) {
558+
KBObject s = t.getSubject();
559+
KBObject o = t.getObject();
560+
if (o != null && !o.getValueAsString().startsWith("http://disk-project.org/ontology/disk#")) {
561+
System.out.println("> " + s + " = " + o);
562+
if (s!= null) {
563+
this.end();
564+
this.start_write();
565+
userKB.removeTriple(t);
566+
this.save(userKB);
567+
this.end();
568+
this.start_read();
569+
}
570+
}
571+
} */
572+
546573
KBObject loiItem = userKB.getIndividual(loiId);
547574
if (loiItem == null) {
548575
this.end();
@@ -988,8 +1015,22 @@ private void writeBindings(String userDomain, String id, KBObject bindingprop,
9881015
for (VariableBinding vBinding : bindings.getBindings()) {
9891016
String varId = vBinding.getVariable();
9901017
String binding = vBinding.getBinding();
991-
Value bindingValue = new Value(binding, KBConstants.XSD_NS + "string");
1018+
String cType = vBinding.getType();
1019+
1020+
String type = cType != null ? cType : KBConstants.XSD_NS + "string"; // default type
1021+
Value bindingValue = new Value(binding, type);
1022+
if (cType != null && !cType.startsWith(KBConstants.XSD_NS)) {
1023+
bindingValue.setType(Value.Type.URI);
1024+
}
1025+
9921026
KBObject varbindingobj = userKB.createObjectOfClass(null, DISKOnt.getClass(DISK.VARIABLE_BINDING));
1027+
if (bindingValue.getType() == Value.Type.URI) {
1028+
// We store the type of the binding on the binding object. We should store it on the binding itself
1029+
// but most of the time the binding is a rdf-literal value.
1030+
KBObject typeProp = userKB.getProperty(KBConstants.RDF_NS + "type");
1031+
userKB.addTriple(varbindingobj, typeProp, userKB.getResource(type));
1032+
//userKB.addClassForInstance(varbindingobj, userKB.getResource(type));
1033+
}
9931034
userKB.setPropertyValue(varbindingobj, DISKOnt.getProperty(DISK.HAS_VARIABLE),
9941035
userKB.getResource(workflowuri + "#" + varId));
9951036
userKB.setPropertyValue(varbindingobj, DISKOnt.getProperty(DISK.HAS_BINDING_VALUE),
@@ -1091,6 +1132,22 @@ private List<WorkflowBindings> loadBindings(String userDomain, String id, KBObje
10911132
KBObject varobj = kb.getPropertyValue(vbobj, DISKOnt.getProperty(DISK.HAS_VARIABLE));
10921133
KBObject bindobj = kb.getPropertyValue(vbobj, DISKOnt.getProperty(DISK.HAS_BINDING_VALUE));
10931134
VariableBinding vBinding = new VariableBinding(varobj.getName(), bindobj.getValueAsString());
1135+
String cls = null;
1136+
try {
1137+
KBObject typeprop = kb.getProperty(KBConstants.RDF_NS + "type");
1138+
for (KBTriple tr : kb.genericTripleQuery(vbobj, typeprop, null)) {
1139+
KBObject o = tr.getObject();
1140+
if (o != null && !o.getValueAsString().startsWith("http://disk-project.org/ontology/disk#")) {
1141+
cls = o.getValueAsString();
1142+
}
1143+
}
1144+
} catch (Exception e) {
1145+
// TODO: handle exception
1146+
System.out.println("Something weird has happened");
1147+
}
1148+
if (cls != null) {
1149+
vBinding.setType(cls);
1150+
}
10941151
bindings.getBindings().add(vBinding);
10951152
}
10961153

shared/src/main/java/org/diskproject/shared/classes/workflow/VariableBinding.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ public class VariableBinding implements Serializable, Comparable<VariableBinding
88

99
String variable;
1010
String binding;
11+
String type;
1112

1213
public VariableBinding(String v, String b) {
1314
variable = v;
1415
binding = b;
1516
}
17+
18+
public VariableBinding(String v, String b, String t) {
19+
variable = v;
20+
binding = b;
21+
type = t;
22+
}
1623

1724
public VariableBinding(){}
1825

@@ -27,7 +34,19 @@ public void setVariable(String variable) {
2734
public String getBinding() {
2835
return binding;
2936
}
37+
38+
public void setBinding(String binding) {
39+
this.binding = binding;
40+
}
3041

42+
public String getType () {
43+
return type;
44+
}
45+
46+
public void setType (String t) {
47+
this.type = t;
48+
}
49+
3150
public String[] getBindingAsArray () {
3251
String b = getBinding().replace("]", "").replace("[", "").replaceAll(" ", "");
3352
return b.split(",");
@@ -37,15 +56,11 @@ public boolean isCollection () {
3756
return (binding.charAt(0) == '[' && binding.charAt(binding.length()-1) == ']');
3857
}
3958

40-
public void setBinding(String binding) {
41-
this.binding = binding;
42-
}
43-
44-
public String toString() {
59+
public String toString () {
4560
return variable+" = "+binding;
4661
}
4762

48-
public int compareTo(VariableBinding o) {
63+
public int compareTo (VariableBinding o) {
4964
return this.toString().compareTo(o.toString());
5065
}
5166
}

0 commit comments

Comments
 (0)