예제 #1
0
        public void WriteToFile(string path)
        {
            List <string> code = new List <string>();

            void Add(params object[] exp)
            {
                code.Add(SExpression.ToSExpression(exp));
            }

            HashSet <Variable> writtenVars = new HashSet <Variable>();

            void WriteVar(Variable v)
            {
                if (writtenVars.Contains(v))
                {
                    return;
                }

                if (v.Condition != null)
                {
                    WriteVar(v.Condition.Variable);
                    Add("variable", v.Name, v.DomainName, ConditionToSExpression(v.Condition));
                }
                else
                {
                    Add("variable", v.Name, v.DomainName);
                }

                writtenVars.Add(v);
            }

            foreach (var pair in Domains)
            {
                Add(new object[] { "domain", pair.Key }.Concat(pair.Value.Elements).ToArray());
            }

            foreach (var pair in Variables)
            {
                var v = pair.Value;
                WriteVar(v);
            }

            foreach (var nogood in Nogoods)
            {
                Add(new object[] { "nogood" }.Concat(nogood.Select(ConditionToSExpression)).ToArray());
            }

            System.IO.File.WriteAllLines(path, code);
            IsDirty = false;
        }
예제 #2
0
        /// <summary>
        /// Add the information in the PCGToy file to the specified problem.
        /// </summary>
        /// <param name="path">File path</param>
        /// <param name="problem">Problem to add the contents to</param>
        /// <exception cref="FileFormatException">If the file is not a valid PCGToy file.</exception>
        public static void LoadFromFile(string path, Problem problem)
        {
            Dictionary <string, FDomain <object> >    domains   = new Dictionary <string, FDomain <object> >();
            Dictionary <string, FDVariable <object> > variables = new Dictionary <string, FDVariable <object> >();

            var f = System.IO.File.OpenText(path);

            while (f.Peek() >= 0)
            {
                var exp = SExpression.Read(f);
                if (!(exp is List <object> l) || l.Count == 0 || !(l[0] is string tag))
                {
                    throw new FileFormatException($"Unknown declaration {exp}");
                }

                switch (tag)
                {
                case "domain":
                    if (l.Count < 2 || !(l[1] is string domainName))
                    {
                        throw new FileFormatException("Malformed domain declaration");
                    }
                    if (l.Count < 3)
                    {
                        throw new FileFormatException($"Domain {domainName} has no elements");
                    }
                    var elements = l.Skip(2).ToArray();
                    domains[domainName] = new FDomain <object>(domainName, elements);
                    break;

                case "variable":
                    if (l.Count < 3 ||
                        l.Count > 4 ||
                        !(l[1] is string varName) ||
                        !(l[2] is string domain))
                    {
                        throw new FileFormatException("Malformed variable declaration");
                    }
                    if (!domains.ContainsKey(domain))
                    {
                        throw new FileFormatException(
                                  $"Unknown domain name: {domain} in declaration of variable {varName}");
                    }
                    Literal c = null;

                    if (l.Count == 4)
                    {
                        c = ConditionFromSExpression(l[3], variables);
                    }
                    var v = new FDVariable <object>(varName, domains[domain], c);
                    variables[varName] = v;
                    break;

                case "nogood":
                    problem.Inconsistent(l.Skip(1).Select(sexp => ConditionFromSExpression(sexp, variables)).ToArray());
                    break;

                default:
                    throw new FileFormatException($"Unknown declaraction {tag}");
                }
            }
        }
예제 #3
0
        public void LoadFromFile(string path)
        {
            Domains.Clear();
            Variables.Clear();
            Nogoods.Clear();
            var f = System.IO.File.OpenText(path);

            while (f.Peek() >= 0)
            {
                var exp = SExpression.Read(f);
                if (!(exp is List <object> l) || l.Count == 0 || !(l[0] is string tag))
                {
                    throw new FileFormatException($"Unknown declaration {exp}");
                }
                else
                {
                    switch (tag)
                    {
                    case "domain":
                        if (l.Count < 2 || !(l[1] is string domainName))
                        {
                            throw new FileFormatException("Malformed domain declaration");
                        }
                        if (l.Count < 3)
                        {
                            throw new FileFormatException($"Domain {domainName} has no elements");
                        }
                        var elements = l.Skip(2).ToArray();
                        Domains[domainName] = new FDomain <object>(domainName, elements);
                        break;

                    case "variable":
                        if (l.Count < 3 ||
                            l.Count > 4 ||
                            !(l[1] is string varName) ||
                            !(l[2] is string domain))
                        {
                            throw new FileFormatException("Malformed variable declaration");
                        }
                        if (!Domains.ContainsKey(domain))
                        {
                            throw new FileFormatException(
                                      $"Unknown domain name: {domain} in declaration of variable {varName}");
                        }
                        Condition c = null;

                        if (l.Count == 4)
                        {
                            c = ConditionFromSExpression(l[3]);
                        }
                        Variables[varName] = new Variable(varName, this, domain, c);
                        break;

                    case "nogood":
                        Nogoods.Add(l.Skip(1).Select(ConditionFromSExpression).ToArray());
                        break;

                    default:
                        throw new FileFormatException($"Unknown declaraction {tag}");
                    }
                }
            }
            Changed();
            IsDirty = false;
        }