All files / universal/gpii/node_modules/flatMatchMaker/src FlatMatchMaker.js

86.96% Statements 20/23
87.5% Branches 7/8
83.33% Functions 5/6
86.96% Lines 20/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                      1x     1x 1x   1x   1x                                                   1x                           1x                       1x 1x 1x   1x 1x 1x                     1x 2x 4x 10x   4x     4x         2x      
/*
* Flat Match Maker
*
* Copyright 2014 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The research leading to these results has received funding from the European Union's
* Seventh Framework Programme (FP7/2007-2013)
* under grant agreement no. 289016.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
 
/* eslint-env browser */
/* eslint strict: ["error", "function"] */
 
(function () {
    "use strict";
 
    var fluid = fluid || require("infusion"),
        gpii = fluid.registerNamespace("gpii");
 
    require("kettle");
 
    fluid.defaults("gpii.flatMatchMaker", {
        gradeNames: ["kettle.app"],
        components: {
            ontologyHandler: {
                "type": "gpii.ontologyHandler"
            }
        },
        requestHandlers: {
            matchPost: {
                route: "/match",
                method: "post",
                type: "gpii.flatMatchMaker.matchPost.handler"
            }
        },
        invokers: {
            match: {
                funcName: "gpii.flatMatchMaker.match",
                args: [ "{ontologyHandler}", "{arguments}.0", "{flatMatchMaker}.disposeStrategy"]
            },
            disposeStrategy: {
                funcName: "gpii.flatMatchMaker.disposeStrategy",
                args: [ "{arguments}.0", "{arguments}.1"]
            }
        }
    });
 
    fluid.defaults("gpii.flatMatchMaker.matchPost.handler", {
        gradeNames: ["kettle.request.http"],
        invokers: {
            handleRequest: {
                funcName: "gpii.flatMatchMaker.matchPost.handleRequest",
                args: [ "{gpii.flatMatchMaker}", "{request}.events.onSuccess", "{request}.req.body" ]
            }
        }
    });
 
    /*
     * Initial function to be called when the user hits the /match URL. Simply calls the
     *  gpii.flatMatchMaker.match function and fires the 'onSuccess' event when it returns
     */
    gpii.flatMatchMaker.matchPost.handleRequest = function (flatMatchMaker, onSuccess, body) {
        var matchedSolutions = flatMatchMaker.match(body);
        onSuccess.fire(matchedSolutions);
    };
 
    /*
     * Main function of the flat matchmaker. Ensures that the inferred common terms are taken
     * into account and that the preferences are stored in a hierarchical format, needed by
     * the flat MM, and then runs the framework's 'disposeSolutions' function with the
     * flatMatchMaker.disposeStrategy strategy. The correct output format is ensured by the
     * buildReturnPayload function of the MM framework.
     */
    gpii.flatMatchMaker.match = function (ontologyHandler, payload, disposeStrategy) {
        payload.preferences = gpii.matchMakerFramework.utils.addInferredCommonTermsToPreferences(payload.preferences, payload.inferredCommonTerms);
        payload.hierarchicalPrefs = ontologyHandler.prefsToOntology(payload.preferences, "flat", "ISO24751");
 
        var transformSpec = gpii.ontologyHandler.getTransformSpec(ontologyHandler.ontologyTransformSpecs, "flat", "ISO24751");
        var disposed = gpii.matchMakerFramework.utils.disposeSolutions(payload, disposeStrategy, transformSpec);
        return gpii.matchMakerFramework.utils.buildReturnPayload(payload, disposed);
    };
 
    /*
     * Very simple strategy that accepts any solution for which any preference el-path matches
     * a one of the solutions capabilities (inferred from the capabilities transformations and
     * capabilities block of that solution).
     *
     * We consider that any accepted solution will be activated because of the simple-minded
     * policy adopted by the flat matchmaker
     */
    gpii.flatMatchMaker.disposeStrategy = function (leaves, solrecs) {
        fluid.each(solrecs, function (solrec) {
            var accepted = fluid.find(leaves, function (leaf) {
                return fluid.get(solrec.skeleton, leaf, fluid.model.escapedGetConfig);
            });
            Iif (solrec.priority !== undefined) {
                accepted = true;
            }
            fluid.extend(solrec, {
                disposition: accepted ? "accept" : "reject",
                active: accepted ? true : false // always true if accepted
            });
        });
        return solrecs;
    };
})();