define(['rules/rules.module'
       ],
function() {
    angular.module('rules').factory('references',
    [
    function() {
        function generateReference(query) {
            var bindings = {};

            angular.forEach(query.bindings, function(binding) {
                if (('id' in binding) && ('name' in binding))  {
                    bindings[binding.name] = binding.id;
            });

            var info = { rule: query.rule,
                         query: query.query,
                         bindings: bindings,
                         constraints: query.constraints
            };
            var url = ('#/rules/explain?inference=' +
                       encodeURIComponent(angular.toJson(info, false)));
            var reference = { P854: [{ datatype: 'url',
                                       datavalue: { type: 'string',
                                                    value: url,
                                                    __sqid_display_string_override__: '(explain inference)'
                                                  },
                                       snaktype: 'value',
                                       property: 'P854'
                                     }]
                            };

            return [{ snaks: reference,
                      'snaks-order': ['P854']
                    }];
        }

        return { generateReference: generateReference
               };
    }]);

    return {};
});

@@ -100,16 +100,22 @@
 ',%0A    %5B
+'ast',
 %0A    fun
@@ -112,32 +112,35 @@
 ',%0A    function(
+ast
 ) %7B%0A        func
@@ -332,16 +332,16 @@
 ng))  %7B%0A
-
         
@@ -389,16 +389,421 @@
 ing.id;%0A
+                %7D%0A            %7D);%0A%0A            angular.forEach(ast.variables(query.rule.head), function(variable) %7B%0A                var name = variable.name;%0A                bindings%5Bname%5D = %7B id: query.bindings%5Bname%5D.id,%0A                                   type: query.bindings%5Bname%5D.type,%0A                                   qualifiers: query.bindings%5Bname%5D.qualifiers%0A                                 %7D;%0A
         

Include qualifiers on reference generation