//why not just Rules.Add()? //FUG public Rule AddRule(RuleType type, OntologyNode.Attribute attr, ISchemeComponent reference, OntologyNode.Attribute inputAttr)//, string value) { Rule rule = new Rule(attr, reference, inputAttr); _rules.Add(attr.Name, rule); return(rule); }
static NodeInfo.SectionInfo TerminVarAttrInfo(Argument argument, OntologyNode.Attribute attr, Vocabularies.Vocabulary vocabulary, NodeInfo nodeInfo) { NodeInfo.SectionInfo attrInfo = new NodeInfo.SectionInfo(); attrInfo.Data = attr; attrInfo.IsOutput = true; var attrName = new TerminAttribute(); attrName.RemoveAttrButton.Click += (s, e) => { Console.WriteLine("removed varattr"); argument.Attributes.Remove(attr); nodeInfo.Sections.Remove(attrInfo); // re-render node }; var vocList = vocabulary.ToList(); attrName.AttrNameComboBox.ItemsSource = vocList; attrName.AttrNameComboBox.SelectedIndex = vocList.FindIndex(t => t.Name == attr.Name); attrName.AttrNameComboBox.SelectionChanged += (s, e) => { attr.Name = e.AddedItems[0].ToString(); }; attrName.ToolTip = attr.AttrType; attrInfo.UIPanel = attrName; return(attrInfo); }
public Functor() { CID = UID.Get(); _name = "F_BASE"; _inputs = new List <FunctorInput>(); Params = new List <string>(); Output = new OntologyNode.Attribute(OntologyNode.Attribute.AttributeType.STRING, "output"); _numArgs = -1; _minArgs = 0; _defaultValue = null; }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="attr"></param> /// <param name="reference"></param> /// <param name="inputAttr">may be null if 'reference' is a functor</param> public Rule(OntologyNode.Attribute attr, ISchemeComponent reference, OntologyNode.Attribute inputAttr) { if (reference is Argument) { Type = RuleType.ATTR; } else if (reference is Functor) { Type = RuleType.FUNC; } else { Type = RuleType.DEF; } Attribute = attr; Reference = reference; InputAttribute = inputAttr; Default = ""; }
public static FactSchemeBank FromXml(XElement root, List <OntologyNode> ontology, Vocabularies.Vocabulary themes) { FactSchemeBank bank = new FactSchemeBank(); foreach (XElement xscheme in root.Elements()) { Scheme scheme = new Scheme(xscheme.Attribute(FatonConstants.XML_SCHEME_NAME).Value); scheme.Segment = xscheme.Attribute(FatonConstants.XML_SCHEME_SEGMENT)?.Value; if (scheme.Segment == null) { scheme.Segment = ""; } var arguments = from x in xscheme.Elements() where x.Name.LocalName == FatonConstants.XML_ARGUMENT_TAG select x; var results = from x in xscheme.Elements() where x.Name.LocalName == FatonConstants.XML_RESULT_TAG select x; var conditionComplexes = from x in xscheme.Elements() where x.Name.LocalName == FatonConstants.XML_CONDITIONCOMPLEX_TAG select x; var functors = from x in xscheme.Elements() where x.Name.LocalName == "Functor" select x; foreach (XElement xarg in arguments) { Argument arg = null; if (xarg.Attribute(FatonConstants.XML_ARGUMENT_OBJECTTYPE).Value.Equals(ArgumentType.TERMIN.ToString())) { Termin term; string termName = xarg.Attribute("ClassName").Value; if (termName.StartsWith("#")) { term = DiglexFunctions.LexFunctions.First(x => x.Name == termName); } else { term = themes[termName]; } arg = scheme.AddArgument(term); var varattrs = from x in xarg.Elements() where x.Name == "VarAttr" select x; foreach (var attr in varattrs) { arg.Attributes.Add(new OntologyNode.Attribute(OntologyNode.Attribute.AttributeType.STRING, attr.Attribute("Name").Value, true)); } } else { OntologyClass argKlass; foreach (OntologyClass klass in ontology) { argKlass = klass.FindChild(xarg.Attribute(FatonConstants.XML_ARGUMENT_CLASSNAME).Value); if (argKlass == null) { continue; } arg = scheme.AddArgument(argKlass); break; } } arg.Order = uint.Parse(xarg.Attribute(FatonConstants.XML_ARGUMENT_ORDER).Value); foreach (XElement xcond in xarg.Elements(FatonConstants.XML_ARGUMENT_CONDITION_TAG)) { var condition = new Argument.ArgumentCondition(); var attrName = xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_ATTRNAME).Value; var type = xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_TYPE).Value; var comparType = xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_OPERATION).Value; var value = xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_DATA).Value; condition.Operation = (ArgumentConditionOperation)Enum.Parse(typeof(ArgumentConditionOperation), comparType); condition.CondType = (ArgumentConditionType)Enum.Parse(typeof(ArgumentConditionType), type); condition.Data = value; var attr = arg.Attributes.Find(x => x.Name.Equals(attrName)); arg.Conditions[attr].Add(condition); } } foreach (XElement xres in results) { OntologyClass resKlass = null; Result result; foreach (OntologyClass klass in ontology) { resKlass = klass.FindChild(xres.Attribute(FatonConstants.XML_RESULT_CLASSNAME).Value); if (resKlass != null) { break; } } if (resKlass == null) { continue; } result = scheme.AddResult(resKlass, xres.Attribute(FatonConstants.XML_RESULT_NAME).Value); foreach (XElement xrul in xres.Elements()) { Result.RuleType ruleType = (Result.RuleType)Enum.Parse(typeof(Result.RuleType), xrul.Attribute(FatonConstants.XML_RESULT_RULE_TYPE).Value); OntologyNode.Attribute attr = result.Reference.AllAttributes.Find(x => x.Name == xrul.Attribute(FatonConstants.XML_RESULT_RULE_ATTR).Value); if (ruleType == Result.RuleType.ATTR) { Argument arg = scheme.Arguments.Find(x => x.Order == int.Parse(xrul.Attribute(FatonConstants.XML_RESULT_RULE_RESOURCE).Value)); OntologyNode.Attribute inputAttr = null; if (attr.AttrType != OntologyNode.Attribute.AttributeType.OBJECT) { inputAttr = arg.Attributes.Find(x => x.Name == xrul.Attribute(FatonConstants.XML_RESULT_RULE_ATTRFROM).Value); } var rule = result.AddRule(ruleType, attr, arg, inputAttr); rule.ResourceType = (RuleResourceType)Enum.Parse(typeof(RuleResourceType), xrul.Attribute(FatonConstants.XML_RESULT_RULE_RESOURCETYPE).Value); if (xrul.Attribute("Default") != null) { rule.Default = xrul.Attribute("Default").Value; } } if (ruleType == Result.RuleType.FUNC) { Functor fun = FunctorFactory.Build(xrul.Attribute(FatonConstants.XML_RESULT_RULE_FUNCTOR_NAME).Value); fun.CID = UID.Take(uint.Parse(xrul.Attribute(FatonConstants.XML_RESULT_RULE_FUNCTOR_ID).Value)); var inputs = xrul.Elements(FatonConstants.XML_RESULT_RULE_FUNCTOR_INPUT); fun.Inputs.Clear(); foreach (var xinput in inputs) { var resourceType = (RuleResourceType)Enum.Parse(typeof(RuleResourceType), xinput.Attribute(FatonConstants.XML_RESULT_RULE_FUNCTOR_RESOURCETYPE).Value); ISchemeComponent resource = null; OntologyNode.Attribute value = null; if (resourceType == RuleResourceType.ARG) { resource = scheme.Arguments.Find(x => x.Order == int.Parse(xinput.Attribute(FatonConstants.XML_RESULT_RULE_RESOURCE).Value)); value = ((Argument)resource).Attributes.Find(x => x.Name == xinput.Attribute(FatonConstants.XML_RESULT_RULE_FUNCTOR_ATTRFROM).Value); } else { }; var input = new Functor.FunctorInput("input"); input.Set(value, resource); fun.Inputs.Add(input); } scheme.Components.Add(fun); var rule = result.AddRule(ruleType, attr, fun, fun.Output); if (xrul.Attribute("Default") != null) { rule.Default = xrul.Attribute("Default").Value; } } } if (xres.Attribute(FatonConstants.XML_RESULT_ARGEDIT) != null) { result.Type = ResultType.EDIT; result.EditObject = scheme.Arguments.Find(x => x.Order == int.Parse(xres.Attribute(FatonConstants.XML_RESULT_ARGEDIT).Value)); } } foreach (var xcomplex in conditionComplexes) { var argName1 = uint.Parse(xcomplex.Attribute("Arg1").Value); var argName2 = uint.Parse(xcomplex.Attribute("Arg2").Value); var conditions = xcomplex.Elements(); foreach (var xcond in conditions) { var cond = scheme.AddCondition(); cond.ID = uint.Parse(xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_ID).Value); cond.Type = (ConditionType)Enum.Parse(typeof(ConditionType), xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_TYPE).Value); cond.Operation = (ConditionOperation)Enum.Parse(typeof(ConditionOperation), xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_OPERATION).Value); var arg1 = scheme.Arguments.Find(x => x.Order == argName1); var arg2 = scheme.Arguments.Find(x => x.Order == argName2); cond.Arg1 = arg1; cond.Arg2 = arg2; cond.Data = xcond.Attribute(FatonConstants.XML_ARGUMENT_CONDITION_DATA).Value; } } bank.Schemes.Add(scheme); } return(bank); }
///convert factscheme argument into nv node /// public static NodeInfo Convert(FactScheme.Argument argument, Vocabularies.Vocabulary vocabulary = null) { NodeInfo info = new NodeInfo(); info.Tag = argument; info.NodeNameProperty = string.Format("arg{0} {1}", argument.Order, argument.Name); argument.PropertyChanged += (s, e) => { if (e.PropertyName == "Order") { info.NodeNameProperty = string.Format("arg{0} {1}", argument.Order, argument.Name); } //if (e.PropertyName == "") }; if (argument.ArgType == ArgumentType.IOBJECT) { foreach (var attr in argument.Attributes) { NodeInfo.SectionInfo attrInfo = new NodeInfo.SectionInfo(); attrInfo.Data = attr; attrInfo.IsInput = false; attrInfo.IsOutput = true; var attrName = new Label(); attrName.Content = attr.Name; attrName.ToolTip = (attr.AttrType == OntologyNode.Attribute.AttributeType.DOMAIN || attr.AttrType == OntologyNode.Attribute.AttributeType.OBJECT) ? attr.Opt : attr.AttrType; attrInfo.UIPanel = attrName; info.Sections.Add(attrInfo); } info.FillColor = System.Windows.Media.Colors.LightSkyBlue; } else { for (int i = 0; i < 2; i++) // $Class and $Value are 2 mandatory attributes { var attr = argument.Attributes[i]; NodeInfo.SectionInfo attrInfo = new NodeInfo.SectionInfo(); attrInfo.Data = attr; attrInfo.IsInput = false; attrInfo.IsOutput = true; var attrName = new Label(); attrName.Content = attr.Name; attrName.ToolTip = (attr.AttrType == OntologyNode.Attribute.AttributeType.DOMAIN || attr.AttrType == OntologyNode.Attribute.AttributeType.OBJECT) ? attr.Opt : attr.AttrType; attrInfo.UIPanel = attrName; info.Sections.Add(attrInfo); } var plusInfo = new NodeInfo.SectionInfo(); var plusBtn = new Button(); plusBtn.Content = "+"; plusBtn.Click += (s, e) => { var newAttr = new OntologyNode.Attribute(OntologyNode.Attribute.AttributeType.STRING, vocabulary.First().Name, true); argument.Attributes.Add(newAttr); info.Sections.Add(TerminVarAttrInfo(argument, newAttr, vocabulary, info)); }; plusInfo.UIPanel = plusBtn; info.Sections.Add(plusInfo); for (int i = 2; i < argument.Attributes.Count; i++) { info.Sections.Add(TerminVarAttrInfo(argument, argument.Attributes[i], vocabulary, info)); } info.FillColor = System.Windows.Media.Colors.DeepSkyBlue; } return(info); }
public static List <OntologyNode> fromXml(XElement ontology) { OntologyClass currentClass = null; List <OntologyNode> result = new List <OntologyNode>(); var classes = from x in ontology.Elements() where x.Name.LocalName == "class" select x; var domains = from x in ontology.Elements() where x.Name.LocalName == "domain" select x; foreach (XElement c in classes) { currentClass = new OntologyClass(c.Attribute("name").Value); var classAttrs = from x in c.Elements() where x.Name.LocalName == "attr" select x; var classParents = from x in c.Elements() where x.Name.LocalName == "base" select x; foreach (XElement attrElement in classAttrs) { var attrName = ((XText)attrElement.FirstNode).Value; //get inner text of <attr> var attrTypeStr = attrElement.Attribute("type").Value; var attrClassName = attrElement.Attribute("class")?.Value; var attrDomName = attrElement.Attribute("domain")?.Value; OntologyNode.Attribute.AttributeType attrType = (OntologyNode.Attribute.AttributeType)Enum.Parse(typeof(OntologyNode.Attribute.AttributeType), attrTypeStr.ToUpper()); var attr = new OntologyNode.Attribute(attrType, attrName); if (attrType == OntologyNode.Attribute.AttributeType.OBJECT && attrClassName != null) { attr.Opt = attrClassName; } if (attrType == OntologyNode.Attribute.AttributeType.DOMAIN && attrDomName != null) { attr.Opt = attrDomName; } currentClass.OwnAttributes.Add(attr); } if (classParents.Any()) { foreach (XElement parentElement in classParents) { OntologyClass parentClass = null; foreach (OntologyClass rootClass in result) { parentClass = rootClass.FindChild(parentElement.Value); if (parentClass != null) { break; } } if (parentClass == null) { throw new Exception(String.Format(Locale.ERR_ONTOLOGY_NOPARENT, parentElement.Value, currentClass.Name)); } parentClass.AddChild(currentClass); currentClass.AddParent(parentClass); } } else { result.Add(currentClass); } } Ontology.Ontology.Domains = new Dictionary <string, List <string> >(); foreach (XElement d in domains) { var valueList = new List <string>(); foreach (XElement el in d.Elements()) { valueList.Add(el.Value); } Ontology.Ontology.Domains.Add(d.Attribute("name").Value, valueList); } Ontology.Ontology.Classes = result; return(result); }
public Rule(OntologyNode.Attribute attr, string defaultValue) { Attribute = attr; Type = RuleType.DEF; Default = defaultValue; }
public void Set(OntologyNode.Attribute attr, ISchemeComponent attrSource) { this.value = attr; this.source = attrSource; }