views:

544

answers:

4

I have the need to express simple conditionals as program input. The input must be human readable.

Is XML suited for this?

For example, I have the following conditional statement:

If AnimalType = Leopard And (SourceCountry = Kenya Or TargetCountry = Kenya) Then
    ZooKeeper=Jack
Else
    ZooKeeper=Jill
End If

Expressing the above in XML might look something like this:

<If>       
    <Conditional>   
     <And>  
      <AnimalType>Leopard<AnimalType> 
      <Or> 
       <SourceCountry>Kenya</SourceCountry>
       <TargetCountry>Kenya</TargetCountry>
      </Or> 
     </And>  
    </Conditional> 
    <True> 
     <ZooKeeper>Jack</ZooKeeper>
    </True> 
    <False> 
     <ZooKeeper>Jill</ZooKeeper>
    <False> 
</If>

Any ideas on how best to represent conditional statements in XML?

I haven't yet explored using attributes. I don't currently have the need for nested 'If' statements or the 'Else If' clause, but I'm going to try and work them in anyway.

Perhaps the VB code is more 'readable' than XML can be in this case and I should create a custom flat-file format instead.

+2  A: 

There's nothing wrong with XML for this, but you could save yourself some overhead with a lighter syntax like a LISP-style lists:

(if
    (and
         (AnimalType Leopard)
         (or
             (SourceCountry Kenya)
             (TargetCountry Kenya)
         )
     )
     (ZooKeeper Jack)
     (ZooKeeper Jill)
 )

You can use ordering of the items inside the if instead of explicitly declaring condition, true, false. The basic requirement that you'll have is that a proper syntax tree is preserved.

Eclipse
A: 

You said you wanted it to be human-readable, and then you presented a nearly impenetrable XML example. I'm not sure what XML is buying you here. A simple parser for a truly human-readable language would probably be a good investment of your time.

Ned Batchelder
A: 

Perhaps you've heard of or seen RuleML?

This seems to be what you're looking for. There is an open source rules engine that uses RuleML for .NET that I've used with C#: NxBRE

NxBRE also has a Visio stencil that makes looking at the rules easy for others to understand. They also experimented with a human readable format but I'm not sure that's still in progress or not.

Sean
A: 

Since XML is convenient in this case and VB is quite 'human readable', I think I'll go with something like this:

    <Conditional>
        If AnimalType = Leopard And (SourceCountry = Kenya Or TargetCountry = Kenya) Then
            ZooKeeper=Jack
        Else
            ZooKeeper=Jill
        End If
    </Conditional>
JRS