public void Save(Stream stream)
        {
            using (var writer = new EndianBinaryWriter(stream, Encoding.Default, true, Endianness.BigEndian))
            {
                var fixups = new List <Tuple <long, Action> >();
                writer.Write(( long )mEntryMap.Count);

                foreach (var entry in mEntryMap.Values.OrderBy(x => x.Handle))
                {
                    fixups.Add(new Tuple <long, Action>(writer.BaseStream.Position, () =>
                    {
                        entry.GetStream().FullyCopyTo(writer.BaseStream);

                        var padBytes = AlignmentUtils.GetAlignedDifference(writer.BaseStream.Position, 4);

                        for (int i = 0; i < padBytes; i++)
                        {
                            writer.Write(( byte )0);
                        }
                    }));
                    writer.Write(( int )0);
                    writer.Write(entry.Length);
                }

                foreach (var fixup in fixups)
                {
                    var offset = writer.BaseStream.Position;
                    fixup.Item2();
                    var nextOffset = writer.BaseStream.Position;
                    writer.BaseStream.Seek(fixup.Item1, SeekOrigin.Begin);
                    writer.Write((int)offset);
                    writer.BaseStream.Seek(nextOffset, SeekOrigin.Begin);
                }
            }
        }
示例#2
0
        public void Load(Stream stream, bool ownsStream)
        {
            mBaseStream = stream;
            mOwnsStream = ownsStream;

            using (var reader = new EndianBinaryReader(stream, Encoding.Default, true, Endianness.LittleEndian))
            {
                while (reader.Position < reader.BaseStreamLength)
                {
                    var type = reader.ReadByte();
                    if (type == 0xFF)   // end
                    {
                        break;
                    }

                    var isCompressed       = reader.ReadBoolean();
                    var userId             = reader.ReadInt16();
                    var length             = reader.ReadInt32() - 16;
                    var extension          = reader.ReadString(StringBinaryFormat.FixedLength, 4);
                    var decompressedLength = reader.ReadInt32();
                    var offset             = ( int )reader.Position;

                    var entry = new StoredEntry(AllocateHandle(), mBaseStream, offset, type, isCompressed, userId, length, extension, decompressedLength);
                    mEntryMap[entry.Handle] = entry;

                    reader.SeekCurrent(length);
                    reader.Position = AlignmentUtils.Align(reader.Position, 64);
                }
            }
        }
        public void ComputeWorstAlignmentOnTrace()
        {
            // Act
            var trace = MakeTrace();

            //DEFAULT TRACE COST
            Assert.AreEqual(5, AlignmentUtils.ComputeWorstCostOfTrace(trace));
            //MODIFIED TRACE COST
            Assert.AreEqual(10, AlignmentUtils.ComputeWorstCostOfTrace(trace, 2));
        }
        public void ComputeWorstAlignmentOnHardModel()
        {
            // Arrange
            ImportedEventLog elog = CSVImport.MakeDataFrame(hardCsv);

            elog.SetActivity("act");
            elog.SetCaseId("id");
            WorkflowLog wlog     = new WorkflowLog(elog);
            PetriNet    petriNet = CNetUtils.ConvertCNetToPetriNet(HeuristicMiner.MineCNet(wlog));

            // Act
            var cost = AlignmentUtils.ComputeWorstCostOfModel(petriNet, 1);

            // Assert
            Assert.AreEqual(7, cost);
        }
        public void MakePetriNetFromTraceTest()
        {
            // Act
            var traceNet = AlignmentUtils.MakePNetFromTrace(MakeTrace());

            // Asserts
            Assert.AreEqual("p0'", traceNet.StartPlace.Id);
            Assert.AreEqual("p5'", traceNet.EndPlace.Id);
            Assert.AreEqual(6, traceNet.Places.Count);
            Assert.AreEqual(5, traceNet.Transitions.Count);
            //EACH TRANSITION HAS 1 INPUT AND OUTPUT
            foreach (var t in traceNet.Transitions)
            {
                Assert.AreEqual(1, t.InputPlaces.Count);
                Assert.AreEqual(1, t.OutputPlaces.Count);
            }
        }
示例#6
0
        public void SyncNetAdvancedCustom()
        {
            // Arrange
            var trace = new WorkflowTrace("0");

            trace.Activities.Add("register");
            trace.Activities.Add("decide");
            trace.Activities.Add("register");
            trace.Activities.Add("send money");
            trace.Activities.Add("inform acceptance");
            var tNet = AlignmentUtils.MakePNetFromTrace(trace);

            List <ITransition> transitions = new List <ITransition>();
            List <IPlace>      places      = new List <IPlace>();

            for (int i = 1; i < 13; i++)
            {
                places.Add(new Place("p" + i));
            }

            transitions.Add(new Transition("t1", "register"));
            transitions[^ 1].InputPlaces.Add(places[0]);
示例#7
0
        public void SyncNetHard()
        {
            // Arrange
            ImportedEventLog elog = CSVImport.MakeDataFrame(hardCsv);

            elog.SetActivity("act");
            elog.SetCaseId("id");
            WorkflowLog wlog = new WorkflowLog(elog);
            var         tNet = AlignmentUtils.MakePNetFromTrace(wlog.WorkflowTraces[23]);
            var         pNet = CNetUtils.ConvertCNetToPetriNet(HeuristicMiner.MineCNet(wlog));

            // Act
            var syncNet = new SynchronousProductNet(tNet, pNet);

            // Assert
            Assert.AreEqual(syncNet.Places.Count, pNet.Places.Count + tNet.Places.Count);
            Assert.AreEqual(pNet.Transitions.Count + 2 * tNet.Transitions.Count, syncNet.Transitions.Count);
            Assert.IsTrue(syncNet.EndPlaces.SetEquals(new HashSet <IPlace> {
                tNet.EndPlace, pNet.EndPlace
            }));
            Assert.IsTrue(syncNet.StartPlaces.SetEquals(new HashSet <IPlace> {
                tNet.StartPlace, pNet.StartPlace
            }));
        }
示例#8
0
        public void Save(Stream stream)
        {
            using (var writer = new EndianBinaryWriter(stream, Encoding.Default, true, IsBigEndian ? Endianness.BigEndian : Endianness.LittleEndian))
            {
                if (Version == FormatVersion.Version2 || Version == FormatVersion.Version2BE || Version == FormatVersion.Version3 || Version == FormatVersion.Version3BE)
                {
                    writer.Write(mEntryMap.Count);
                }

                int nameLength;
                switch (Version)
                {
                case FormatVersion.Version1:
                    nameLength = 252;
                    break;

                case FormatVersion.Version2:
                case FormatVersion.Version2BE:
                    nameLength = 32;
                    break;

                case FormatVersion.Version3:
                case FormatVersion.Version3BE:
                    nameLength = 24;
                    break;

                default:
                    throw new NotImplementedException("Invalid format version");
                }

                foreach (var entry in mEntryMap.Values)
                {
                    // write entry name
                    for (int j = 0; j < nameLength; j++)
                    {
                        byte b = 0;
                        if (j < entry.FileName.Length)
                        {
                            b = ( byte )entry.FileName[j];
                        }

                        writer.Write(b);
                    }

                    var entryLength      = entry.Length;
                    var paddingByteCount = 0;

                    if (Version != FormatVersion.Version1)
                    {
                        paddingByteCount = AlignmentUtils.GetAlignedDifference(entry.Length, 32);
                        entryLength     += paddingByteCount;
                    }

                    // write entry length
                    writer.Write(entryLength);

                    // write data
                    var dataStream = entry.GetStream();
                    dataStream.FullyCopyTo(writer.BaseStream);

                    switch (Version)
                    {
                    case FormatVersion.Version1:
                    {
                        paddingByteCount = AlignmentUtils.GetAlignedDifference(writer.BaseStream.Position, 64);
                        for (int i = 0; i < paddingByteCount; i++)
                        {
                            writer.Write(( byte )0);
                        }
                    }
                    break;

                    case FormatVersion.Version2:
                    case FormatVersion.Version2BE:
                    case FormatVersion.Version3:
                    case FormatVersion.Version3BE:
                    {
                        for (int i = 0; i < paddingByteCount; i++)
                        {
                            writer.Write(( byte )0);
                        }
                    }
                    break;

                    default:
                        throw new NotImplementedException("Invalid format version");
                    }
                }

                if (Version == FormatVersion.Version1)
                {
                    for (int i = 0; i < 256; i++)
                    {
                        writer.Write(( byte )0);
                    }
                }
            }
        }
示例#9
0
        private void ReadEntries()
        {
            mEntryMap = new Dictionary <string, IEntry>(StringComparer.InvariantCultureIgnoreCase);

            using (var reader = new EndianBinaryReader(mBaseStream, Encoding.Default, true, IsBigEndian ? Endianness.BigEndian : Endianness.LittleEndian))
            {
                var stringBuilder = new StringBuilder();

                if (Version == FormatVersion.Version1)
                {
                    while (true)
                    {
                        long entryStartPosition = reader.BaseStream.Position;
                        if (entryStartPosition == reader.BaseStream.Length)
                        {
                            break;
                        }

                        // read entry name
                        while (true)
                        {
                            byte b = reader.ReadByte();
                            if (b == 0)
                            {
                                break;
                            }

                            stringBuilder.Append(( char )b);

                            // just to be safe
                            if (stringBuilder.Length == 252)
                            {
                                break;
                            }
                        }

                        string fileName = stringBuilder.ToString();

                        // set position to length field
                        reader.BaseStream.Position = entryStartPosition + 252;

                        // read entry length
                        int length = reader.ReadInt32();

                        if (fileName.Length == 0 || length <= 0 || length > 1024 * 1024 * 100)
                        {
                            break;
                        }

                        // make an entry
                        var entry = new StoredEntry(mBaseStream, fileName, length, ( int )reader.BaseStream.Position);

                        // clear string builder for next iteration
                        stringBuilder.Clear();

                        reader.BaseStream.Position = AlignmentUtils.Align(reader.BaseStream.Position + entry.Length, 64);

                        mEntryMap[entry.FileName] = entry;
                    }
                }
                else if (Version == FormatVersion.Version2 || Version == FormatVersion.Version2BE || Version == FormatVersion.Version3 || Version == FormatVersion.Version3BE)
                {
                    int entryCount = reader.ReadInt32();
                    int nameLength = 32;
                    if (Version == FormatVersion.Version3)
                    {
                        nameLength = 24;
                    }

                    for (int i = 0; i < entryCount; i++)
                    {
                        long entryStartPosition = reader.BaseStream.Position;
                        if (entryStartPosition == reader.BaseStream.Length)
                        {
                            break;
                        }

                        // read entry name
                        for (int j = 0; j < nameLength; j++)
                        {
                            byte b = reader.ReadByte();

                            if (b != 0)
                            {
                                stringBuilder.Append(( char )b);
                            }
                        }

                        string fileName = stringBuilder.ToString();

                        // read entry length
                        int length = reader.ReadInt32();

                        if (fileName.Length == 0 || length <= 0 || length > 1024 * 1024 * 100)
                        {
                            break;
                        }

                        // make an entry
                        var entry = new StoredEntry(mBaseStream, fileName, length, ( int )reader.BaseStream.Position);

                        // clear string builder for next iteration
                        stringBuilder.Clear();

                        reader.BaseStream.Position += entry.Length;

                        mEntryMap[entry.FileName] = entry;
                    }
                }
            }
        }