Beispiel #1
0
        internal double MaxPumpAmt()
        {
            double maxPumpAmount = 0;

            if (FromParts == null || ToParts == null || FromParts.Count == 0 || ToParts.Count == 0)
            {
                return(maxPumpAmount);
            }
            List <Part> .Enumerator toList   = ToParts.GetEnumerator();
            List <Part> .Enumerator fromList = FromParts.GetEnumerator();
            double fromAmount          = 0;
            double toCapacityRemaining = 0;

            while (fromList.MoveNext())
            {
                if (fromList.Current == null)
                {
                    continue;
                }
                fromAmount += fromList.Current.Resources[Resource].amount;
            }
            fromList.Dispose();
            while (toList.MoveNext())
            {
                if (toList.Current == null)
                {
                    continue;
                }
                toCapacityRemaining += PartRemainingCapacity(toList.Current, Resource);
            }
            toList.Dispose();
            maxPumpAmount = toCapacityRemaining - fromAmount;
            maxPumpAmount = maxPumpAmount > fromAmount ? fromAmount : maxPumpAmount;
            maxPumpAmount = maxPumpAmount < SMSettings.Tolerance ? 0 : maxPumpAmount;
            return(maxPumpAmount);
        }
Beispiel #2
0
        /// <summary>
        /// Builds a primary select query
        /// </summary>
        /// <returns>Query string</returns>
        private string BuildSelectQuery()
        {
            var sb = new StringBuilder();

            if (QueryType == N1QlQueryType.Subquery)
            {
                if (!string.IsNullOrEmpty(PropertyExtractionPart))
                {
                    // Subqueries will always return a list of objects
                    // But we need to use an ARRAY statement to convert it into an array of a particular property of that object

                    sb.AppendFormat("ARRAY `ArrayExtent`.{0} FOR `ArrayExtent` IN (", PropertyExtractionPart);
                }
                else
                {
                    sb.Append('(');
                }
            }
            else if (QueryType == N1QlQueryType.SubqueryAny)
            {
                sb.AppendFormat("ANY {0} IN (", PropertyExtractionPart);
            }
            else if (QueryType == N1QlQueryType.SubqueryAll)
            {
                sb.AppendFormat("EVERY {0} IN (", PropertyExtractionPart);
            }

            if (!string.IsNullOrWhiteSpace(ExplainPart))
            {
                sb.Append(ExplainPart);
            }

            if (!string.IsNullOrEmpty(AggregateFunction))
            {
                sb.AppendFormat("SELECT {0}({1}{2})",
                                AggregateFunction,
                                !string.IsNullOrWhiteSpace(DistinctPart) ? DistinctPart : string.Empty,
                                SelectPart);
            }
            else
            {
                sb.AppendFormat("SELECT {0}{1}",
                                !string.IsNullOrWhiteSpace(DistinctPart) ? DistinctPart : string.Empty,
                                SelectPart);
                //TODO support multiple select parts: http://localhost:8093/tutorial/content/#5
            }

            if (!IsBucketSubquery && !string.IsNullOrEmpty(PropertyExtractionPart))
            {
                sb.AppendFormat(" as {0}", PropertyExtractionPart);
            }

            if (FromParts.Any())
            {
                var mainFrom = FromParts.First();
                sb.AppendFormat(" FROM {0} as {1}",
                                mainFrom.Source,
                                mainFrom.ItemName);

                if (!string.IsNullOrEmpty(UseKeysPart))
                {
                    sb.AppendFormat(" USE KEYS {0}", UseKeysPart);
                }

                foreach (var joinPart in FromParts.Skip(1))
                {
                    sb.AppendFormat(" {0} {1} as {2}",
                                    joinPart.JoinType,
                                    joinPart.Source,
                                    joinPart.ItemName);

                    if (!string.IsNullOrEmpty(joinPart.OnKeys))
                    {
                        sb.AppendFormat(" ON KEYS {0}", joinPart.OnKeys);
                    }
                }
            }

            ApplyLetParts(sb);

            if (WhereParts.Any())
            {
                sb.AppendFormat(" WHERE {0}", String.Join(" AND ", WhereParts));
            }
            if ((GroupByParts != null) && GroupByParts.Any())
            {
                sb.AppendFormat(" GROUP BY {0}", string.Join(", ", GroupByParts));
            }
            if ((HavingParts != null) && HavingParts.Any())
            {
                sb.AppendFormat(" HAVING {0}", string.Join(" AND ", HavingParts));
            }
            if (OrderByParts.Any())
            {
                sb.AppendFormat(" ORDER BY {0}", String.Join(", ", OrderByParts));
            }
            if (LimitPart != null)
            {
                sb.Append(LimitPart);
            }
            if (LimitPart != null && OffsetPart != null)
            {
                sb.Append(OffsetPart);
            }

            if (QueryType == N1QlQueryType.Subquery)
            {
                if (!string.IsNullOrEmpty(PropertyExtractionPart))
                {
                    sb.Append(") END");
                }
                else
                {
                    sb.Append(')');
                }
            }
            else if (QueryType == N1QlQueryType.SubqueryAny)
            {
                sb.Append(") SATISFIES true END");
            }
            else if (QueryType == N1QlQueryType.SubqueryAll)
            {
                sb.AppendFormat(") SATISFIES {0} END", WhereAllPart);
            }

            return(sb.ToString());
        }
Beispiel #3
0
 public void AddFromPart(N1QlFromQueryPart fromPart)
 {
     FromParts.Add(fromPart);
 }
        /// <summary>
        /// Builds a subquery using the ANY or EVERY expression to test a nested array
        /// </summary>
        /// <returns>Query string</returns>
        private string BuildAnyAllQuery()
        {
            var sb = new StringBuilder();

            var mainFrom = FromParts.FirstOrDefault();

            if (mainFrom == null)
            {
                throw new InvalidOperationException("N1QL Any Subquery Missing From Part");
            }

            var extentName = mainFrom.ItemName;

            var source = mainFrom.Source;

            if (QueryType == N1QlQueryType.ArrayAll)
            {
                extentName = PropertyExtractionPart;

                if (WhereParts.Any())
                {
                    // WhereParts should be used to filter the source before the EVERY query
                    // This is done using the ARRAY operator with a WHEN clause

                    source = string.Format("(ARRAY {1} FOR {1} IN {0} WHEN {2} END)",
                                           source,
                                           mainFrom.ItemName,
                                           string.Join(" AND ", WhereParts));
                }
            }

            sb.AppendFormat("{0} {1} IN {2} ",
                            QueryType == N1QlQueryType.ArrayAny ? "ANY" : "EVERY",
                            extentName,
                            source);

            if (QueryType == N1QlQueryType.ArrayAny)
            {
                // WhereParts should be applied to the SATISFIES portion of the query

                if (WhereParts.Any())
                {
                    sb.AppendFormat("SATISFIES {0}", String.Join(" AND ", WhereParts));
                }
                else
                {
                    sb.Append("SATISFIES true");
                }
            }
            else // N1QlQueryType.All
            {
                // WhereAllPart is applied as the SATISFIES portion of the query

                sb.Append("SATISFIES ");
                sb.Append(WhereAllPart);
            }

            sb.Append(" END");

            return(sb.ToString());
        }
Beispiel #5
0
 public void AddFromPart(LuceneIndexExpression querySource)
 {
     FromParts.Add(querySource);
 }
Beispiel #6
0
        internal void DrainParts(double cycleAmount)
        {
            //Utilities.LogMessage("Entering:  TransferPump.DrainParts.", Utilities.LogType.Info, SMSettings.VerboseLogging);
            // Lets account for any empty/full containers
            // now split up the xfer amount evenly across the number of tanks that can send/receive resources
            if (cycleAmount < SMSettings.Tolerance)
            {
                return;
            }

            double cycleBalance = cycleAmount;

            while (cycleBalance > SMSettings.Tolerance)
            {
                // calc average of parts in list.
                double fromPartAvgAmt = cycleAmount / FromParts.Count;

                // reduce to the smallest container.
                double minAmt = fromPartAvgAmt;
                int    remainingPartsCount         = 0;
                List <Part> .Enumerator theseParts = FromParts.GetEnumerator();
                while (theseParts.MoveNext())
                {
                    if (theseParts.Current == null)
                    {
                        continue;
                    }
                    if (theseParts.Current.Resources[Resource].amount <= SMSettings.Tolerance)
                    {
                        continue;
                    }
                    if (theseParts.Current.Resources[Resource].amount >= minAmt)
                    {
                        remainingPartsCount++;
                        continue;
                    }
                    minAmt = theseParts.Current.Resources[Resource].amount;
                    remainingPartsCount++;
                }
                theseParts.Dispose();

                //Utilities.LogMessage(string.Format("Inside:  TransferPump.DrainParts:  fromPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", fromPartAmt, minAmt, fromPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
                // Decrement.
                if (remainingPartsCount > 0)
                {
                    List <Part> .Enumerator fromParts = FromParts.GetEnumerator();
                    while (fromParts.MoveNext())
                    {
                        if (fromParts.Current == null)
                        {
                            continue;
                        }
                        if (fromParts.Current.Resources[Resource].amount <= SMSettings.Tolerance)
                        {
                            continue;
                        }
                        Part part = fromParts.Current;
                        part.Resources[Resource].amount -= minAmt;
                        cycleBalance -= minAmt;
                        AmtPumped    += minAmt;

                        // Ensure part is empty and does not contain less than 0.
                        if (part.Resources[Resource].amount <= SMSettings.Tolerance)
                        {
                            part.Resources[Resource].amount = 0;
                        }
                    }
                    fromParts.Dispose();
                }
                if (remainingPartsCount == 0 && cycleBalance > SMSettings.Tolerance)
                {
                    cycleBalance = 0;
                }
                //Utilities.LogMessage(string.Format("Inside:  TransferPump.DrainParts:  fromPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", fromPartAmt, minAmt, fromPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
            }
        }
Beispiel #7
0
 public void AddFromPart(IQuerySource querySource) => FromParts.Add(string.Format("{0}", querySource.ItemName));
Beispiel #8
0
 public void AddFromPart(IQuerySource querySource)
 {
     FromParts.Add(querySource.ItemName);
 }
Beispiel #9
0
 public void AddFromPart(IQuerySource querySource)
 {
     FromParts.Add($"\"{querySource.ItemType.Name}\" \"{querySource.ItemName}\"");
 }
Beispiel #10
0
        internal void DrainParts(double cycleAmount)
        {
            //Utilities.LogMessage("Entering:  TransferPump.DrainParts.", Utilities.LogType.Info, SMSettings.VerboseLogging);
            // Lets account for any empty/full containers
            // now split up the xfer amount evenly across the number of tanks that can send/receive resources
            if (cycleAmount < SMSettings.Tolerance)
            {
                return;
            }

            double cycleBalance = cycleAmount;

            while (cycleBalance > SMSettings.Tolerance)
            {
                // find least but positive amount of resource in single tank!
                double minAmt = Double.MaxValue;
                List <Part> .Enumerator theseParts    = FromParts.GetEnumerator();
                List <Part>             nonEmptyParts = new List <Part>();
                while (theseParts.MoveNext())
                {
                    Part part = theseParts.Current;
                    if (part == null)
                    {
                        continue;
                    }
                    if (part.Resources[Resource].amount <= SMSettings.Tolerance)
                    {
                        continue;
                    }
                    if (part.Resources[Resource].amount < minAmt)
                    {
                        minAmt = part.Resources[Resource].amount;
                    }
                    nonEmptyParts.Add(theseParts.Current);
                }
                theseParts.Dispose();

                //Utilities.LogMessage(string.Format("Inside:  TransferPump.DrainParts:  fromPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", fromPartAmt, minAmt, fromPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
                // Decrement.
                if (nonEmptyParts.Count > 0)
                {
                    double toTransfer = Math.Min(cycleBalance / nonEmptyParts.Count, minAmt);
                    List <Part> .Enumerator fromParts = nonEmptyParts.GetEnumerator();
                    while (fromParts.MoveNext())
                    {
                        Part part = fromParts.Current;
                        part.Resources[Resource].amount -= toTransfer;
                        cycleBalance -= toTransfer;
                        AmtPumped    += toTransfer;

                        // Ensure part is empty and does not contain less than 0.
                        if (part.Resources[Resource].amount <= SMSettings.Tolerance)
                        {
                            part.Resources[Resource].amount = 0;
                        }
                    }
                    fromParts.Dispose();
                }
                if (nonEmptyParts.Count == 0 && cycleBalance > SMSettings.Tolerance)
                {
                    cycleBalance = 0;
                }
                //Utilities.LogMessage(string.Format("Inside:  TransferPump.DrainParts:  fromPartAmt = {0}, minAmt = {1}, PartsLeft = {2}, cycleBalance = {3}", fromPartAmt, minAmt, fromPartCount, cycleBalance), Utilities.LogType.Info, SMSettings.VerboseLogging);
            }
        }
Beispiel #11
0
 public void AddFromPart(IQuerySource querySource)
 {
     FromParts.Add(string.Format("{0} {1}", GetEntityName(querySource), querySource.ItemName));
 }
 public void AddFromPart(IQuerySource querySource)
 {
     FromParts.Add($"{GetEntityName(querySource)} {querySource.ItemName}");
 }