Commit ed53cc2e authored by Floreal Cabanettes's avatar Floreal Cabanettes
Browse files

Change rules + update doc

parent c3c8b552
......@@ -1101,12 +1101,12 @@ bwamem = self.add_component("BWAmem", [bwaindex.databank, self.reads])</code></p
<p>To create the rule, create a class that inherit from the LinkRule class:</p>
<div>
<pre class="pre-hl "><code class="python">class YourRule (ConditionalLinkRule):
<pre class="pre-hl "><code class="python">class YourRule (ConditionalRule):
def check():
# Your code</code></pre>
</div>
<p>Into the <code>check()</code> function, write how to check the rule.</p>
<p>As the rule inherit from the ConditionalLinkRule class, it has the same attributes a Link rule have. But it has
<p>As the rule inherit from the ConditionalRule class, it has the same attributes a Link rule have. But it has
also additional attributes:</p>
<table class="table table-bordered table-striped">
<thead>
......
......@@ -56,9 +56,9 @@ class LinkRule (SimpleRule):
pass
class ConditionalLinkRule(LinkRule):
def __init__(self, user_args, wf_instance, src_arg, targets_args, conditions, which, all_files=None):
LinkRule.__init__(self, user_args, wf_instance, src_arg, targets_args, all_files)
class ConditionalRule(SimpleRule):
def __init__(self, user_args, wf_instance, src_arg, conditions, which, all_files=None):
SimpleRule.__init__(self, user_args, wf_instance, src_arg, all_files)
self.conditions = conditions
# Check parameter is correct:
......@@ -68,22 +68,31 @@ class ConditionalLinkRule(LinkRule):
self.condition_raised = False
all_raised = True
for condition in self.conditions:
c_match = re.match(r"(\w+)(!)?=(.+)", condition)
c_match = re.match(r"([\w>]+)(!)?=(.+)", condition)
name_arg = c_match.group(1)
if name_arg == "self":
name_arg = self.parameter_name
test_val = c_match.group(3)
is_equal = c_match.group(2) is None
#TODO: considère-t-on que si on a un !valeur, si le paramètre en question pas définit => condition is raised ?
#MA REPONSE : si on a !=* oui (et c'est déjà codé), sinon non
if name_arg in self.user_args:
if (not is_equal and str(self.user_args[name_arg]) != str(test_val[1:])) or (is_equal and
str(self.user_args[name_arg]) == str(test_val)):
str(self.user_args[name_arg]) == str(test_val)) or (is_equal and test_val == "*" and
len(str(self.user_args[name_arg])) > 0):
if which == "ANY":
self.condition_raised = True
break
elif which == "ALL":
all_raised = False
break
elif is_equal and test_val == "None":
if which == "ANY":
self.condition_raised = True
break
elif which == "ALL":
all_raised = False
if which == "ALL" and all_raised:
self.condition_raised = True
......@@ -91,7 +100,7 @@ class ConditionalLinkRule(LinkRule):
if isinstance(self.wf_parameter, InputFile) or isinstance(self.wf_parameter, InputFileList) or \
isinstance(self.wf_parameter, InputDirectory) or isinstance(self.wf_parameter, MultiParameter) or \
isinstance(self.wf_parameter, MultiParameterList):
raise Exception("Rule " + str(self.__class__.__name__) + " (ConditionalLinkRule) is not available on files "
raise Exception("Rule " + str(self.__class__.__name__) + " (ConditionalRule) is not available on files "
"and directories parameters or multiple parameters")
@abstractmethod
......@@ -117,20 +126,19 @@ class ToBeRequired(LinkRule):
" to be defined")
class RequiredIf(ConditionalLinkRule):
class RequiredIf(ConditionalRule):
def check(self):
if self.condition_raised is not None:
if self.condition_raised:
if self.parameter_name not in self.user_args or not self.user_args[self.parameter_name]:
raise Exception("Argument " + self.parameter_name + " is required, due to it's required rule")
class ActivateIf(ConditionalLinkRule):
class ActivateIf(ConditionalRule):
def check(self):
if not self.condition_raised:
for target in self.targets_args:
if target in self.user_args and \
str(self.user_args[target]) != str(self.wf_instance.__dict__[target]):
print("\033[93mWarning: parameter " + target + " is ignored: " + self.parameter_name +
" is set to " + str(self.user_args[self.parameter_name]) + " \033[0m")
if not self.condition_raised and self.parameter_name in self.user_args and \
self.user_args[self.parameter_name] is not None and \
str(self.user_args[self.parameter_name]) != str(self.wf_instance.__dict__[self.parameter_name]):
print("\033[93mWarning: parameter " + self.parameter_name + " is ignored due to its ActivateIf rule "
"\033[0m")
......@@ -1284,7 +1284,6 @@ class Workflow(threading.Thread):
@param rule: string describing the rule
@param src_arg: the argument containing the rule (string)
@param user_args: all arguments given by the user, with their values: {parameter_name1: value, ...}
@param parameters: all workflow parameters objects as dict: {parameter_name1: parameter_object1, ...}
@param all_files: all files given by the user
"""
......@@ -1293,24 +1292,22 @@ class Workflow(threading.Thread):
# There is 3 types of rules: #
# - SimpleRule: <name> #
# - LinkRule: <name>=<targets> #
# - ConditionalLinkRule: <name>?[<conditions>]=<targets> #
# - ConditionalRule: <name>?[<conditions>]=<targets> #
# With: #
# - <targets> = target1,target2,... #
# - <conditions> = condition1,condition2,... ("," is an "or") #
###############################################################
# Regexp for special rules:
conditional_link_rule = r"(.+)\?(ANY|ALL)\[(.+)\](=(.+))?"
conditional_rule = r"^(.+)\?(ANY|ALL)\[(.+)\]$"
link_rule = r"(.+)=(.+)"
is_conditional_link_rule = re.match(conditional_link_rule, rule) # Check first special rule
if is_conditional_link_rule:
is_conditional_rule = re.match(conditional_rule, rule) # Check first special rule
if is_conditional_rule:
# Check this special rule:
name = is_conditional_link_rule.group(1)
which = is_conditional_link_rule.group(2)
conditions = is_conditional_link_rule.group(3).split(",")
targets = is_conditional_link_rule.group(5)
targets = targets.split(",") if targets is not None else None
name = is_conditional_rule.group(1)
which = is_conditional_rule.group(2)
conditions = is_conditional_rule.group(3).split(",")
# Get validator:
if hasattr(wf_rules, name): # Check the rule exists
......@@ -1321,8 +1318,9 @@ class Workflow(threading.Thread):
raise Exception("Rule is not defined: " + name)
# Launch validator:
if issubclass(validator_class, j_rules.ConditionalLinkRule): # Check the rule is the same special rule
validator = validator_class(user_args, self, src_arg, targets, conditions, which, all_files)
if issubclass(validator_class, j_rules.ConditionalRule): # Check the rule is the same special rule
print(validator_class)
validator = validator_class(user_args, self, src_arg, conditions, which, all_files)
validator.check()
else:
raise Exception("Rule is not a conditional link rule: " + name)
......
......@@ -739,13 +739,13 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
* @param parameter_type: type of the parameter (str, bool, inputfile__s10, ...)
* @private
*/
var _add_event_rule_to_parameter = function (parameter_name, rule, parameter_type) {
var _add_event_rule_to_parameter = function (parameter_name, rule, parameter_type, $this) {
var ruleExclude = /Exclude=(.+)/;
var ruleExcludeIf = /ExcludeIf\?(ANY|ALL)\[(.+)\]=(.+)/;
var ruleToBeRequired = /ToBeRequired=(.+)/;
//var ruleToBeRequiredIf = /ToBeRequiredIf\?\[(.+)\]=(.+)/;
var ruleRequiredIf = /RequiredIf\?(ANY|ALL)\[(.+)\]/;
var ruleActivateIf = /ActivateIf\?(ANY|ALL)\[(.+)\]=(.+)/;
var ruleActivateIf = /ActivateIf\?(ANY|ALL)\[(.+)\]/;
var match;
/**
......@@ -869,13 +869,28 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
for (var param in conditions_params) {
var obj_param = $("#" + param);
if (obj_param.length > 0) {
var value = obj_param.val();
var value = obj_param.val();
}
else if (param.indexOf(">") > -1) {
var data = $("#handsontable_" + param.split(">")[0]).handsontable("getData");
var column_name = param.replace("_", "-").replace(">", "___")
for (var i in data) {
var line = data[i]
value = line[column_name]
}
}
else if (which == "ALL") {
all_raised = false;
break;
}
if (value !== undefined) {
if (obj_param.is("input[type=checkbox]")) {
value = obj_param.is(":checked") ? "True" : "False";
}
var c_values = conditions_params[param].values;
if ((conditions_params[param].equal && ("*" in c_values && value != "" || c_values.indexOf(value) > -1)) ||
(!conditions_params[param].equal && ("*" in c_values && value == "" || c_values.indexOf(value) == -1))) {
if ((conditions_params[param].equal && ("*" in c_values && value != "" || c_values.indexOf(value) > -1
|| "None" in c_values && value == "")) ||
(!conditions_params[param].equal && c_values.indexOf(value) == -1)) {
if (which == "ANY") {
condition_raised = true;
break;
......@@ -886,10 +901,6 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
break;
}
}
else if (which == "ALL") {
all_raised = false;
break;
}
}
if (which == "ALL" && all_raised) {
condition_raised = true;
......@@ -898,23 +909,18 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
return condition_raised
}
var __check_activate_targets = function(parameter_object, targets) {
var __check_activate_targets = function() {
var condition_raised = __check_condition_raised();
for (var t in targets) {
var target = $("#" + targets[t]);
if (target.length > 0) {
if (condition_raised) {
target.closest("div.form-group").show()
target.removeClass("hidden-exclude")
}
else {
target.closest("div.form-group").hide()
target.addClass("hidden-exclude")
}
}
}
if (condition_raised) {
param_input.closest("div.form-group").show()
param_input.removeClass("hidden-exclude")
}
else {
param_input.closest("div.form-group").hide()
param_input.addClass("hidden-exclude")
}
}
var __check_required = function() {
......@@ -930,7 +936,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
var __get_conditions_params = function() {
var conditions_params = {};
var re_cond = /(\w+)(!)?=(.*)/;
var re_cond = /([\w>]+)(!)?=(.*)/;
for (var c in conditions) {
var condition = conditions[c];
var match = null;
......@@ -948,6 +954,23 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
return conditions_params;
}
var __add_event = function(calling_function, params) {
for (var p in params) {
var param = params[p];
if (param.indexOf(">") == -1) {
$("#" + param).on("change", function() {
calling_function();
});
}
else {
$this.$element.on("change_" + param.replace("_", "-").replace(">", "___"), function(e, value) {
//calling_function();
console.log("coucou", value);
});
}
}
}
//Check rule Exclude
if (match = rule.match(ruleExclude)) {
var targets = match[1].split(",");
......@@ -982,16 +1005,13 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
//Check rule ActivateIf
else if (match = rule.match(ruleActivateIf)) {
var which = match[1]
var which = match[1]
var conditions = match[2].split(",");
var conditions_params = __get_conditions_params();
var tags = "#" + Object.keys(conditions_params).join(", #");
var targets = match[3].split(",");
var param_input = $("#" + parameter_name);
$(tags).on("change", function() {
__check_activate_targets(param_input, targets);
});
__check_activate_targets(param_input, targets);
__add_event(__check_activate_targets, Object.keys(conditions_params));
__check_activate_targets();
}
//Check rule RequiredIf
......@@ -999,11 +1019,14 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
var which = match[1]
var conditions = match[2].split(",");
var conditions_params = __get_conditions_params();
console.log(parameter_name)
console.log(Object.keys(conditions_params))
var tags = "#" + Object.keys(conditions_params).join(", #");
var param_input = $("#" + parameter_name);
$(tags).on("change", function() {
/*$(tags).on("change", function() {
__check_required();
});
});*/
__add_event(__check_required, Object.keys(conditions_params))
__check_required();
}
......@@ -1014,7 +1037,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
* @param parameters: list of parameters
* @private
*/
var _check_parameters_rules = function(parameters) {
var _check_parameters_rules = function(parameters, $this) {
for (var i in parameters) {
var parameter = parameters[i];
var rules, r, rule;
......@@ -1022,7 +1045,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
rules = parameter.rules.split(";")
for (r in rules) {
rule = rules[r];
_add_event_rule_to_parameter(parameter.name, rule, parameter.type)
_add_event_rule_to_parameter(parameter.name, rule, parameter.type, $this)
}
}
if (parameter.type == "MultipleParameters" && parameter.sub_parameters) {
......@@ -1032,7 +1055,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
rules = parameter.rules.split(";");
for (r in rules) {
rule = rules[r];
_add_event_rule_to_parameter(parameter.name, rule, parameter.type)
_add_event_rule_to_parameter(parameter.name, rule, parameter.type, $this)
}
}
}
......@@ -1246,7 +1269,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
$("#workflow_form").validate({ rules: rules });
_check_parameters_rules($this.workflow.parameters);
_check_parameters_rules($this.workflow.parameters, $this);
$(".to-readonly").on("focusin", function(event) {
$(this).prop('readonly', true);
......@@ -1389,6 +1412,8 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
$("#error_handsontable_"+param_name).hide();
},
afterValidate: function(isValid, value, row, prop, source) {
console.log(prop)
$this.$element.trigger("change_" + prop, value);
if (!isValid) {
// do not add this error if it's on the last row
if (this.countRows() == 1 || this.countRows()-1 != row) {
......
......@@ -15,7 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from jflow.rules import SimpleRule, LinkRule, ConditionalLinkRule
from jflow.rules import SimpleRule, LinkRule, ConditionalRule
# Define your rules check below
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment