public static List<FirewallGroup> GetSelectedFirewallGroups(Dictionary<String, List<String>> FirewallGroups, IList SelectedFirewallGroups)
        {
            List<FirewallGroup> fwgrps = null;

            if (FirewallGroups != null)
            {
                fwgrps = new List<FirewallGroup>();
                FirewallGroup fwgrp = null;

                foreach (string grp in SelectedFirewallGroups)
                {
                    fwgrp = new FirewallGroup();
                    fwgrp.GroupName = grp.ToString();
                    fwgrp.URLs = new List<string>();

                    var selectedgroups = FirewallGroups.Where(selectedgrp => selectedgrp.Key == grp);

                    if (selectedgroups != null)
                    {
                        foreach (var selectedgroup in selectedgroups)
                        {
                            fwgrp.URLs.AddRange(selectedgroup.Value);
                            fwgrps.Add(fwgrp);
                        }
                    }
                }

            }

            return fwgrps;
        }
Example #2
0
        public IEnumerable<SubFingerprintData> ReadSubFingerprintDataByHashBucketsWithThreshold(
            long[] hashBins, int thresholdVotes)
        {
            int table = 0;
            var hashTables = storage.HashTables;
            var subFingeprintCount = new Dictionary<IModelReference, int>();
            foreach (var hashBin in hashBins)
            {
                if (hashTables[table].ContainsKey(hashBin))
                {
                    foreach (var subFingerprintId in hashTables[table][hashBin])
                    {
                        if (!subFingeprintCount.ContainsKey(subFingerprintId))
                        {
                            subFingeprintCount[subFingerprintId] = 0;
                        }

                        subFingeprintCount[subFingerprintId]++;
                    }
                }

                table++;
            }

            return subFingeprintCount.Where(pair => pair.Value >= thresholdVotes)
                                     .Select(pair => storage.SubFingerprints[pair.Key]);
        }
        private bool CanPermutePalindrome(string s)
        {
            if (string.IsNullOrEmpty(s)) return true;

            int length = s.Length;
            var dic = new Dictionary<char, int>();

            for (int index = 0; index < length; index++)
                if (dic.ContainsKey(s[index]))
                    dic[s[index]]++;
                else
                    dic.Add(s[index],1);


            return dic.Where(p => p.Value % 2 == 1).Count()<=1;
        }
Example #4
0
        private static string cleanCoordinates(string poly)
        {
            Dictionary<int, string> scondSplit = new Dictionary<int, string>();

            //CLEANING STRING FROM NON-COORDINATES CHARACTERS
            int pos = poly.LastIndexOf('(') + 1;
            string firstHalf = poly.Substring(0, pos);
            string secondHalf = poly.Substring(pos);
            try
            {
                //ADD UNIQUE ITEMS TO THE DICTIONARY
                int j = 0;
                int i, dPos;
                string coordinates;
                for (i = 0; i < secondHalf.Length; i++)
                {
                    coordinates = "";
                    dPos = secondHalf.IndexOf(", ", i);
                    if (dPos > -1)
                    {
                        coordinates = secondHalf.Substring(i, dPos - i);
                        i = dPos + 1;
                        if (scondSplit.Where(x => x.Value == coordinates).Count() == 0)
                            scondSplit.Add(j++, coordinates);
                    }
                    else
                    {
                        scondSplit.Add(j, secondHalf.Substring(i));
                        break;
                    }
                }

            }
            finally
            {
                if (scondSplit.Count > 0)
                {   //CONCATENATION OF POLY
                    poly = firstHalf;
                    foreach (var pair in scondSplit)
                    {
                        poly += pair.Value + ", ";
                    }
                    poly = poly.Substring(0, poly.Length - 2);
                }
            }
            return poly;
        }
Example #5
0
 /// <summary>
 /// Attempts to infer the type of cells present by obtaining
 /// the first 30 cells and counting the possible types.
 /// </summary>
 /// <returns>The cell type for column.</returns>
 /// <param name="column">The column to use.</param>
 public Type InferCellTypeForColumn(string column)
 {
     string startRow = "1";
                 string endRow = "30";
                 ICollection<ICell> cells = _dataSource.GetCells (column, startRow, endRow);
                 IDictionary<Type, int> typeCounts = new Dictionary<Type, int>();
                 foreach (ICell cell in cells) {
                         int oldValue = 0;
                         if (typeCounts.TryGetValue(cell.GetType(), out oldValue)) {
                             typeCounts.Add(cell.GetType(), oldValue + 1);
                         } else {
                                 typeCounts.Add (cell.GetType(), 0);
                         }
                 }
                 int max = typeCounts.Max (k => k.Value);
                 return typeCounts.Where(k => k.Value.Equals(max)).FirstOrDefault().Key;
 }
Example #6
0
 public bool Logit_Reg_Check()
 {
     Dictionary<string, string> depDic = new Dictionary<string, string>();
     if (ViewState["dict"] != null)
     { depDic = (Dictionary<string, string>)ViewState["dict"]; }
     if (depDic.Count > 0 && depDic.Values.Contains("i") && depDic.Values.Contains("d"))
     {
         foreach (KeyValuePair<string, string> kp in depDic.Where(D => D.Value.Contains("d")))
         {
             List<string> logits = (List<string>) ViewState["logits"];
             if (logits != null && logits.Count > 0 && logits.Contains(kp.Key))
             {
                 return true;
             }
         }
     }
     return false;
 }
        public static string EncodeUsing(this string inputString, Dictionary<string, string> huffmanCodesToSymbols)
        {
            var encodedMessage = new StringBuilder();

            foreach (char c in inputString)
            {
                var correspondingHuffmanCodes = huffmanCodesToSymbols.Where(kvp => kvp.Value == c.ToString());

                if (correspondingHuffmanCodes.Count() > 1)
                {
                    throw new ArgumentException(String.Format("The character '{0}' has more than one corresponding Huffman code.", c));
                }

                encodedMessage.Append(correspondingHuffmanCodes.First().Key);
            }

            return encodedMessage.ToString();
        }
 public InstanceContext(Dictionary<Type, SitecoreClassConfig> classes, IEnumerable<AbstractSitecoreDataHandler> datas)
 {
     //This needs reworking
     //this will be simplified to remove the need for three sets of data
     
     Classes = classes;
     ClassesById = new Dictionary<Guid, IList<SitecoreClassConfig>>();
     foreach (var record in classes.Where(x => x.Value.TemplateId != Guid.Empty))
     {
         if (!ClassesById.ContainsKey(record.Value.TemplateId))
         {
             ClassesById.Add(record.Value.TemplateId, new List<SitecoreClassConfig>());
         }
      
         ClassesById[record.Value.TemplateId].Add(record.Value);
     }
     
     Datas = datas;
 }
        public static Dictionary<int, string> GetTeams(FederationsEnum federationUrl)
        {
            bool isFemale = federationUrl == FederationsEnum.WFTDA;

            var db = new ManagementContext();

            // Display all Teams from the database 
            var getTeamsByFederation = from b in db.RTeams
                                       where b.IsFemale == isFemale && b.IsDeleted == false
                                       orderby b.Name
                                       select b;
            Dictionary<int, string> dicSessions = new Dictionary<int, string>();

            foreach (var item in getTeamsByFederation)
            {
                if (dicSessions.Where(x => x.Key == item.ID).FirstOrDefault().Key != item.ID)
                    dicSessions.Add(item.ID, item.Name);
            }
            return dicSessions;

        }
Example #10
0
        private void SerializeAttributes(Dictionary<string, CfgMetadata> meta, object obj, StringBuilder sb) {

            if (meta.Count > 0) {
                var pairs = meta.Where(kv => kv.Value.ListType == null).ToArray();
                for (var i = 0; i < pairs.Length; i++) {
                    var pair = pairs[i];
                    if (!pair.Value.Attribute.serialize) {
                        continue;
                    }
                    var value = pair.Value.Getter(obj);
                    if (value == null || value.Equals(pair.Value.Attribute.value) || (!pair.Value.Attribute.ValueIsSet && pair.Value.Default != null && pair.Value.Default.Equals(value))) {
                        continue;
                    }

                    var type = pair.Value.PropertyInfo.PropertyType;
                    var stringValue = ValueToString(type, value);

                    sb.Append(" \"");
                    sb.Append(pair.Key);
                    sb.Append("\":");
                    sb.Append(stringValue);
                    sb.Append(",");
                }

            } else if (obj is Dictionary<string, string>) {
                var dict = (Dictionary<string, string>)obj;
                foreach (var pair in dict) {
                    sb.Append(" \"");
                    sb.Append(pair.Key);
                    sb.Append("\":\"");
                    sb.Append(Encode(pair.Value));
                    sb.Append("\"");
                    sb.Append(",");
                }
            }

        }
 private ArrayList TypeForCategoryJsonArray(string Category)
 {            
     ArrayList typeList = new ArrayList();
     dictionaryItem = TestTypes.TypeWithSecureFlag(Category);
     isSecuredFlag = dictionaryItem != null && dictionaryItem.Where(x => Boolean.Parse(x.Value.ToString())).Select(y => y.Key).ToList().Distinct().Any();
     if (dictionaryItem != null && dictionaryItem.Select(c => c.Key).Any())
     {
         if (isSecuredFlag && UserHasPermission(Permission.Access_SecureTesting))
         {
             typeList.Add(new object[] { dictionaryItem.Select(c => c.Key).ToArray() });
         }
         else
         {
             typeList.Add(new object[] { dictionaryItem.Where(c => !c.Value).Select(c => c.Key).ToArray() });
         }
         
     }
     return typeList;
 }
 /// <summary>
 /// If a table alias in the MATCH clause is defined in an upper-level context,
 /// to be able to translate this MATCH clause, this table alias must be re-materialized
 /// in the FROM clause of the current context and joined with the corresponding table
 /// in the upper-level context.
 /// </summary>
 /// <param name="query">Select query</param>
 /// <param name="nodes">A collection of node alias and match node instance</param>
 private void RematerilizeExtrenalNodeTableReference(WSelectQueryBlock query, Dictionary<string, MatchNode> nodes)
 {
     var tableRefs = query.FromClause.TableReferences;
     var tableSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
     var newTableRefs = new List<WTableReference>();
     for (int index = 0; index < tableRefs.Count; ++index)
     {
         var table = tableRefs[index] as WNamedTableReference;
         if (table == null)
         {
             newTableRefs.Add(tableRefs[index]);
             continue;
         }
         if (!nodes.ContainsKey(table.ExposedName.Value))
         {
             newTableRefs.Add(table);
         }
         else
         {
             tableSet.Add(table.ExposedName.Value);
         }
     }
     query.FromClause = new WFromClause
     {
         TableReferences = newTableRefs,
     };
     WBooleanExpression whereCondiction = null;
     foreach (var node in nodes.Where(node => !tableSet.Contains(node.Key)))
     {
         node.Value.External = true;
         var newWhereCondition = new WBooleanComparisonExpression
         {
             ComparisonType = BooleanComparisonType.Equals,
             FirstExpr = new WColumnReferenceExpression
             {
                 MultiPartIdentifier = new WMultiPartIdentifier(
                 new Identifier { Value = node.Key },
                 new Identifier { Value = "GlobalNodeId" })
             },
             SecondExpr = new WColumnReferenceExpression
             {
                 MultiPartIdentifier = new WMultiPartIdentifier(
                 new Identifier { Value = node.Value.RefAlias },
                 new Identifier { Value = "GlobalNodeId" })
             },
         };
         whereCondiction = WBooleanBinaryExpression.Conjunction(whereCondiction, newWhereCondition);
     }
     if (whereCondiction != null)
     {
         if (query.WhereClause == null)
         {
             query.WhereClause = new WWhereClause { SearchCondition = whereCondiction };
         }
         else
         {
             if (query.WhereClause.SearchCondition == null)
             {
                 query.WhereClause.SearchCondition = whereCondiction;
             }
             else
             {
                 query.WhereClause.SearchCondition = new WBooleanBinaryExpression
                 {
                     BooleanExpressionType = BooleanBinaryExpressionType.And,
                     FirstExpr = new WBooleanParenthesisExpression
                     {
                         Expression = query.WhereClause.SearchCondition
                     },
                     SecondExpr = new WBooleanParenthesisExpression
                     {
                         Expression = whereCondiction
                     }
                 };
             }
         }
     }
 }
Example #13
0
 static Dictionary<string, string> GetProperties(Dictionary<string, object> input)
 {
     string[] ignoreKeys = new []{ "targets", "actions", "thresholds" };
     return input.Where(item => !ignoreKeys.Contains(item.Key))
         .ToDictionary(item => item.Key, item => item.Value as string);
 }
Example #14
0
        private static short[] GetMostCommonGotos(IEnumerable<GotoTableValue> gotos, int maxToken)
        {
            var defaultGotos = new short[maxToken];
            var gotoCounts = new Dictionary<Tuple<int, int>, int>();
            foreach (var g in gotos)
            {
                var t = new Tuple<int, int>(g.Token, g.NewState);

                if (!gotoCounts.ContainsKey(t))
                    gotoCounts.Add(t, 0);
                gotoCounts[t] = gotoCounts[t] + 1;
            }

            // For every token in the grammar, store the most stored count as the default goto
            for (int t = 0; t < maxToken; ++t)
            {
                var mostCommonNewState = gotoCounts.Where(f => f.Key.Item1 == t).OrderBy(f => -f.Value).Select(f => f.Key.Item2);
                defaultGotos[t] = (short) mostCommonNewState.First();
            }
            return defaultGotos;
        }
 /// <summary>
 /// This will find the string name of the date facet based off the JSON parse query from the handler
 /// </summary>
 /// <returns>The name of the facet</returns>
 /// <value>Within a Year</value>
 /// <param name="dateTime">The JSON date</param>
 /// <param name="listOfDates">The available facets</param>
 private static string DecodeDateSearch(DateTime dateTime, Dictionary<DateTime, string> listOfDates)
 {
     return listOfDates.Where(date => date.Key.ToString() == dateTime.ToString()).First().Value;
 }
Example #16
0
        static List<MergeAction> FindDuplicateStories(Dictionary<long, SimpleStory> stories)
        {
            List<MergeAction> mergeActions = new List<MergeAction>();

            foreach (SimpleStory story in stories.OrderBy(n => n.Key).Select(n => n.Value))
            {
                var newerStories = stories
                    .Where(n => n.Key > story.StoryID).OrderBy(n => n.Key).Select(n => n.Value);

                if (!newerStories.Any())
                    break;

                //Get the distribution of similarity scores between this cluster and each of the most active stories
                var similarities = newerStories
                    .Select(n => new { StoryID = n.StoryID, Similarity = n.WordVector * story.WordVector })
                    .OrderByDescending(n => n.Similarity)
                    .ToList();

                //Check if there is a rapid drop somewhere in the similarity distribution
                bool distrHasRapidDrop = false;
                if (similarities.Count > 1)
                {
                    for (int i = 1; i < similarities.Count; i++)
                    {
                        if (similarities[i].Similarity > 0.01 
                            && similarities[i].Similarity < Settings.TweetClusterer_SW_MergeDropScale * similarities[i - 1].Similarity)
                        {
                            distrHasRapidDrop = true;
                            break;
                        }
                    }
                }

                //Merge the stories if similar
                if ((similarities[0].Similarity > Settings.TweetClusterer_SW_MergeThreshold
                    || similarities[0].Similarity > Settings.TweetClusterer_SW_MergeThresholdWithDrop && distrHasRapidDrop))
                {
                    long targetStoryID = story.StoryID;
                    var thisStoryMerges = mergeActions.Where(n => n.MergedStoryID == story.StoryID);
                    if (thisStoryMerges.Any())
                        targetStoryID = thisStoryMerges.Min(n => n.KeptStoryID);
                    mergeActions.Add(new MergeAction() { KeptStoryID = targetStoryID, MergedStoryID = similarities[0].StoryID });
                }
            }

            return mergeActions;
        }
Example #17
0
        async Task<IHttpResponseAction> dataMethod(SHA1Hashed<ServicesOffering> main, Method method, System.Collections.Specialized.NameValueCollection queryString)
        {
            // Check for descriptor errors:
            if (method.Errors.Count > 0)
            {
                return new JsonRootResponse(
                    statusCode: 500,
                    statusDescription: "Bad method descriptor",
                    message: "Bad method descriptor",
                    meta: new
                    {
                        configHash = main.HashHexString,
                        serviceName = method.Service.Name,
                        methodName = method.Name,
                        errors = method.Errors.ToArray()
                    }
                );
            }

            // Check required parameters:
            if (method.Parameters != null)
            {
                // Create a hash set of the query-string parameter names:
                var q = new HashSet<string>(queryString.AllKeys, StringComparer.OrdinalIgnoreCase);

                // Create a list of missing required parameter names:
                var missingParams = new List<string>(method.Parameters.Count(p => !p.Value.IsOptional));
                missingParams.AddRange(
                    from p in method.Parameters
                    where !p.Value.IsOptional && !q.Contains(p.Key)
                    select p.Key
                );

                if (missingParams.Count > 0)
                    return new JsonRootResponse(
                        statusCode: 400,
                        statusDescription: "Missing required parameters",
                        message: "Missing required parameters",
                        meta: new
                        {
                            configHash = main.HashHexString,
                            serviceName = method.Service.Name,
                            methodName = method.Name
                        },
                        errors: new[]
                        {
                            new
                            {
                                missingParams = missingParams.ToDictionary(
                                    p => p,
                                    p => new ParameterSerialized(method.Parameters[p]),
                                    StringComparer.OrdinalIgnoreCase
                                )
                            }
                        }
                    );

                missingParams = null;
            }

            // Open a connection and execute the command:
            using (var conn = new SqlConnection(method.ConnectionString))
            using (var cmd = conn.CreateCommand())
            {
                var parameterValues = new Dictionary<string, ParameterValue>(method.Parameters == null ? 0 : method.Parameters.Count);

                // Add parameters:
                if (method.Parameters != null)
                {
                    foreach (var param in method.Parameters)
                    {
                        bool isValid = true;
                        string message = null;
                        object sqlValue, clrValue;
                        var paramType = (param.Value.SqlType ?? param.Value.Type);
                        string rawValue = queryString[param.Key];

                        if (param.Value.IsOptional & (rawValue == null))
                        {
                            // Use the default value if the parameter is optional and is not specified on the query-string:
                            sqlValue = param.Value.DefaultSQLValue;
                            clrValue = param.Value.DefaultCLRValue;
                        }
                        else
                        {
                            try
                            {
                                sqlValue = getSqlValue(paramType.SqlDbType, rawValue);
                                if (sqlValue == null)
                                {
                                    isValid = false;
                                    message = "Unsupported SQL type '{0}'".F(paramType.SqlDbType);
                                }
                            }
                            catch (Exception ex)
                            {
                                isValid = false;
                                sqlValue = DBNull.Value;
                                message = ex.Message;
                            }

                            try
                            {
                                clrValue = getCLRValue(paramType.SqlDbType, rawValue);
                            }
                            catch { clrValue = null; }
                        }

                        parameterValues.Add(param.Key, isValid ? new ParameterValue(clrValue) : new ParameterValue(message, rawValue));

                        // Add the SQL parameter:
                        var sqlprm = cmd.Parameters.Add(param.Value.Name, paramType.SqlDbType);
                        sqlprm.IsNullable = param.Value.IsOptional;
                        if (paramType.Length != null) sqlprm.Precision = (byte)paramType.Length.Value;
                        if (paramType.Scale != null) sqlprm.Scale = (byte)paramType.Scale.Value;
                        sqlprm.SqlValue = sqlValue;
                    }
                }

                // Abort if we have invalid parameters:
                var invalidParameters = parameterValues.Where(p => !p.Value.isValid);
                if (invalidParameters.Any())
                {
                    return new JsonRootResponse(
                        statusCode: 400,
                        statusDescription: "Invalid parameter value(s)",
                        message: "Invalid parameter value(s)",
                        meta: new
                        {
                            configHash = main.HashHexString,
                            serviceName = method.Service.Name,
                            methodName = method.Name
                        },
                        errors: invalidParameters.Select(p => (object)new { name = p.Key, attemptedValue = p.Value.attemptedValue, message = p.Value.message }).ToArray()
                    );
                }

                //cmd.CommandTimeout = 360;   // seconds
                cmd.CommandType = CommandType.Text;
                // Set TRANSACTION ISOLATION LEVEL and optionally ROWCOUNT before the query:
                const string setIsoLevel = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;\r\n";
                var sbCmd = new StringBuilder(setIsoLevel, setIsoLevel.Length + method.Query.SQL.Length);
                //if (rowLimit > 0)
                //    sbCmd.Append("SET ROWCOUNT {0};\r\n".F(rowLimit));
                sbCmd.Append(method.Query.SQL);
                cmd.CommandText = sbCmd.ToString();

                // Stopwatches used for precise timing:
                Stopwatch swOpenTime, swExecTime, swReadTime;

                swOpenTime = Stopwatch.StartNew();
                try
                {
                    // Open the connection asynchronously:
                    await conn.OpenAsync();
                    swOpenTime.Stop();
                }
                catch (Exception ex)
                {
                    swOpenTime.Stop();
                    return getErrorResponse(ex);
                }

                // Execute the query:
                SqlDataReader dr;
                swExecTime = Stopwatch.StartNew();
                try
                {
                    // Execute the query asynchronously:
                    dr = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection);
                    swExecTime.Stop();
                }
                catch (ArgumentException aex)
                {
                    swExecTime.Stop();
                    // SQL Parameter validation only gives `null` for `aex.ParamName`.
                    return new JsonRootResponse(400, aex.Message);
                }
                catch (Exception ex)
                {
                    swExecTime.Stop();
                    return getErrorResponse(ex);
                }

                swReadTime = Stopwatch.StartNew();
                try
                {
                    var results = await ReadResult(method, dr, RowMapperUseMapping);
                    swReadTime.Stop();

                    var meta = new MetadataSerialized
                    {
                        configHash = main.HashHexString,
                        serviceName = method.Service.Name,
                        methodName = method.Name,
                        deprecated = method.DeprecatedMessage,
                        parameters = parameterValues,
                        // Timings are in msec:
                        timings = new MetadataTimingsSerialized
                        {
                            open = Math.Round(swOpenTime.ElapsedTicks * 1000m / (decimal)Stopwatch.Frequency, 2),
                            exec = Math.Round(swExecTime.ElapsedTicks * 1000m / (decimal)Stopwatch.Frequency, 2),
                            read = Math.Round(swReadTime.ElapsedTicks * 1000m / (decimal)Stopwatch.Frequency, 2),
                            total = Math.Round((swOpenTime.ElapsedTicks + swExecTime.ElapsedTicks + swReadTime.ElapsedTicks) * 1000m / (decimal)Stopwatch.Frequency, 2),
                        }
                    };

                    return new JsonRootResponse(
                        links: new RestfulLink[]
                        {
                        },
                        meta: meta,
                        results: results
                    );
                }
                catch (JsonResultException jex)
                {
                    swReadTime.Stop();
                    return new JsonRootResponse(statusCode: jex.StatusCode, message: jex.Message);
                }
                catch (Exception ex)
                {
                    swReadTime.Stop();
                    return getErrorResponse(ex);
                }
            }
        }
Example #18
0
        /// <summary>
        /// Transit from current component to the new component in the next state given the Node Unit
        /// </summary>
        /// <param name="candidateTree"></param>
        /// <param name="densityDict"></param>
        /// <param name="subGraph"></param>
        /// <param name="statisticsCalculator"></param>
        /// <returns></returns>
        public MatchComponent GetNextState(
            OneHeightTree candidateTree, 
            Dictionary<string, double> densityDict, 
            IMatchJoinStatisticsCalculator statisticsCalculator)
        {
            var newComponent = new MatchComponent(this);
            var root = candidateTree.TreeRoot;

            WBooleanExpression joinCondition = null;
            string nodeName = "";


            // Update Nodes
            if (newComponent.MaterializedNodeSplitCount.ContainsKey(root))
            {
                newComponent.MaterializedNodeSplitCount[root]++;
                nodeName = newComponent.GetNodeRefName(root);
                joinCondition = new WBooleanComparisonExpression
                {
                    FirstExpr = new WColumnReferenceExpression
                    {
                        ColumnType = ColumnType.Regular,
                        MultiPartIdentifier = new WMultiPartIdentifier(
                            new Identifier {Value = root.RefAlias},
                            new Identifier {Value = "GlobalNodeId"}
                            ),
                    },
                    SecondExpr = new WColumnReferenceExpression
                    {
                        ColumnType = ColumnType.Regular,
                        MultiPartIdentifier = new WMultiPartIdentifier(
                            new Identifier {Value = nodeName},
                            new Identifier {Value = "GlobalNodeId"}
                            ),
                    },
                    ComparisonType = BooleanComparisonType.Equals
                };
            }
            else
            {
                nodeName = root.RefAlias;
                newComponent.Nodes.Add(root);
                newComponent.MaterializedNodeSplitCount[root] = 0;
                newComponent.StatisticsDict[root] = new ColumnStatistics {Selectivity = 1.0/root.TableRowCount};

            }

            // Constructs table reference
            WTableReference nodeTable = new WNamedTableReference
            {
                Alias = new Identifier { Value = nodeName },
                TableObjectName = root.TableObjectName
            };
            WTableReference compTable = newComponent.TableRef;

            // Updates join conditions
            double selectivity = 1.0;
            double degrees = 1.0;
            var DensityCount = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);

            List<MatchEdge> inEdges;
            if (newComponent.UnmaterializedNodeMapping.TryGetValue(root, out inEdges))
            {
                var firstEdge = inEdges.First();
                bool materialized = newComponent.EdgeMaterilizedDict[firstEdge];
                newComponent.UnmaterializedNodeMapping.Remove(root);
                selectivity *= 1.0/root.TableRowCount;

                // Component materialized edge to root
                if (materialized)
                {
                    joinCondition = WBooleanBinaryExpression.Conjunction(joinCondition, new WBooleanComparisonExpression
                    {
                        FirstExpr = new WColumnReferenceExpression
                        {
                            ColumnType = ColumnType.Regular,
                            MultiPartIdentifier = new WMultiPartIdentifier(
                                new Identifier {Value = firstEdge.EdgeAlias},
                                new Identifier {Value = "Sink"}
                                ),
                        },
                        SecondExpr = new WColumnReferenceExpression
                        {
                            ColumnType = ColumnType.Regular,
                            MultiPartIdentifier = new WMultiPartIdentifier(
                                new Identifier {Value = nodeName},
                                new Identifier {Value = "GlobalNodeId"}
                                )
                        },
                        ComparisonType = BooleanComparisonType.Equals
                    });

                    //var statistics = ColumnStatistics.UpdateHistogram(newComponent.StatisticsDict[root],
                    //    new ColumnStatistics {Selectivity = 1.0/root.TableRowCount});
                    //selectivity *= statistics.Selectivity;
                    //newComponent.StatisticsDict[root] = statistics;

                    if (DensityCount.ContainsKey(root.TableObjectName.ToString()))
                        DensityCount[root.TableObjectName.ToString()]++;
                    else
                        DensityCount[root.TableObjectName.ToString()] = 1;
                }
                // Component unmaterialized edge to root                
                else
                {
                    ColumnStatistics statistics = null;
                    foreach (var edge in inEdges)
                    {
                        // Update component table
                        compTable = SpanTableRef(compTable, edge, newComponent.GetNodeRefName(edge.SourceNode));

                        newComponent.EdgeMaterilizedDict[edge] = true;
                        joinCondition = WBooleanBinaryExpression.Conjunction(joinCondition,
                            new WBooleanComparisonExpression
                            {
                                FirstExpr = new WColumnReferenceExpression
                                {
                                    ColumnType = ColumnType.Regular,
                                    MultiPartIdentifier = new WMultiPartIdentifier(
                                        new Identifier {Value = edge.EdgeAlias},
                                        new Identifier {Value = "Sink"}
                                        ),
                                },
                                SecondExpr = new WColumnReferenceExpression
                                {
                                    ColumnType = ColumnType.Regular,
                                    MultiPartIdentifier = new WMultiPartIdentifier(
                                        new Identifier {Value = nodeName},
                                        new Identifier {Value = "GlobalNodeId"}
                                        )
                                },
                                ComparisonType = BooleanComparisonType.Equals
                            });
                        statistics = ColumnStatistics.UpdateHistogram(statistics,
                            newComponent.Context.GetEdgeStatistics(edge));
                        selectivity *= statistics.Selectivity;

                        
                    }
                    newComponent.StatisticsDict[root] = statistics;

                    if (DensityCount.ContainsKey(root.TableObjectName.ToString()))
                        DensityCount[root.TableObjectName.ToString()]+=inEdges.Count;
                    else
                        DensityCount[root.TableObjectName.ToString()] = inEdges.Count;
                }
            }

            var jointEdges = candidateTree.MaterializedEdges;
            int sinkToSinkCount = 0;
            foreach (var jointEdge in jointEdges)
            {
                // Update node table
                nodeTable = SpanTableRef(nodeTable, jointEdge, nodeName);
                degrees *= jointEdge.AverageDegree;

                newComponent.EdgeMaterilizedDict[jointEdge] = true;
                var sinkNode = jointEdge.SinkNode;
                // Leaf to component materialized node
                if (newComponent.MaterializedNodeSplitCount.ContainsKey(sinkNode))
                {
                    joinCondition = WBooleanBinaryExpression.Conjunction(joinCondition,
                        new WBooleanComparisonExpression
                        {
                            FirstExpr = new WColumnReferenceExpression
                            {
                                ColumnType = ColumnType.Regular,
                                MultiPartIdentifier = new WMultiPartIdentifier(
                                    new Identifier {Value = jointEdge.EdgeAlias},
                                    new Identifier {Value = "Sink"}
                                    ),
                            },
                            SecondExpr = new WColumnReferenceExpression
                            {
                                ColumnType = ColumnType.Regular,
                                MultiPartIdentifier = new WMultiPartIdentifier(
                                    new Identifier {Value = sinkNode.RefAlias},
                                    new Identifier {Value = "GlobalNodeId"}
                                    )
                            },
                            ComparisonType = BooleanComparisonType.Equals
                        });
                    var statistics = ColumnStatistics.UpdateHistogram(newComponent.StatisticsDict[sinkNode],
                        newComponent.Context.GetEdgeStatistics(jointEdge));
                    selectivity *= statistics.Selectivity;
                    newComponent.StatisticsDict[sinkNode] = statistics;

                    if (DensityCount.ContainsKey(sinkNode.TableObjectName.ToString()))
                        DensityCount[sinkNode.TableObjectName.ToString()]++;
                    else
                        DensityCount[sinkNode.TableObjectName.ToString()] = 1;
                }
                // Leaf to component unmaterialized node
                else
                {
                    inEdges = newComponent.UnmaterializedNodeMapping[sinkNode];
                    var firstEdge = inEdges.First();
                    bool materlizedEdge = newComponent.EdgeMaterilizedDict[firstEdge];
                   
                    // Leaf to materialized leaf
                    if (materlizedEdge)
                    {
                        joinCondition = WBooleanBinaryExpression.Conjunction(joinCondition,
                            new WBooleanComparisonExpression
                            {
                                FirstExpr = new WColumnReferenceExpression
                                {
                                    ColumnType = ColumnType.Regular,
                                    MultiPartIdentifier = new WMultiPartIdentifier(
                                        new Identifier {Value = jointEdge.EdgeAlias},
                                        new Identifier {Value = "Sink"}
                                        ),
                                },
                                SecondExpr = new WColumnReferenceExpression
                                {
                                    ColumnType = ColumnType.Regular,
                                    MultiPartIdentifier = new WMultiPartIdentifier(
                                        new Identifier { Value = firstEdge.EdgeAlias},
                                        new Identifier {Value = "Sink"}
                                        )
                                },
                                ComparisonType = BooleanComparisonType.Equals
                            });

                        sinkToSinkCount++;
                        var statistics = ColumnStatistics.UpdateHistogram(newComponent.StatisticsDict[sinkNode],
                            newComponent.Context.GetEdgeStatistics(jointEdge));
                        selectivity *= statistics.Selectivity;
                        newComponent.StatisticsDict[sinkNode] = statistics;
                    }
                    // Leaf to unmaterialized leaf
                    else
                    {
                        ColumnStatistics compSinkNodeStatistics = null;
                        foreach (var inEdge in inEdges)
                        {
                            compTable = SpanTableRef(compTable, inEdge, newComponent.GetNodeRefName(inEdge.SourceNode));
                            newComponent.EdgeMaterilizedDict[inEdge] = true;
                            joinCondition = WBooleanBinaryExpression.Conjunction(joinCondition,
                            new WBooleanComparisonExpression
                            {
                                FirstExpr = new WColumnReferenceExpression
                                {
                                    ColumnType = ColumnType.Regular,
                                    MultiPartIdentifier = new WMultiPartIdentifier(
                                        new Identifier { Value = jointEdge.EdgeAlias },
                                        new Identifier { Value = "Sink" }
                                        ),
                                },
                                SecondExpr = new WColumnReferenceExpression
                                {
                                    ColumnType = ColumnType.Regular,
                                    MultiPartIdentifier = new WMultiPartIdentifier(
                                        new Identifier { Value = inEdge.EdgeAlias },
                                        new Identifier { Value = "Sink" }
                                        )
                                },
                                ComparisonType = BooleanComparisonType.Equals
                            });

                            sinkToSinkCount++;
                            var leafToLeafStatistics = statisticsCalculator.GetLeafToLeafStatistics(jointEdge, inEdge);
                            selectivity *= leafToLeafStatistics.Selectivity;
                            compSinkNodeStatistics =
                                ColumnStatistics.UpdateHistogram(compSinkNodeStatistics,
                                    newComponent.Context.GetEdgeStatistics(inEdge));
                        }
                        newComponent.StatisticsDict[sinkNode] = compSinkNodeStatistics;
                    }
                }
            }

            var unmatEdges = candidateTree.UnmaterializedEdges;
            foreach (var unmatEdge in unmatEdges)
            {
                newComponent.EdgeMaterilizedDict[unmatEdge] = false;
                newComponent.Nodes.Add(unmatEdge.SinkNode);
                var sinkNodeInEdges = newComponent.UnmaterializedNodeMapping.GetOrCreate(unmatEdge.SinkNode);
                sinkNodeInEdges.Add(unmatEdge);
                degrees *= unmatEdge.AverageDegree;

            }

            // Calculate Estimated Join Selectivity & Estimated Node Size
            double estimatedSelectity = 1.0;
            int count = 0;
            bool sinkJoin = false;
            foreach (var item in densityDict.Where(e => DensityCount.ContainsKey(e.Key)))
            {
                var density = item.Value;
                var curJoinCount = DensityCount[item.Key];
                var curJoinSelectitivy = Math.Pow(density, 2 - Math.Pow(2, 1 - curJoinCount));
                if (!sinkJoin && ColumnStatistics.DefaultDensity < density)
                {
                    var curSinkJoinSelectivity = Math.Pow(ColumnStatistics.DefaultDensity,
                        2 - Math.Pow(2, 1 - sinkToSinkCount));
                    estimatedSelectity *= Math.Pow(curSinkJoinSelectivity, Math.Pow(2, -count));
                    count += sinkToSinkCount;
                    sinkJoin = true;
                }
                estimatedSelectity *= Math.Pow(curJoinSelectitivy, Math.Pow(2, -count));
                count += curJoinCount;
            }

            var estimatedNodeUnitSize = root.EstimatedRows*
                                        Math.Pow(1000, candidateTree.MaterializedEdges.Count + candidateTree.UnmaterializedEdges.Count);


            // Update Table Reference
            newComponent.TableRef = GetPlanAndUpdateCost(candidateTree, newComponent, nodeTable, compTable, joinCondition,
                degrees, selectivity, estimatedNodeUnitSize, estimatedSelectity);

            return newComponent;
        }
Example #19
0
        /// <summary>
        /// Device matching
        /// 
        /// Plan of attack :
        /// 1) Look for opera headers first - as they're definitive
        /// 2) Try profile match - only devices which have unique profiles will match.
        /// 3) Try user-agent match
        /// 4) Try other x-headers
        /// 5) Try all remaining headers
        /// </summary>
        /// <param name="headers"></param>
        /// <returns>array The matched device or null if not found</returns>
        private Dictionary<string, dynamic> MatchDevice(Dictionary<string, dynamic> headers)
        {
            // Opera mini sometimes puts the vendor # model in the header - nice! ... sometimes it puts ? # ? in as well
            if (headers.ContainsKey("x-operamini-phone") && headers["x-operamini-phone"].ToString() != "? # ?")
            {
                dynamic id = this.GetMatch("x-operamini-phone", headers["x-operamini-phone"], _detectionv4Standard, "x-operamini-phone", "device");
                if (!(id is bool))
                {
                    return this.FindById(id);
                }
                headers.Remove("x-operamini-phone");
            }

            // Profile header matching
            if (headers.ContainsKey("profile"))
            {
                dynamic id = this.GetMatch("profile", headers["profile"], _detectionv4Standard, "profile", "device");
                if (!(id is bool))
                {
                    return this.FindById(id);
                }
                headers.Remove("profile");
            }

            // Profile header matching
            if (headers.ContainsKey("x-wap-profile"))
            {
                dynamic id = this.GetMatch("x-wap-profile", headers["x-wap-profile"], _detectionv4Standard, "x-wap-profile", "device");
                if (!(id is bool))
                {
                    return this.FindById(id);
                }
                headers.Remove("x-wap-profile");
            }

            List<string> order = DetectionConfig["device-ua-order"];
            foreach (KeyValuePair<string, dynamic> item in headers.Where(item => !order.Contains(item.Key) && Regex.IsMatch(item.Key, "^x-")))
            {
                order.Add(item.Key);
            }

            foreach (dynamic id in from item in order where headers.ContainsKey(item) select this.GetMatch("user-agent", headers[item], _detectionv4Standard, item, "device") into id where !(id is bool) select id)
            {
                return this.FindById(id);
            }

            bool hasGetData = false;
            dynamic itemid = "";
            // Generic matching - Match of last resort
            if (headers.ContainsKey("x-operamini-phone-ua"))
            {
                itemid = this.GetMatch("x-operamini-phone-ua", headers["x-operamini-phone-ua"], _detectionv4Generic, "x-operamini-phone-ua", "device");
            }
            if (!hasGetData && headers.ContainsKey("agent"))
            {
                itemid = this.GetMatch("agent", headers["agent"], _detectionv4Generic, "agent", "device");
            }
            if (!hasGetData && headers.ContainsKey("user-agent"))
            {
                itemid = this.GetMatch("user-agent", headers["user-agent"], _detectionv4Generic, "user-agent", "device");
                if (itemid is string)
                    hasGetData = true;
            }

            if (hasGetData)
            {
                return this.FindById(itemid);
            }

            return null;
        }
Example #20
0
        private void UpdateWindowButtonPosition()
        {
            if (!ControlBox) return;

            var priorityOrder = new Dictionary<int, WindowButtons>(3) { { 0, WindowButtons.Close }, { 1, WindowButtons.Maximize }, { 2, WindowButtons.Minimize } };

            var buttonsWidth = 0;

            foreach (var button in priorityOrder.Where(button => _windowButtonList.ContainsKey(button.Value))) {
                buttonsWidth += _windowButtonList[button.Value].Width;
                _windowButtonList[button.Value].Location = new Point(ClientRectangle.Width - BorderWidth - buttonsWidth, BorderWidth);
            }

            if (_windowButtonList.ContainsKey(WindowButtons.CloseAllVisible)) {
                _windowButtonList[WindowButtons.CloseAllVisible].Location = new Point(ClientRectangle.Width - BorderWidth - 25, BorderWidth + 25);
            }

            Refresh();
        }
Example #21
0
		internal static object GetPrefetchingQueueStatusForDebug(DocumentDatabase database)
		{
			var result = new List<object>();

			foreach (var prefetchingBehavior in database.IndexingExecuter.PrefetchingBehaviors)
			{
				var prefetcherDocs = prefetchingBehavior.DebugGetDocumentsInPrefetchingQueue().ToArray();
				var futureBatches = prefetchingBehavior.DebugGetDocumentsInFutureBatches();

				var compareToCollection = new Dictionary<Etag, int>();

				for (int i = 1; i < prefetcherDocs.Length; i++)
					compareToCollection.Add(prefetcherDocs[i - 1].Etag, prefetcherDocs[i].Etag.CompareTo(prefetcherDocs[i - 1].Etag));

				if (compareToCollection.Any(x => x.Value < 0))
				{
				    result.Add(new
				    {
						AdditionaInfo = prefetchingBehavior.AdditionalInfo,
                        HasCorrectlyOrderedEtags = false,
                        IncorrectlyOrderedEtags = compareToCollection.Where(x => x.Value < 0),
                        EtagsWithKeys = prefetcherDocs.ToDictionary(x => x.Etag, x => x.Key),
						FutureBatches = futureBatches
				    });
				}
				else
				{
                    var prefetcherDocsToTake = Math.Min(5, prefetcherDocs.Count());
                    var etagsWithKeysTail = Enumerable.Range(0, prefetcherDocsToTake).Select(
                        i => prefetcherDocs[prefetcherDocs.Count() - prefetcherDocsToTake + i]).ToDictionary(x => x.Etag, x => x.Key);

                    result.Add(new
                    {
                        AdditionaInfo = prefetchingBehavior.AdditionalInfo,
                        HasCorrectlyOrderedEtags = true,
                        EtagsWithKeysHead = prefetcherDocs.Take(5).ToDictionary(x => x.Etag, x => x.Key),
                        EtagsWithKeysTail = etagsWithKeysTail,
                        EtagsWithKeysCount = prefetcherDocs.Count(),
						FutureBatches = futureBatches
                    });
				}
			}

			return result;
		}
Example #22
0
        /// <summary>
        /// Sorts a list of vertex type predefinitions topologically regarding their parentPredef type name.
        /// </summary>
        /// <param name="myDefsByVertexName"></param>
        /// <param name="myDefsByParentVertexName"></param>
        /// <returns> if the vertex type predefinition can be sorted topologically regarding their parentPredef type, otherwise false.</returns>
        protected static LinkedList<VertexTypePredefinition> CanAddSortTopolocically(
            Dictionary<String, VertexTypePredefinition> myDefsByVertexName,
            Dictionary<String, IEnumerable<VertexTypePredefinition>> myDefsByParentVertexName)
        {
            //The list of topolocically sorted vertex types
            //In this step, we assume that parent types, that are not in the list of predefinitons are correct.
            //Correct means: either they are in fs or they are not in fs but then they are not defined. (this will be detected later)
            var correctRoots = myDefsByParentVertexName
                .Where(parent => !myDefsByVertexName.ContainsKey(parent.Key))
                .SelectMany(x => x.Value);
            var result = new LinkedList<VertexTypePredefinition>(correctRoots);

            //Here we step throught the list of topolocically sorted predefinitions.
            //Each predefinition that is in this list, is a valid parent type for other predefinitions.
            //Thus we can add all predefinitions, that has parent predefinition in the list to the end of the list.
            for (var current = result.First; current != null; current = current.Next)
            {
                if (!myDefsByParentVertexName.ContainsKey(current.Value.VertexTypeName))
                    continue;

                //All predefinitions, that has the current predefintion as parent vertex type.
                var corrects = myDefsByParentVertexName[current.Value.VertexTypeName];

                foreach (var correct in corrects)
                {
                    result.AddLast(correct);
                }
            }

            if (myDefsByVertexName.Count > result.Count)
                //There are some defintions that are not in the vertex...so they must contain a circle.
                throw new CircularTypeHierarchyException(myDefsByVertexName.Values.Except(result));

            return result;
        }
        public void Save()
        {
            if (this.ResFiles == null || this.Datas == null)
                return;

            Dictionary<string, ResXResourceWriter> writers = new Dictionary<string, ResXResourceWriter>();
            foreach (var f in this.ResFiles) {
                writers.Add(f.Key, new ResXResourceWriter(f.Value));
            }

            foreach (var d in this.Datas) {

                if (string.IsNullOrEmpty(d.Key))
                    continue;

                var writer = writers.First(w => w.Key == "Default");
                writer.Value.AddResource(d.Key, (string)d.Default);

                foreach (var w in writers.Where(ww => !ww.Key.Equals("Default"))) {
                    var k = w.Key.Replace("-", "_");
                    PropertyInfo p = d.GetType().GetProperty(k);
                    if (p != null) {
                        var value = (string)p.GetValue(d);
                        w.Value.AddResource(d.Key, value);
                    }
                }

                d.IsExists = true;
            }

            foreach (var w in writers) {
                w.Value.Dispose();
            }
        }
        /// <summary>
        /// Returns a tuple to indicate the hand type and the hand value, values of any hands of any size can be compared using the hand value
        /// </summary>
        /// <param name="cards">an enumerable of card strings "1h, 2h....Kh, Ah"</param>
        /// <returns>tuple with the first int representing the type and the second int representing the hand value</returns>
        private static Tuple<int, int> badhandEval(IEnumerable<string> cards)
        {
            int handValue = 0;
            int handType = 0;

            Dictionary<int, int> cardrankoccurrence = new Dictionary<int, int>();
            Dictionary<string, int> cardsuitoccurrence = new Dictionary<string, int>();

            cardsuitoccurrence.Add("c", 0);
            cardsuitoccurrence.Add("s", 0);
            cardsuitoccurrence.Add("d", 0);
            cardsuitoccurrence.Add("h", 0);

            for (int i = 1; i < 15; i++)
            {
                cardrankoccurrence.Add(i, 0);
            }

            foreach (string card in cards)
            {
                cardsuitoccurrence[card.Substring(card.Length - 1, 1)]++;
                cardrankoccurrence[parsedRank(card.Substring(0, card.Length - 1))]++;
            }

            bool isFlush = cardrankoccurrence.Values.Any(s => s == 5);
            bool isStraight = IsSequential(cardrankoccurrence.Where(s => s.Value == 1).Select(t => t.Key));
            bool isQuad = cardrankoccurrence.Values.Any(s => s == 4);
            bool isTrip= cardrankoccurrence.Values.Any(s => s == 3);
            bool isTwoPair = cardrankoccurrence.Values.Where(s=>s==2).Count() == 2;
            bool isOnePair = cardrankoccurrence.Values.Where(s=>s==2).Count() == 1;

            if (isFlush && isStraight)
            {
                handValue = 8000000;
                handType = STRAIGHT_FLUSH;
            }
            else if (isQuad)
            {
                handValue = 7000000;
                handType = FOUR_OF_A_KIND;
            }
            else if (isTrip && isOnePair)
            {
                handValue = 6000000;
                handType = FULL_HOUSE;
            }
            else if (isFlush)
            {
                handValue = 5000000;
                handType = FLUSH;
            }
            else if (isStraight)
            {
                handValue = 4000000;
                handType = STRAIGHT;
            }
            else if (isTrip)
            {
                handValue = 3000000;
                handType = THREE_OF_A_KIND;
            }
            else if (isTwoPair)
            {
                handValue = 2000000;
                handType = TWO_PAIR;
            }
            else if (isOnePair)
            {
                handValue = 1000000;
                handType = ONE_PAIR;
            }

            int tenbounce = 0;
            foreach (int i in cardrankoccurrence.Where(s => s.Value >= 1).Select(t => t.Value).OrderBy(u => u))
            {
                handValue += i * (int)Math.Pow(10, tenbounce);
                tenbounce++;
            }

            return new Tuple<int,int> (handType, handValue);
        }
Example #25
0
        private List<KeyValuePair<AiAction, int>> GetPotentialActions()
        {
            var potential = new Dictionary<AiAction, int>();

            foreach (var t in _actions)
            {
                int n;
                if (t.Trigger(out n))
                    potential.Add(t, n);
            }

            if (potential.Count == 0)
                return new List<KeyValuePair<AiAction, int>>();

            var max = potential.Max(kvp => kvp.Value);

            var matches = potential.Where(kvp => kvp.Value == max).ToList();
            return matches;
        }
Example #26
0
		/// <summary>
		/// Get the LangCode from the Database.ssf file and insert it in css file
		/// </summary>
		public static string ParaTextDcLanguage(string dataBaseName, bool hyphenlang)
		{
			string dcLanguage = string.Empty;
			if (AppDomain.CurrentDomain.FriendlyName.ToLower() == "paratext.exe") // is paratext00
			{
				// read Language Code from ssf
				SettingsHelper settingsHelper = new SettingsHelper(dataBaseName);
				string fileName = settingsHelper.GetSettingsFilename();
				string xPath = "//ScriptureText/EthnologueCode";
				XmlNode xmlLangCode = GetXmlNode(fileName, xPath);
				if (xmlLangCode != null && xmlLangCode.InnerText != string.Empty)
				{
					dcLanguage = xmlLangCode.InnerText;
				}
				xPath = "//ScriptureText/Language";
				XmlNode xmlLangNameNode = GetXmlNode(fileName, xPath);
				if (xmlLangNameNode != null && xmlLangNameNode.InnerText != string.Empty)
				{
					if (dcLanguage == string.Empty)
					{
						Dictionary<string, string> _languageCodes = new Dictionary<string, string>();
						_languageCodes = LanguageCodesfromXMLFile();
						if (_languageCodes.Count > 0)
						{
							foreach (
								var languageCode in
									_languageCodes.Where(
										languageCode => languageCode.Value.ToLower() == xmlLangNameNode.InnerText.ToLower()))
							{
								if (hyphenlang)
								{
									dcLanguage = _languageCodes.ContainsValue(languageCode.Key.ToLower())
										? _languageCodes.FirstOrDefault(x => x.Value == languageCode.Key.ToLower()).Key
										: languageCode.Key;
								}
								else
								{
									dcLanguage = languageCode.Key;
								}
								break;
							}
						}
					}
					dcLanguage += ":" + xmlLangNameNode.InnerText;
				}
			}
			return dcLanguage;
		}
Example #27
0
        /// <summary>
        /// Injects mods as per selected in lsvMods.
        /// </summary>
        /// <param name="createBackup">Creates a backup of modified .pp files if true. Default is false.</param>
        /// <param name="checkConflicts">Checks for conflicts in pending mods. Default is true.</param>
        /// <param name="suppressPopups">If true, does not generate message boxes. Default is false.</param>
        public bool inject(bool createBackup = false, bool checkConflicts = true, bool suppressPopups = false)
        {
            initializeBench();
            cancelPending = false;
            updateStatus("Compiling changes...", LogIcon.Processing);

            //Compile changes
            _7z.ProgressUpdatedEventArgs updateProgress;
            List<string> rsub = new List<string>();
            List<Mod> mods = new List<Mod>();
            List<Mod> unmods = new List<Mod>();
            foreach (ListViewItem l in lsvMods.Items)
            {
                Mod m = (Mod)l.Tag;
                if (l.Checked != m.Installed)
                {
                    if (l.Checked)
                    {
                        mods.Add(m);
                    }
                    else
                    {
                        unmods.Add(m);
                        rsub.AddRange(m.SubFilenames);
                    }
                }
            }

            if (mods.Count + unmods.Count == 0)
            {
                updateStatus("No changes in selected mods found.", LogIcon.Error, false);
                updateStatus("FAILED: No changes in selected mods", LogIcon.Error, false, true);
                return false;
            }

            //Reset controls
            setEnabled(false);
            btnCancel.Enabled = true;
            prgMinor.Value = 0;
            prgMajor.Value = 0;
            int index = 0;

            Action Fail = () =>
            {
                setEnabled(true);
                prgMinor.Value = 0;
                prgMajor.Value = 0;
            };

            //Check if directory exists
            if (!(Directory.Exists(Paths.AA2Play) && Directory.Exists(Paths.AA2Edit)))
            {
                updateStatus("AA2Play/AA2Edit is not installed/cannot be found", LogIcon.Error, false);
                updateStatus("FAILED: AA2Play/AA2Edit is not installed/cannot be found", LogIcon.Error, false, true);
                Fail();
                return false;
            }
            
            refreshModList(true, txtSearch.Text);

            foreach (ListViewItem l in lsvMods.Items)
            {
                Mod m = (Mod)l.Tag;
                foreach (Mod n in mods)
                    if (n.Filename == m.Filename)
                        l.Checked = true;
                
                foreach (Mod n in unmods)
                    if (n.Filename == m.Filename)
                        l.Checked = false;
            }

            //Clear and create temp
            updateStatus("Clearing TEMP folder...");
            updateStatus("Clearing temporary folders...");

            if (Directory.Exists(Paths.TEMP))
                TryDeleteDirectory(Paths.TEMP);

            if (Directory.Exists(Paths.WORKING))
                TryDeleteDirectory(Paths.WORKING); 

            if (!Directory.Exists(Paths.BACKUP))
                Directory.CreateDirectory(Paths.BACKUP + @"\"); 

            
            Directory.CreateDirectory(Paths.TEMP + @"\");
            Directory.CreateDirectory(Paths.WORKING + @"\");
            Directory.CreateDirectory(Paths.TEMP + @"\AA2_PLAY\");
            Directory.CreateDirectory(Paths.TEMP + @"\AA2_MAKE\");
            Directory.CreateDirectory(Paths.TEMP + @"\BACKUP\");
            Directory.CreateDirectory(Paths.TEMP + @"\BACKUP\AA2_PLAY\");
            Directory.CreateDirectory(Paths.TEMP + @"\BACKUP\AA2_MAKE\");

            //Check conflicts
            if (checkConflicts) { 
                updateStatus("Checking conflicts...");

                Dictionary<string, List<Mod>> files = new Dictionary<string, List<Mod>>();

                foreach (Mod m in modDict.Values)
                    if (lsvMods.Items[m.Name].Checked)
                        m.SubFilenames.ForEach(x =>
                            {
                                if (files.ContainsKey(x))
                                    files[x].Add(m);
                                else
                                    files[x] = new List<Mod> { m };
                            }); //Set each subfile to their respective owner(s)

                bool conflict = false;

                foreach (ListViewItem item in lsvMods.Items) //Loop through list items
                {
                    if (!item.Checked) //We only care about ones that are / will be installed
                        continue;

                    Mod m = item.Tag as Mod; //The current mod we are checking

                    List<string> conflicts = files.Where(x => x.Value.Any(y => y.Name == m.Name)) //If the subfile is contained by the mod
                                                  .Where(x => x.Value.Count > 1) //If there is more than one owner
                                                  .Select(x => x.Key)
                                                  .ToList(); //Convert it to a list
                    

                    if (conflicts.Count > 0)
                    {
                        conflict = true;

                        foreach (string s in conflicts)
                            updateStatus(item.Text + ": " + s, LogIcon.Error, false);

                        item.BackColor = Color.FromArgb(255, 255, 160);
                    }
                }
                if (conflict)
                {
                    updateStatus("Collision detected.", LogIcon.Error, false);
                    updateStatus("FAILED: The highlighted mods have conflicting files", LogIcon.Error, false, true);
                    currentOwner.InvokeMessageBox("Some mods have been detected to have conflicting files.\nYou can use the log to manually fix the conflicting files in the mods (if they can be fixed) or you can proceed anyway by changing the relevant setting in the preferences.\nNote: if you proceed anyway, to uninstall you must uninstall mods in the reverse order you installed them to ensure that wrong files are not left behind.", "Collision detected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    Fail();
                    return false;
                }
            }

            //Check for free space

            long total7zSize = mods.Select(x => new SevenZipExtractor(x.Filename).UnpackedSize)
                                   .Sum(x => (long)x);

            long totalAA2PlaySize = mods.Select(x => new SevenZipExtractor(x.Filename).Files
                                            .Where(y => y.Filename.StartsWith("AA2_PLAY"))
                                            .Select(y => y.UnpackedSize))
                                        .Sum(x => x.Sum(y => (long)y));

            long totalAA2EditSize = mods.Select(x => new SevenZipExtractor(x.Filename).Files
                                            .Where(y => y.Filename.StartsWith("AA2_MAKE"))
                                            .Select(y => y.UnpackedSize))
                                        .Sum(x => x.Sum(y => (long)y));

            AdditionDictionary requiredSizes = new AdditionDictionary();

            requiredSizes[Paths.TEMP.Remove(1)] = total7zSize;

            if (Paths.TEMP.Remove(1) != Paths.AA2Edit.Remove(1))
                requiredSizes[Paths.AA2Edit.Remove(1)] = totalAA2EditSize + 0x40000000; //an approx. extra 1gb for temp files

            if (Paths.TEMP.Remove(1) != Paths.AA2Play.Remove(1))
                requiredSizes[Paths.AA2Play.Remove(1)] = totalAA2PlaySize + 0x40000000; //an approx. extra 1gb for temp files

            foreach (var kv in requiredSizes)
            {
                bool tryAgain = false;
                do
                {
                    tryAgain = false;
                    if (!IsEnoughFreeSpace(kv.Key, kv.Value))
                    {
                        updateStatus("FAILED: There is not enough free space", LogIcon.Error, false);

                        string spaces = requiredSizes.Select(x => "Drive " + x.Key + ":\\ : Required " + BytesToString(x.Value) + "; Available: " + BytesToString(new DriveInfo(kv.Key).AvailableFreeSpace))
                                        .Aggregate((a, b) => a + "\n" + b);
                        
                        var result = currentOwner.InvokeMessageBox("There is not enough free space to allow an approximate safe installation.\n" + spaces, "Not enough free space", MessageBoxButtons.RetryCancel, MessageBoxIcon.Exclamation);

                        switch(result)
                        {
                            case DialogResult.Retry:
                                tryAgain = true;
                                break;
                            case DialogResult.Cancel:
                            default:
                                Fail();
                                return false;
                        }
                    }
                } while (tryAgain);
            }

            //Verify mods
            List<Mod> combined = mods.Concat(unmods).ToList();
            List<string> estPP = new List<string>();

            foreach (var m in combined)
            {
                SevenZipExtractor s;
                if (m.Installed)
                    s = new SevenZipExtractor(m.BackupFilename);
                else
                    s = new SevenZipExtractor(m.Filename);

                updateStatus("(" + index + "/" + combined.Count + ") Verifying " + m.Name + " (0%)...", LogIcon.Processing, false, true);

                foreach (var d in s.Files)
                {
                    if (d.Attributes.HasFlag(Attributes.Directory) && d.Filename.Length > 9)
                    {
                        string n = d.Filename.Remove(0, 9);

                        if (d.Filename.StartsWith("AA2_MAKE"))
                            estPP.Add(Paths.AA2Edit + @"\" + n + ".pp");
                        else
                            estPP.Add(Paths.AA2Play + @"\" + n + ".pp");
                    }
                }

                SevenZipBase.ProgressUpdatedEventArgs progress = (i) => {
                    this.prgMinor.GetCurrentParent().HybridInvoke(() => {
                        prgMinor.Value = i;
                    });
                    updateStatus("(" + index + "/" + combined.Count + ") Verifying " + m.Name + " (" + i + "%)...", LogIcon.Processing, false, true);
                };

                s.ProgressUpdated += progress;

                bool result = new Func<bool>(() => s.TestArchive()).SemiAsyncWait();

                s.ProgressUpdated -= progress;

                if (!result)
                {
                    updateStatus("FAILED: " + m.Name + " failed to verify.", LogIcon.Error, false);
                    Fail();
                    return false;
                }
            }

            //Verify PPs
            foreach (string p in estPP)
            {
                updateStatus("(" + index + "/" + combined.Count + ") Verifying " + p.Remove(0, p.LastIndexOf('\\') + 1) + "...", LogIcon.Processing);
                if (File.Exists(p))
                    if (!ppHeader.VerifyHeader(p))
                    {
                        updateStatus("FAILED: " + p.Remove(0, p.LastIndexOf('\\') + 1) + " failed to verify.", LogIcon.Error, false);
                        Fail();
                        return false;
                    }
            }

            //Extract all mods

            index = 0;

            string name = "";

            updateProgress = (i) => {
                prgMinor.GetCurrentParent().HybridInvoke(() => {
                    prgMinor.Value = i;
                });
                updateStatus("(" + index + "/" + combined.Count + ") Extracting " + name + " (" + i + "%)...", LogIcon.Processing, false, true);
            };

            _7z.ProgressUpdated += updateProgress;

            foreach (Mod item in combined)
            {
                index++;
                name = item.Name;
                updateStatus("(" + index + "/" + combined.Count + ") Extracting " + name + " (0%)...", LogIcon.Processing);

                if (mods.Contains(item))
                    _7z.Extract(item.Filename);
                else
                    _7z.Extract(item.BackupFilename, Paths.TEMP + @"\BACKUP\");

                prgMajor.Value = (100 * index / combined.Count);
                if (tryCancel())
                {
                    Fail();
                    return false;
                }
            }

            _7z.ProgressUpdated -= updateProgress;

            //Reached point of no return.
            btnCancel.Enabled = false;

            //Build ppQueue
            index = 0;
           
            Queue<basePP> ppQueue = new Queue<basePP>();
            List<basePP> ppList = new List<basePP>();
            updateStatus("Creating .pp file queue...");


            List<string> tempPLAY = new List<string>(Directory.GetDirectories(Paths.TEMP + @"\BACKUP\AA2_PLAY", "jg2*", SearchOption.TopDirectoryOnly));
            List<string> tempEDIT = new List<string>(Directory.GetDirectories(Paths.TEMP + @"\BACKUP\AA2_MAKE", "jg2*", SearchOption.TopDirectoryOnly));

            foreach (string path in tempPLAY)
            {
                ppQueue.Enqueue(new basePP(path, Paths.AA2Play));
            }
            foreach (string path in tempEDIT)
            {
                ppQueue.Enqueue(new basePP(path, Paths.AA2Edit));
            }

            //Prioritise removing subfiles from mods that are being uninstalled
            while (ppQueue.Count > 0)
            {
                basePP bp = ppQueue.Dequeue();

                var r = rsub.ToArray();
                foreach (string s in r)
                {
                    foreach (IWriteFile iw in bp.pp.Subfiles)
                        if (bp.ppDir + "\\" + iw.Name == s.Remove(0, 9))
                        {
                            rsub.Remove(s);
                            bp.pp.Subfiles.Remove(iw);
                            break;
                        }
                }

                prgMinor.Style = ProgressBarStyle.Continuous;
                int i = 1;

                foreach (string s in Directory.GetFiles(bp.ppRAW))
                {
                    string fname = s.Remove(0, s.LastIndexOf('\\') + 1);
                    foreach (IWriteFile sub in bp.pp.Subfiles)
                    {
                        if (fname == sub.Name)
                        {
                            bp.pp.Subfiles.Remove(sub);
                            break;
                        }
                    }
                    prgMinor.Value = (i * 100 / Directory.GetFiles(bp.ppRAW).Length);
                    bp.pp.Subfiles.Add(new Subfile(s));
                    i++;
                }

                ppList.Add(bp);
            }

            tempPLAY = new List<string>(Directory.GetDirectories(Paths.TEMP + @"\AA2_PLAY", "jg2*", SearchOption.TopDirectoryOnly));
            tempEDIT = new List<string>(Directory.GetDirectories(Paths.TEMP + @"\AA2_MAKE", "jg2*", SearchOption.TopDirectoryOnly));

            //Sort the uninstalled .pp files back into the main queue
            foreach (string path in tempPLAY)
            {
                var p = new basePP(path, Paths.AA2Play);
                var o = ppList.Find(x => x.ppDir == p.ppDir);
                if (o != null)
                {
                    p.pp = o.pp;
                    ppList.Remove(o);
                }
                ppQueue.Enqueue(p);
            }
            foreach (string path in tempEDIT)
            {
                var p = new basePP(path, Paths.AA2Edit);
                var o = ppList.Find(x => x.ppDir == p.ppDir);
                if (o != null)
                {
                    p.pp = o.pp;
                    ppList.Remove(o);
                }
                ppQueue.Enqueue(p);
            }

            int ii = 0;
            prgMajor.Value = 0;
            prgMinor.Value = 0;
            foreach (basePP b in ppList)
            {
                ii++;
                updateStatus("(" + ii + "/" + ppList.Count + ") Reverting " + b.ppFile + " (0%)...", LogIcon.Processing);
                if (b.pp.Subfiles.Count > 0)
                {
                    BackgroundWorker bb = b.pp.WriteArchive(b.pp.FilePath, createBackup);

                    bb.ProgressChanged += ((s, e) =>
                    {
                        prgMinor.GetCurrentParent().HybridInvoke(() => {
                            prgMinor.Value = e.ProgressPercentage;
                        });
                        updateStatus("(" + ii + "/" + ppList.Count + ") Reverting " + b.ppFile + " (" + e.ProgressPercentage + "%)...", LogIcon.Processing, false, true);
                    });

                    bb.SemiAsyncWait();
                }
                else
                {
                    File.Delete(b.pp.FilePath);
                }
                prgMajor.Value = (100 * ii / ppList.Count);
            }

            prgMinor.Value = 0;
            prgMajor.Value = 0;

            //Process .pp files
            int initial = ppQueue.Count;
            updateTaskbarProgress();
            index = 0;
            while (ppQueue.Count > 0)
            {
                basePP b = ppQueue.Dequeue();

                updateStatus("(" + (index + 1) + "/" + initial + ") Injecting " + b.ppFile + " (0%)...", LogIcon.Processing);

                prgMinor.Style = ProgressBarStyle.Continuous;
                int i = 1;

                foreach (Mod m in mods)
                {
                    foreach (string s in m.SubFilenames)
                    {
                        if (s.Contains(b.ppDir))
                        {
                            string r = s.Remove(0, 9);
                            string rs = r.Remove(0, r.LastIndexOf('\\') + 1);
                            string workingdir = Paths.WORKING + "\\BACKUP\\" + m.Name.Replace(".7z", "").Replace(".zip", "") + "\\";
                            string directory;
                            if (tempPLAY.Contains(b.ppRAW))
                            {
                                directory = workingdir + "AA2_PLAY\\" + r.Remove(r.LastIndexOf('\\') + 1);
                            }
                            else
                            {
                                directory = workingdir + "AA2_MAKE\\" + r.Remove(r.LastIndexOf('\\') + 1);
                            }

                            Directory.CreateDirectory(directory);
                            foreach (IWriteFile iw in b.pp.Subfiles)
                            {
                                if (iw.Name == rs)
                                {
                                    using (FileStream fs = new FileStream(directory + rs, FileMode.Create))
                                    {
                                        iw.WriteTo(fs);
                                    }
                                }
                            }
                        }
                    }
                }

                foreach (string s in rsub)
                {
                    foreach (IWriteFile iw in b.pp.Subfiles)
                        if (b.ppDir + "\\" + iw.Name == s.Remove(0, 9))
                        {
                            b.pp.Subfiles.Remove(iw);
                            break;
                        }
                }

                foreach (string s in Directory.GetFiles(b.ppRAW))
                {
                    string fname = s.Remove(0, s.LastIndexOf('\\')+1);
                    foreach (IWriteFile sub in b.pp.Subfiles)
                    {
                        if (fname == sub.Name)
                        {
                            b.pp.Subfiles.Remove(sub);
                            break;
                        }
                    }
                    prgMinor.Value = (100 * i / Directory.GetFiles(b.ppRAW).Length);
                    b.pp.Subfiles.Add(new Subfile(s));
                    i++;
                }
                if (b.pp.Subfiles.Count > 0)
                {
                    prgMinor.Value = 0;
                    BackgroundWorker bb = b.pp.WriteArchive(b.pp.FilePath, createBackup);

                    bb.ProgressChanged += ((s, e) =>
                    {
                        prgMinor.GetCurrentParent().HybridInvoke(() =>
                            { prgMinor.Value = e.ProgressPercentage; });
                        
                        updateStatus("(" + (index + 1) + "/" + initial + ") Injecting " + b.ppFile + " (" + e.ProgressPercentage + "%)...", LogIcon.Processing, false, true);
                    });

                    bb.SemiAsyncWait();
                }
                else
                {
                    File.Delete(b.pp.FilePath);
                }
                //Loop complete

                TryDeleteDirectory(b.ppRAW + "\\");

                index++;
                prgMajor.Value = (100 * index / initial);
                updateTaskbarProgress();
            }

            int ind = 0;
            //Archive backups
            prgMinor.Value = 0;
            prgMajor.Value = 0;
            if (!Directory.Exists(Paths.WORKING + "\\BACKUP\\")) { Directory.CreateDirectory(Paths.WORKING + "\\BACKUP\\"); }
            List<string> tempBackup = new List<string>(Directory.GetDirectories(Paths.WORKING + "\\BACKUP\\"));
            foreach (string s in tempBackup)
            {
                ind++;
                prgMajor.Value = (100 * ind / tempBackup.Count);
                updateStatus("(" + ind + "/" + tempBackup.Count + ") Archiving backup of " + s + " (0%)...", LogIcon.Processing);

                updateProgress = (i) => {
                    prgMinor.GetCurrentParent().HybridInvoke(() => {
                        prgMinor.Value = i;
                    });
                    updateStatus("(" + ind + "/" + tempBackup.Count + ") Archiving backup of " + s + " (" + i + "%)...", LogIcon.Processing, false, true);
                };

                _7z.ProgressUpdated += updateProgress;

                string item = s.Remove(0, s.LastIndexOf('\\') + 1);
                string archive = Paths.BACKUP + "\\" + item + ".7z";
                if (Directory.Exists(s + "\\AA2_PLAY\\"))
                {
                    foreach (string sub in Directory.GetDirectories(s + "\\AA2_PLAY\\"))
                    {
                        string g = sub.Remove(0, s.Length + 1) + "\\";
                        _7z.Compress(archive, s, g);
                    }
                }
                if (Directory.Exists(s + "\\AA2_MAKE\\"))
                {
                    foreach (string sub in Directory.GetDirectories(s + "\\AA2_MAKE\\"))
                    {
                        string g = sub.Remove(0, s.Length + 1) + "\\";
                        _7z.Compress(archive, s, g);
                    }
                }

                _7z.ProgressUpdated -= updateProgress;
            }

            //Finish up
            prgMinor.Style = ProgressBarStyle.Continuous;
            updateStatus("Finishing up...");
            mods.AddRange(unmods);

            foreach (Mod m in unmods)
            {
                string s = Paths.BACKUP + "\\" + m.Name.Replace(".zip", ".7z");
                if (File.Exists(s))
                {
                    tryDelete(s);
                }
            }

            Configuration.saveMods(modDict);

            TryDeleteDirectory(Paths.TEMP);
            TryDeleteDirectory(Paths.WORKING);

            updateStatus("Success!", LogIcon.OK, false);
            updateTaskbarProgress(TaskbarProgress.TaskbarStates.NoProgress);
            if (!suppressPopups)
                currentOwner.InvokeMessageBox("Mods successfully synced.");
            refreshModList();
            return true;
        }
        public void EncodeToPackageMergeList(Dictionary<Byte, int> input,Dictionary<Byte, int> wertInput)
        {
            List<List<Knoten>> baum = new List<List<Knoten>>();
            int maxValue = input.Values.Max();
            for (int i = 0; i < maxValue; i++)
            {
                baum.Add(new List<Knoten>());
            }
            //huffmanTable
            for (int i = maxValue; i > 0; i--)
            {
                var matches = input.Where(pair => pair.Value == i)
                  .Select(pair => pair.Key);
                foreach (var item in matches)
                {
                    huffmanTable.Add(item, new List<bool>());
                    baum[i-1].Add(new Knoten(item,wertInput[item]));
                }
            }
            for (int i = maxValue-1; i >= 0; i--)
            {
                baum[i]=baum[i].OrderBy(o => o.wert).ToList();
                bool wert = false;

                for (int ib = 0; ib < baum[i].Count(); ib++)
                {
                    foreach (var itemInter in baum[i][ib].punkte)
                    {
                        huffmanTable[itemInter].Add(wert);
                    }

                    if (wert==true&&i!=0)
                    {
                        baum[i - 1].Add(new Knoten(new List<byte>(baum[i][ib].punkte),
                            new List<byte>(baum[i][ib - 1].punkte),
                            baum[i][ib].wert + baum[i][ib - 1].wert));
                    }
                    else if(ib == baum[i].Count()-1 && i != 0)
                    {
                        baum[i - 1].Add(new Knoten(new List<byte>(baum[i][ib].punkte), baum[i][ib].wert));
                    }
                    wert= !wert;
                }
            }
        }
Example #29
0
        /// <summary>
        /// Gets a ist of installable extensions sorted to ensure dependencies are installed first
        /// </summary>
        /// <returns></returns>
        public static IDictionary<string, PackageInfo> GetInstallPackages()
        {
            var packageTypes = new string[] { "Module", "Skin", "Container", "JavaScriptLibrary", "Language", "Provider", "AuthSystem", "Package" };
            var invalidPackages = new List<string>();

            var packages = new Dictionary<string, PackageInfo>();

            foreach (string packageType in packageTypes)
            {
                var installPackagePath = Globals.ApplicationMapPath + "\\Install\\" + packageType;
                if (Directory.Exists(installPackagePath))
                {
                    var files = Directory.GetFiles(installPackagePath);
                    if (files.Length > 0)
                    {
                        foreach (string file in files)
                        {
                            if (Path.GetExtension(file.ToLower()) == ".zip")
                            {
                                PackageController.ParsePackage(file, installPackagePath, packages, invalidPackages);
                                //HtmlUtils.WriteFeedback(HttpContext.Current.Response, 2, "Parsing - " + file.Replace(installPackagePath + @"\", "") + "<br/>");
                            }
                        }
                    }
                }
            }

            //Add packages with no dependency requirements
            var sortedPackages = packages.Where(p => p.Value.Dependencies.Count == 0).ToDictionary(p => p.Key, p => p.Value);

            int prevDependentCount = -1;

            var dependentPackages = packages.Where(p => p.Value.Dependencies.Count > 0).ToDictionary(p=> p.Key, p => p.Value);
            int dependentCount = dependentPackages.Count;
            //HtmlUtils.WriteFeedback(HttpContext.Current.Response, 2, "Start - Parsing Dependencies<br/>");
            while (dependentCount != prevDependentCount)
            {
                prevDependentCount = dependentCount;
                var addedPackages = new List<string>();
                foreach (var package in dependentPackages)
                {
                    //HtmlUtils.WriteFeedback(HttpContext.Current.Response, 4, "Parsing - " + package.Value.Name + "<br/>");
                    foreach (var dependency in package.Value.Dependencies)
                    {
                        if (sortedPackages.Count(p => p.Value.Name == dependency.PackageName && p.Value.Version >= dependency.Version) > 0)
                        {
                            //HtmlUtils.WriteFeedback(HttpContext.Current.Response, 4, "Dependency Resolved - " + package.Value.Name + "<br/>");
                            sortedPackages.Add(package.Key, package.Value);
                            addedPackages.Add(package.Key);
                        }
                    }
                }
                foreach (var packageKey in addedPackages)
                {
                    dependentPackages.Remove(packageKey);
                }
                dependentCount = dependentPackages.Count;
            }

            //Add any packages whose dependency cannot be resolved
            foreach (var package in dependentPackages)
            {
                sortedPackages.Add(package.Key, package.Value);
            }

            //HtmlUtils.WriteFeedback(HttpContext.Current.Response, 2, "End - Parsing Dependencies<br/>");

            //foreach (var package in sortedPackages)
            //{
            //    HtmlUtils.WriteFeedback(HttpContext.Current.Response, 2, "Installing - " + package.Key + "<br/>");
            //}
            return sortedPackages;
        }
Example #30
0
        static void StoreGeotags(Dictionary<long, StoryTagCollection> stories)
        {
            if (stories.Count == 0)
                return;

            Console.Write("Saving geotags...");

            var hasTags = stories.Where(n => n.Value.HasTags);
            foreach (var item in hasTags)
            {
                //Insert geotags
                foreach (Geotag geotag in item.Value.Geotags)
                {
                    string lon = geotag.Longitude.ToString(CultureInfo.InvariantCulture);
                    string lat = geotag.Latitude.ToString(CultureInfo.InvariantCulture);
                    Helpers.RunSqlStatement(Name, "call AddRemoveStoryTag(1, 'location', 1, 1, " + item.Key + ", null, " + lon + "," + lat + ");");
                }
                //Insert keywords
                foreach (string tag in item.Value.Keywords)
                {
                    string tag2 = tag.Replace("'", "&#39;").ToLower();
                    Helpers.RunSqlStatement(Name, "insert ignore into InfoKeyword (Keyword) values ('" + tag2 + "');");
                    List<long> tagID = new List<long>();
                    Helpers.RunSelect(Name, "select InfoKeywordID from InfoKeyword where Keyword = '" + tag2 + "';", tagID, (values, reader) => values.Add(reader.GetInt64("InfoKeywordID")));
                    Helpers.RunSqlStatement(Name, "call AddRemoveStoryTag(1, 'keyword', 1, 1, " + item.Key + ", " + tagID[0] + ", null, null);");
                }
                //Insert entities
                foreach (string tag in item.Value.Entities)
                {
                    string tag2 = tag.Replace("'", "&#39;").ToLower();
                    Helpers.RunSqlStatement(Name, "insert ignore into InfoEntity (Entity) values ('" + tag2 + "');");
                    List<long> tagID = new List<long>();
                    Helpers.RunSelect(Name, "select InfoEntityID from InfoEntity where Entity = '" + tag2 + "';", tagID, (values, reader) => values.Add(reader.GetInt64("InfoEntityID")));
                    Helpers.RunSqlStatement(Name, "call AddRemoveStoryTag(1, 'entity', 1, 1, " + item.Key + ", " + tagID[0] + ", null, null);");
                }
            }

            //Update Story.ProcessedForMetadata
            Helpers.RunSqlStatement(Name, "update Story set ProcessedForMetadata=1 where StoryID in (" + string.Join(",", stories.Keys.Select(n => n.ToString()).ToArray()) + ");");

            Console.WriteLine(" done");
        }