/// <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}"); } } }
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; }