Newer
Older
package wrapper;
import sofia_kp.SIBResponse;
import sofia_kp.iKPIC_subscribeHandler2;
import sofia_kp.KPICore;
import java.util.ArrayList;
import java.util.Vector;
public class SmartSpaceKPI {
private KPICore core;
private ArrayList<String> subscriptionIdList;
public SmartSpaceKPI(String host, int port, String spaceName) throws SmartSpaceException {
core = new KPICore(host, port, spaceName);
subscriptionIdList = new ArrayList<String>();
SIBResponse joinResponse = core.join();
if (!joinResponse.isConfirmed())
throw new SmartSpaceException(joinResponse.Message);
core.disable_debug_message();
}
public void insert(SmartSpaceTriplet triplet) throws SmartSpaceException {
SIBResponse insertResponse = core.insert(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getSubjectType(), triplet.getObjectType());
if (!insertResponse.isConfirmed()) {
String text = String.format("KPI failed to insert triplet: (%s, %s, %s, %s, %s)",
triplet.getSubject(), triplet.getPredicate(), triplet.getObject(),
triplet.getSubjectType(), triplet.getObjectType());
throw new SmartSpaceException(text + '\n' + insertResponse.Message);
}
}
public void remove(SmartSpaceTriplet triplet) throws SmartSpaceException {
SIBResponse removeResponse = core.remove(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getSubjectType(), triplet.getObjectType());
if (!removeResponse.isConfirmed()) {
String text = String.format("KP failed to remove triplet: (%s, %s, %s, %s, %s)",
triplet.getSubject(), triplet.getPredicate(), triplet.getObject(),
triplet.getSubjectType(), triplet.getObjectType());
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
throw new SmartSpaceException(text + '\n' + removeResponse.Message);
}
}
public Vector<SmartSpaceTriplet> query(SmartSpaceTriplet triplet) throws SmartSpaceException {
SIBResponse queryResponse = core.queryRDF(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getSubjectType(), triplet.getObjectType());
if (queryResponse.isConfirmed()) {
Vector<Vector<String>> stringVectorResult = queryResponse.query_results;
Vector<SmartSpaceTriplet> result = new Vector<SmartSpaceTriplet>();
for (Vector<String> it : stringVectorResult) {
result.add(new SmartSpaceTriplet(it));
}
return result;
} else {
throw new SmartSpaceException(queryResponse.Message);
}
}
public void update(SmartSpaceTriplet newTriplet, SmartSpaceTriplet oldTriplet) throws SmartSpaceException {
SIBResponse updateResponse = core.update(
newTriplet.getSubject(), newTriplet.getPredicate(), newTriplet.getObject(), newTriplet.getSubjectType(), newTriplet.getObjectType(),
oldTriplet.getSubject(), oldTriplet.getPredicate(), oldTriplet.getObject(), oldTriplet.getSubjectType(), oldTriplet.getObjectType()
);
if (!updateResponse.isConfirmed()) {
String text = String.format("KP failed to update triplet! Old triplet: (%s, %s, %s, %s, %s), new triplet (%s, %s, %s, %s, %s)",
newTriplet.getSubject(), newTriplet.getPredicate(), newTriplet.getObject(), newTriplet.getSubjectType(), newTriplet.getObjectType(),
oldTriplet.getSubject(), oldTriplet.getPredicate(), oldTriplet.getObject(), oldTriplet.getSubjectType(), oldTriplet.getObjectType());
throw new SmartSpaceException(text + '\n' + updateResponse.Message);
}
}
public String subscribe(SmartSpaceTriplet triplet, iKPIC_subscribeHandler2 handler) throws SmartSpaceException {
SIBResponse subscribeResponse = core.subscribeRDF(triplet.getSubject(), triplet.getPredicate(), triplet.getObject(), triplet.getObjectType(), handler);
if (subscribeResponse != null && subscribeResponse.isConfirmed()) {
subscriptionIdList.add(subscribeResponse.subscription_id);
return subscribeResponse.subscription_id;
} else {
System.err.println("Some problems with subscribing");
throw new SmartSpaceException(subscribeResponse != null ? subscribeResponse.Message : null);
}
}
public void leave() throws SmartSpaceException {
try {
unsubscribe();
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
130
131
System.err.println(exception.getMessage());
}
SIBResponse leaveResponse = core.leave();
if (!leaveResponse.isConfirmed()) {
throw new SmartSpaceException(leaveResponse.Message);
}
}
private void unsubscribe() throws SmartSpaceException {
String exceptionMessage = "";
for (String id : subscriptionIdList) {
SIBResponse unsubscribeResponse = core.unsubscribe(id);
// у нас проблемы с отпиской от интеллектуального пространства
if (!unsubscribeResponse.isConfirmed()) {
exceptionMessage += id + ": " + unsubscribeResponse.Message + '\n';
}
}
subscriptionIdList.clear();
// проблемы во время отписки были, сигнализируем это
if (!exceptionMessage.isEmpty()) {
throw new SmartSpaceException(exceptionMessage);
}
}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
public void unsubscribe(Iterable<String> subscriptionIds) throws SmartSpaceException {
String exceptionMessage = "";
for (String subscriptionId : subscriptionIds) {
try {
unsubscribe(subscriptionId);
} catch (SmartSpaceException e) {
exceptionMessage += e.getMessage() + "\n";
}
}
// проблемы во время отписки были, сигнализируем это
if (!exceptionMessage.isEmpty()) {
throw new SmartSpaceException(exceptionMessage);
}
}
public void unsubscribe(String subscriptionId) throws SmartSpaceException {
SIBResponse unsubscribeResponse = core.unsubscribe(subscriptionId);
// у нас проблемы с отпиской от интеллектуального пространства
if (!unsubscribeResponse.isConfirmed()) {
throw new SmartSpaceException(subscriptionId + ": " + unsubscribeResponse.Message + '\n');
} else {
int index = subscriptionIdList.indexOf(subscriptionId);
if (index >= 0) {
subscriptionIdList.remove(index);
tripletList.remove(index);
}
}
}
public void unsubscribe(Iterable<SmartSpaceTriplet> triplets, boolean fullMatch) throws SmartSpaceException {
String exceptionMessage = "";
for (SmartSpaceTriplet triplet : triplets)
try {
unsubscribe(triplet, fullMatch);
} catch (SmartSpaceException e) {
exceptionMessage += e.getMessage() + "\n";
}
// проблемы во время отписки были, сигнализируем это
if (!exceptionMessage.isEmpty()) {
throw new SmartSpaceException(exceptionMessage);
}
}
public void unsubscribe(SmartSpaceTriplet triplet, boolean fullMatch) throws SmartSpaceException {
String exceptionMessage = "";
String subject = triplet.getSubject(), predicate = triplet.getPredicate(), object = triplet.getObject();
ArrayList<String> oldIdList = new ArrayList<String>(subscriptionIdList);
for (int i = oldIdList.size() - 1; i >= 0; i--) {
SmartSpaceTriplet curTriplet = tripletList.get(i);
if (checkTwoStrings(subject, curTriplet.getSubject(), fullMatch) &&
checkTwoStrings(predicate, curTriplet.getPredicate(), fullMatch) &&
checkTwoStrings(object, curTriplet.getObject(), fullMatch)) {
SIBResponse unsubscribeResponse = core.unsubscribe(oldIdList.get(i));
// у нас проблемы с отпиской от интеллектуального пространства
if (!unsubscribeResponse.isConfirmed()) {
exceptionMessage += oldIdList.get(i) + ": " + unsubscribeResponse.Message + '\n';
} else {
subscriptionIdList.remove(i);
tripletList.remove(i);
}
// проблемы во время отписки были, сигнализируем это
if (!exceptionMessage.isEmpty()) {
throw new SmartSpaceException(exceptionMessage);
}
}
private boolean checkTwoStrings(String mainString, String curString, boolean fullMatch) {
if (mainString == null)
if (fullMatch)
return (curString == null);
else
return true;
return mainString.equals(curString);
}