Skip to content

Conditional Command

Rubens de Oliveira Moraes Filho edited this page Apr 30, 2021 · 9 revisions

The framework provides a conditional if-then or if-then-else to be used to compose scripts. To support some examples, we will use the following script as support: if(HaveQtdUnitsHarversting(20)) (attack(Worker,weakest)) (attack(Ranged,weakest) attack(Light,weakest))
Where if(HaveQtdUnitsHarversting(20)) represents the if + boolean function, (attack(Worker,weakest)) represents then + action function, and (attack(Ranged,weakest) attack(Light,weakest)) represents the else + action function. The basic structure to produce this script, in code, is:

line 1. //boolean
line 2. iBooleanDSL boolC = new BooleanDSL("HaveQtdUnitsHarversting(20)");
line 3. //then
line 4. CommandDSL c1 = new CommandDSL("attack(Worker,weakest)");
line 5. CDSL cdslThen = new CDSL(c1);
line 6. //else
line 7. CommandDSL c2 = new CommandDSL("attack(Ranged,weakest)");
line 8. CommandDSL c3 = new CommandDSL("attack(Light,weakest)");
line 9. CDSL cdsl2 = new CDSL(c3);
line 10. CDSL cdslElse = new CDSL(c2, cdsl2);

Example 1. If-Then

The complete structure can be interpreted (composed) as:

IF (boolean function) Then(list(action functions))

Using the fragments above, we can compose a if-then structure by using the code:

new S2DSL(new S5DSL(boolC), cdslThen);

Example 2. If-Then-Else

The complete structure can be interpreted (composed) as:

IF (boolean function) Then(list(action functions)) Else(list(action functions))

Using the fragments above, we can compose a if-then-else structure by using the code:

new S2DSL(new S5DSL(boolC), cdslThen, cdslElse);

Example 3. Using BuilderDSLTreeSingleton

Class BuilderDSLTreeSingleton provides a method to get the structure If (S2DSL) in a simple way:

  • BuilderDSLTreeSingleton.getInstance().buildS2Grammar(true); -- Parameter true to compose if with parameter
  • BuilderDSLTreeSingleton.getInstance().buildS2Grammar(false);
  • BuilderDSLTreeSingleton.getInstance().buildS2ThenGrammar(true); -- Parameter true to compose if with parameter
  • BuilderDSLTreeSingleton.getInstance().buildS2ThenGrammar(false);
  • BuilderDSLTreeSingleton.getInstance().buildS2ElseGrammar(true); -- Parameter true to compose if with parameter
  • BuilderDSLTreeSingleton.getInstance().buildS2ElseGrammar(false);
Clone this wiki locally