-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPod.java
129 lines (115 loc) · 5.16 KB
/
Pod.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package solid;
import cartago.Artifact;
import cartago.OPERATION;
import cartago.OpFeedbackParam;
/**
* A CArtAgO artifact that agent can use to interact with LDP containers in a Solid pod.
*/
public class Pod extends Artifact {
private String podURL; // the location of the Solid pod
/**
* Method called by CArtAgO to initialize the artifact.
*
* @param podURL The location of a Solid pod
*/
public void init(String podURL) {
this.podURL = podURL;
log("Pod artifact initialized for: " + this.podURL);
}
/**
* CArtAgO operation for creating a Linked Data Platform container in the Solid pod
*
* @param containerName The name of the container to be created
*
*/
@OPERATION
public void createContainer(String containerName) {
log("1. Implement the method createContainer()");
}
/**
* CArtAgO operation for publishing data within a .txt file in a Linked Data Platform container of the Solid pod
*
* @param containerName The name of the container where the .txt file resource will be created
* @param fileName The name of the .txt file resource to be created in the container
* @param data An array of Object data that will be stored in the .txt file
*/
@OPERATION
public void publishData(String containerName, String fileName, Object[] data) {
log("2. Implement the method publishData()");
}
/**
* CArtAgO operation for reading data of a .txt file in a Linked Data Platform container of the Solid pod
*
* @param containerName The name of the container where the .txt file resource is located
* @param fileName The name of the .txt file resource that holds the data to be read
* @param data An array whose elements are the data read from the .txt file
*/
@OPERATION
public void readData(String containerName, String fileName, OpFeedbackParam<Object[]> data) {
data.set(readData(containerName, fileName));
}
/**
* Method for reading data of a .txt file in a Linked Data Platform container of the Solid pod
*
* @param containerName The name of the container where the .txt file resource is located
* @param fileName The name of the .txt file resource that holds the data to be read
* @return An array whose elements are the data read from the .txt file
*/
public Object[] readData(String containerName, String fileName) {
log("3. Implement the method readData(). Currently, the method returns mock data");
// Remove the following mock responses once you have implemented the method
switch(fileName) {
case "watchlist.txt":
Object[] mockWatchlist = new Object[]{"The Matrix", "Inception", "Avengers: Endgame"};
return mockWatchlist;
case "sleep.txt":
Object[] mockSleepData = new Object[]{"6", "7", "5"};
return mockSleepData;
case "trail.txt":
Object[] mockTrailData = new Object[]{"3", "5.5", "5.5"};
return mockTrailData;
default:
return new Object[0];
}
}
/**
* Method that converts an array of Object instances to a string,
* e.g. the array ["one", 2, true] is converted to the string "one\n2\ntrue\n"
*
* @param array The array to be converted to a string
* @return A string consisting of the string values of the array elements separated by "\n"
*/
public static String createStringFromArray(Object[] array) {
StringBuilder sb = new StringBuilder();
for (Object obj : array) {
sb.append(obj.toString()).append("\n");
}
return sb.toString();
}
/**
* Method that converts a string to an array of Object instances computed by splitting the given string with delimiter "\n"
* e.g. the string "one\n2\ntrue\n" is converted to the array ["one", "2", "true"]
*
* @param str The string to be converted to an array
* @return An array consisting of string values that occur by splitting the string around "\n"
*/
public static Object[] createArrayFromString(String str) {
return str.split("\n");
}
/**
* CArtAgO operation for updating data of a .txt file in a Linked Data Platform container of the Solid pod
* The method reads the data currently stored in the .txt file and publishes in the file the old data along with new data
*
* @param containerName The name of the container where the .txt file resource is located
* @param fileName The name of the .txt file resource that holds the data to be updated
* @param data An array whose elements are the new data to be added in the .txt file
*/
@OPERATION
public void updateData(String containerName, String fileName, Object[] data) {
Object[] oldData = readData(containerName, fileName);
Object[] allData = new Object[oldData.length + data.length];
System.arraycopy(oldData, 0, allData, 0, oldData.length);
System.arraycopy(data, 0, allData, oldData.length, data.length);
publishData(containerName, fileName, allData);
}
}