示例#1
0
 public EditorProject()
 {
     _paths    = new Dictionary <string, string>();
     _ontology = new List <OntologyNode>();
     _themes   = new Vocabulary();
     Bank      = new FactSchemeBank();
     _gramtab  = new Dictionary <string, List <string> >();
     _segments = new List <string>();
 }
        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);
        }
示例#3
0
 public void Export(string filename, FactSchemeBank bank)
 {
 }
示例#4
0
        public EditorProject(Stream fstream, string path) : this()
        {
            StreamReader sr        = new StreamReader(fstream);
            string       xmlString = sr.ReadToEnd();
            XDocument    doc       = XDocument.Parse(xmlString);

            XElement root = doc.Element(EditorConstants.XML_EDITOR_ROOT_NAME);
            //load ontology
            var ontPath = root.Element(EditorConstants.XML_PROJECT_ONTOLOGY)?.Value;

            _paths[EditorConstants.XML_PROJECT_ONTOLOGY] = ontPath;
            if (!string.IsNullOrEmpty(ontPath))
            {
                if (!System.IO.Path.IsPathRooted(ontPath))
                {
                    ontPath = path + ontPath;
                }
                var ontStream = new FileStream(ontPath, FileMode.Open);
                LoadOntology(ontStream);
                ontStream.Close();
            }

            //load dictionary
            var themesPath = root.Element(EditorConstants.XML_PROJECT_DICTIONARY)?.Value;

            if (!string.IsNullOrEmpty(themesPath))
            {
                var tmp = themesPath;
                if (!System.IO.Path.IsPathRooted(themesPath))
                {
                    themesPath = path + themesPath;
                }
                _themes = LoadDictionary(themesPath);
                _paths[EditorConstants.XML_PROJECT_DICTIONARY] = tmp;
            }


            //load segments
            var segPath = root.Element(EditorConstants.XML_PROJECT_SEGMENTS)?.Value;

            _paths[EditorConstants.XML_PROJECT_SEGMENTS] = segPath;
            if (!string.IsNullOrEmpty(segPath))
            {
                if (!System.IO.Path.IsPathRooted(segPath))
                {
                    segPath = path + segPath;
                }
                var segStream = new FileStream(segPath, FileMode.Open);
                Segments = LoadSegments(segStream);
                segStream.Close();
            }

            //load gramtab
            var gramtabPath = root.Element(EditorConstants.XML_PROJECT_GRAMTAB)?.Value;

            _paths[EditorConstants.XML_PROJECT_GRAMTAB] = gramtabPath;
            if (!string.IsNullOrEmpty(gramtabPath))
            {
                if (!System.IO.Path.IsPathRooted(gramtabPath))
                {
                    gramtabPath = path + gramtabPath;
                }
                var gramtabStream = new FileStream(gramtabPath, FileMode.Open);
                _gramtab = LoadGramtab(gramtabStream);
                gramtabStream.Close();
            }

            XElement xbank = root.Element(FatonConstants.XML_BANK_TAG);

            Markup = root.Element(EditorConstants.XML_EDITOR_MARKUP);

            //saved for the future
            //if (xthemes != null)
            //{
            //    _themes = new List<VocTheme>();
            //    foreach (XElement xtheme in xthemes.Elements())
            //    {
            //        string name = xtheme.Attribute(EditorConstants.XML_PROJECT_DICTIONARYTHEMENAME).Value;
            //        VocTheme theme = new VocTheme(ref name);
            //        if (xtheme.Elements().Count() > 0)
            //            theme.root = false;
            //        foreach (XElement xbase in xtheme.Elements())
            //        {
            //            string basename = xbase.Value;
            //            VocTheme parent = Themes.Find(x => x.name == basename);
            //            if (parent == null)
            //                throw new Exception(string.Format(Locale.ERR_DICTIONARY_NOPARENT, basename, theme.name));
            //            else
            //            {
            //                parent.children.Add(theme);
            //                theme.parents.Add(parent);
            //            }
            //        }
            //        _themes.Add(theme);
            //    }
            //}

            if (xbank != null)
            {
                Bank = FactSchemeBank.FromXml(xbank, Ontology, Dictionary);
            }

            _projectPath = path;
        }