示例#1
0
        /*
         * public override BaseMultiset GetMultiset(Store store)
         * {
         *  return Materialize().GetMultiset(store);
         * }
         */

        public Accumulator Materialize()
        {
            var ret = new Accumulator(this);

            foreach (var row in Rows)
            {
                ret.AddRow(row);
            }
            return(ret);
        }
        public override IAccumulator BuildAccumulator(IEnumerable <string> variables)
        {
            var acc             = new Accumulator(variables, SortVariables);
            var variableIndexes = new int[_matchLength];

            for (int i = 0; i < _matchLength; i++)
            {
                variableIndexes[i] = acc.Columns.IndexOf(SortVariables[i]);
            }

            if (_matchEnumerations.Count == 1)
            {
                return(new VirtualizingAccumulator(acc.Columns, _matchEnumerations[0], variableIndexes, SortVariables));
            }

            bool keepGoing = true;

            ulong[] toMatch = new ulong[_matchLength];
            foreach (var enumerator in _matchEnumerations)
            {
                keepGoing &= enumerator.MoveNext();
                if (CompareArrays(enumerator.Current, toMatch) > 0)
                {
                    toMatch = enumerator.Current;
                }
            }
            while (keepGoing)
            {
                for (int i = 0; i < _matchEnumerations.Count; i++)
                {
                    while (keepGoing && CompareArrays(toMatch, _matchEnumerations[i].Current) > 0)
                    {
                        keepGoing &= _matchEnumerations[i].MoveNext();
                    }
                    if (keepGoing && CompareArrays(_matchEnumerations[i].Current, toMatch) > 0)
                    {
                        toMatch = _matchEnumerations[i].Current;
                    }
                }
                if (keepGoing && _matchEnumerations.All(x => CompareArrays(toMatch, x.Current) == 0))
                {
                    // Bingo!
                    var newRow = new ulong[acc.Columns.Count];
                    for (int i = 0; i < _matchLength; i++)
                    {
                        newRow[variableIndexes[i]] = toMatch[i];
                    }
                    acc.AddRow(newRow);
                    keepGoing &= _matchEnumerations[0].MoveNext();
                    toMatch    = _matchEnumerations[0].Current;
                }
            }
            return(acc);
        }
示例#3
0
        private IAccumulator Product(IAccumulator other)
        {
            var product = new Accumulator(Columns, BoundVariables.Union(other.BoundVariables));

            /*
             * IEnumerable<ulong[]> outerEnumerator, innerEnumerator;
             * if (this.HighestBoundColumnIndex < other.HighestBoundColumnIndex)
             * {
             *  outerEnumerator = this.Rows;
             *  innerEnumerator = other.Rows;
             * }
             * else
             * {
             *  // This accumulator needs to be inner, so if it is a virtual one we need to materialize it first
             *  if (this is VirtualizingAccumulator)
             *  {
             *      return (this as VirtualizingAccumulator).Materialize().Product(other);
             *  }
             *  outerEnumerator = other.Rows;
             *  innerEnumerator = this.Rows;
             * }
             * foreach(var outer in outerEnumerator)
             * {
             *  foreach(var inner in innerEnumerator)
             *  {
             *      product.AddRow(RowJoin(outer, inner));
             *  }
             * }
             */

            IEnumerable <ulong[]> outerEnumerator = this.Rows;
            IEnumerable <ulong[]> innerEnumerator = other.Rows;

            foreach (var outer in outerEnumerator)
            {
                foreach (var inner in innerEnumerator)
                {
                    product.AddRow(RowJoin(outer, inner));
                }
            }

            // Update the sort order for the product
            product.SortOrder = this.SortOrder;
            foreach (var sortCol in other.SortOrder)
            {
                if (!product.SortOrder.Contains(sortCol))
                {
                    product.SortOrder.Add(sortCol);
                }
            }

            return(product);
        }
        public override IAccumulator BuildAccumulator(IEnumerable <string> variables)
        {
            var acc   = new Accumulator(variables, _triplePatterns[0].Variables.Take(1));
            var varIx = acc.Columns.IndexOf(_triplePatterns[0].Variables[0]);

            bool keepGoing = true;

            ulong[] topItems = new ulong[_matchEnumerations.Count];
            ulong   toMatch  = 0;

            for (int i = 0; i < _matchEnumerations.Count; i++)
            {
                keepGoing &= _matchEnumerations[i].MoveNext();
                if (!keepGoing)
                {
                    break;
                }
                topItems[i] = _matchEnumerations[i].Current;
                if (topItems[i] > toMatch)
                {
                    toMatch = topItems[i];
                }
            }
            while (keepGoing)
            {
                for (int i = 0; i < _matchEnumerations.Count; i++)
                {
                    while (_matchEnumerations[i].Current < toMatch && keepGoing)
                    {
                        keepGoing &= _matchEnumerations[i].MoveNext();
                    }
                    if (keepGoing && _matchEnumerations[i].Current > toMatch)
                    {
                        toMatch = _matchEnumerations[i].Current;
                    }
                }
                if (keepGoing && _matchEnumerations.All(x => x.Current.Equals(toMatch)))
                {
                    // Bingo!
                    var newRow = new ulong[acc.Columns.Count];
                    newRow[varIx] = toMatch;
                    acc.AddRow(newRow);
                    keepGoing &= _matchEnumerations[0].MoveNext();
                    toMatch    = _matchEnumerations[0].Current;
                }
            }
            return(acc);
        }
示例#5
0
        public override IAccumulator BuildAccumulator(IEnumerable <string> variables)
        {
            var acc             = new Accumulator(variables, SortVariables);
            var variableIndexes = new int[_matchLength];

            for (int i = 0; i < _matchLength; i++)
            {
                variableIndexes[i] = acc.Columns.IndexOf(SortVariables[i]);
            }

            foreach (var triple in _store.MatchAllTriples(_graphUris))
            {
                var newRow = new ulong[acc.Columns.Count];
                for (int i = 0; i < _matchLength; i++)
                {
                    newRow[variableIndexes[i]] = triple[i];
                }
                acc.AddRow(newRow);
            }
            return(acc);
        }
示例#6
0
        private static IAccumulator JoinSorted(IAccumulator outer, IAccumulator inner)
        {
            var output = new Accumulator(outer, inner);
            IEnumerator <ulong[]> outerEnumerator = outer.Rows.GetEnumerator();

            if (inner is VirtualizingAccumulator)
            {
                inner = (inner as VirtualizingAccumulator).Materialize();
            }
            var innerAcc = inner as Accumulator;
            RewindableListEnumerator <ulong[]> innerEnumerator = innerAcc.RewindableEnumerator;
            //IEnumerator<ulong[]> innerEnumerator = inner.Rows.GetEnumerator();

            bool keepGoing = outerEnumerator.MoveNext() && innerEnumerator.MoveNext();

            while (keepGoing)
            {
                while (keepGoing && RowMatch(outerEnumerator.Current, innerEnumerator.Current))
                {
                    output.AddRow(RowJoin(outerEnumerator.Current, innerEnumerator.Current));
                    innerEnumerator.SetMark();
                    bool innerKeepGoing = innerEnumerator.MoveNext();
                    while (innerKeepGoing && RowMatch(outerEnumerator.Current, innerEnumerator.Current))
                    {
                        output.AddRow(RowJoin(outerEnumerator.Current, innerEnumerator.Current));
                        innerKeepGoing = innerEnumerator.MoveNext();
                    }
                    innerEnumerator.RewindToMark();
                    keepGoing = outerEnumerator.MoveNext();
                }
                if (keepGoing)
                {
                    var cmp = RowCompare(outerEnumerator.Current, innerEnumerator.Current);
                    while (cmp != 0 && keepGoing)
                    {
                        keepGoing = cmp > 0 ? innerEnumerator.MoveNext() : outerEnumerator.MoveNext();
                        if (keepGoing)
                        {
                            cmp = RowCompare(outerEnumerator.Current, innerEnumerator.Current);
                        }
                    }
                }
                if (!keepGoing)
                {
                    keepGoing = outerEnumerator.MoveNext();
                    if (keepGoing)
                    {
                        innerEnumerator.Reset();
                        keepGoing &= innerEnumerator.MoveNext();
                    }
                }

                /*
                 * while (keepGoing && RowCompare(outerEnumerator.Current, innerEnumerator.Current) < 0)
                 *  keepGoing = outerEnumerator.MoveNext();
                 * while (keepGoing && RowCompare(outerEnumerator.Current, innerEnumerator.Current) > 0)
                 *  keepGoing = innerEnumerator.MoveNext();
                 */
            }
            return(output);
        }