Diese Klasse repräsentiert einen Parser für das Knoten-Austauschformat und enthält die eingelesenen Informationen wie den Namen des Knotens und die Kantenliste als Eigenschaften.
Esempio n. 1
0
        public static Knot coloredKnot(string name)
        {
            string content = "\nY#FF0000FF#\nZ#FF0000FF#\ny#FF0000FF#\nz#FF0000FF#";
            content = name + content;

            KnotStringIO stringIO = new KnotStringIO (content);

            KnotMetaData metaData = new KnotMetaData (name, stringIO.Edges.ToList ().Count<Edge>, null, null);
            Knot coloredKnot = new Knot (metaData, stringIO.Edges.ToList ());
            return coloredKnot;
        }
Esempio n. 2
0
        public void KnotStringIO_Decode_Test()
        {
            String content_rgba = "Start\nY#FF0000FF#\nZ#FF0000FF#\ny#FF0000FF#\nz#FF0000FF#";
            String content_rgb = "Start\nY#FF0000#\nz#FF0000#\ny#FF0000#\nZ#FF0000#";
            String content_rectangle = "Start\nY#FF0000#1000#\nZ#FF0000#1000\ny#FF0000#1000\nz#FF0000#1000";
            KnotStringIO rgba = new KnotStringIO (content_rgba);
            KnotStringIO rgb = new KnotStringIO (content_rgb);

            KnotStringIO rectangle = new KnotStringIO (content_rectangle);
            Assert.DoesNotThrow (() => {
                complexKnotStringIO.Edges.ToList ();
                complexKnotStringIO.Edges.ToList ();
                rgba.Edges.ToList ();
                rgb.Edges.ToList ();
                rectangle.Edges.ToList ();
            }, " Erstellung");
        }
Esempio n. 3
0
        public static Knot noMetadataKnot(string name)
        {
            string content = "\nY#FF0000FF#\nZ#FF0000FF#\ny#FF0000FF#\nz#FF0000FF#";
            content = name + content;

            KnotStringIO stringIO = new KnotStringIO (content);

            KnotMetaData metaData = null;
            Knot noMeta = new Knot (metaData, stringIO.Edges.ToList ());
            return noMeta;
        }
Esempio n. 4
0
 /// <summary>
 /// Lädt eines Knotens aus einer angegebenen Datei.
 /// </summary>
 public Knot Load(string filename)
 {
     if (KnotCache.ContainsKey (filename)) {
         return KnotCache [filename];
     }
     else {
         Log.Debug ("Load knot from ", filename);
         KnotStringIO parser = new KnotStringIO (content: string.Join ("\n", FileUtility.ReadFrom (filename)));
         return KnotCache [filename] = new Knot (
             new KnotMetaData (parser.Name, () => parser.CountEdges, this, filename),
             parser.Edges
         );
     }
 }
Esempio n. 5
0
 /// <summary>
 /// Speichert einen Knoten in dem Dateinamen, der in dem Knot-Objekt enthalten ist.
 /// </summary>
 public void Save(Knot knot,bool force)
 {
     KnotStringIO parser = new KnotStringIO (knot);
     Log.Debug ("KnotFileIO.Save (", knot, ") = #", parser.Content.Length);
     if (knot.MetaData.Filename == null) {
         throw new NoFilenameException ("Error! knot has no filename: " + knot);
     }
     else if (!force && File.Exists (knot.MetaData.Filename)) {
         throw new FileAlreadyExistsException ("Error! Knot already exists!");
     }
     else {
         File.WriteAllText (knot.MetaData.Filename, parser.Content);
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Lädt die Metadaten eines Knotens aus einer angegebenen Datei.
 /// </summary>
 public KnotMetaData LoadMetaData(string filename)
 {
     if (KnotMetaDataCache.ContainsKey (filename)) {
         return KnotMetaDataCache [filename];
     }
     else {
         KnotStringIO parser = new KnotStringIO (content: string.Join ("\n", FileUtility.ReadFrom (filename)));
         return KnotMetaDataCache [filename] = new KnotMetaData (
             name: parser.Name,
             countEdges: () => parser.CountEdges,
             format: this,
             filename: filename
         );
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Lädt eine Challenge aus einer angegebenen Datei.
        /// </summary>
        public Challenge Load(string filename)
        {
            ChallengeMetaData meta = LoadMetaData (filename: filename);
            Knot start = null;
            Knot target = null;

            using (ZipFile zip = ZipFile.Read (filename)) {
                foreach (ZipEntry entry in zip) {
                    string content = entry.ReadContent ();

                    // für die Datei mit dem Startknoten
                    if (entry.FileName.ToLower ().Contains ("start")) {
                        KnotStringIO parser = new KnotStringIO (content: content);
                        start = new Knot (
                            new KnotMetaData (parser.Name, () => parser.CountEdges, null, null),
                            parser.Edges
                        );
                    }

                    // für die Datei mit dem Zielknoten
                    else if (entry.FileName.ToLower ().Contains ("target")) {
                        KnotStringIO parser = new KnotStringIO (content: content);
                        target = new Knot (
                            new KnotMetaData (parser.Name, () => parser.CountEdges, null, null),
                            parser.Edges
                        );
                    }
                }
            }

            if (meta != null && start != null && target != null) {
                return new Challenge (meta, start, target);
            }
            else {
                throw new IOException (
                    "Error! Invalid challenge file: " + filename
                    + " (meta=" + meta + ",start=" + start + ",target=" + target + ")"
                );
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Speichert eine Challenge in dem Dateinamen, der in dem Challenge-Objekt enthalten ist.
 /// </summary>
 public void Save(Challenge challenge, bool force)
 {
     if (!force && File.Exists (challenge.MetaData.Filename)) {
         throw new FileAlreadyExistsException ("Error! Challenge already exists!");
     }
     else {
         using (ZipFile zip = new ZipFile ()) {
             // Namen
             zip.AddEntry ("name.txt", challenge.Name);
             // Startknoten
             KnotStringIO parser = new KnotStringIO (challenge.Start);
             zip.AddEntry ("start.knot", parser.Content);
             // Zielknoten
             parser = new KnotStringIO (challenge.Target);
             zip.AddEntry ("target.knot", parser.Content);
             // Highscore
             zip.AddEntry ("highscore.txt", string.Join ("\n", printHighscore (challenge.Highscore)));
             // ZIP-Datei speichern
             zip.Save (challenge.MetaData.Filename);
         }
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Lädt die Metadaten einer Challenge aus einer angegebenen Datei.
        /// </summary>
        public ChallengeMetaData LoadMetaData(string filename)
        {
            string name = null;
            KnotMetaData start = null;
            KnotMetaData target = null;
            IEnumerable<KeyValuePair<string, int>> highscore = null;
            using (ZipFile zip = ZipFile.Read (filename)) {
                foreach (ZipEntry entry in zip) {
                    string content = entry.ReadContent ();

                    // für die Datei mit dem Startknoten
                    if (entry.FileName.ToLower ().Contains ("start")) {
                        KnotStringIO parser = new KnotStringIO (content: content);
                        start = new KnotMetaData (parser.Name, () => parser.CountEdges, null, null);
                    }

                    // für die Datei mit dem Zielknoten
                    else if (entry.FileName.ToLower ().Contains ("target")) {
                        KnotStringIO parser = new KnotStringIO (content: content);
                        target = new KnotMetaData (parser.Name, () => parser.CountEdges, null, null);
                    }

                    // für die Datei mit dem Namen
                    else if (entry.FileName.ToLower ().Contains ("name")) {
                        name = content.Trim ();
                    }

                    // für die Datei mit den Highscores
                    else if (entry.FileName.ToLower ().Contains ("highscore")) {
                        highscore = parseHighscore (content.Split (new char[] {'\r','\n'}, StringSplitOptions.RemoveEmptyEntries));
                    }
                }
            }
            if (name != null && start != null && target != null) {
                Log.Debug ("Load challenge file: ", filename, " (name=", name, ",start=", start, ",target=", target, ",highscore=", highscore);
                return new ChallengeMetaData (
                           name: name,
                           start: start,
                           target: target,
                           filename: filename,
                           format: this,
                           highscore: highscore
                       );
            }
            else {
                throw new IOException (
                    "Error! Invalid challenge file: " + filename
                    + " (name=" + name + ",start=" + start + ",target=" + target + ",highscore=" + highscore + ")"
                );
            }
        }
Esempio n. 10
0
 public void KnotStringIO_invalid_Decode_Test()
 {
     String content_rgba = "Start\nY#FZ0000FF#\nZ#FF0000FF#\ny#FF0000FF#\nz#FF0000FF#";
     String content_rgb = "Start\nY#FF000Z#\nZ#FF00<10#\ny#FF0000#\nz#FF0000#";
     String content_rectangle = "Start\nY#FF0000#D000#\nZ#FF0000#1!00\ny#FF0000#1000\nz#FF0000#1000";
     KnotStringIO rgba = new KnotStringIO (content_rgba);
     KnotStringIO rgb = new KnotStringIO (content_rgb);
     KnotStringIO rectangle = new KnotStringIO (content_rectangle);
     Assert.Catch ( () => {
         complexKnotStringIO.Edges.ToList ();
         complexKnotStringIO.Edges.ToList ();
         rgba.Edges.ToList ();
         rgb.Edges.ToList ();
         rectangle.Edges.ToList ();
     }, " Erstellung");
 }
Esempio n. 11
0
 public void Init()
 {
     squaredKnotStringIO = new KnotStringIO (KnotGenerator.generateSquareKnot (10, KnotGenerator.FakeName));
     complexKnotStringIO = new KnotStringIO (KnotGenerator.generateComplexKnot (KnotGenerator.FakeName));
 }
Esempio n. 12
0
 public void KnotStringIO_SetEdges_Test()
 {
     KnotStringIO copy = new KnotStringIO (squaredKnotStringIO.Name);
     copy.Edges = squaredKnotStringIO.Edges;
     Assert.AreEqual (copy.Content, squaredKnotStringIO.Content, "Copy");
 }
Esempio n. 13
0
        public void KnotStringIO_Invalid_Test()
        {
            KnotStringIO other = new KnotStringIO (squaredKnotStringIO.Content);

            Assert.AreEqual (squaredKnotStringIO.CountEdges, 40, "Count Edges");

            Assert.AreEqual (squaredKnotStringIO.Content, other.Content, "Contetnt equal");
            KnotStringIO invalidContent = null;

            invalidContent = new KnotStringIO ("Name \n" + "Invalid Line \n");
            Assert.Catch<IOException> (() => {
                // damit der Compiler den Aufruf der Decode...-Methoden nicht wegoptimiert,
                // muss man zurück zum Konstruktur noch das eigentlich dort abgespeicherte
                // Attribut Edges abrufen (das ist ein Iterator mit lazy evaluation)
                // und das dann in eine Liste umwandeln
                Log.Debug (invalidContent.Edges.ToList ());
            }
                                      );
            Assert.AreEqual (squaredKnotStringIO.Content, other.Content, "Content equal");
        }