Exemple #1
0
        public override bool Equals(object obj)
        {
            TableTwo p = obj as TableTwo;

            if (p == null ||
                p.name != name ||
                p.capacity != this.capacity ||
                !p.fieldNames.SequenceEqual(this.fieldNames) ||
                !p.fieldDataTypeNames.SequenceEqual(this.fieldDataTypeNames))
            {
                return(false);
            }
            for (int i = 0; i < capacity; i++)
            {
                bool containsRow = ContainsRow(i);
                if (containsRow != p.ContainsRow(i))
                {
                    return(false);
                }
                if (containsRow && !this[i].valuesAsStrings.SequenceEqual(p[i].valuesAsStrings))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemple #2
0
        public void InsertToTable(TableTwo pTable)
        {
            pTable._usedRows[row] = true;
            TableRow tr = pTable.GetRow(row);

            tr.valuesAsStrings = values;
        }
Exemple #3
0
        private void AddFieldsToTable(TableTwo pTable, string[] pFieldNames, string[] pDataTypeNames)
        {
            D.assert(pFieldNames.Length == pDataTypeNames.Length, "field definitions does not match");

            for (int i = 0; i < pFieldNames.Length; i++)
            {
                Type   genericType  = typeof(TableField <>);
                string dataTypeName = pDataTypeNames[i];
                //Console.WriteLine(dataTypeName);
                Type[] typeArgs = null;

                // Check through all assemblies for the type arguemnts
                foreach (Assembly assm in AppDomain.CurrentDomain.GetAssemblies())
                {
                    typeArgs = new Type[] { assm.GetType(dataTypeName) };
                    if (typeArgs[0] != null)
                    {
                        break; //found type
                    }
                }

                D.isNull(typeArgs[0], dataTypeName + " was not found ");
                Type repositoryType = genericType.MakeGenericType(typeArgs);

                ITableField result = (ITableField)Activator.CreateInstance(repositoryType, pFieldNames[i]);
                pTable.AddField(result);
            }
        }
Exemple #4
0
        public static T Create <T>(TableTwo pTable) where T : RelayObjectTwo
        {
            Type t       = typeof(T);
            T    newItem = Activator.CreateInstance(t) as T;

            newItem.CreateNewRelayEntry(pTable, t.Name);
            return(newItem);
        }
Exemple #5
0
 public void CreateNewRelayEntry(TableTwo pTable, string pClassName)
 {
     table    = pTable;
     objectId = table.CreateRow().row;
     table.EnsureField <string>(CSHARP_CLASS_FIELD_NAME);
     table.SetValue <string>(objectId, CSHARP_CLASS_FIELD_NAME, pClassName);
     SetupCells();
 }
Exemple #6
0
        public void LoadFromExistingRelayEntry(TableTwo pTable, int pObjectID, string pClassName)
        {
            table    = pTable;
            objectId = pObjectID;
#if DEBUG
            D.assert(table.ContainsRow(objectId), "object id does not exist! " + objectId);
            D.assert(table.GetValue <string>(pObjectID, CSHARP_CLASS_FIELD_NAME) == pClassName, "classname missmatch!");
#endif
            SetupCells();
        }
Exemple #7
0
        public TableTwo CreateTable(string pTableName)
        {
#if DEBUG
            if (tables.ContainsKey(pTableName))
            {
                throw new RelayException("Database already contains a table with name " + pTableName);
            }
#endif
            TableTwo newTable = new TableTwo(pTableName);
            tables.Add(pTableName, newTable);
            return(newTable);
        }
Exemple #8
0
        public IEnumerable <float> Load(string pFilename)
        {
#if PRINT_TIME_DATA
            DateTime startTime = DateTime.Now;
#endif

            FileStream   f  = new FileStream(pFilename, FileMode.Open);
            StreamReader sw = new StreamReader(f);

            while (!sw.EndOfStream)
            {
                string   tableName  = sw.ReadLine();
                int      tableCount = Convert.ToInt32(sw.ReadLine());
                string[] fieldNames = JsonConvert.DeserializeObject <string[]>(sw.ReadLine());
                string[] typeNames  = JsonConvert.DeserializeObject <string[]>(sw.ReadLine());

                TableTwo newTable = new TableTwo(tableName);
                AddFieldsToTable(newTable, fieldNames, typeNames);

                for (int i = 0; i < tableCount; i++)
                {
                    SerializableTableRow r = JsonConvert.DeserializeObject <SerializableTableRow>(sw.ReadLine());
                    if (r.row >= newTable.capacity)
                    {
                        newTable.SetCapacity(r.row + 1);
                    }
                    r.InsertToTable(newTable);
                }

                tables.Add(newTable.name, newTable);
                yield return(((float)sw.BaseStream.Position) / ((float)sw.BaseStream.Length));
            }

            f.Flush();
            f.Close();
            f.Dispose();

#if PRINT_TIME_DATA
            TimeSpan span = DateTime.Now - startTime;
            Console.WriteLine("Loading " + pFilename + " took " + span.TotalSeconds + " seconds");
#endif
        }
Exemple #9
0
        public RelayTreeRunner(RelayTwo pRelay, string pTableName)
        {
            D.isNull(pRelay);
            if (!pRelay.tables.ContainsKey(pTableName))
            {
                pRelay.CreateTable(pTableName);
            }
            _table = pRelay.GetTable(pTableName);
            List <RelayTreeNode> nodes = InstantiatorTwo.Process <RelayTreeNode>(_table);

            foreach (RelayTreeNode t in nodes)
            {
                D.assert(t.hasSetupCells, "an object of type " + t.GetType().Name + " did not call base method of SetupCells");
                _nodes.Add(t.objectId, t);
                t.SetRunner(this);
            }
            foreach (RelayTreeNode t in nodes)
            {
                t.RestoreRelations();
            }
        }
Exemple #10
0
        public RelayTwo Subset(string pTableName, Func <TableRow, bool> pPredicate)
        {
            RelayTwo relaySubset = new RelayTwo();
            TableTwo tableSubset = relaySubset.CreateTable(pTableName);
            TableTwo sourceTable = tables[pTableName];

            foreach (ITableField f in sourceTable.fields)
            {
                tableSubset.AddField(f.GetEmptyCopy());
            }

            tableSubset.SetCapacity(sourceTable.capacity);

            foreach (TableRow sourceRow in sourceTable.Where(pPredicate))
            {
                SerializableTableRow newRow = sourceRow.GetSerializableTableRow();
                newRow.InsertToTable(tableSubset);
                //Console.WriteLine("added row " + newRow.row);
            }

            return(relaySubset);
        }
Exemple #11
0
        public static List <T> Process <T>(TableTwo pTable, Type[] pSubTypes) where T : RelayObjectTwo
        {
            Type[] subTypes = pSubTypes;
            //int[] objectIds = pTable.GetIndexesOfPopulatedRows();
            List <T> newInstances = new List <T>();

            foreach (TableRow tr in pTable)
            {
                string className = tr.Get <string>(RelayObjectTwo.CSHARP_CLASS_FIELD_NAME);
                Type   type      = GetType(className, subTypes);
                if (type == null)
                {
                    throw new CantFindClassWithNameException("Can't find class with name " + className + " to instantiate");
                }
                T newInstance = Activator.CreateInstance(type) as T;
                D.isNull(newInstance);
                newInstance.LoadFromExistingRelayEntry(pTable, tr.row, className);
                newInstances.Add(newInstance);
            }

            return(newInstances);
        }
Exemple #12
0
        /// <summary>
        /// Append all entries in all tables, source IDs will be discarded.
        /// </summary>
        public void AppendTables(RelayTwo pRelay)
        {
            RelayTwo additiveRelay = pRelay;

            // Cycle through all new tables in the loaded relay
            foreach (String tableName in additiveRelay.tables.Keys)
            {
                TableTwo fromTable = additiveRelay.tables[tableName];
                // If the table already exists, append the entries from the new table.
                if (tables.ContainsKey(tableName))
                {
                    TableTwo toTable = tables[tableName];
                    // Ensure that all fields exists in the old table.
                    foreach (ITableField f in fromTable.fields)
                    {
                        if (!toTable.fieldNames.Contains(f.name))
                        {
                            toTable.AddField(f.GetEmptyCopy());
                        }
                    }
                    foreach (TableRow fromRow in fromTable)
                    {
                        TableRow toRow = toTable.CreateRow();
                        foreach (ITableField fromField in fromTable.fields)
                        {
                            ITableField toField = toTable.GetField(fromField.name);
                            toField.SetValueFromString(toRow.row, fromField.GetValueAsString(fromRow.row));
                        }
                    }
                }
                else
                {
                    this.tables[fromTable.name] = fromTable;
                }
            }
        }
Exemple #13
0
 internal TableRow(TableTwo pTable, int pRow)
 {
     _table = pTable;
     _row   = pRow;
 }
Exemple #14
0
        public void MergeWith(RelayTwo pSource)
        {
            foreach (TableTwo targetTable in tables.Values)
            {
                if (pSource.tables.ContainsKey(targetTable.name))
                {
                    TableTwo sourceTable = pSource.tables[targetTable.name];

                    // Add missing fields to targetTable
                    foreach (ITableField field in sourceTable.fields)
                    {
                        if (!targetTable.fieldNames.Contains(field.name))
                        {
                            targetTable.AddField(field.GetEmptyCopy());
                        }
                    }

                    // Add missing fields to sourceTable
                    foreach (ITableField field in targetTable.fields)
                    {
                        if (!sourceTable.fieldNames.Contains(field.name))
                        {
                            sourceTable.AddField(field.GetEmptyCopy());
                        }
                    }

                    if (sourceTable.capacity > targetTable.capacity)
                    {
                        targetTable.SetCapacity(sourceTable.capacity);
                    }

                    foreach (TableRow r in sourceTable)
                    {
                        SerializableTableRow sr = r.GetSerializableTableRow();

                        if (targetTable.ContainsRow(sr.row))
                        {
                            throw new RelayMergeException("table " + targetTable.name + " does already contain row " + sr.row);
                        }

                        sr.InsertToTable(targetTable);
                    }
                }
            }

            // Copy complete tables from subsetB if they don't exist in this relay
            foreach (TableTwo tableB in pSource.tables.Values)
            {
                if (!tables.ContainsKey(tableB.name))
                {
                    TableTwo newTable = CreateTable(tableB.name);

                    // Add fields to the new table
                    foreach (ITableField field in tableB.fields)
                    {
                        newTable.AddField(field.GetEmptyCopy());
                    }

                    newTable.SetCapacity(tableB.capacity);

                    foreach (TableRow r in tableB)
                    {
                        SerializableTableRow sr = r.GetSerializableTableRow();
                        sr.InsertToTable(newTable);
                    }
                }
            }
        }
Exemple #15
0
 public static List <T> Process <T>(TableTwo pTable) where T : RelayObjectTwo
 {
     return(Process <T>(pTable, typeof(T)));
 }
Exemple #16
0
 public static List <T> Process <T>(TableTwo pTable, Type pType) where T : RelayObjectTwo
 {
     Type[] subTypes = GetSubclasses(pType);
     return(Process <T>(pTable, subTypes));
 }