Ejemplo n.º 1
0
        /// <summary>
        /// if there is no conversion method between source and target units, creates one otherwise fails
        /// </summary>
        /// <param name="description"></param>
        /// <param name="formula"></param>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public ConversionMethod CreateConversionMethod(string formula, string description, Unit source, Unit target)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(formula), "formula can not be empty");
            Contract.Requires(formula.Contains("s"), "Formula must use \'s\' as the representative for the source unit");
            Contract.Requires(source != null, "source unit can not be null");
            Contract.Requires(source.Id >= 0, "source unit must be persisted before this call");
            Contract.Requires(target != null, "target unit can not be null");
            Contract.Requires(target.Id >= 0, "target unit must be persisted before this call");

            Contract.Ensures(Contract.Result <ConversionMethod>() != null && Contract.Result <ConversionMethod>().Id >= 0, "No Conversion Method persisted!");

            ConversionMethod cm = new ConversionMethod()
            {
                Formula     = formula,
                Description = description,
                Source      = source,
                Target      = target,
            };

            source.ConversionsIamTheSource.Add(cm);
            target.ConversionsIamTheTarget.Add(cm);

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository <ConversionMethod> repoCM = uow.GetRepository <ConversionMethod>();
                ConversionMethod temp = repoCM.Get(c => c.Source.Id == source.Id && c.Target.Id == target.Id).FirstOrDefault(); // change it to Count instead of Get
                if (temp != null)
                {
                    throw new Exception(string.Format("There is already a conversion method between {0} and {1} having [{2}] formula", cm.Source.Name, cm.Target.Name, cm.Formula));
                }
                repoCM.Put(cm);
                uow.Commit();
            }
            return(cm);
        }
Ejemplo n.º 2
0
        public Conversion Add(Metric from, Metric to, ConversionMethod method)
        {
            Conversion conversion = new Conversion(from, to, method);

            conversions.Add(conversion);
            return(conversion);
        }
Ejemplo n.º 3
0
        public void CreateConversionsBetweenUnitsTest()
        {
            UnitManager um = new UnitManager();

            try
            {
                var dummyDimension = um.DimensionRepo.Query().First();

                Unit km = um.Create("KilometerTest", "KmTest", "This is the Kilometer", dummyDimension, MeasurementSystem.Metric);
                Unit m  = um.Create("MeterTest", "MTest", "This is the Meter", dummyDimension, MeasurementSystem.Metric);

                ConversionMethod cm2 = um.CreateConversionMethod("s*1000", "Converts kilometer to meter", km, m);
                ConversionMethod cm3 = um.CreateConversionMethod("s/1000", "Converts meter to kilometer", m, km);

                km.ConversionsIamTheSource.First().Target.Should().BeEquivalentTo(m);
                cm3.Source.Name.Should().BeEquivalentTo(m.Name);
                cm3.Target.Name.Should().BeEquivalentTo(cm2.Source.Name);

                // cleanup the DB.
                um.DeleteConversionMethod(new List <ConversionMethod>()
                {
                    cm2, cm3
                });
                um.Delete(new List <Unit>()
                {
                    km, m
                });
            }
            finally
            {
                um.Dispose();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a conversion rule to the system
        /// </summary>
        /// <param name="symbolfrom">A single unit symbol that is known in the system</param>
        /// <param name="symbolto">A formula containing numbers and unit symbols defining the target units</param>
        /// <param name="method">A delegate that does the actual conversion of an Exponential</param>
        /// <returns></returns>
        public Conversion AddConversion(string symbolfrom, string symbolto, ConversionMethod method)
        {
            Metric from = Metrics.ParseMetric(symbolfrom);
            Metric to   = Metrics.ParseMetric(symbolto);

            return(Conversions.Add(from, to, method));
        }
Ejemplo n.º 5
0
        public override void Execute(string[] args)
        {
            string file = QuotationRemover(args[0]);
            string ext  = Path.GetExtension(file).ToLower();

            string           method     = args.Length > 1 ? "." + args[1] : ext;
            ConversionMethod converter  = ConversionMethod.FindConvertor(method);
            string           outputFile = Path.GetFileNameWithoutExtension(file);

            if (converter == null)
            {
                Console.WriteLine("These are not the formats we are looking for...");
            }
            else if (converter.Inputs.Contains(ext))
            {
                Convert(file, outputFile + converter.Outputs[0], converter, true);
            }
            else if (converter.Outputs.Contains(ext))
            {
                Convert(file, outputFile + converter.Inputs[0], converter, false);
            }
            else
            {
                Console.WriteLine("These are not the formats we are looking for...");
            }
        }
Ejemplo n.º 6
0
 public AbstractColor this[ConversionMethod type]
 {
     get
     {
         switch (type)
         {
             case ConversionMethod.toCIELab:
                 return toCIELab();
             case ConversionMethod.toCIELCh:
                 return toCIELCh();
             case ConversionMethod.toCMY:
                 return toCMY();
             case ConversionMethod.toCMYK:
                 return toCMYK();
             case ConversionMethod.toHex:
                 return toHex();
             case ConversionMethod.toHSV:
                 return toHSV();
             case ConversionMethod.toRGB:
                 return toRGB();
             case ConversionMethod.toXYZ:
                 return toXYZ();
             case ConversionMethod.toYxy:
                 return toYxy();
             default:
                 return toRGB();
         }
     }
 }
Ejemplo n.º 7
0
 private static void RunStringTrimTests(ConversionMethod <string, string> trimString)
 {
     Console.WriteLine("Testing {0}...", trimString.Method.Name);
     ConversionTest(trimString, "AAA BBB AAA", "A B A");
     ConversionTest(trimString, "ABC", "ABC");
     Console.WriteLine("{0} is OK.", trimString.Method.Name);
 }
Ejemplo n.º 8
0
 private static void RunBinaryConversionTests(ConversionMethod <int, string> decToBin)
 {
     Console.WriteLine("Testing {0}...", decToBin.Method.Name);
     ConversionTest(decToBin, 0, Convert.ToString(0, 2));
     ConversionTest(decToBin, 1, Convert.ToString(1, 2));
     ConversionTest(decToBin, 58, Convert.ToString(58, 2));
     ConversionTest(decToBin, -58, Convert.ToString(-58, 2));
     Console.WriteLine("{0} is OK.", decToBin.Method.Name);
 }
Ejemplo n.º 9
0
        public void AddConversion(SystemOfUnits system, string from, string formula, Exponential number)
        {
            Metric metricfrom = system.Metrics.ParseMetric(from);
            Metric metricto   = system.Metrics.ParseMetric(Parser.ToUnaryTokens(formula).NonNumerics());

            if ((metricfrom != null) && (metricto != null))
            {
                ConversionMethod method = BuildConversion(formula, number);
                system.Conversions.Add(metricfrom, metricto, method);
            }
        }
Ejemplo n.º 10
0
    private static void ConversionTest <S, R>(ConversionMethod <S, R> method, S source, R expected)
    {
        R result = method.Invoke(source);

        if (result.Equals(expected))
        {
            Console.WriteLine("OK: {0} -> {1}", source, result);
            return;
        }
        string message = String.Format("FAIL: {0} -> {1}, but expected {2}", source, result, expected);

        throw new Exception(message);
    }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            CompressionMethod.Load();
            ConversionMethod.Load();
            ArchiveMethod.Load();

            Console.ForegroundColor = ConsoleColor.White;

            Command root = new CommandRouter("", new Command[] {
                new CompressCommand(),
                new ConvertCommand(),
                new PackCommand()
            });

            if (args.Length > 0)
            {
                root.Execute(args);
                return;
            }

            while (true)
            {
                Console.Write("> ");
                string junk = Console.ReadLine();

                Stopwatch watch = new Stopwatch();

                watch.Start();
                try
                {
                    root.Execute(junk.Split(' '));
                }
                catch (CommandParseException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(ex.Message);
                    Console.ForegroundColor = ConsoleColor.White;
                }

                watch.Stop();

                Console.WriteLine();
                Console.WriteLine("Command took " + watch.ElapsedMilliseconds + "ms to execute.");
            }
        }
Ejemplo n.º 12
0
        public ConversionMethod UpdateConversionMethod(ConversionMethod entity)
        {
            Contract.Requires(entity != null, "provided entity can not be null");
            Contract.Requires(entity.Id >= 0, "provided entity must have a permant ID");

            Contract.Ensures(Contract.Result <ConversionMethod>() != null && Contract.Result <ConversionMethod>().Id >= 0, "No entity is persisted!");

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository <ConversionMethod> repo = uow.GetRepository <ConversionMethod>();
                repo.Merge(entity);
                var merged = repo.Get(entity.Id);
                repo.Put(merged);
                uow.Commit();
            }
            return(entity);
        }
    private void GetConversionMethod()
    {
        bool swapRedBlue = false;

        _conversionMethod = ConversionMethod.UnityScript;

//#if UNITY_4_3 || UNITY_4_2 || UNITY_4_1 || UNITY_4_0_1 || UNITY_4_0
#if !UNITY_3_5 && !UNITY_3_4 && !UNITY_3_3 && !UNITY_3_2 && !UNITY_3_1 && !UNITY_3_0
        _conversionMethod = ConversionMethod.Unity4;
        if (SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11"))
        {
            swapRedBlue = true;
        }
#elif UNITY_3_5 || UNITY3_4
        if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
        {
#if UNITY_3_4
            _conversionMethod = ConversionMethod.Unity34_OpenGL;
#elif UNITY_3_5
            _conversionMethod = ConversionMethod.Unity35_OpenGL;
#endif
        }
        else
        {
            swapRedBlue = true;
        }
#else
        _conversionMethod = ConversionMethod.UnityScript;
        swapRedBlue       = true;
#endif

        if (swapRedBlue)
        {
            Shader.DisableKeyword("SWAP_RED_BLUE_OFF");
            Shader.EnableKeyword("SWAP_RED_BLUE_ON");
        }
        else
        {
            Shader.DisableKeyword("SWAP_RED_BLUE_ON");
            Shader.EnableKeyword("SWAP_RED_BLUE_OFF");
        }
    }
Ejemplo n.º 14
0
        public AbstractColor this[ConversionMethod type]
        {
            get
            {
                switch (type)
                {
                case ConversionMethod.toCIELab:
                    return(toCIELab());

                case ConversionMethod.toCIELCh:
                    return(toCIELCh());

                case ConversionMethod.toCMY:
                    return(toCMY());

                case ConversionMethod.toCMYK:
                    return(toCMYK());

                case ConversionMethod.toHex:
                    return(toHex());

                case ConversionMethod.toHSV:
                    return(toHSV());

                case ConversionMethod.toRGB:
                    return(toRGB());

                case ConversionMethod.toXYZ:
                    return(toXYZ());

                case ConversionMethod.toYxy:
                    return(toYxy());

                default:
                    return(toRGB());
                }
            }
        }
Ejemplo n.º 15
0
        private static void Convert(string file, string outputFile, ConversionMethod converter, bool to)
        {
            FileStream input, output;

            try
            {
                input  = new FileStream(file, FileMode.Open);
                output = new FileStream(outputFile, FileMode.Create);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            try
            {
                if (to)
                {
                    converter.ConvertTo(input, output);
                }
                else
                {
                    converter.ConvertFrom(input, output);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
            finally
            {
                input.Close();
                output.Close();
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Deletes the proveded conversion method, but does not touch the source and target units
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool DeleteConversionMethod(ConversionMethod entity)
        {
            Contract.Requires(entity != null, "provided unit can not be null");
            Contract.Requires(entity.Id >= 0, "Id can not be empty");

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository <ConversionMethod> repoCM = uow.GetRepository <ConversionMethod>();

                entity = repoCM.Reload(entity);

                // remove all associations
                entity.Source = null;
                entity.Target = null;

                //delete the entity
                repoCM.Delete(entity);

                // commit changes
                uow.Commit();
            }
            // if any problem was detected during the commit, an exception will be thrown!
            return(true);
        }
Ejemplo n.º 17
0
        public static ConversionMethod BuildConversion(string formula, Exponential number)
        {
            ParameterExpression param = Expression.Parameter(typeof(Exponential), "value");

            Expression body = Expression.Multiply(param, Expression.Constant(number));

            foreach (Unary u in Parser.ToUnaryTokens(formula).Numerics())
            {
                Exponential factor = u.Numeric();
                if (u.Exponent == 1)
                {
                    body = Expression.Multiply(body, Expression.Constant(factor));
                }
                else if (u.Exponent == -1)
                {
                    body = Expression.Divide(body, Expression.Constant(factor));
                }
            }

            Expression <ConversionMethod> expression = Expression.Lambda <ConversionMethod>(body, param);
            ConversionMethod method = expression.Compile();

            return(method);
        }
Ejemplo n.º 18
0
 public Conversion Add(Metric from, Metric to, ConversionMethod method)
 {
     Conversion conversion = new Conversion(from, to, method);
     conversions.Add(conversion);
     return conversion;
 }
Ejemplo n.º 19
0
 public Conversion(Metric from, Metric to, ConversionMethod method)
 {
     this.From = from;
     this.To = to;
     this.method = method;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Constructor used to setup method and decimal value.
 /// </summary>
 /// <param name="val">Decimal value</param>
 /// <param name="method">Conversion method (<seealso cref="ConversionMethod"/> enum) </param>
 public BinaryUInt(uint val, ConversionMethod method = ConversionMethod.StandardMethod)
 {
     Method       = method;
     DecimalValue = val;
 }
Ejemplo n.º 21
0
 public Conversion(Metric from, Metric to, ConversionMethod method)
 {
     this.From   = from;
     this.To     = to;
     this.method = method;
 }
Ejemplo n.º 22
0
	private void GetConversionMethod()
	{
		bool swapRedBlue = false;

		_conversionMethod = ConversionMethod.UnityScript;

#if AVPRO_UNITY_4_X
		_conversionMethod = ConversionMethod.Unity4;
		if (SystemInfo.graphicsDeviceVersion.StartsWith("Direct3D 11"))
			swapRedBlue = true;

#elif UNITY_3_5 || UNITY3_4
		if (SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
		{
#if UNITY_3_4
			_conversionMethod = ConversionMethod.Unity34_OpenGL;
#elif UNITY_3_5
			_conversionMethod = ConversionMethod.Unity35_OpenGL;
#endif
		}
		else
		{
			swapRedBlue = true;
		}
#else

		_conversionMethod = ConversionMethod.UnityScript;
		swapRedBlue = true;
#endif

		if (swapRedBlue)
		{
			Shader.DisableKeyword("SWAP_RED_BLUE_OFF");
			Shader.EnableKeyword("SWAP_RED_BLUE_ON");
		}
		else
		{
			Shader.DisableKeyword("SWAP_RED_BLUE_ON");
			Shader.EnableKeyword("SWAP_RED_BLUE_OFF");
		}
	}
Ejemplo n.º 23
0
 /// <summary>
 /// Adds a conversion rule to the system
 /// </summary>
 /// <param name="symbolfrom">A single unit symbol that is known in the system</param>
 /// <param name="symbolto">A formula containing numbers and unit symbols defining the target units</param>
 /// <param name="method">A delegate that does the actual conversion of an Exponential</param>
 /// <returns></returns>
 public Conversion AddConversion(string symbolfrom, string symbolto, ConversionMethod method)
 {
     Metric from = Metrics.ParseMetric(symbolfrom);
     Metric to = Metrics.ParseMetric(symbolto);
     return Conversions.Add(from, to, method);
 }