예제 #1
0
 /**
  * Reads an Xml node representation of a rule-set element.
  * @param emptyElt the empty xml node.
  */
 public static RuleSet readXml(XmlNode ruleSetElt)
 {
     RuleSet rs = null;
     if (ruleSetElt == null) return rs;
     Log log = Log.getOnly();
     if (ruleSetElt.HasChildNodes)
     { // it contains rules
         string id = XmlFiler.getStringAttr((XmlElement)ruleSetElt,"id",NoValue);
         if (id.Equals(NoValue))
         {
             log.writeElt("fail");
             log.writeAttr("node", ruleSetElt.Name);
             log.writeAttr("expected", "id");
             log.writeAttr("was", "not found");
             log.endElt();
             return rs;
         }
         string desc = XmlFiler.getStringAttr((XmlElement)ruleSetElt,"desc",NoValue);
         rs = new RuleSet(id, desc);
         XmlAttributeCollection attrs = ruleSetElt.Attributes;
         for (int a = 0; a < attrs.Count; a++)
         { // read all attributes
             XmlNode attr = attrs.Item(a);
             string nn = attr.Name;
             if (!nn.Equals("id") && !nn.Equals("desc"))
             {
                 rs.setParameter(attr.Name, attr.Value);
             }
         }
         XmlNodeList rules = ruleSetElt.ChildNodes;
         for (int r = 0; r < rules.Count; r++)
         {
             XmlNode ruleN = rules.Item(r);
             if (ruleN.NodeType == XmlNodeType.Element)
             {
                 Rule rule = Rule.readXml((XmlElement)ruleN);
                 rs.addRule(rule);
             }
         }
     }
     else // this is a truly empty node
     {
         log.writeElt("fail");
         log.writeAttr("node", ruleSetElt.Name);
         log.writeAttr("expected", "rules");
         log.writeAttr("was", "empty");
         log.endElt();
     }
     return rs;
 }
예제 #2
0
 private bool doSubGoal(RuleSet ruleSet, EmptyElement goal)
 {
     Sensact subGoal = new Sensact(m_ruleSets, m_sensors, m_actions);
     subGoal.setGoal(goal);
     return subGoal.act();
 }
예제 #3
0
 /**
  * Sets the goal for the rule-sets supplied.
  * @param A goal with the name of a rule-set and parameters.
  * @return true if the goal can be met by a rule-set.
  */
 public bool setGoal(EmptyElement goal)
 {
     if (m_ruleSets == null || m_ruleSets.Count == 0)
     {
         m_log.writeEltTime("fail");
         m_log.writeAttr("rule-set","none");
         m_log.endElt();
         return false;
     }
     if (m_goal == null && goal == null)
     {
         m_log.writeEltTime("fail");
         m_log.writeAttr("goal","none");
         m_log.endElt();
         return false;
     }
     if (goal != null) m_goal = goal; // overwrite m_goal
     else              goal = m_goal;
     m_log.writeEltTime("new-goal");
     goal.log();
     m_log.endElt();
     string name = goal.getName();
     foreach (RuleSet rs in m_ruleSets)
     {   if (name.Equals(rs.getName()))
         { m_ruleSet = rs; break; } // skip the rest of the rule-sets
     }
     if (m_ruleSet == null)
     {
         m_log.writeEltTime("fail");
         m_log.writeAttr("goal","no rule-set can solve");
         m_log.endElt();
         return false;
     }
     m_substitutes = new ArrayList(2);
     for (int p = 0; p < m_ruleSet.formals(); p++)
     { // goal attributes are named the same as rule-set parameters
         string paramName = m_ruleSet.getParameterName(p);
         string value = goal.getValue(paramName);
         string formal = m_ruleSet.getParameterValue(p);
         m_substitutes.Add(new Substitute(formal,value));
     }
     m_rules = m_ruleSet.getRules(); // use the active rule set
     return true;
 }