API Code examples

// crappy, dirty and quick API for MELODa 4.10 reusability metric
// full model in http://www.meloda.org/wp-content/uploads/2016/10/Meloda4.102.pdf
// More academic info about it in http://www.meloda.org/scientific-article-about-meloda/

// Copyleft Alberto Abella García alberto.abella@meloda.org

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// format it and check if they have more than one parameter
$respuesta=format_parameters($request);

// connect to the mysql database
// this user has only rights to select into the database
$link = mysqli_connect(“localhost”, “calendb0_api”, “meloda410”, “calendb0_meloda”);

//mysqli_set_charset($link,’utf8′);

// if a command has been detected in the URL
// otherwise returns nothing
if ($respuesta[“command”]<>”No command”)
{
// the different action allowed in this API, currently WEIGHT, TEXT, MELODA
switch($respuesta[“command”]):

case “WEIGHT”:
// command WEIGHT returns the weight for the level of a specific dimension
// example
// http://meloda.org/api/api.php/weight/data_model/3
// it asks for the WEIGHT of dimension data model in its level 3
//exmaple
//http://meloda.org/api/api.php/weight/4/3
// it returns the same as the previous one becuase data_model is the parameter 4
// check variable $dimension at the beginning of this code

// check if the # of paramters is the right one for this action
if ($respuesta[“parameters_number”]==3)
{
// check if the parameters is asked by its number
if (strlen($request[1])==1) // parameter send directly
{
$param=$request[1];
}
else
{
//looking for the name of the parameter in the array $dimension
$param=array_search($request[1], $dimensions); // founding the string of parameter in the dimensions

case “TEXT”:
// instruction for retrieving a parameter and level value based
// on the name of a standard

if ($respuesta[“parameters_number”]==2)
{
// looking for the text into the database
// parameters are stored in a text field separated with commas
// that’s why the search string is concatenated with a comma at the end
$sql=”SELECT * FROM `M_API_values` WHERE LOWER(`Texts`) LIKE LOWER(\”%”.$request[1].”,%\”)”;

if ($respuesta[“parameters_number”]==7)
{
$product=1; // variable to store the result of the product
$meloda_ok=TRUE; // vairable to check if the sent parameters are in range
for ($i=1;$i<7;$i++) // loop for the 6 dimensions
{
$dimen=$dimensions[$i]; // auxiliar variable
$URL=”http://meloda.org/api/api410.php/weight/”.$dimen.”/”.$respuesta[$i]; // look for the weight with this API

// retrieve the element using the API
$pre_answer=(array)retrieve_URL($URL);

// it is only needed the value not the full object
// but it is embeded into an object that’s why we need to select the element 0
$answer=(array)json_decode($pre_answer[“0”]);

$meloda_ok=$meloda_ok*$answer[“result”]; // checking if the parameters were valid
$product=$product*$answer[“value”]; // multiplying weights for each level’s dimension