/// <param name="originalOntology">Ontologia funcționalității la care cererea se referă</param>
        /// <param name="requestShapes">Formele cererilor</param>
        public SelectionRules(Functionality originalOntology, RequestShape[] requestShapes)
        {
            this.numberOfOutcomes = ClientSettings.Default.RULE_GENERATION_NUMBER_OF_OUTCOMES;

            foreach (RequestShape requestShape in requestShapes) {

                foreach (Property property in originalOntology.properties) {

                    if (property.name == requestShape.shape.name) {

                        int exactlyLocation;

                        this._requirementOntologies.Add(this.constructRequirementOntology(requestShape, property.start, property.end, out exactlyLocation));

                        this._exactlyLocations.Add(exactlyLocation);

                        this.requirementImportances.Add(InferenceMachine.calculateImportance(requestShape.importance));

                        break;
                    }
                }
            }

            this.requirements = new int[this._requirementOntologies.Count];
            this.rules = this.bktCombinations(0, null);

            this.constructDecisionOntology();
        }
        /// <param name="originalOntology">Ontologia funcționalității la care cererea se referă</param>
        /// <param name="requestShapes">Formele cererilor</param>
        public MulticriteriaDecisionMaker(Functionality originalOntology, RequestShape[] requestShapes)
        {
            foreach (RequestShape requestShape in requestShapes) {

                foreach (Property property in originalOntology.properties) {

                    if (property.name == requestShape.shape.name) {

                        Term term = requestShape.shape.clone();

                        this.requestImportances.Add(term, InferenceMachine.calculateImportance(requestShape.importance));

                        break;
                    }
                }
            }
        }
Exemple #3
0
        /// <summary>
        ///   Interoghează serviciul de găsire a funcţionalităţii pentru a găsii toate serviciile care au o 
        ///   anumita functionalitate si aplica pe acestea algoritmul selectat (FRI sau MCDM).
        /// </summary>
        /// <param name="functionalityName">Numele funcţionalităţii pe care serviciile căutate trebuie să o ofere.</param>
        /// <param name="requestShapes">Formele căutate ale proprietăţilor funcţionalităţii.</param>
        /// <returns>Serviciile găsite.</returns>
        public string[] findServices(Functionality functionality, RequestShape[] reqShapes)
        {
            try {

                List<RequestShape> reqShp = new List<RequestShape>();

                foreach (RequestShape requestShape in reqShapes) {

                    if (requestShape != null) {

                        reqShp.Add(requestShape);
                    }
                }

                if(reqShp.Count == 0) {

                    this._lastError = "No request shapes specified";

                    return null;
                }

                RequestShape[] requestShapes = reqShp.ToArray();

                Message answer = new Message("findServices", new string[1] { functionality.name }).deliverAndWaitFeedback(this._functionalityFindingServiceAddress);

                if (answer != null) {

                    if (answer.request.Equals("receiveServices")) {

                        this._lastError = null;

                        List<Service> servicesProvidingRequestedFunctionality = new List<Service>();

                        int increment = 3* functionality.propertiesCount + 2;

                        string[] parameters = answer.parameters;

                        for(int i = 0;i<answer.numberOfParameters;i=i+increment) {

                            string name = null;
                            string accessPoint = null;

                            List<string> propertyNames  = new List<string>();
                            List<object> propertyValues = new List<object>();

                            int j = i;
                            int end = i + increment;

                            while(j < end) {

                                if (j % increment == 0) {

                                    name = parameters[j];
                                    j++;
                                }
                                else {

                                    if (j % increment == 1) {

                                        accessPoint = parameters[j];
                                        j++;
                                    }
                                    else {

                                        propertyNames.Add(answer.parameters[j]);

                                        if (parameters[j+1] == "fuzzy") {

                                            propertyValues.Add(parameters[j+2]);

                                        }
                                        else {

                                            if (parameters[j+1] == "crisp") {

                                                propertyValues.Add(double.Parse(parameters[j+2]));
                                            }
                                        }

                                        j=j+3;
                                    }
                                }
                            }

                            servicesProvidingRequestedFunctionality.Add(new Service(name, accessPoint, functionality.name, propertyNames, propertyValues));
                        }

                        List<Service> completeServices = new List<Service>();

                        foreach(Service service in servicesProvidingRequestedFunctionality) {

                            completeServices.Add(service.addActualOntology(functionality));
                        }

                        InferenceMachine.InferenceMachine inferenceMachine = null;

                        switch (ClientSettings.Default.INFERENCE_ALGORITHM) {

                            case (int)InferenceAlgorithms.multicriteriaDecisionMaking:

                                inferenceMachine = new MulticriteriaDecisionMaker(functionality, requestShapes);

                                break;

                            default:

                                inferenceMachine = new SelectionRules(functionality, requestShapes);

                                break;
                        }

                        string[] result = new string[completeServices.Count];

                        for (int i = 0; i < completeServices.Count; ++i) {

                            result[i] = inferenceMachine.applyToServiceInCSVFormat(completeServices[i]);
                        }

                        return result;
                    }
                    else {

                        if (answer.request.Equals("ERROR") && answer.numberOfParameters > 0) {

                            this._lastError = string.Join("\n", answer.parameters);

                            return null;
                        }
                    }
                }

                this._lastError = this.resourceManager.GetString("ERROR_NO_RESPONSE_FFS");

                return null;
            }
            catch (Exception e) {

                this._lastError = e.Message;

                return null;
            }
        }
        /// <summary>
        ///   Funcţia efectivă de transformare a mesajelor cerere în mesaje raspuns folosind serviciul ataşat.
        ///   Realizează interpretarea tipului cererii şi determină acţiunile necesare pentru a afla răspunsul.
        /// </summary>
        /// <param name="message">Cererea</param>
        /// <param name="from">Adresa de unde provine cererea</param>
        /// <returns>Raspunsul la cerere</returns>
        public Message answer(Message message, IPAddress from)
        {
            if (message.request.Equals("findServices")) {

                if (message.numberOfParameters == 1) {

                    string[] services = this.functionalityFindingService.findServices(message.getParameter(0));

                    if (services != null) {

                        return new Message("receiveServices", services);
                    }
                    else {

                        return new Message("ERROR", new string[1] { this.functionalityFindingService.lastError });
                    }
                }
                else {

                    return new Message("ERROR", new string[2] { "Missing parameter", "Please send functionality name, followed by request shapes in proper format." });
                }
            }
            else
                if (message.request.Equals("setServerLoadLevel"))
                {

                    if (message.numberOfParameters > 1)
                    {

                        RequestShape[] requestShapes = new RequestShape[message.numberOfParameters - 2];

                        for (int i = 2; i < message.numberOfParameters; ++i)
                        {

                            requestShapes[i - 2] = new RequestShape(message.getParameter(i));
                        }

                        string[] serverLoadLevel = new string[1];

                        serverLoadLevel[0] = this.functionalityFindingService.setServerLoadLevel(int.Parse(message.getParameter(0)), message.getParameter(1), requestShapes);

                        if (serverLoadLevel != null)
                        {
                            return new Message("receiveServerLoad", serverLoadLevel);
                        }
                        else
                        {

                            return new Message("ERROR", new string[1] { this.functionalityFindingService.lastError });
                        }
                    }
                    else
                    {

                        return new Message("ERROR", new string[2] { "Missing parameter", "Please send functionality name, followed by request shapes in proper format." });
                    }
                }
            else {

                return new Message("ERROR", new string[2] { "Unknown request", message.request });
            }
        }
Exemple #5
0
        /// <summary>
        ///   Indică apăsarea butonului pentru trimiterea căutării.
        ///   Cere clientului să găsească rezultatul cererii.
        /// </summary>
        private void btnFind_Click(object sender, EventArgs e)
        {
            RequestShape[] requestShapes = new RequestShape[this.propertyTabPageStates.Length];

            for (int i = 0; i < this.propertyTabPageStates.Length; ++i)
            {

                requestShapes[i] = this.propertyTabPageStates[i].customTerm == null ? null : new RequestShape(this.propertyTabPageStates[i].customTerm, this.propertyTabPageStates[i].importance);
            }

            string[] result = this.client.findServices(this.currentFunctionality, requestShapes);

            this.ActiveControl = null;

            if (result != null && this.ckBShowALLRes.Checked)
            {

                Thread thread = new Thread(delegate()
                {

                    Application.Run(new ClientResultsForm(result, true, -1));
                });

                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
            else
                if (result != null && !this.ckBShowALLRes.Checked)
                {
                    Thread thread = new Thread(delegate()
                    {

                        Application.Run(new ClientResultsForm(result, false, (int)nudShowRes.Value));
                    });

                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                }
            else
            {

                if (this.client.lastError != null)
                {

                    MessageBox.Show(this.client.lastError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
        /// <summary>
        ///   Construiește ontologia unei premise a regulilor prin adăugarea de vecini formelor cererilor.
        /// </summary>
        /// <param name="requestedShape">Forma cererii pentru care se construiește ontologia</param>
        /// <param name="domainStart">Începutul intervalului în care proprietatea poate lua valori și deci până unde se pot întinde vecinii</param>
        /// <param name="domainEnd">Sfârșitul intervalului în care proprietatea poate lua valori și deci până unde se pot întinde vecinii</param>
        /// <param name="exactlyLocation">Parametru de ieșire - indică ce termen din proprietate este cel identic cu cererea inițială</param>
        /// <returns>Ontologia premisei</returns>
        private Property constructRequirementOntology(RequestShape requestedShape, double domainStart, double domainEnd, out int exactlyLocation)
        {
            int numberOfNeighborsGeneratedOnEachSide = ClientSettings.Default.RULE_GENERATION_NUMBER_OF_NEIGHBOURS_ON_EACH_SIDE;

            numberOfNeighborsGeneratedOnEachSide = numberOfNeighborsGeneratedOnEachSide <= 1 ? 1 : numberOfNeighborsGeneratedOnEachSide >= 6 ? 6 : numberOfNeighborsGeneratedOnEachSide;

            List<Term> resultTerms = new List<Term>();

            double addLength = 0;

            if (ClientSettings.Default.RULE_GENERATION_FORCE_NEIGHBOURS) {

                addLength = (requestedShape.shape.start - domainStart) / numberOfNeighborsGeneratedOnEachSide;
            }
            else {

                addLength = (requestedShape.shape.end - requestedShape.shape.start) / 4;

                if (double.IsInfinity(addLength) || double.IsNaN(addLength)) {

                    addLength = (requestedShape.shape.right - requestedShape.shape.left) / 2;
                    addLength = double.IsInfinity(addLength) || double.IsNaN(addLength) ? requestedShape.shape.left - requestedShape.shape.start : addLength;
                    addLength = double.IsInfinity(addLength) || double.IsNaN(addLength) ? requestedShape.shape.end - requestedShape.shape.right : addLength;
                }
            }

            double nowValue = requestedShape.shape.start - addLength;

            List<double> marginalValues = new List<double>();

            for (int i = 0; i < numberOfNeighborsGeneratedOnEachSide - 1 && ((addLength > 0 && nowValue > domainStart) || (addLength < 0 && nowValue < domainStart)); ++i) {

                marginalValues.Add(nowValue);

                nowValue -= addLength;
            }

            if (requestedShape.shape.left != domainStart && (!ClientSettings.Default.RULE_GENERATION_FORCE_NEIGHBOURS || addLength != 0)) {

                marginalValues.Add(domainStart);
            }

            Term previewsTerm = requestedShape.shape;

            for (int i = 0; i < marginalValues.Count; ++i) {

                double auxStart = 0;
                double auxLeft  = 0;
                double auxRight = 0;
                double auxEnd   = 0;

                auxEnd   = previewsTerm.left;
                auxRight = previewsTerm.start;
                auxStart = marginalValues[i];
                auxLeft  = auxStart + (auxEnd - auxRight) / 2;

                if ((addLength > 0 && auxLeft < auxStart) || (addLength < 0 && auxLeft > auxStart)) {

                    auxLeft = auxStart;
                }
                else {

                    if ((addLength > 0 && auxLeft > auxRight) || (addLength < 0 && auxLeft < auxRight)) {

                        auxLeft = auxRight;
                    }
                }

                Term auxTerm = new Term(this.resourceManager.GetString(prefixes[marginalValues.Count - 1][i]).Replace("*", "Left"), auxStart, auxLeft, auxRight, auxEnd);

                resultTerms.Insert(0, auxTerm);

                previewsTerm = auxTerm;
            }

            if (resultTerms.Count > 0) {

                Term auxTerm = resultTerms[0];

                resultTerms[0] = new Term(auxTerm.name, auxTerm.start, auxTerm.start, auxTerm.right, auxTerm.end);
            }

            exactlyLocation = resultTerms.Count;
            resultTerms.Add(new Term("Exactly", requestedShape.shape.start, requestedShape.shape.left, requestedShape.shape.right, requestedShape.shape.end));

            if (ClientSettings.Default.RULE_GENERATION_FORCE_NEIGHBOURS) {

                addLength = (domainEnd - requestedShape.shape.end) / numberOfNeighborsGeneratedOnEachSide;
            }

            nowValue = requestedShape.shape.end + addLength;
            marginalValues = new List<double>();

            for (int i = 0; i < numberOfNeighborsGeneratedOnEachSide - 1 && ((addLength > 0 && nowValue < domainEnd) || (addLength < 0 && nowValue > domainEnd)); ++i) {

                marginalValues.Add(nowValue);
                nowValue += addLength;
            }

            if (requestedShape.shape.right != domainEnd && (!ClientSettings.Default.RULE_GENERATION_FORCE_NEIGHBOURS || addLength != 0)) {

                marginalValues.Add(domainEnd);
            }

            previewsTerm = requestedShape.shape;

            for (int i = 0; i < marginalValues.Count; ++i) {

                double auxStart = 0;
                double auxLeft = 0;
                double auxRight = 0;
                double auxEnd = 0;

                auxStart = previewsTerm.right;
                auxLeft  = previewsTerm.end;
                auxEnd   = marginalValues[i];
                auxRight = auxEnd - (auxLeft - auxStart) / 2;

                if ((addLength > 0 && auxRight > auxEnd) || (addLength < 0 && auxRight < auxEnd)) {

                    auxRight = auxEnd;
                }
                else {

                    if ((addLength > 0 && auxRight < auxLeft) || (addLength < 0 && auxRight > auxLeft)) {

                        auxRight = auxLeft;
                    }
                }

                Term auxTerm = new Term(this.resourceManager.GetString(prefixes[marginalValues.Count - 1][i]).Replace("*", "Right"), auxStart, auxLeft, auxRight, auxEnd);

                resultTerms.Add(auxTerm);

                previewsTerm = auxTerm;
            }

            if (resultTerms.Count > 0) {

                Term auxTerm = resultTerms[resultTerms.Count - 1];

                resultTerms[resultTerms.Count - 1] = new Term(auxTerm.name, auxTerm.start, auxTerm.left, auxTerm.end, auxTerm.end);
            }

            return new Property(requestedShape.shape.name, domainStart, domainEnd, resultTerms);
        }
        private void sendAsyncRequest(string functionalityName, RequestShape[] requestShapes)
        {
            List<string> messParams = new List<string>();

            messParams.Add(functionalityName);

            foreach (RequestShape requestShape in requestShapes)
            {

                if (requestShape != null)
                {

                    messParams.Add(requestShape.serialize());
                }
            }

            try
            {

                string[] parameters = messParams.ToArray();

                Message answer = new Message("findServices", parameters).deliverAndWaitFeedback(this._localFunctionalityFindingServiceAddress);

            }
            catch (Exception e)
            {

                this._lastError = e.Message;

            }
        }
        /// <summary>
        /// Metoda apelata in cadrul testarii realizate prin intermediul SmartSpammer pentru a seta nivelul de
        /// incarcare al FFS-ului inainte de trimiterea cererilor de tip client a caror timp de rezolvare este
        /// masurat
        /// </summary>
        /// <param name="minLoadLevel">Nivelul minim de incarcare al FFS</param>
        /// <param name="functionalityName">Numele functionalitatii din cadrul cererii de tip client</param>
        /// <param name="requestShapes">Detaliile cererii</param>
        /// <returns>'True' dupa atingerea nivelului de incarcare de catre FFS</returns>
        public string setServerLoadLevel(int minLoadLevel, string functionalityName, RequestShape[] requestShapes)
        {
            killPreviousThreads();

            this._minLoadLevel = minLoadLevel;

            threads = new SendAsyncRequest[minLoadLevel];
            startThreads = new IAsyncResult[minLoadLevel];

            for (int i = 0; i < _minLoadLevel; i++)
            {
                threads[i] = sendAsyncRequest;
            }

            for (int i = 0; i < _minLoadLevel; i++)
            {
                startThreads[i] = threads[i].BeginInvoke(functionalityName, requestShapes, null, null);
            }

            return "true";
        }
        /// <summary>
        /// Seteaza nivelul de incarcare pe FFS, trimite nrul de cereri specificat dupa atingerea nivelului de 
        /// incarcare pe FFS si masoara timpul de raspuns al FFS-ului pentru cererile trimise.
        /// </summary>
        /// <param name="functionalityName">Numele functionalitatii din cadrul configuratiei de testare</param>
        /// <param name="requestShapes">Termenii cererii din cadrul configuratiei de testare</param>
        /// <param name="minLoadLevel">Nivelul de incarcare al FFS din cadrul configuratiei de testare</param>
        /// <param name="nrRequests">Nrul de cereri trimise dupa atingerea 'minLoadLevel' de catre FFS</param>
        /// <returns></returns>
        private TimeSpan spamRequest(string functionalityName, RequestShape[] requestShapes, int minLoadLevel, int nrRequests)
        {
            string actualServerLoadStatus = this.SmartSpammer.setServerLoadLevel(minLoadLevel, functionalityName, requestShapes);

            if (actualServerLoadStatus == null)
            {

                if (this.SmartSpammer.lastError != null)
                {
                    MessageBox.Show(this.SmartSpammer.lastError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    return TimeSpan.Zero;
                }
            }
            else
            {

                TimeSpan results = TimeSpan.Zero;

                Functionality simpleFunctionality = new Functionality(functionalityName);
                Functionality completeFunctionality = this.SmartSpammer.getOntology(simpleFunctionality);

                if (actualServerLoadStatus.Equals("true"))
                {

                    DateTime startTime = DateTime.Now;

                    for (int i = 0; i < nrRequests; i++)
                    {

                        this.SmartSpammer.findServices(completeFunctionality, requestShapes);
                    }

                    DateTime stopTime = DateTime.Now;

                    results = stopTime - startTime;

                    return results;

                }

                else
                {
                    MessageBox.Show("Server Load not achieved !");

                    return TimeSpan.Zero;
                }
            }

            return TimeSpan.Zero;
        }
        /// <summary>
        /// Metoda care realizeaza procedura de testare in mod manual, adica ruleaza o singura configuratie de testare,
        /// incarcand apoi Form-ul cu timpul de raspuns pentru respectiva configuratie testata.
        /// </summary>
        private void spamManually()
        {
            string functionalityName="";
            List<RequestShape> requestShapes = new List<RequestShape>();
            string[] serializedRequestShapes;
            int minLoadLevel = 0;
            int noOfReqAfterLoadLevelAchieved = 0;
            string[] testDetails = new string[1];

            if (this.lvTestConfigs.SelectedItems.Count == 0)
            {
                MessageBox.Show("Select a test configuration from the list first !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            ListViewItem lvi = this.lvTestConfigs.SelectedItems[0];

            functionalityName = lvi.SubItems[0].Text;

            serializedRequestShapes = (string[])lvi.Tag;

            minLoadLevel = int.Parse(lvi.SubItems[2].Text);

            noOfReqAfterLoadLevelAchieved = int.Parse(lvi.SubItems[3].Text);

            for (int i = 0; i < serializedRequestShapes.Length; i++)
            {
                RequestShape rs = new RequestShape(serializedRequestShapes[i]);

                requestShapes.Add(rs);
            }
            string[] result = new string[1];

            TimeSpan results = spamRequest(functionalityName, requestShapes.ToArray(), minLoadLevel, noOfReqAfterLoadLevelAchieved);

            result[0] = 1 + "*" + results.TotalSeconds / noOfReqAfterLoadLevelAchieved + "*" + results.TotalMilliseconds / noOfReqAfterLoadLevelAchieved;

            testDetails[0] = functionalityName + "*" + minLoadLevel + "*" + noOfReqAfterLoadLevelAchieved + "*" + requestShapes.Count;

            this.ActiveControl = null;

            Thread thread = new Thread(delegate()
            {

                Application.Run(new SmartSpammerResultsForm(testDetails, result));
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        /// <summary>
        /// Metoda care realizeaza procedura de testare in mod automat luand pe rand fiecare configuratie de testare
        /// prezenta in lista din SmartSpammer si care a fost incarcata in prealabil. Incarca Form-ul cu rezultatele
        /// constand in timpii de raspuns pentru fiecare din configuratiile de testare rulate.
        /// </summary>
        private void spamAutomatically()
        {
            string functionalityName = "";
            List<RequestShape> requestShapes = new List<RequestShape>();
            string[] serializedRequestShapes;
            int minLoadLevel = 0;
            int counter = 0;
            int[] noOfReqAfterLoadLevelAchieved = new int[lvTestConfigs.Items.Count];
            TimeSpan[] results=new TimeSpan[lvTestConfigs.Items.Count];
            string[] testDetails = new string[lvTestConfigs.Items.Count];

            foreach (ListViewItem lvi in this.lvTestConfigs.Items)
            {
                functionalityName = lvi.SubItems[0].Text;

                serializedRequestShapes = (string[])lvi.Tag;

                minLoadLevel = int.Parse(lvi.SubItems[2].Text);

                noOfReqAfterLoadLevelAchieved[counter] = int.Parse(lvi.SubItems[3].Text);

                for (int i = 0; i < serializedRequestShapes.Length; i++)
                {
                    RequestShape rs = new RequestShape(serializedRequestShapes[i]);

                    requestShapes.Add(rs);
                }

                results[counter] = spamRequest(functionalityName, requestShapes.ToArray(), minLoadLevel, noOfReqAfterLoadLevelAchieved[counter]);
                testDetails[counter] = functionalityName + "*" + minLoadLevel + "*" + noOfReqAfterLoadLevelAchieved[counter] + "*" + requestShapes.Count;

                counter++;
            }

            string[] finalResults = new string[results.Length];

            for (int i = 0; i < results.Length; i++)
            {
                finalResults[i] = i+1 + "*" + results[i].TotalSeconds / noOfReqAfterLoadLevelAchieved[i] + "*" + results[i].TotalMilliseconds / noOfReqAfterLoadLevelAchieved[i];
            }

            this.ActiveControl = null;

            Thread thread = new Thread(delegate()
            {

                Application.Run(new SmartSpammerResultsForm(testDetails, finalResults));
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }