<commit_before>// ==========================================================================
// Project:   MySystem.DiagramRule
// Copyright: ©2011 My Concord Consortium
// ==========================================================================
/*globals MySystem */

/** @class

  (Document your Model here)

  @extends SC.Record
  @version 0.1
*/
MySystem.rubricController = SC.ObjectController.create({

  // for now, just evaluate one rubric Item.
  // rubricExpressionBinding:    SC.Binding.oneWay('MySystem.activityController.rubricExpression'),
  categoriesBinding: SC.Binding.oneWay('MySystem.activityController.rubricCategories'),


  score: function(submit) {
    var self = this,
        rubricScore = MySystem.RubricScore.instance(),
        expression = MySystem.activityController.get('rubricExpression') || "",
        categories = MySystem.activityController.get('rubricCategories'),
        nodes      = MySystem.nodesController.get('content'),
        rules      = MySystem.activityController.get('diagramRules'),
        result     = MySystem.rubricController.UNSCORED,

        ruleResults = [],

        resultsOfCategories = categories.forEach(function(c) {
          var name  = c.get('name'),
              value = false,
              found = null,
              rulesInCategory = rules.filterProperty('category',name);

          found = rulesInCategory.find(function(r) {
            if (r.check(nodes, MySystem.rulesController) === true) {
              return true;
            }
          });
          if (found) {
            value = true;
          }
          ruleResults.push({name: name, value: value});
        }),

        category = function(categoryName) {
          var category = ruleResults.findProperty('name',categoryName);
          if (category) {
            return category.value;
          }
          return false;
        },

        hasScored = false,

        score = function(_score) {
          if(!hasScored) {
            hasScored = true;
            result = _score;
          }
        },

        categoryReduce = function(args,comparefunc) {

          // call like this: result = all('a','b','c',function(){...});
          var results = args.reduce(function(previousValue,current) {
            var a = previousValue,
                b = current;

            // convert items to booleans
            if (typeof a === 'string')   { a = category(a); }
            if (typeof b === 'string')   { b = category(b); }

            // run conditional block
            if (typeof b === 'function') {
              if (a === true) {
                b.apply(this);
                return a;
              }
            }

            // set score if last argument.
            if (typeof b === 'number') {
              if (a === true) {
                score(b);
                return a;
              }
            }

            // coerce to true or false
            return (!!comparefunc(a,b));
          });
          return results;
        },


        any = function() {
          var args = [].slice.apply(arguments);
          categoryReduce(args,function(a,b) {
            return (a||b);
          });
        },

        all = function() {
          var args = [].slice.apply(arguments);
          categoryReduce(args,function(a,b) {
            return (a&&b);
          });
        },

        errorMsg = "Rubric Evaluation Error: \n%@";


    (function(){
      try {
        //var script = CoffeeScript.compile(expression,{thing: 10});
        eval(expression);
      }
      catch(e) {
        errorMsg = errorMsg.fmt(e);
        if (console && typeof console.log == 'function') {
          console.log(errorMsg);
        }
        alert(errorMsg);
      }
    }).call(self);

    rubricScore.update(result, ruleResults.filterProperty('value').mapProperty('name'));
    //TODO: remove this alert!
    // if (submit) {
    //   alert("score: %@\ncategories: %@\nTime: %@".fmt(rubricScore.get('score'),rubricScore.get('categories'),rubricScore.get('timeStamp')));
    // }<commit_msg>Update inline documentation for how rubric works.<commit_after>// ==========================================================================
// Project:   MySystem.DiagramRule
// Copyright: ©2011 My Concord Consortium
// ==========================================================================
/*globals MySystem */

/** @class

  (Document your Model here)

  @extends SC.Record
  @version 0.1
*/
MySystem.rubricController = SC.ObjectController.create({

  // for now, just evaluate one rubric Item.
  // rubricExpressionBinding:    SC.Binding.oneWay('MySystem.activityController.rubricExpression'),
  categoriesBinding: SC.Binding.oneWay('MySystem.activityController.rubricCategories'),


  // An example rubric:
  // all ('noah-transform','storage','source','release', 6);
  // all ('noah-transform','storage', 5);
  // all ('noah-transform','source', 5);
  // all ('noah-transform','release', 5);
  // all ('noah-transform',4);
  // any('source','storage','release',3);
  // any('link',2);
  // score(1);
  score: function(submit) {
    var self = this,
        rubricScore = MySystem.RubricScore.instance(),
        expression = MySystem.activityController.get('rubricExpression') || "",
        categories = MySystem.activityController.get('rubricCategories'),
        nodes      = MySystem.nodesController.get('content'),
        rules      = MySystem.activityController.get('diagramRules'),
        result     = MySystem.rubricController.UNSCORED,

        ruleResults = [],

        resultsOfCategories = categories.forEach(function(c) {
          var name  = c.get('name'),
              value = false,
              found = null,
              rulesInCategory = rules.filterProperty('category',name);

          found = rulesInCategory.find(function(r) {
            if (r.check(nodes, MySystem.rulesController) === true) {
              return true;
            }
          });
          if (found) {
            value = true;
          }
          ruleResults.push({name: name, value: value});
        }),

        category = function(categoryName) {
          var category = ruleResults.findProperty('name',categoryName);
          if (category) {
            return category.value;
          }
          return false;
        },

        hasScored = false,

        score = function(_score) {
          if(!hasScored) {
            hasScored = true;
            result = _score;
          }
        },

        categoryReduce = function(args,comparefunc) {

          // call like this: result = all('a','b','c',function(){...});
          var results = args.reduce(function(previousValue,current) {
            var a = previousValue,
                b = current;

            // convert items to booleans
            if (typeof a === 'string')   { a = category(a); }
            if (typeof b === 'string')   { b = category(b); }

            // run conditional block
            if (typeof b === 'function') {
              if (a === true) {
                b.apply(this);
                return a;
              }
            }

            // set score if last argument.
            if (typeof b === 'number') {
              if (a === true) {
                score(b);
                return a;
              }
            }

            // coerce to true or false
            return (!!comparefunc(a,b));
          });
          return results;
        },


        any = function() {
          var args = [].slice.apply(arguments);
          categoryReduce(args,function(a,b) {
            return (a||b);
          });
        },

        all = function() {
          var args = [].slice.apply(arguments);
          categoryReduce(args,function(a,b) {
            return (a&&b);
          });
        },

        errorMsg = "Rubric Evaluation Error: \n%@";


    (function(){
      try {
        //var script = CoffeeScript.compile(expression,{thing: 10});
        eval(expression);
      }
      catch(e) {
        errorMsg = errorMsg.fmt(e);
        if (console && typeof console.log == 'function') {
          console.log(errorMsg);
        }
        alert(errorMsg);
      }
    }).call(self);

    rubricScore.update(result, ruleResults.filterProperty('value').mapProperty('name'));
{'fraction_non_alphanumeric': 0.16999153669447467, 'fraction_numerical': 0.0029017047515415306, 'mean_word_length': 1.869233437391606, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6030372', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2132, 'n_words': 448}