开发者

XML Architecture

For my application I have to use a undefined number of different detection strategies. A strategy is defined in the following fashion:

XML Architecture

The AND gate can also be a OR gate. For now, I hard coded all these rules in my code. For better extensability, I'd like to define all the detection startegies respectively rules in a XML file and parse it. But I'm not really sure how I can define such a detection strategy in a XML file. Any 开发者_如何转开发hints how to start?


Look at RuleML http://ruleml.org/

RuleML (Rule Markup Language) is a unifying family of XML-serialized rule languages spanning across all industrially relevant kinds of Web rules. It accommodates and extends other recent rule languages, building interoperation bridges between them. The Specification of RuleML is currently being developed from Version 0.91 to Version 1.0.

also

The goal of the Rule Markup Initiative is to develop RuleML as the canonical Web language for rules using XML markup, formal semantics, and efficient implementations.

RuleML covers the entire rule spectrum, from derivation rules to transformation rules to reaction rules. RuleML can thus specify queries and inferences in Web ontologies, mappings between Web ontologies, and dynamic Web behaviors of workflows, services, and agents.

This should avoid you reinventing the wheel. Also the XML is only part of it, if you roll your own you also have to build the toolkit.

EDIT: Also look at Drools (Java-specific) http://www.jboss.org/drools


Can they get any more complicated than in your diagram?

If not, something like the below, which just translates the structure of your diagram into

<Strategy>
    <Name>Test-1</name>
    <Quality-problem>Too much splurge in the windler<Quality-problem>
    <Gate type="AND">
       <Left>
           <Metric>foo</Metric>
           <Comparison>less-than</Comparison>
           <Threshold>100</Threshhold>
       </Left>
       <Right>
           <Metric>bar</Metric>
           <Comparison>greater-than</Comparison>
           <Threshold>200</Threshhold>
       </Right>
    </Gate>
</Strategy>

If they do get more complex, you can still use something like the above, just a little more complex. You can/shuld write a XML schema to describe what you come up with so you can use standard validation tools, validating parsers etc.


If I have understood the problem you could make an XML file with this structure:

<root>
    <rules>
        <rule name="rule1" value="METRIC 1 > ThreShold 1" />
        <rule name="rule2" value="METRIC 2 > ThreShold 2" />
    </rules>
    <connectors>
        <port type="AND">
            <value name="rule1" />
            <value name="rule2" />
        </port>
    </connectors>
</root>

And then parse it with LINQ.

Port Class

class Port
{
    public PortType Type 
    { 
        get;
        set; 
    }

    public IEnumerable<Rule> Rules 
    { 
        get; 
        set;
    }

    public Port(PortType type, IEnumerable<Rule> rules)
    {
        this.Type = type;
        this.Rules = rules ?? Enumerable.Empty<Rule>();
    }
}

enum PortType
{
    AND,
    OR
}

Rule Class

class Rule : IEquatable<Rule>
{
    public string Value 
    { 
        get; 
        set;
    }

    public string Name 
    { 
        get;
        set; 
    }

    public Rule(string name, string value)
    {
        this.Name = name;
        this.Value = value;
    }

    public bool Equals(Rule other)
    {
        return this.Name == other.Name;
    }

    public override int GetHashCode()
    {
        return this.Name.GetHashCode();
    }
}

LINQ

        XDocument document = XDocument.Parse(xml);
        XElement root = document.Root;

        var rules = from rule in root.Element("rules").Elements()
                    let name = rule.Attribute("name").Value
                    let value = rule.Attribute("value").Value
                    select new Rule(name, value);

        var ports = from port in root.Elements("connectors").Elements()
                    let portType = (PortType)Enum.Parse(typeof(PortType), port.Attribute("type").Value)
                    let portRules = from rule in port.Elements()
                                    let name = rule.Attribute("name").Value
                                    select new Rule(name, null)
                    select new Port(portType, rules.Intersect(portRules));

EDIT: If you need to parse rule value you can just expand value attribute in XML and then parsing it with LINQ.


Two possiblies come to mind:

1 - Are the rules only comparing numeric values? In that case Xml could work.

<Rules>
<Rule name="MyRule" left="3" operation=">" right="1" />
</Rules>

2 - You could also define a class that stores your detection strategies. Once you've discovered all the strategies simply serialize the class and save it to the file-system or a database.


There exist standards for this kind of thing, like XCL. Does look like a bit of overkill though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜