0

Imagine I have a set of choices having different set of values but also the chance to input a free value (usually a number)

e.g.

<select name="choiceA">
    <option value="One"> 
    <option value="Two"> 
    ....
    <option value="Nth">
</select>

or

<input name="freeNumChoice type="number"/>

Not all combinations are possible so I need to represent rules of exclusion in some data structure.

I'm not asking for language syntax but more on how to data strcture my rules for combinations.

Programming language would be javascript and data for rules would be expressed as JSON documents.

Example (pseudo code) possible rules

choiceA whitelist only o1,o2,o3 (options) for choiceB

choiceE blacklists choiceA's o1

choiceC if greater than 5 then whitelist only o1 for choiceB

what would you suggest? is there around ant theory about this problem (looks like to be a common one)

thanks

  • 1
    Usually Rules are implemented using a decorator pattern, each one having it's own specific parameters. So you would have a rule of type whitelist. Another rule blacklisting data from another rules and the last poiting applying B only if the value is <=5. – Walfrat Feb 14 '17 at 8:24
3

I've once written a web application that have a series of complex options that depends on each other. The approach I used to store this relationship and rules in a database is to represent the rules as a state machine.

I designed a state machine using a graphical state machine editor (I used Fizzim), and then I converted the output file to a number of entries in two database tables: states and transitions. In my design, in the states' attributes I write the rules that limits the choices available to the user at that point, and in the transitions' attributes I write rules that represents the conditions that the choice has to fulfill to move to the next nodes.

I then have the server outputs the states and transition rules to a format that Javascript can read. The Javascript uses this state machine tables to show/hide options/fields based on user selections and the state and transitions' rules, and I also have a server side script that validates the choices that the user submits based on the same state and transition table.

The result is very robust as I only need to implement code to interpret the conditions in each states, rather than having to worry about all the combinations.

  • I m really curious to have a look at your Javascript output structure about states and transitions – koalaok Feb 14 '17 at 21:24
2

For theory you should probably start with the constraint satisfaction program. The way I would go about this is create a validation method for each kind of rule and add a type field to map to the method. You can even use javascript to look up the function directly from this name. The first parameter would be the value you want to check and the rest of the data in the json object would be the remaining parameters which your function will use to evaluate the data. This allows you to have optional parameters in your rule.

IMPORTANT: You should not rely on client-side validation. Anything you are doing with javascript in the user's browser can be subverted. You also need to ensure that your validations are implemented on the server side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.