Skip to content

Commit b179946

Browse files
committed
discovery document and query example
1 parent 0f7950f commit b179946

File tree

3 files changed

+126
-17
lines changed

3 files changed

+126
-17
lines changed

Examples/ServiceExamples/Scripts/ExampleDiscoveryV1.cs

Lines changed: 126 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,19 @@ public class ExampleDiscoveryV1 : MonoBehaviour
3434
private string m_CreatedCollectionID;
3535
private string m_CreatedCollectionName = "Unity SDK Created Collection";
3636
private string m_CreatedCollectionDescription = "A collection created by the Unity SDK. Please delete me.";
37+
private string m_CreatedDocumentID;
38+
private string m_DocumentFilePath;
39+
private string m_Query = "What is the capital of china?";
40+
41+
private bool m_IsConfigDeleted = false;
42+
private bool m_IsCollectionDeleted = false;
3743
private void Start()
3844
{
3945
LogSystem.InstallDefaultReactors();
4046

4147
m_ConfigurationJsonPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/Discovery/exampleConfigurationData.json";
4248
m_FilePathToIngest = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";
49+
m_DocumentFilePath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/watson_beats_jeopardy.html";
4350

4451
//// Get Environments
4552
//Log.Debug("ExampleDiscoveryV1", "Attempting to get environments");
@@ -72,8 +79,6 @@ private void Start()
7279
// Log.Debug("ExampleDiscovery", "Failed to get collections");
7380
}
7481

75-
private bool isEnvironmentReady = false;
76-
7782
private void CheckState()
7883
{
7984
Log.Debug("ExampleDiscoveryV1", "Attempting to get environment state");
@@ -93,13 +98,20 @@ private void HandleCheckEnvironmentState(Environment resp, string data)
9398
Log.Debug("ExampleDiscoveryV1", "Environment {0} is {1}", resp.environment_id, resp.status);
9499

95100
if (resp.status == "active")
96-
isEnvironmentReady = true;
101+
TestAddCollection();
97102
else
98103
{
99104
Invoke("CheckState", 10f);
100105
}
101106
}
102107

108+
private void BeginDeleteCycle()
109+
{
110+
TestDeleteDocument();
111+
Invoke("TestDeleteCollection", 1f);
112+
Invoke("TestDeleteConfiguration", 2f);
113+
}
114+
103115
private void TestPreviewConfiguration()
104116
{
105117
Log.Debug("ExampleDiscoveryV1", "Attempting to preview configuration");
@@ -110,15 +122,15 @@ private void TestPreviewConfiguration()
110122
private void TestDeleteConfiguration()
111123
{
112124
// DeleteEnvironment
113-
Log.Debug("ExampleDiscoveryV1", "Attempting to delete configuration");
125+
Log.Debug("ExampleDiscoveryV1", "Attempting to delete configuration {0}", m_CreatedConfigurationID);
114126
if (!m_Discovery.DeleteConfiguration(OnDeleteConfiguration, m_CreatedEnvironmentID, m_CreatedConfigurationID))
115127
Log.Debug("ExampleDiscoveryV1", "Failed to delete configuration");
116128
}
117129

118130
private void TestDeleteEnvironment()
119131
{
120132
// DeleteEnvironment
121-
Log.Debug("ExampleDiscoveryV1", "Attempting to delete environment");
133+
Log.Debug("ExampleDiscoveryV1", "Attempting to delete environment {0}", m_CreatedEnvironmentID);
122134
if (!m_Discovery.DeleteEnvironment(OnDeleteEnvironment, m_CreatedEnvironmentID))
123135
Log.Debug("ExampleDiscoveryV1", "Failed to delete environment");
124136
}
@@ -150,11 +162,46 @@ private void TestGetCollection()
150162
private void TestDeleteCollection()
151163
{
152164
// Delete Collection
153-
Log.Debug("ExampleDiscoveryV1", "Attempting to delete collection");
165+
Log.Debug("ExampleDiscoveryV1", "Attempting to delete collection {0}", m_CreatedCollectionID);
154166
if (!m_Discovery.DeleteCollection(OnDeleteCollection, m_CreatedEnvironmentID, m_CreatedCollectionID))
155167
Log.Debug("ExampleDiscovery", "Failed to add collection");
156168
}
157169

170+
private void TestAddDocument()
171+
{
172+
Log.Debug("ExampleDiscoveryV1", "Attempting to add document");
173+
if (!m_Discovery.AddDocument(OnAddDocument, m_CreatedEnvironmentID, m_CreatedCollectionID, m_DocumentFilePath, m_CreatedConfigurationID, null))
174+
Log.Debug("ExampleDiscovery", "Failed to add document");
175+
}
176+
177+
private void TestGetDocument()
178+
{
179+
Log.Debug("ExampleDiscoveryV1", "Attempting to get document");
180+
if (!m_Discovery.GetDocument(OnGetDocument, m_CreatedEnvironmentID, m_CreatedCollectionID, m_CreatedDocumentID))
181+
Log.Debug("ExampleDiscovery", "Failed to get document");
182+
}
183+
184+
private void TestUpdateDocument()
185+
{
186+
Log.Debug("ExampleDiscoveryV1", "Attempting to update document");
187+
if (!m_Discovery.UpdateDocument(OnUpdateDocument, m_CreatedEnvironmentID, m_CreatedCollectionID, m_CreatedDocumentID, m_DocumentFilePath, m_CreatedConfigurationID, null))
188+
Log.Debug("ExampleDiscovery", "Failed to update document");
189+
}
190+
191+
private void TestDeleteDocument()
192+
{
193+
Log.Debug("ExampleDiscoveryV1", "Attempting to delete document {0}", m_CreatedDocumentID);
194+
if (!m_Discovery.DeleteDocument(OnDeleteDocument, m_CreatedEnvironmentID, m_CreatedCollectionID, m_CreatedDocumentID))
195+
Log.Debug("ExampleDiscovery", "Failed to delete document");
196+
}
197+
198+
private void TestQuery()
199+
{
200+
Log.Debug("ExampleDiscoveryV1", "Attempting to query");
201+
if (!m_Discovery.Query(OnQuery, m_CreatedEnvironmentID, m_CreatedCollectionID, null, m_Query, null, 10, null, 0))
202+
Log.Debug("ExampleDiscovery", "Failed to delete document");
203+
}
204+
158205
private void OnGetEnvironments(GetEnvironmentsResponse resp, string data)
159206
{
160207
if (resp != null)
@@ -261,8 +308,10 @@ private void OnDeleteConfiguration(bool success, string data)
261308
{
262309
Log.Debug("ExampleDiscoveryV1", "Delete configuration successful");
263310
m_CreatedConfigurationID = default(string);
311+
m_IsConfigDeleted = true;
264312

265-
TestDeleteEnvironment();
313+
if(m_IsConfigDeleted && m_IsCollectionDeleted)
314+
Invoke("TestDeleteEnvironment", 1f);
266315
}
267316
else
268317
Log.Debug("ExampleDiscoveryV1", "Delete configuration failed");
@@ -308,7 +357,8 @@ private void OnGetCollection(Collection resp, string data)
308357
{
309358
Log.Debug("ExampleDiscoveryV1", "Collection: {0}, {1}", resp.collection_id, resp.name);
310359

311-
TestDeleteCollection();
360+
361+
TestAddDocument();
312362
}
313363
else
314364
{
@@ -337,10 +387,77 @@ private void OnDeleteCollection(bool success, string data)
337387
{
338388
Log.Debug("ExampleDiscoveryV1", "Delete collection successful");
339389
m_CreatedCollectionID = default(string);
390+
m_IsCollectionDeleted = true;
340391

341-
TestDeleteConfiguration();
392+
if (m_IsConfigDeleted && m_IsCollectionDeleted)
393+
Invoke("TestDeleteEnvironment", 1f);
342394
}
343395
else
344396
Log.Debug("ExampleDiscoveryV1", "Delete collection failed");
345397
}
398+
399+
private void OnAddDocument(DocumentAccepted resp, string data)
400+
{
401+
if(resp != null)
402+
{
403+
Log.Debug("ExampleDiscoveryV1", "Added Document {0} {1}", resp.document_id, resp.status);
404+
m_CreatedDocumentID = resp.document_id;
405+
406+
TestGetDocument();
407+
}
408+
else
409+
{
410+
Log.Debug("ExampleDiscoveryV1", "resp is null, {0}", data);
411+
}
412+
}
413+
414+
private void OnGetDocument(DocumentStatus resp, string data)
415+
{
416+
if(resp != null)
417+
{
418+
Log.Debug("ExampleDiscoveryV1", "Got Document {0} {1}", resp.document_id, resp.status);
419+
TestUpdateDocument();
420+
}
421+
else
422+
{
423+
Log.Debug("ExampleDiscoveryV1", "resp is null, {0}", data);
424+
}
425+
}
426+
427+
private void OnUpdateDocument(DocumentAccepted resp, string data)
428+
{
429+
if (resp != null)
430+
{
431+
Log.Debug("ExampleDiscoveryV1", "Updated Document {0} {1}", resp.document_id, resp.status);
432+
TestQuery();
433+
}
434+
else
435+
{
436+
Log.Debug("ExampleDiscoveryV1", "resp is null, {0}", data);
437+
}
438+
}
439+
440+
private void OnDeleteDocument(bool success, string data)
441+
{
442+
if (success)
443+
{
444+
Log.Debug("ExampleDiscoveryV1", "Delete document successful");
445+
m_CreatedDocumentID = default(string);
446+
}
447+
else
448+
Log.Debug("ExampleDiscoveryV1", "Delete collection failed");
449+
}
450+
451+
private void OnQuery(QueryResponse resp, string data)
452+
{
453+
if(resp != null)
454+
{
455+
foreach(QueryResult result in resp.results)
456+
Log.Debug("ExampleDiscoveryV1", "Query response: id: {0}, score: {1}", result.id, result.score);
457+
}
458+
else
459+
{
460+
Log.Debug("ExampleDiscoveryV1", "resp is null, {0}", data);
461+
}
462+
}
346463
}

Scripts/Services/Discovery/v1/Discovery.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,14 +1598,8 @@ private void OnUpdateDocumentResponse(RESTConnector.Request req, RESTConnector.R
15981598
throw new ArgumentNullException("environmentID");
15991599
if (string.IsNullOrEmpty(collectionID))
16001600
throw new ArgumentNullException("collectionID");
1601-
if(string.IsNullOrEmpty(filter))
1602-
throw new ArgumentNullException("filter");
16031601
if (string.IsNullOrEmpty(query))
16041602
throw new ArgumentNullException("query");
1605-
if (string.IsNullOrEmpty(aggregation))
1606-
throw new ArgumentNullException("aggregation");
1607-
if (string.IsNullOrEmpty(_return))
1608-
throw new ArgumentNullException("_return");
16091603

16101604
QueryRequest req = new QueryRequest();
16111605
req.Callback = callback;

Scripts/Services/TradeoffAnalytics/TradeoffAnalytics.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public bool GetDilemma(OnDilemma callback, Problem problem, Boolean generateVisu
6464
fsData tempData = null;
6565
sm_Serializer.TrySerialize<Problem>(problem, out tempData);
6666

67-
Log.Status("GetDilemma", "JSON: {0}", tempData.ToString());
68-
6967
req.Send = Encoding.UTF8.GetBytes(tempData.ToString());
7068
req.Headers["Content-Type"] = "application/json";
7169

0 commit comments

Comments
 (0)