示例#1
0
    void Start()
    {
        LogSystem.InstallDefaultReactors();

        _testClusterConfigName  = "cranfield_solr_config";
        _testClusterConfigPath  = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/cranfield_solr_config.zip";
        _testRankerTrainingPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/ranker_training_data.csv";
        _testAnswerDataPath     = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/ranker_answer_data.csv";
        _indexDataPath          = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/cranfield_data.json";
        _testQuery = "What is the basic mechanisim of the transonic aileron buzz";
        _collectionNameToDelete = "TestCollectionToDelete";
        _createdRankerName      = "RankerToDelete";

        //  Create credential and instantiate service
        Credentials credentials = new Credentials(_username, _password, _url);

        _retrieveAndRank = new RetrieveAndRank(credentials);

        Runnable.Run(Examples());
    }
示例#2
0
        public override IEnumerator RunTest()
        {
            LogSystem.InstallDefaultReactors();

            _testClusterConfigName  = "cranfield_solr_config";
            _testClusterConfigPath  = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/cranfield_solr_config.zip";
            _testRankerTrainingPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/ranker_training_data.csv";
            _testAnswerDataPath     = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/ranker_answer_data.csv";
            _indexDataPath          = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/RetrieveAndRank/cranfield_data.json";
            _testQuery = "What is the basic mechanisim of the transonic aileron buzz";
            _collectionNameToDelete = "TestCollectionToDelete";
            _createdRankerName      = "RankerToDelete";

            try
            {
                VcapCredentials vcapCredentials = new VcapCredentials();
                fsData          data            = null;

                //  Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
                //  See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
                var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
                var fileContent           = File.ReadAllText(environmentalVariable);

                //  Add in a parent object because Unity does not like to deserialize root level collection types.
                fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");

                //  Convert json to fsResult
                fsResult r = fsJsonParser.Parse(fileContent, out data);
                if (!r.Succeeded)
                {
                    throw new WatsonException(r.FormattedMessages);
                }

                //  Convert fsResult to VcapCredentials
                object obj = vcapCredentials;
                r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
                if (!r.Succeeded)
                {
                    throw new WatsonException(r.FormattedMessages);
                }

                //  Set credentials from imported credntials
                Credential credential = vcapCredentials.VCAP_SERVICES["retrieve_and_rank"][TestCredentialIndex].Credentials;
                _username = credential.Username.ToString();
                _password = credential.Password.ToString();
                _url      = credential.Url.ToString();
            }
            catch
            {
                Log.Debug("TestRetrieveAndRank.RunTest()", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
            }

            //  Create credential and instantiate service
            Credentials credentials = new Credentials(_username, _password, _url);

            //  Or authenticate using token
            //Credentials credentials = new Credentials(_url)
            //{
            //    AuthenticationToken = _token
            //};

            _retrieveAndRank = new RetrieveAndRank(credentials);

            //  Get clusters
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get clusters.");
            if (!_retrieveAndRank.GetClusters(OnGetClusters, OnFail))
            {
                Log.Debug("TestRetrieveAndRank.GetClusters()", "Failed to get clusters!");
            }
            while (!_getClustersTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Create cluster
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to create cluster.");
            if (!_retrieveAndRank.CreateCluster(OnCreateCluster, OnFail, "unity-test-cluster", "1"))
            {
                Log.Debug("TestRetrieveAndRank.CreateCluster()", "Failed to create cluster!");
            }
            while (!_createClusterTested || !_readyToContinue)
            {
                yield return(null);
            }

            //  Wait for cluster status to be `READY`.
            Runnable.Run(CheckClusterStatus(10f));
            while (!_isClusterReady)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  List cluster configs
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get cluster configs.");
            if (!_retrieveAndRank.GetClusterConfigs(OnGetClusterConfigs, OnFail, _clusterToDelete))
            {
                Log.Debug("TestRetrieveAndRank.GetClusterConfigs()", "Failed to get cluster configs!");
            }
            while (!_getClusterConfigsTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Upload cluster config
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to upload cluster config.");
            if (!_retrieveAndRank.UploadClusterConfig(OnUploadClusterConfig, OnFail, _clusterToDelete, _testClusterConfigName, _testClusterConfigPath))
            {
                Log.Debug("TestRetrieveAndRank.UploadClusterConfig()", "Failed to upload cluster config {0}!", _testClusterConfigPath);
            }
            while (!_uploadClusterConfigTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Get cluster
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get cluster.");
            if (!_retrieveAndRank.GetCluster(OnGetCluster, OnFail, _clusterToDelete))
            {
                Log.Debug("TestRetrieveAndRank.GetCluster()", "Failed to get cluster!");
            }
            while (!_getClusterTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Get cluster config
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get cluster config.");
            if (!_retrieveAndRank.GetClusterConfig(OnGetClusterConfig, OnFail, _clusterToDelete, _testClusterConfigName))
            {
                Log.Debug("TestRetrieveAndRank.GetClusterConfig()", "Failed to get cluster config {0}!", _testClusterConfigName);
            }
            while (!_getClusterConfigTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  List Collection request
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get collections.");
            if (!_retrieveAndRank.ForwardCollectionRequest(OnGetCollections, OnFail, _clusterToDelete, CollectionsAction.List))
            {
                Log.Debug("TestRetrieveAndRank.ForwardCollectionRequest()", "Failed to get collections!");
            }
            while (!_getCollectionsTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Create Collection request
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to create collection.");
            if (!_retrieveAndRank.ForwardCollectionRequest(OnCreateCollection, OnFail, _clusterToDelete, CollectionsAction.Create, _collectionNameToDelete, _testClusterConfigName))
            {
                Log.Debug("TestRetrieveAndRank.ForwardCollectionRequest()", "Failed to create collections!");
            }
            while (!_createCollectionTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Index documents
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to index documents.");
            if (!_retrieveAndRank.IndexDocuments(OnIndexDocuments, OnFail, _indexDataPath, _clusterToDelete, _collectionNameToDelete))
            {
                Log.Debug("TestRetrieveAndRank.IndexDocuments()", "Failed to index documents!");
            }
            while (!_indexDocumentsTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Get rankers
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get rankers.");
            if (!_retrieveAndRank.GetRankers(OnGetRankers, OnFail))
            {
                Log.Debug("TestRetrieveAndRank.GetRankers()", "Failed to get rankers!");
            }
            while (!_getRankersTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Create ranker
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to create ranker.");
            if (!_retrieveAndRank.CreateRanker(OnCreateRanker, OnFail, _testRankerTrainingPath, _createdRankerName))
            {
                Log.Debug("TestRetrieveAndRank.CreateRanker()", "Failed to create ranker!");
            }
            while (!_createRankerTested || !_readyToContinue)
            {
                yield return(null);
            }

            //  Wait for ranker status to be `Available`.
            Log.Debug("TestRetrieveAndRank.RunTest()", "Checking ranker status in 10 seconds");
            Runnable.Run(CheckRankerStatus(10f));
            while (!_isRankerReady || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Standard Search
            string[] fl = { "title", "id", "body", "author", "bibliography" };
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to search standard.");
            if (!_retrieveAndRank.Search(OnSearchStandard, OnFail, _clusterToDelete, _collectionNameToDelete, _testQuery, fl))
            {
                Log.Debug("TestRetrieveAndRank.Search()", "Failed to search!");
            }
            while (!_searchStandardTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Rank
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to rank.");
            if (!_retrieveAndRank.Rank(OnRank, OnFail, _rankerIdToDelete, _testAnswerDataPath))
            {
                Log.Debug("TestRetrieveAndRank.Rank()", "Failed to rank!");
            }
            while (!_rankTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Get ranker info
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to get rankers.");
            if (!_retrieveAndRank.GetRanker(OnGetRanker, OnFail, _rankerIdToDelete))
            {
                Log.Debug("TestRetrieveAndRank.GetRanker()", "Failed to get ranker!");
            }
            while (!_getRankerTested)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete rankers
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to delete ranker {0}.", _rankerIdToDelete);
            if (!_retrieveAndRank.DeleteRanker(OnDeleteRanker, OnFail, _rankerIdToDelete))
            {
                Log.Debug("TestRetrieveAndRank.DeleteRanker()", "Failed to delete ranker {0}!", _rankerIdToDelete);
            }
            while (!_deleteRankerTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete Collection request
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to delete collection {0}.", "TestCollectionToDelete");
            if (!_retrieveAndRank.ForwardCollectionRequest(OnDeleteCollection, OnFail, _clusterToDelete, CollectionsAction.Delete, "TestCollectionToDelete"))
            {
                Log.Debug("TestRetrieveAndRank.ForwardCollectionRequest()", "Failed to delete collections!");
            }
            while (!_deleteCollectionTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete cluster config
            string clusterConfigToDelete = "test-config";

            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to delete cluster config.");
            if (!_retrieveAndRank.DeleteClusterConfig(OnDeleteClusterConfig, OnFail, _clusterToDelete, clusterConfigToDelete))
            {
                Log.Debug("TestRetrieveAndRank.DeleteClusterConfig()", "Failed to delete cluster config {0}", clusterConfigToDelete);
            }
            while (!_deleteClusterConfigTested || !_readyToContinue)
            {
                yield return(null);
            }

            _readyToContinue = false;
            //  Delete cluster
            Log.Debug("TestRetrieveAndRank.RunTest()", "Attempting to delete cluster {0}.", _clusterToDelete);
            if (!_retrieveAndRank.DeleteCluster(OnDeleteCluster, OnFail, _clusterToDelete))
            {
                Log.Debug("TestRetrieveAndRank.DeleteCluster()", "Failed to delete cluster!");
            }
            while (!_deleteClusterTested || !_readyToContinue)
            {
                yield return(null);
            }

            Log.Debug("TestRetrieveAndRank.RunTest()", "Retrieve and rank examples complete!");
            yield break;
        }