Esempio n. 1
0
        public void Benchmark()
        {
            var rnd   = new Random(123);
            var lines = Enumerable.Range(0, 10000).Select(i => new string(Enumerable.Repeat('X', rnd.Next() % 2048).ToArray())).ToList();
            var data  = string.Join("\n", lines);

            var reader1 = new CountingReader(new MemoryStream(_encoding.GetBytes(data)));
            var bytes   = 0;
            var i1      = 0;
            var sw      = Stopwatch.StartNew();

            while (!reader1.EndOfStream)
            {
                var line = reader1.ReadLine(out bytes);
                Assert.AreEqual(lines[i1++], line);
            }
            Trace.WriteLine($"LogReader completed reading {lines.Count} in {sw.ElapsedMilliseconds} ms");

            var reader2 = new StreamReader(new MemoryStream(_encoding.GetBytes(data)), _encoding);
            var i2      = 0;

            sw.Restart();
            while (!reader2.EndOfStream)
            {
                var line = reader2.ReadLine();
                Assert.AreEqual(lines[i2++], line);
            }
            Trace.WriteLine($"StreamReader completed reading {lines.Count} in {sw.ElapsedMilliseconds} ms");
            Assert.AreEqual(i2, i1);
        }
Esempio n. 2
0
        public void StartParse()
        {
            string fullPath = this.Path + "/" + this.Filename;

            Lines = new List <IParserLine>();

            CountingReader reader;

            try {
                reader = new CountingReader(fullPath);
            }
            catch (Exception e) {
                throw new Exception("Error opening file " + fullPath + " - ", e);
            }

            try {
                Parse(reader, this.context);
            }
            catch (Exception e) {
                reader.Close();

                string[] data       = ParserLogger.GetFileData(fullPath);
                string   joinedData = "";
                for (int i = 0; i < data.Length; i++)
                {
                    joinedData += data[i];
                }
                ParserLogger.TempData = joinedData;

                throw new Exception("Error reading file " + fullPath + " - Line " + reader.GetCurrentLine + " - ", e);
            }
            finally {
                reader.Close();
            }
        }
Esempio n. 3
0
        //Input Test Methods

        //In this method, we are getting the list of valid zones, to ensure that all other files only
        // include these zone_ids
        private static Dictionary <int, int> GetValidZones()
        {
            Dictionary <int, int> zones = new Dictionary <int, int>();
            FileInfo inputFile          = Global.GetInputPath(Global.Configuration.RawZonePath).ToFile();

            using (CountingReader reader = new CountingReader(inputFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                int    newId = 0;
                string line;
                int    lineNum = 1;

                while ((line = reader.ReadLine()) != null)
                {
                    //the first line is the header
                    if (lineNum != 1)
                    {
                        string[] row        = line.Split(new[] { Global.Configuration.RawZoneDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                        int      originalId = Convert.ToInt32(row[0]);
                        zones.Add(originalId, newId);
                        if (originalId < 0)
                        {
                            PrintError("Zone", "zoneId", originalId, lineNum);
                            errorCount++;
                        }
                    }
                    lineNum++;
                }
                return(zones);
            }
        }
Esempio n. 4
0
        public static Dictionary <int, IShadowPriceParcel> ReadShadowPrices()
        {
            var shadowPrices    = new Dictionary <int, IShadowPriceParcel>();
            var shadowPriceFile = new FileInfo(Global.ShadowPricesPath);

            if (!shadowPriceFile.Exists || !Global.Configuration.ShouldUseShadowPricing || (!Global.Configuration.ShouldRunWorkLocationModel && !Global.Configuration.ShouldRunSchoolLocationModel))
            {
                return(shadowPrices);
            }

            using (var reader = new CountingReader(shadowPriceFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                reader.ReadLine();

                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    var tokens = line.Split(new[] { Global.Configuration.ShadowPriceDelimiter }, StringSplitOptions.RemoveEmptyEntries);

                    var shadowPriceParcel = new ShadowPriceParcel {
                        ParcelId                         = Convert.ToInt32(tokens[0]),
                        EmploymentDifference             = Convert.ToDouble(tokens[1]), // DELTSPUW
                        ShadowPriceForEmployment         = Convert.ToDouble(tokens[2]), // SHADEMP
                        StudentsK12Difference            = Convert.ToDouble(tokens[3]), // DELTSPUS
                        ShadowPriceForStudentsK12        = Convert.ToDouble(tokens[4]), // SHADPK12
                        StudentsUniversityDifference     = Convert.ToDouble(tokens[5]), // DELTSPUU
                        ShadowPriceForStudentsUniversity = Convert.ToDouble(tokens[6]), // SHADPUNI
                    };

                    shadowPrices.Add(shadowPriceParcel.ParcelId, shadowPriceParcel);
                }
            }

            return(shadowPrices);
        }
Esempio n. 5
0
        public void StartParse()
        {
            string fullPath = this.Path + "/" + this.Filename;
            Lines = new List<IParserLine>();

            CountingReader reader;

            try {
                reader = new CountingReader(fullPath);
            }
            catch (Exception e) {
                throw new Exception("Error opening file " + fullPath + " - ", e);
            }

            try {
                Parse(reader, this.context);
            }
            catch (Exception e) {
                reader.Close();

                string[] data = ParserLogger.GetFileData(fullPath);
                string joinedData = "";
                for (int i = 0; i < data.Length; i++) {
                    joinedData += data[i];
                }
                ParserLogger.TempData = joinedData;

                throw new Exception("Error reading file " + fullPath + " - Line " + reader.GetCurrentLine + " - ", e);
            }
            finally {
                reader.Close();
            }
        }
Esempio n. 6
0
        private static Dictionary <int, int> TestParcels(Dictionary <int, int> zones)
        {
            var parcels   = new Dictionary <int, int>();
            var inputFile = Global.GetInputPath(Global.Configuration.RawParcelPath).ToFile();

            using (var reader = new CountingReader(inputFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                int    lineNum   = 1;
                var    sequences = new Dictionary <int, int>();
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    //first line is header
                    if (lineNum != 1)
                    {
                        var row            = line.Split(new[] { Global.Configuration.RawParcelDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                        var id             = int.Parse(row[0]);
                        int originalZoneId = int.Parse(row[4]);
                        int countBadZones  = 0;

                        if (!zones.ContainsKey(originalZoneId))
                        {
                            PrintError("Parcels", "ZoneID", originalZoneId, lineNum);
                            countBadZones++;
                            errorCount++;
                        }

                        parcels.Add(id, originalZoneId);

                        for (int i = 2; i < row.Length; i++)
                        {
                            //only for variables after lutype_p and not the variable:aparks (in sq ft)
                            float value     = Convert.ToSingle(row[i]);
                            int   value_int = (int)value;
                            // some of variables are sqft in area so they end up being pretty large
                            // and overflow the cast from single to int; like i=47; so don't test them
                            if (value_int < 0 && i > 5 && i < 47)
                            {
                                PrintError("Parcels", "a variable", value_int, lineNum, i);
                                errorCount++;
                            }
                            else if (value_int > Global.Settings.MaxInputs.MaxParcelVals && i > 5 && i != 40 && i != 41 && i < 47)
                            {
                                PrintError("Parcels", "a variable", value_int, lineNum, i);
                                errorCount++;
                            }
                        }
                    }

                    lineNum++;
                }
            }
            return(parcels);
        }
Esempio n. 7
0
        private static Dictionary <string, int> GetIndex(char delimiter, string inputPath)
        {
            FileInfo file = new FileInfo(inputPath);

            string header;

            using (CountingReader reader = new CountingReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                header = reader.ReadLine();
            }

            return(header == null ? new Dictionary <string, int>() : header.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries).Select((t, i) => new { Name = t, Index = i }).ToDictionary(t => t.Name, t => t.Index));
        }
Esempio n. 8
0
        public static Dictionary <int, IDestinationParkingShadowPriceNode> ReadDestinationParkingShadowPrices()
        {
            var shadowPrices    = new Dictionary <int, IDestinationParkingShadowPriceNode>();
            var shadowPriceFile = new FileInfo(Global.DestinationParkingShadowPricesPath);

            if (!Global.DestinationParkingNodeIsEnabled || !shadowPriceFile.Exists || !Global.Configuration.ShouldUseDestinationParkingShadowPricing || Global.Configuration.IsInEstimationMode)
            {
                return(shadowPrices);
            }

            using (var reader = new CountingReader(shadowPriceFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                reader.ReadLine();

                string line = null;
                try {
                    while ((line = reader.ReadLine()) != null)
                    {
                        var tokens = line.Split(new[] { Global.Configuration.DestinationParkingShadowPriceDelimiter }, StringSplitOptions.RemoveEmptyEntries);

                        var shadowPriceNode = new DestinationParkingShadowPriceNode
                        {
                            NodeId = Convert.ToInt32(tokens[0]),
                        };

                        shadowPriceNode.ShadowPriceDifference = new double[Global.Settings.Times.MinutesInADay];
                        shadowPriceNode.ShadowPrice           = new double[Global.Settings.Times.MinutesInADay];
                        shadowPriceNode.ExogenousLoad         = new double[Global.Settings.Times.MinutesInADay];
                        shadowPriceNode.ParkingLoad           = new double[Global.Settings.Times.MinutesInADay];

                        for (var i = 1; i <= Global.Settings.Times.MinutesInADay; i++)
                        {
                            shadowPriceNode.ShadowPriceDifference[i - 1] = Convert.ToDouble(tokens[i]);
                            shadowPriceNode.ShadowPrice[i - 1]           = Convert.ToDouble(tokens[Global.Settings.Times.MinutesInADay + i]);
                            shadowPriceNode.ExogenousLoad[i - 1]         = Convert.ToDouble(tokens[2 * Global.Settings.Times.MinutesInADay + i]);
                            shadowPriceNode.ParkingLoad[i - 1]           = Convert.ToDouble(tokens[3 * Global.Settings.Times.MinutesInADay + i]);
                        }

                        shadowPrices.Add(shadowPriceNode.NodeId, shadowPriceNode);
                    }
                } catch (FormatException e) {
                    throw new Exception("Format problem in file '" + shadowPriceFile.FullName + "' at line " + reader.LineNumber + " with content '" + line + "'.", e);
                }
            }

            return(shadowPrices);
        }
Esempio n. 9
0
        private static void TestIxxi(Dictionary <int, int> zones)
        {
            // the ixxi dictionary holds, for each zone, the share of workers living in the region,
            // who work outside and vice versa
            var ixxi = new Dictionary <int, Tuple <double, double> >();
            var file = Global.GetInputPath(Global.Configuration.IxxiPath).ToFile();

            using (var reader = new CountingReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                int lineNum = 1;

                if (Global.Configuration.IxxiFirstLineIsHeader)
                {
                    reader.ReadLine();
                    lineNum++;
                }

                try {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        var row    = line.Split(new[] { Global.Configuration.IxxiDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                        var zoneId = Convert.ToInt32(row[0]);
                        if (!zones.ContainsKey(zoneId))
                        {
                            PrintWarning("Internal-external worker", "zoneId", zoneId, lineNum);
                        }

                        var workersWithJobsOutside = Convert.ToDouble(row[1]);
                        {
                            if (workersWithJobsOutside > 1 || workersWithJobsOutside < 0)
                            {
                                PrintWarning("Internal-external worker", "fraction of workers with jobs outside", workersWithJobsOutside, lineNum);
                            }
                        }
                        var workersWithJobsFilledFromOutside = Convert.ToDouble(row[2]);
                        if (workersWithJobsFilledFromOutside > 1 || workersWithJobsFilledFromOutside < 0)
                        {
                            PrintWarning("Internal-external worker", "fraction of workers with jobs filled from outside", workersWithJobsOutside, lineNum);
                        }
                        lineNum++;
                    }
                } catch (Exception e) {
                    throw new Exception("Error reading internal-external worker file on line " + lineNum, innerException: e);
                }
            }
        }
Esempio n. 10
0
        public void Import(string path)
        {
            FileInfo inputFile = new FileInfo(_inputPath);
            FileInfo ifile     = ModelUtility.GetIndexFile(path);
            FileInfo file      = new FileInfo(path);

            if (Global.PrintFile != null)
            {
                Global.PrintFile.WriteLine(@"Importing ""{0}"" into ""{1}"" for type {2}.", inputFile.Name, file.Name, typeof(TModel).Name);
                Global.PrintFile.WriteLine(@"Creating file ""{0}"".", file.FullName);
                Global.PrintFile.WriteLine(@"Creating index file ""{0}"" for primary key.", ifile.FullName);
            }

            using (CountingReader reader = new CountingReader(inputFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                using (BinaryWriter iwriter = new BinaryWriter(ifile.Open(FileMode.Create, FileAccess.Write, FileShare.Read))) {
                    using (BinaryWriter writer = new BinaryWriter(file.Open(FileMode.Create, FileAccess.Write, FileShare.Read))) {
                        long position = 0L;

                        reader.ReadLine();

                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            string[] row   = line.Split(new[] { _delimiter });
                            TModel   model = new TModel();

                            SetModel(model, row);

                            Write(iwriter, new Element(model.Id, position));
                            Write(writer, model);

                            position += _size;

                            if (reader.LineNumber % 1024 == 0)
                            {
                                iwriter.Flush();
                                writer.Flush();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        private void CreateDataFile()
        {
            if (!Global.Configuration.ShouldOutputAlogitData)
            {
                return;
            }

            var totalObservationItems = _observation.Count(oi => oi != null);
            var tempFile = new FileInfo(Global.GetEstimationPath(Global.Configuration.OutputAlogitDataPath) + ".tmp");
            var dataFile = new FileInfo(Global.GetEstimationPath(Global.Configuration.OutputAlogitDataPath));

            using (var tempReader = new CountingReader(tempFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                using (var dataWriter = new StreamWriter(dataFile.Open(FileMode.Create, FileAccess.Write, FileShare.Read))) {
                    string line;

                    while ((line = tempReader.ReadLine()) != null)
                    {
                        var tokens = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray();

                        dataWriter.Write(tempReader.LineNumber);
                        dataWriter.Write(" ");

                        for (var i = 0; i < totalObservationItems; i++)
                        {
                            if (i < tokens.Length)
                            {
                                dataWriter.Write(tokens[i]);
                                dataWriter.Write(" ");
                            }
                            else
                            {
                                dataWriter.Write(0);
                                dataWriter.Write(" ");
                            }
                        }

                        dataWriter.WriteLine();
                    }
                }
            }

            tempFile.Delete();
        }
Esempio n. 12
0
        private static void AppendFile(FileInfo local, FileInfo remote, ref bool appendHeader)
        {
            if (!remote.Exists)
            {
                return;
            }

            var fileMode =
                appendHeader
                    ? FileMode.Open
                    : FileMode.Create;

            using (var reader = new CountingReader(remote.OpenRead())) {
                using (var stream = local.Open(fileMode, FileAccess.Write, FileShare.Read)) {
                    stream.Seek(0, SeekOrigin.End);

                    using (var writer = new StreamWriter(stream)) {
                        var    firstLine = true;
                        string line;

                        while ((line = reader.ReadLine()) != null)
                        {
                            if (firstLine)
                            {
                                if (!appendHeader)
                                {
                                    writer.WriteLine(line);

                                    appendHeader = true;
                                }

                                firstLine = false;

                                continue;
                            }

                            writer.WriteLine(line);
                        }
                    }
                }
            }
        }
Esempio n. 13
0
        private static void TestParkAndRideNodes(Dictionary <int, int> zones)
        {
            // this dictionary holds the
            var parkAndRideNodes = new Dictionary <int, Tuple <int, int, int, int, int> >();
            var rawFile          = Global.GetInputPath(Global.Configuration.RawParkAndRideNodePath).ToFile();
            var inputFile        = Global.GetInputPath(Global.Configuration.InputParkAndRideNodePath).ToFile();
            int lineNum          = 1;

            try {
                using (var reader = new CountingReader(rawFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                    reader.ReadLine();

                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        var row    = line.Split(new[] { Global.Configuration.RawParkAndRideNodeDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                        var id     = Convert.ToInt32(row[0]);
                        var zoneId = Convert.ToInt32(row[1]);

                        if (!zones.ContainsKey(zoneId))
                        {
                            PrintWarning("Park and Ride Node", "zoneID", zoneId, lineNum);
                        }

                        var capacity = Convert.ToInt32(row[4]);
                        if (capacity < 0 || capacity > Global.Settings.MaxInputs.MaxPnrCap)
                        {
                            PrintWarning("Park and Ride Node", "Capacity", capacity, lineNum);
                        }
                        var cost = Convert.ToInt32(row[5]);
                        if (cost < 0 || cost > Global.Settings.MaxInputs.MaxPnrCost)
                        {
                            PrintWarning("Park and Ride Node", "cost", cost, lineNum);
                        }
                        lineNum++;
                    }
                }
            } catch (Exception e) {
                throw new Exception("Error reading Park and Ride Nodes file on line " + lineNum, innerException: e);
            }
        }
Esempio n. 14
0
        private static void RunRemoteProcesses()
        {
            using (var reader = new CountingReader(new FileInfo(Global.GetInputPath(Global.Configuration.RawHouseholdPath)).OpenRead())) {
                while (reader.ReadLine() != null)
                {
                    _householdCount++;
                }
            }

            _householdCount--;

            var machines = RemoteMachine.GetAll();
            var range    = (int)Math.Ceiling(_householdCount / machines.Count);

            for (var i = 0; i < machines.Count; i++)
            {
                var start = (range * i);
                var end   = (int)Math.Min((range * i) + range - 1, _householdCount - 1);

                var machine = machines[i];

                //run a local remote DaySim session for debugging if desired
                if (Environment.MachineName.Equals(machine.Name, StringComparison.OrdinalIgnoreCase))
                {
                    var process = new Process {
                        StartInfo =
                        {
                            UseShellExecute = false,
                            CreateNoWindow  = true,
                            FileName        = machine.Filename,
                            Arguments       = machine.Arguments + " /s=" + start + " /e=" + end + " /i=" + i
                        }
                    };

                    process.Start();

                    _processes.Add(Tuple.Create(process, machine, new Timer()));
                }
                else
                {
                    //remote a remote DaySim session using WMI and RPC
                    var connectionOptions = new ConnectionOptions {
                        Username = Global.Configuration.RemoteUsername, Password = Global.Configuration.RemotePassword
                    };
                    var managementScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", machine.Name), connectionOptions);

                    managementScope.Connect();

                    var processClass = new ManagementClass(managementScope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
                    var inParameters = processClass.GetMethodParameters("Create");

                    inParameters["CurrentDirectory"] = machine.CurrentDirectory;
                    inParameters["CommandLine"]      = machine.CommandLine + " /s=" + start + " /e=" + end + " /i=" + i;

                    var outParameters = processClass.InvokeMethod("Create", inParameters, null);

                    if ((uint)outParameters.Properties["ReturnValue"].Value == 0)
                    {
                        _instances.Add(Tuple.Create(managementScope, outParameters, machine, new Timer()));
                    }
                }
            }

            var threads = new List <Thread>();

            foreach (var process in _processes)
            {
                var start  = new ParameterizedThreadStart(BeginLocalWatch);
                var thread = new Thread(start);

                thread.Start(process);
                threads.Add(thread);
            }

            foreach (var instance in _instances)
            {
                var start  = new ParameterizedThreadStart(BeginRemoteWatch);
                var thread = new Thread(start);

                thread.Start(instance);
                threads.Add(thread);
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }
        }
Esempio n. 15
0
        internal void Parse(CountingReader reader, ParserContext context)
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                line = line.Trim();
                LineType type = GetLineType(line);

                if (type == LineType.Empty || type == LineType.Comment)
                {
                    continue;
                }
                if (type == LineType.Unknown)
                {
                    throw new ParserExceptions.UnknownLineTypeException();
                }

                if (type == LineType.ComponentClose)
                {
                    if (context.Type != ParserContext.ContextType.Component)
                    {
                        throw new Exception("Syntax error - Cannot end component outside of component block");
                    }

                    Lines.Add(new LineTypes.ComponentClose());
                    return;
                }
                if (type == LineType.ObjectClose)
                {
                    if (context.Type != ParserContext.ContextType.Object)
                    {
                        throw new Exception("Syntax error - Cannot end object outside of object block");
                    }

                    Lines.Add(new LineTypes.ObjectClose());
                    return;
                }

                if (type == LineType.Property)
                {
                    if (context.Properties.Length == 0)
                    {
                        throw new Exception("Properties are not allowed in this context. Valid members are: " + context.ListValidMembers());
                    }

                    string propName = line.Split('=')[0].Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Properties.Length; i++)
                    {
                        if (propName == context.Properties[i].name.ToLower())
                        {
                            found = true;
                            Lines.Add(GetProperty(line, context.Properties[i]));
                            break;
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Invalid property member \"" + propName + "\". Valid members are: " + context.ListValidMembers());
                    }
                    continue;
                }
                if (type == LineType.Component)
                {
                    if (context.Components.Length == 0)
                    {
                        throw new Exception("Components are not allowed in this context. Valid members are: " + context.ListValidMembers());
                    }

                    string compName = line.Substring(0, line.IndexOf('(')).Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Components.Length; i++)
                    {
                        if (compName == context.Components[i].name.ToLower())
                        {
                            found = true;
                            Lines.Add(new LineTypes.Component(context.Components[i].name, context.Components[i].context));
                            Parse(reader, context.Components[i].context);
                            break;
                        }
                    }

                    if (!found)
                    {
                        throw new Exception("Invalid component member \"" + compName + "\". Valid members are: " + context.ListValidMembers());
                    }
                    continue;
                }
                if (type == LineType.Object)
                {
                    if (!context.ObjectsAllowed)
                    {
                        throw new Exception("Objects are not allowed in this context. Valid members are: " + context.ListValidMembers());
                    }

                    string objectName = line.Substring(0, line.IndexOf('[')).Trim();
                    Lines.Add(new LineTypes.Object(objectName));
                    Parse(reader, this.objectContext);

                    continue;
                }
            }
        }
Esempio n. 16
0
        public virtual IEnumerable <RosterEntry> LoadRoster(string filename, bool checkCombination = true)
        {
            var file = filename.ToFile();

            Global.PrintFile.WriteFileInfo(file, true);

            _path = file.DirectoryName;

            var entries = new List <RosterEntry>();

            using (var reader = new CountingReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    var tokens = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray();

                    if (tokens.Length == 0)
                    {
                        continue;
                    }

                    var entry = new RosterEntry {
                        Variable      = tokens[0].Clean(),
                        Mode          = tokens[1].ToMode(),
                        PathType      = tokens[2].ToPathType(),
                        VotGroup      = tokens[3].ToVotGroup(),
                        StartMinute   = int.Parse(tokens[4]).ToMinutesAfter3AM(),
                        EndMinute     = int.Parse(tokens[5]).ToMinutesAfter3AM(),
                        Length        = tokens[6].Clean(),
                        FileType      = tokens[7].Clean(),
                        Name          = tokens[8],
                        Field         = int.Parse(tokens[9]),
                        Transpose     = bool.Parse(tokens[10]),
                        BlendVariable = tokens[11].Clean(),
                        BlendPathType = tokens[12].ToPathType(),
                        Factor        = tokens[13].ToFactor(),
                        Scaling       = ParseScaling(tokens[14])
                    };

                    if (checkCombination)
                    {
                        if (!IsPossibleCombination(entry.Mode, entry.PathType))
                        {
                            throw new InvalidCombinationException(
                                      string.Format(
                                          "The combination of mode: {0} and path type: {1} is invalid. Please adjust the roster accordingly.", entry.Mode,
                                          entry.PathType));
                        }

                        ActualCombinations[entry.Mode][entry.PathType] = true;
                    }

                    entries.Add(entry);
                }
            }

            return(entries);
        }
Esempio n. 17
0
		static void Main(string[] args)
		{
			var arguments = new List<string>(args);
			var switches = new List<string>(arguments.Where(a => a[0] == '-'));
			arguments.RemoveAll(a => switches.Contains(a));

			if (Console.IsInputRedirected)
			{
				string line;
				while (!string.IsNullOrEmpty(line = Console.ReadLine()))
				{
					arguments.Add(line);
				}
			}

			if (!arguments.Any())
			{
				PrintHelp();
				return;
			}

			List<F> files = new List<F>();

			try
			{
				foreach (var arg in arguments)
				{
					files.Add(new F{
						Name = Path.GetFileName(arg).Trim(),
						FullPath = Path.GetFullPath(arg).Trim()
					});
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e.Message);
				return;
			}

			var formatters = new List<string>();

			if (switches.Contains("-f"))
			{
				var longest = files.Max(f => f.FullPath.Length);
				formatters.Add("{2," + longest + "}");
			}

			if (switches.Contains("-n"))
			{
				var longest = files.Max(f => f.Name.Length);
				formatters.Add("{1," + longest + "}");
			}

			if (switches.Contains("-l"))
			{
				formatters.Add("({3,5})");
			}

			var formatString = string.Join(" ", formatters);

			if (formatters.Any())
			{
				formatString += ": ";
			}

			formatString += "{0}";

			foreach(var f in files)
			{
				try
				{
					using (var stream = new CountingReader(new FileStream(f.FullPath, FileMode.Open, FileAccess.Read)))
					{
						string line;
						while ((line = stream.ReadLine()) != null)
						{
							Console.WriteLine(
								formatString,
								line,
								f.Name,
								f.FullPath,
								stream.LineNumber);
						}
					}
				}
				catch(UnauthorizedAccessException e)
				{
				}
			}
		}
Esempio n. 18
0
        public virtual ICoefficient[] Read(string path, out string title, out ICoefficient sizeFunctionMultiplier, out ICoefficient nestCoefficient)
        {
            title = null;
            sizeFunctionMultiplier = null;
            nestCoefficient        = null;

            var coefficients          = new Dictionary <int, Coefficient>();
            var file                  = new FileInfo(path);
            var baseSizeVariableFound = false;

            using (var reader = new CountingReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Trim() == "END")
                    {
                        break;
                    }

                    if (string.IsNullOrEmpty(title))
                    {
                        title = line;
                    }
                }

                while ((line = reader.ReadLine()) != null)
                {
                    if (line.Trim() == "-1")
                    {
                        break;
                    }

                    var tokens = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    if (tokens.Length < 1)
                    {
                        continue;
                    }

                    int parameter;

                    int.TryParse(tokens[0], out parameter);

                    var label      = tokens[1].Trim();
                    var constraint = tokens[2];

                    double coefficientValue;

                    double.TryParse(tokens[3], out coefficientValue);

                    var coefficient = new Coefficient {
                        Parameter                = parameter,
                        Label                    = label,
                        Constraint               = constraint,
                        Value                    = coefficientValue,
                        IsSizeVariable           = label.StartsWith("Gamm"),
                        IsParFixed               = constraint.ToLower() == "t" || constraint.ToLower() == "c",
                        IsSizeFunctionMultiplier = label.StartsWith("LSM_"),
                        IsNestCoefficient        = label.StartsWith("Nest")
                    };

                    if (coefficient.IsSizeFunctionMultiplier)
                    {
                        sizeFunctionMultiplier = coefficient;
                    }

                    if (coefficient.IsNestCoefficient)
                    {
                        nestCoefficient = coefficient;
                    }

                    if (!baseSizeVariableFound && coefficient.IsSizeVariable && coefficient.IsParFixed && coefficient.Value.AlmostEquals(0))
                    {
                        baseSizeVariableFound = true;

                        coefficient.IsBaseSizeVariable = true;
                    }

                    coefficients.Add(parameter, coefficient);
                }
            }

            var max   = coefficients.Values.Max(c => c.Parameter) + 1;
            var array = new Coefficient[max];

            for (var i = 0; i <= max; i++)
            {
                Coefficient coefficient;

                if (coefficients.TryGetValue(i, out coefficient))
                {
                    array[i] = coefficient;
                }
            }

            return(array);
        }
Esempio n. 19
0
        public virtual void LoadRosterCombinations()
        {
            var file = Global.GetInputPath(Global.Configuration.RosterCombinationsPath).ToFile();

            Global.PrintFile.WriteFileInfo(file, true);

            PossibleCombinations = new bool[Global.Settings.Modes.RosterModes][];
            ActualCombinations   = new bool[Global.Settings.Modes.RosterModes][];

            for (var mode = Global.Settings.Modes.Walk; mode < Global.Settings.Modes.RosterModes; mode++)
            {
                PossibleCombinations[mode] = new bool[Global.Settings.PathTypes.TotalPathTypes];
                ActualCombinations[mode]   = new bool[Global.Settings.PathTypes.TotalPathTypes];
            }

            using (var reader = new CountingReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                string line;

                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    var tokens = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).ToArray();

                    if (tokens.Length == 0)
                    {
                        continue;
                    }

                    int pathType;

                    switch (tokens[0])
                    {
                    case "full-network":
                        pathType = Global.Settings.PathTypes.FullNetwork;

                        break;

                    case "no-tolls":
                        pathType = Global.Settings.PathTypes.NoTolls;

                        break;

                    case "local-bus":
                        pathType = Global.Settings.PathTypes.LocalBus;

                        break;

                    case "light-rail":
                        pathType = Global.Settings.PathTypes.LightRail;

                        break;

                    case "premium-bus":
                        pathType = Global.Settings.PathTypes.PremiumBus;

                        break;

                    case "commuter-rail":
                        pathType = Global.Settings.PathTypes.CommuterRail;

                        break;

                    case "ferry":
                        pathType = Global.Settings.PathTypes.Ferry;

                        break;

                    default:
                        throw new InvalidPathTypeException(string.Format("The value of \"{0}\" used for path type is invalid. Please adjust the roster accordingly.", tokens[0]));
                    }

                    for (var mode = Global.Settings.Modes.Walk; mode < Global.Settings.Modes.RosterModes; mode++)
                    {
                        PossibleCombinations[mode][pathType] = bool.Parse(tokens[mode]);
                    }
                }
            }
        }
Esempio n. 20
0
        internal void Parse(CountingReader reader, ParserContext context)
        {
            while (!reader.EndOfStream) {

                string line = reader.ReadLine();
                line = line.Trim();
                LineType type = GetLineType(line);

                if (type == LineType.Empty || type == LineType.Comment) continue;
                if (type == LineType.Unknown) throw new ParserExceptions.UnknownLineTypeException();

                if (type == LineType.ComponentClose) {
                    if (context.Type != ParserContext.ContextType.Component)
                        throw new Exception("Syntax error - Cannot end component outside of component block");

                    Lines.Add(new LineTypes.ComponentClose());
                    return;
                }
                if (type == LineType.ObjectClose) {
                    if (context.Type != ParserContext.ContextType.Object)
                        throw new Exception("Syntax error - Cannot end object outside of object block");

                    Lines.Add(new LineTypes.ObjectClose());
                    return;
                }

                if (type == LineType.Property) {
                    if (context.Properties.Length == 0) throw new Exception("Properties are not allowed in this context. Valid members are: " + context.ListValidMembers());

                    string propName = line.Split('=')[0].Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Properties.Length; i++) {
                        if (propName == context.Properties[i].name.ToLower()) {
                            found = true;
                            Lines.Add(GetProperty(line, context.Properties[i]));
                            break;
                        }
                    }

                    if (!found) throw new Exception("Invalid property member \"" + propName + "\". Valid members are: " + context.ListValidMembers());
                    continue;
                }
                if (type == LineType.Component) {
                    if (context.Components.Length == 0) throw new Exception("Components are not allowed in this context. Valid members are: "+context.ListValidMembers());

                    string compName = line.Substring(0, line.IndexOf('(')).Trim().ToLower();

                    bool found = false;
                    for (int i = 0; i < context.Components.Length; i++) {
                        if (compName == context.Components[i].name.ToLower()) {
                            found = true;
                            Lines.Add(new LineTypes.Component(context.Components[i].name, context.Components[i].context));
                            Parse(reader, context.Components[i].context);
                            break;
                        }
                    }

                    if (!found) throw new Exception("Invalid component member \"" + compName + "\". Valid members are: " + context.ListValidMembers());
                    continue;
                }
                if (type == LineType.Object) {
                    if (!context.ObjectsAllowed) throw new Exception("Objects are not allowed in this context. Valid members are: "+context.ListValidMembers());

                    string objectName = line.Substring(0, line.IndexOf('[')).Trim();
                    Lines.Add(new LineTypes.Object(objectName));
                    Parse(reader, this.objectContext);

                    continue;
                }

            }
        }
Esempio n. 21
0
        public SkimMatrix Read(string filename, int field, float scale)
        {
            var file = new FileInfo(Path.Combine(_path, filename));

            Console.WriteLine("Loading skim file: {0}, field: {1}.", file.Name, field);
            Global.PrintFile.WriteFileInfo(file, true);

            if (!file.Exists)
            {
                throw new FileNotFoundException(string.Format("The skim file {0} could not be found.", file.FullName));
            }

            var count  = _mapping.Count;
            var matrix = new ushort[count][];

            for (var i = 0; i < count; i++)
            {
                matrix[i] = new ushort[count];
            }

            //            List<float[]> rows;
            List <double[]> rows;   // 20150703 JLB

            if (!_cache.TryGetValue(file.Name, out rows))
            {
                //                rows = new List<float[]>();
                rows = new List <double[]>();  // 20150703 JLB

                using (var reader = new CountingReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.Read))) {
                    string line;
                    if (Global.TextSkimFilesContainHeaderRecord)
                    {
                        reader.ReadLine();
                    }
                    while ((line = reader.ReadLine()) != null)
                    {
                        try {
                            char delimiter = Global.SkimDelimiter;
                            var  p1        = line.Split(new[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
                            //                            var row = p1.Select(Convert.ToSingle).ToArray();
                            var row = p1.Select(Convert.ToDouble).ToArray();     // 20150703 JLB

                            rows.Add(row);
                        } catch (Exception e) {
                            throw new InvalidSkimRowException(string.Format("Error parsing row in {0}.\nError in row {1}.\n{{{2}}}", file.FullName, reader.LineNumber, line), e);
                        }
                    }
                }

                _cache.Add(file.Name, rows);
            }
            for (var i = 0; i < rows.Count; i++)
            {
                var row = rows[i];

                try {
                    // test code
                    var index  = Convert.ToInt32(row[0]);
                    var origin = _mapping[index];
                    //var origin = _mapping[Convert.ToInt32(row[0])];

                    index = Convert.ToInt32(row[1]);
                    var destination = _mapping[index];
                    //var destination = _mapping[Convert.ToInt32(row[1])];

                    var rawValue = Convert.ToSingle(row[field + FIELD_OFFSET]) * scale;

                    if (rawValue > ushort.MaxValue - 1)
                    {
                        rawValue = ushort.MaxValue - 1;
                    }
                    else if (rawValue < 0)
                    {
                        rawValue = 0;
                    }

                    var value = Convert.ToUInt16(rawValue);

                    matrix[origin][destination] = value;
                } catch (Exception e) {
                    throw new ErrorReadingSkimFileException(string.Format("Error reading row {0}, {1}", i, string.Join(" ", row)), e);
                }
            }

            var skimMatrix = new SkimMatrix(matrix);

            return(skimMatrix);
        }