forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExampleFilesGenerator.cs
144 lines (132 loc) · 4.87 KB
/
ExampleFilesGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
using GPSRCmdGen.Containers;
namespace GPSRCmdGen
{
/// <summary>
/// Example files generator.
/// </summary>
public static class ExampleFilesGenerator
{
/// <summary>
/// Adds the location of the objects in the given category list to the given rooms list
/// </summary>
/// <param name="rooms">The list of locations grouped by room</param>
/// <param name="categories">The list of objects grouped by category</param>
private static void AddObjectLocations(List<Room> rooms, List<Category> categories)
{
foreach (Category cat in categories)
{
int ix = -1;
for (int i = 0; i < rooms.Count; ++i)
{
if (rooms[i].Name == cat.RoomString) ix = i;
}
if (ix == -1)
{
ix = rooms.Count;
rooms.Add(new Room(cat.RoomString));
}
rooms[ix].AddLocation(cat.DefaultLocation);
}
}
/// <summary>
/// Writes down a set of example data files with information gathered from the Factory
/// </summary>
public static void GenerateExampleFiles(){
GestureContainer gestures = new GestureContainer(Factory.GetDefaultGestures());
CategoryContainer categories = new CategoryContainer(Factory.GetDefaultObjects());
RoomContainer rooms = new RoomContainer (Factory.GetDefaultLocations ());
NameContainer names = new NameContainer (Factory.GetDefaultNames ());
QuestionsContainer questions = new QuestionsContainer(Factory.GetDefaultQuestions());
AddObjectLocations(rooms.Rooms, categories.Categories);
SaveGrammars ();
WriteDatafiles (gestures, categories, rooms, names, questions);
}
/// <summary>
/// Queries the user for file overwritten permission
/// </summary>
/// <param name="file">The name of the file which will be overwritten</param>
/// <returns><c>true</c> if the user authorizes the overwrite, otherwise <c>false</c></returns>
private static bool Overwrite (string file)
{
FileInfo fi = new FileInfo (file);
if (!fi.Exists)
return true;
Console.Write ("File {0} already exists. Overwrite? [yN]", fi.Name);
string answer = Console.ReadLine().ToLower();
if ((answer == "y") || (answer == "yes")) {
fi.Delete ();
return true;
}
return false;
}
static void SaveGrammarFile (string name, string header, string formatSpec, string content)
{
string fileName = String.Format ("{0}.txt", name);
fileName = Loader.GetPath("grammars", fileName);
if (!Overwrite (fileName))
return;
string Name = name.Substring (0, 1).ToUpper () + name.Substring (1);
header = header.Replace ("${GrammarName}", Name);
using (StreamWriter writer = new StreamWriter(fileName)) {
writer.WriteLine (header);
writer.WriteLine (formatSpec);
writer.WriteLine (content);
writer.Close ();
}
}
/// <summary>
/// Writes down a set of example grammar files in the grammars sub directory
/// </summary>
private static void SaveGrammars()
{
string path = Loader.GetPath("grammars");
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
string formatSpec = Resources.FormatSpecification;
string authoring = Resources.GrammarHeader;
Dictionary<string, string> grammars = new Dictionary<string, string> ();
grammars.Add ("count", Resources.CountGrammar);
grammars.Add("incomplete", Resources.IncompleteCommandsGrammar);
grammars.Add("incongruent", Resources.IncongruentCommandsGrammar);
grammars.Add("category1", Resources.Category1Grammar);
grammars.Add("category2", Resources.Category2Grammar);
grammars.Add("category3", Resources.Category3Grammar);
foreach (KeyValuePair<string, string> g in grammars) {
try{
SaveGrammarFile (g.Key, authoring, formatSpec, g.Value);
}catch{}
}
}
/// <summary>
/// Writes the datafiles, asking the user for overwriting.
/// </summary>
/// <param name="gestures">Gestures list container</param>
/// <param name="categories">Categories list container.</param>
/// <param name="locations">Locations list container.</param>
/// <param name="names">Names list container.</param>
/// <param name="questions">Questions list container.</param>
private static void WriteDatafiles (GestureContainer gestures, CategoryContainer categories, RoomContainer locations, NameContainer names, QuestionsContainer questions)
{
string path = Loader.GetPath ("Gestures.xml");
if (Overwrite (path))
Loader.Save (path, gestures);
path = Loader.GetPath ("Locations.xml");
if (Overwrite (path))
Loader.Save (path, locations);
path = Loader.GetPath ("Names.xml");
if (Overwrite (path))
Loader.Save (path, names);
path = Loader.GetPath ("Objects.xml");
if (Overwrite (path))
Loader.Save (path, categories);
path = Loader.GetPath ("Questions.xml");
if (Overwrite (path))
Loader.Save (path, questions);
}
}
}