Ejemplo n.º 1
0
        public static Statement CreateStatement(Insert insert, IEntityMapping mapping)
        {
            var writer = new SqlWriter();
            IDictionary <string, object> parameters = null;

            writer.InsertInto.QuotedName(insert.Into.Name);

            var resultType = Statement.ResultType.None;

            switch (insert.Type)
            {
            case Insert.SetType.Values:
                writer.OpenBlock.Trim().FieldList(x => x.Comma.Flush(), insert.Values.Keys).Trim().CloseBlock.Flush();
                parameters = new Dictionary <string, object>();
                writer.Values.OpenBlock.Trim().ParameterList(x => x.Comma.Flush(), insert.Values.Values.Select(x => parameters.AddWithUniquelyNamedKey(x))).
                Trim().CloseBlock.Flush();
                if (insert.HasIdentityKey)
                {
                    writer.Trim().QuerySeperator.Select.ScopeIdentity(typeof(int));
                    resultType = Statement.ResultType.Scalar;
                }
                break;

            case Insert.SetType.Query:
                var select = SelectWriter <TEntity> .CreateStatement(insert.Query, mapping);

                parameters = select.Parameters;
                writer.OpenBlock.Trim().FieldList(x => x.Comma.Flush(), SelectWriter <TEntity> .BuildProjection(insert.Query, mapping, parameters)).Trim().CloseBlock.Flush();
                writer.Write(select.Text);
                break;
            }

            return(new Statement(writer.ToString(), Statement.StatementType.Text, resultType, parameters));
        }
Ejemplo n.º 2
0
        /// <summary>Read binary relation data from file</summary>
        /// <remarks>
        /// The expected (sparse) line format is:
        /// ENTITY_ID whitespace ENTITY_ID
        /// for the relations that hold.
        /// </remarks>
        /// <param name="reader">a StreamReader to be read from</param>
        /// <param name="mapping">the mapping object for the given entity type</param>
        /// <returns>the relation data</returns>
        public static SparseBooleanMatrix Read(StreamReader reader, IEntityMapping mapping)
        {
            var matrix = new SparseBooleanMatrix();

            char[] split_chars = new char[]{ '\t', ' ' };
            string line;

            while (!reader.EndOfStream)
            {
               	line = reader.ReadLine();

                // ignore empty lines
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(split_chars);

                if (tokens.Length != 2)
                    throw new IOException("Expected exactly two columns: " + line);

                int entity1_id = mapping.ToInternalID(int.Parse(tokens[0]));
                int entity2_id = mapping.ToInternalID(int.Parse(tokens[1]));

               	matrix[entity1_id, entity2_id] = true;
            }

            return matrix;
        }
Ejemplo n.º 3
0
        public IHttpActionResult Put(int id, int mappingid, [IfMatch] ETag etag, [FromBody] Mapping mapping)
        {
            return(WebHandler(() =>
            {
                IEntityMapping returnedMapping = null;

                var request = new AmendMappingRequest
                {
                    EntityId = id,
                    MappingId = mappingid,
                    Mapping = mapping,
                    Version = etag.ToVersion()
                };

                using (var scope = new TransactionScope(TransactionScopeOption.Required, WriteOptions()))
                {
                    returnedMapping = this.service.UpdateMapping(request);
                    scope.Complete();
                }

                if (returnedMapping != null)
                {
                    notificationService.Notify(() => GetContract(id, etag.ToVersion()).Contract, service.ContractVersion,
                                               Operation.Modified);
                    return new StatusCodeResultWithLocation(this.Request, HttpStatusCode.NoContent,
                                                            this.Request.RequestUri.AbsolutePath.Substring(1));
                }

                return NotFound();
            }));
        }
Ejemplo n.º 4
0
        Read(TextReader reader, IEntityMapping user_mapping, IEntityMapping item_mapping)
        {
            var ratings = new Ratings();

            var    split_chars = new char[] { '\t', ' ', ',' };
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.Length == 0)
                {
                    continue;
                }

                string[] tokens = line.Split(split_chars);

                if (tokens.Length < 3)
                {
                    throw new IOException("Expected at least three columns: " + line);
                }

                int    user_id = user_mapping.ToInternalID(int.Parse(tokens[0]));
                int    item_id = item_mapping.ToInternalID(int.Parse(tokens[1]));
                double rating  = double.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            return(ratings);
        }
Ejemplo n.º 5
0
        IDbSet GetDbSet(IEntityMapping entity)
        {
            IDbSet table;

            table = this.CreateDbSet(entity);
            return(table);
        }
Ejemplo n.º 6
0
        /// <summary>Read in implicit feedback data from a TextReader</summary>
        /// <param name="reader">the TextReader to be read from</param>
        /// <param name="user_mapping">user <see cref="IEntityMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IEntityMapping"/> object</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        static public IPosOnlyFeedback Read(TextReader reader, IEntityMapping user_mapping, IEntityMapping item_mapping)
        {
            var feedback = new PosOnlyFeedback <SparseBooleanMatrix>();

            var    split_chars = new char[] { '\t', ' ', ',' };
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.Trim().Length == 0)
                {
                    continue;
                }

                string[] tokens = line.Split(split_chars);

                if (tokens.Length < 2)
                {
                    throw new IOException("Expected at least two columns: " + line);
                }

                int user_id = user_mapping.ToInternalID(int.Parse(tokens[0]));
                int item_id = item_mapping.ToInternalID(int.Parse(tokens[1]));

                feedback.Add(user_id, item_id);
            }

            return(feedback);
        }
Ejemplo n.º 7
0
        /// <inheritdoc />
        public override Uri SelectGraph(EntityId entityId, IEntityMapping entityMapping, IPropertyMapping predicate)
        {
            Uri result;
            var request = (CurrentRequest != null ? CurrentRequest() : null);

            if (request != null)
            {
                IDictionary <EntityId, Uri> requestMap;
                if ((_requestMappings.TryGetValue(request, out requestMap)) && (requestMap.TryGetValue(entityId, out result)))
                {
                    return(result);
                }
            }

            if (Cache.TryGetValue(entityId, out result))
            {
                return(result);
            }

            if (NamedGraph != null)
            {
                return(Cache[entityId] = NamedGraph);
            }

            return(base.SelectGraph(entityId, entityMapping, predicate));
        }
Ejemplo n.º 8
0
        private void BindPocoRepresentation(IEntityMapping classMapping, PersistentClass entity)
        {
            string className = classMapping.Name == null
                                                                ? null
                                                                   : ClassForNameChecked(classMapping.Name, mappings, "persistent class {0} not found").AssemblyQualifiedName;

            entity.ClassName = className;

            if (!string.IsNullOrEmpty(classMapping.Proxy))
            {
                entity.ProxyInterfaceName = ClassForNameChecked(classMapping.Proxy, mappings, "proxy class not found: {0}").AssemblyQualifiedName;
                entity.IsLazy             = true;
            }
            else if (entity.IsLazy)
            {
                entity.ProxyInterfaceName = className;
            }

            HbmTuplizer tuplizer = classMapping.Tuplizers.FirstOrDefault(tp => tp.entitymode == HbmTuplizerEntitymode.Poco);

            if (tuplizer != null)
            {
                string tupClassName = FullQualifiedClassName(tuplizer.@class, mappings);
                entity.AddTuplizer(EntityMode.Poco, tupClassName);
            }
        }
Ejemplo n.º 9
0
        /// <summary>Read in rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <returns>the rating data</returns>
        public static ITimedRatings Read(TextReader reader, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null)
        {
            if (user_mapping == null)
                user_mapping = new IdentityMapping();
            if (item_mapping == null)
                item_mapping = new IdentityMapping();

            var ratings = new TimedRatings();

            string[] separators = { "::" };
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                string[] tokens = line.Split(separators, StringSplitOptions.None);

                if (tokens.Length < 4)
                    throw new FormatException(string.Format("Expected at least 4 columns: {0}", line));

                int user_id = user_mapping.ToInternalID(tokens[0]);
                int item_id = item_mapping.ToInternalID(tokens[1]);
                float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture);
                long seconds = uint.Parse(tokens[3]);

                var time = new DateTime(seconds * 10000000L).AddYears(1969);
                var offset = TimeZone.CurrentTimeZone.GetUtcOffset(time);
                time -= offset;

                ratings.Add(user_id, item_id, rating, time);
            }
            return ratings;
        }
Ejemplo n.º 10
0
        public static string GetInsertSql(this IEntityMapping mapping)
        {
            var properties = mapping.ValueProperties.Where(propertyMapping => !propertyMapping.IsDbGenerated).ToList();

            return
                ($"{SqlTerm.Insert} {SqlTerm.Into} {mapping.TableName} ({string.Join(",", properties.GetColumnNames())}) {SqlTerm.Values} ({string.Join(",", properties.GetPropertyNames())}) ");
        }
Ejemplo n.º 11
0
        internal Expression GetEntityExistsTest(IEntityMapping entity, Expression instance)
        {
            ProjectionExpression tq = this.GetQueryExpression(entity);

            Expression where = this.GetIdentityCheck(tq.Select, entity, instance);
            return(new ExistsExpression(new SelectExpression(new TableAlias(), null, tq.Select, where)));
        }
Ejemplo n.º 12
0
        /// <summary>Read in rating data which will be interpreted as implicit feedback data from a TextReader</summary>
        /// <param name="reader">the TextReader to be read from</param>
        /// <param name="rating_threshold">the minimum rating value needed to be accepted as positive feedback</param>
        /// <param name="user_mapping">user <see cref="IEntityMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IEntityMapping"/> object</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        public static IPosOnlyFeedback Read(TextReader reader, float rating_threshold, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null, bool ignore_first_line = false)
        {
            if (user_mapping == null)
                user_mapping = new IdentityMapping();
            if (item_mapping == null)
                item_mapping = new IdentityMapping();
            if (ignore_first_line)
                reader.ReadLine();

            var feedback = new PosOnlyFeedback<SparseBooleanMatrix>();

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Trim().Length == 0)
                    continue;

                string[] tokens = line.Split(Constants.SPLIT_CHARS);

                if (tokens.Length < 3)
                    throw new FormatException("Expected at least 3 columns: " + line);

                int user_id   = user_mapping.ToInternalID(tokens[0]);
                int item_id   = item_mapping.ToInternalID(tokens[1]);
                float rating  = float.Parse(tokens[2], CultureInfo.InvariantCulture);

                if (rating >= rating_threshold)
                    feedback.Add(user_id, item_id);
            }

            return feedback;
        }
Ejemplo n.º 13
0
 /// <summary>Read binary attribute data from a file</summary>
 /// <remarks>
 /// The expected (sparse) line format is:
 /// ENTITY_ID tab/space/comma ATTRIBUTE_ID
 /// for the relations that hold.
 /// </remarks>
 /// <param name="filename">the name of the file to be read from</param>
 /// <param name="mapping">the mapping object for the given entity type</param>
 /// <returns>the attribute data</returns>
 public static SparseBooleanMatrix Read(string filename, IEntityMapping mapping)
 {
     return Wrap.FormatException<SparseBooleanMatrix>(filename, delegate() {
         using ( var reader = new StreamReader(filename) )
             return Read(reader, mapping);
     });
 }
Ejemplo n.º 14
0
        public EntityContext(
            IEntityContextFactory factory,
            IMappingsRepository mappings,
            IEntityStore entityStore,
            IEntitySource entitySource,
            IBaseUriSelectionPolicy baseUriSelector,
            IRdfTypeCache typeCache,
            IBlankNodeIdGenerator blankIdGenerator,
            IResultTransformerCatalog transformerCatalog,
            IEntityCaster caster,
            IDatasetChangesTracker changeTracker,
            IResourceResolutionStrategy resourceResolutionStrategy,
            ILogger log)
            : this(changeTracker)
        {
            _factory                    = factory;
            _entityStore                = entityStore;
            _entitySource               = entitySource;
            _baseUriSelector            = baseUriSelector;
            _mappings                   = mappings;
            _typeCache                  = typeCache;
            _blankIdGenerator           = blankIdGenerator;
            _transformerCatalog         = transformerCatalog;
            _caster                     = caster;
            _typedEntityMapping         = _mappings.MappingFor <ITypedEntity>();
            _typesPropertyMapping       = _typedEntityMapping.PropertyFor("Types");
            _resourceResolutionStrategy = resourceResolutionStrategy;
            _log = log;

            if (_baseUriSelector == null)
            {
                _log.Warning("No Base URI Selection Policy. It will not be possible to use relative URIs");
            }
        }
        public ColumnSetStatementCollectionBuilder(object entity, IEntityMapping entityMapping, SqlParameterCollection updateParameters)
        {
            _entity        = entity;
            _entityMapping = entityMapping;

            Parameters = updateParameters;
        }
Ejemplo n.º 16
0
 /// <inheritdoc />
 public void Visit(IEntityMapping entityMapping)
 {
     if (!entityMapping.EntityType.GetTypeInfo().Assembly.IsDynamic)
     {
         _mappingAssemblies.Add(entityMapping.EntityType.GetTypeInfo().Assembly);
     }
 }
Ejemplo n.º 17
0
 public static ISqlCommandMapping LinqToSqlWhere <T>(this IEntityMapping mapping,
                                                     Expression <Func <T, bool> > expression)
 {
     return(mapping.LinqToSqlWhere(new List <Expression <Func <T, bool> > > {
         expression
     }));
 }
Ejemplo n.º 18
0
        public MemberMapping(MemberInfo memberInfo, MemberAttribute memberAttribute, IEntityMapping entityMapping)
        {
            this.entity = entityMapping;

            this.memberInfo = memberInfo;

            this.memberAttribute = memberAttribute;

            this.memberType = memberInfo.GetMemberType();

            if (memberAttribute is ColumnAttribute)
            {
                this.InitializeColumnAttributeMapping((ColumnAttribute)memberAttribute);
            }
            else if (memberAttribute is AssociationAttribute)
            {
                var isEnumerableType = memberType != typeof(string) &&
                                       memberType != typeof(byte[]) &&
                                       typeof(IEnumerable).IsAssignableFrom(memberType);

                this.isRelationship = true;

                this.InitializeAssociationAttributeMapping((AssociationAttribute)memberAttribute, isEnumerableType);
            }
        }
Ejemplo n.º 19
0
        public static ISqlCommandMapping LinqToSqlWhere <T>(this IEntityMapping mapping,
                                                            IEnumerable <Expression <Func <T, bool> > > expressionList)
        {
            var sqlMapping = new SqlCommandMapping
            {
                CommandText = string.Empty,
                Parameters  = new Dictionary <string, object>()
            };

            foreach (var expression in expressionList)
            {
                var expressionMap = expression.GetWhereMapping();
                var property      = mapping.GetPropertyByName(expressionMap.PropertyName);

                sqlMapping.Parameters.Add($"@{expressionMap.PropertyName}", expressionMap.Value);

                if (sqlMapping.CommandText.Contains(SqlTerm.Where))
                {
                    sqlMapping.CommandText +=
                        $"{SqlTerm.And} {property.ColumnName} {expressionMap.Operator} :{expressionMap.PropertyName} ";
                }
                else
                {
                    sqlMapping.CommandText +=
                        $"{SqlTerm.Where} {property.ColumnName} {expressionMap.Operator} :{expressionMap.PropertyName} ";
                }
            }

            return(sqlMapping);
        }
Ejemplo n.º 20
0
        public static Statement CreateStatement(Delete delete, IEntityMapping mapping)
        {
            Statement statement = null;
            var       writer    = SqlWriter.CreateWriter().Delete;

            if (!delete.AllowMultiple)
            {
                writer.Top(1);
            }

            writer.From.QuotedName(delete.Table.Name);

            switch (delete.Filter)
            {
            case Delete.FilterType.Where:
                statement = WhereWriter <TEntity> .CreateStatement(delete.Where, mapping);

                writer.Where.Write(statement.Text);
                break;

            case Delete.FilterType.Select:
                var keyColumn = mapping.Key.GetColumnName();
                statement = SelectWriter <TEntity> .CreateStatement(delete.Select, mapping);

                writer.Where.Exists.OpenBlock.Trim().Select.QuotedName(keyColumn).From.OpenBlock.Trim().
                Write(statement.Text).Trim().CloseBlock.As.SubQueryAlias.Where.SubQueryColumn(keyColumn).Equal.QuotedName(delete.Table.Name, keyColumn).Trim().CloseBlock.Flush();
                break;
            }

            return(new Statement(writer.ToString(), Statement.StatementType.Text,
                                 Statement.ResultType.None, statement.Parameters));
        }
Ejemplo n.º 21
0
 IDbSet CreateDbSet(IEntityMapping entity)
 {
     return(typeof(DbSet <>)
            .MakeGenericType(entity.EntityType)
            .GetConstructor(new Type[] { typeof(InternalDbContext), typeof(IEntityMapping) })
            .FastInvoke(this, entity) as IDbSet);
 }
Ejemplo n.º 22
0
        public MemberMapping(MemberInfo memberInfo,  MemberAttribute memberAttribute, IEntityMapping entityMapping)
        {
            this.entity = entityMapping;

            this.memberInfo = memberInfo;

            this.memberAttribute = memberAttribute;

            this.memberType = memberInfo.GetMemberType();

            if (memberAttribute is ColumnAttribute)
            {
                this.InitializeColumnAttributeMapping((ColumnAttribute)memberAttribute);
            }
            else if (memberAttribute is AssociationAttribute)
            {
                var isEnumerableType = memberType != typeof(string) &&
                                       memberType != typeof(byte[]) &&
                                       typeof(IEnumerable).IsAssignableFrom(memberType);

                this.isRelationship = true;

                this.InitializeAssociationAttributeMapping((AssociationAttribute)memberAttribute, isEnumerableType);
            }
        }
        public UpdateStatementBuilder(object entity, IEntityMapping entityMapping)
        {
            _entity        = entity;
            _entityMapping = entityMapping;

            _parameters = new SqlParameterCollection();
        }
Ejemplo n.º 24
0
            public Uri SelectGraph(EntityId id, IEntityMapping entityMapping, IPropertyMapping predicate)
            {
                string replacementProtocol = "http";

                if (predicate != null && predicate.Uri != null)
                {
                    switch (predicate.Uri.ToString())
                    {
                    case "http://xmlns.com/foaf/0.1/familyName":
                    case "http://xmlns.com/foaf/0.1/givenName":
                        replacementProtocol = "personal";
                        break;

                    case "http://xmlns.com/foaf/0.1/knows":
                        replacementProtocol = "friendsOf";
                        break;

                    case "http://xmlns.com/foaf/0.1/homePage":
                    case "http://xmlns.com/foaf/0.1/interest":
                        replacementProtocol = "interestsOf";
                        break;
                    }
                }

                return(new Uri(id.ToString().Replace("http", replacementProtocol)));
            }
Ejemplo n.º 25
0
        private static string BuildMergeInsertSet(this IEntityMapping mapping)
        {
            var command    = new StringBuilder();
            var columns    = new List <string>();
            var values     = new List <string>();
            var properties = mapping.Properties
                             .Where(propertyMapping => !propertyMapping.IsDbGenerated)
                             .ToList();

            command.Append(" WHEN NOT MATCHED BY TARGET THEN INSERT ");
            if (properties.Any())
            {
                foreach (var column in properties)
                {
                    columns.Add($"[{column.ColumnName}]");
                    values.Add($"[{Source}].[{column.ColumnName}]");
                }

                command.Append($"({string.Join(", ", columns)}) VALUES ({string.Join(", ", values)})");
            }
            else
            {
                command.Append("DEFAULT VALUES");
            }

            return(command.ToString());
        }
Ejemplo n.º 26
0
        /// <summary>
        /// </summary>
        /// <param name="mapping"></param>
        /// <param name="tableName"></param>
        /// <param name="operationType"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        internal static string BuildStagingTableCommand(this IEntityMapping mapping, string tableName, Operation operationType,
                                                        BulkOptions options)
        {
            var paramList = mapping
                            .GetPropertiesByOperation(operationType)
                            .ToList();

            if (paramList.All(s => s.IsPk && s.IsDbGenerated) &&
                operationType == Operation.Update)
            {
                return(null);
            }

            var paramColumns = paramList
                               .Select(column => $"{Source}.[{column.ColumnName}]")
                               .ToList();

            if (mapping.WillOutputGeneratedValues(options))
            {
                paramColumns.Add($"1 as [{Identity}]");
            }

            var paramListConcatenated = string.Join(", ", paramColumns);

            return($"SELECT TOP 0 {paramListConcatenated} INTO {tableName} FROM {mapping.FullTableName} AS A " +
                   $"LEFT JOIN {mapping.FullTableName} AS {Source} ON 1 = 2");
        }
Ejemplo n.º 27
0
        /// <summary>Read in rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(TextReader reader, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null, bool ignore_first_line = false)
        {
            if (user_mapping == null)
                user_mapping = new IdentityMapping();
            if (item_mapping == null)
                item_mapping = new IdentityMapping();
            if (ignore_first_line)
                reader.ReadLine();

            var ratings = new Ratings();

            string line;
            while ( (line = reader.ReadLine()) != null )
            {
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(Constants.SPLIT_CHARS);

                if (tokens.Length < 3)
                    throw new FormatException("Expected at least 3 columns: " + line);

                int user_id = user_mapping.ToInternalID(tokens[0]);
                int item_id = item_mapping.ToInternalID(tokens[1]);
                float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            return ratings;
        }
Ejemplo n.º 28
0
        /// <summary>Read in static rating data from a file</summary>
        /// <param name="filename">the name of the file to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="rating_type">the data type to be used for storing the ratings</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(
            string filename,
			IEntityMapping user_mapping = null, IEntityMapping item_mapping = null,
			RatingType rating_type = RatingType.FLOAT,
			bool ignore_first_line = false)
        {
            string binary_filename = filename + ".bin.StaticRatings";
            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
                return (IRatings) FileSerializer.Deserialize(binary_filename);

            int size = 0;
            using ( var reader = new StreamReader(filename) )
                while (reader.ReadLine() != null)
                    size++;
            if (ignore_first_line)
                size--;

            return Wrap.FormatException<IRatings>(filename, delegate() {
                using ( var reader = new StreamReader(filename) )
                {
                    var ratings = (StaticRatings) Read(reader, size, user_mapping, item_mapping, rating_type);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                        ratings.Serialize(binary_filename);
                    return ratings;
                }
            });
        }
Ejemplo n.º 29
0
        public void ProcessFiles()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(ConfigurationManager.AppSettings[CSVConstants.FilePathLookup]);

            FileInfo[] files = directoryInfo.GetFiles(CSVConstants.FileExtension);
            foreach (var file in files)
            {
                if (this.fileTypeMapperDictionary.ContainsKey(Path.GetFileNameWithoutExtension(file.Name)))
                {
                    string        fileTypeClass    = this.fileTypeMapperDictionary[Path.GetFileNameWithoutExtension(file.Name)];
                    ErrorInfo[]   errorInfo        = null;
                    List <object> processedRecords = this.businessProcessor.ProcessCSVData(fileTypeClass, file.FullName, out errorInfo);

                    //Creating Instance using Factory Pattern
                    IEntityMapping entity = ObjectFactory.CreateFactoryInstance(fileTypeClass);

                    //Processing records for DB insert. Only at run time, the corresponding class is known - Order or Customer
                    entity.CreateEntityMapping(processedRecords);
                }
                else
                {
                    throw new Exception("Invalid FileName. Not found in repository");
                }
            }

            //Fetch Order and Customer mapping records from DB
            IEnumerable <CustomerOrder> dataset = edr.GetCustomerOrderData();
        }
Ejemplo n.º 30
0
        /// <summary>Read in implicit feedback data from a TextReader</summary>
        /// <param name="reader">the TextReader to be read from</param>
        /// <param name="user_mapping">user <see cref="IEntityMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IEntityMapping"/> object</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        public static IPosOnlyFeedback Read(TextReader reader, IEntityMapping user_mapping, IEntityMapping item_mapping)
        {
            var feedback = new PosOnlyFeedback<SparseBooleanMatrix>();

            var split_chars = new char[]{ '\t', ' ', ',' };
            string line;

            while ( (line = reader.ReadLine()) != null )
            {
                if (line.Trim().Length == 0)
                    continue;

                string[] tokens = line.Split(split_chars);

                if (tokens.Length < 2)
                    throw new IOException("Expected at least two columns: " + line);

                int user_id = user_mapping.ToInternalID(int.Parse(tokens[0]));
                int item_id = item_mapping.ToInternalID(int.Parse(tokens[1]));

               	feedback.Add(user_id, item_id);
            }

            return feedback;
        }
Ejemplo n.º 31
0
        private void InitializeAssociationAttributeMapping(AssociationAttribute association, bool isEnumerableType)
        {
            this.isForeignKey = association.IsForeignKey;

            if (this.isForeignKey ||
                association is ManyToOneAttribute)
            {
                this.isManyToOne = true;
            }

            if (isEnumerableType)
            {
                this.relatedType = ReflectionHelper.GetElementType(this.MemberType);
            }
            else
            {
                this.relatedType = this.memberType;
            }

            var tableAttribute = this.relatedType.GetCustomAttributes <TableAttribute>().FirstOrDefault();

            string thisKey  = association.ThisKey;
            string otherKey = association.OtherKey;

            this.relatedEntity = new EntityMapping(tableAttribute.Name, this.relatedType, null);

            this.thisKeyMembers = thisKey.Split(separators).Select(m => GetMemberMapping(this.Entity.EntityType, m, this.entity)).ToArray();

            this.otherKeyMembers = this.relatedEntity.PrimaryKeys;
        }
Ejemplo n.º 32
0
        /// <summary>Read binary attribute data from a StreamReader</summary>
        /// <param name="reader">a StreamReader to be read from</param>
        /// <param name="mapping">the mapping object for the given entity type</param>
        /// <returns>the attribute data</returns>
        static public SparseBooleanMatrix Read(StreamReader reader, IEntityMapping mapping)
        {
            var matrix = new SparseBooleanMatrix();

            var    split_chars = new char[] { '\t', ' ' };
            string line;

            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();

                // ignore empty lines
                if (line.Length == 0)
                {
                    continue;
                }

                string[] tokens = line.Split(split_chars);

                if (tokens.Length != 2)
                {
                    throw new IOException("Expected exactly two columns: " + line);
                }

                int entity_id = mapping.ToInternalID(int.Parse(tokens[0]));
                int attr_id   = int.Parse(tokens[1]);

                matrix[entity_id, attr_id] = true;
            }

            return(matrix);
        }
Ejemplo n.º 33
0
        private static string BuildWhereClause(Select select, IEntityMapping mapping, IDictionary <string, object> parameters)
        {
            if (!select.HasWhere && !select.HasSetOperations)
            {
                return(null);
            }

            var writer = new SqlWriter();

            if (select.HasWhere)
            {
                writer.Write(BuildOperators(select.Where, mapping, parameters));
            }

            if (select.HasSetOperations)
            {
                foreach (var setOperation in select.SetOperatons)
                {
                    var statement = CreateStatement(setOperation.Select, mapping);
                    parameters.AddRange(statement.Parameters);
                    if (!writer.Empty)
                    {
                        writer.And.Flush();
                    }
                    if (setOperation.Type == SetOperation.OperationType.Compliment)
                    {
                        writer.Not.Flush();
                    }
                    writer.Exists.OpenBlock.Trim().Write(statement.Text).Trim().CloseBlock.Flush();
                }
            }

            return(writer.ToString());
        }
Ejemplo n.º 34
0
        private static string BuildProjection(Projection projection, IEntityMapping mapping, IDictionary <string, object> parameters)
        {
            var projectionStatement = ProjectionWriter <TEntity> .CreateStatement(projection, mapping);

            parameters.AddRange(projectionStatement.Parameters);
            return(projectionStatement.Text);
        }
Ejemplo n.º 35
0
        /// <summary>Read in static rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="size">the number of ratings in the file</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="rating_type">the data type to be used for storing the ratings</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(TextReader reader, int size,
            IEntityMapping user_mapping, IEntityMapping item_mapping,
            RatingType rating_type)
        {
            IRatings ratings;
            if (rating_type == RatingType.BYTE)
                ratings = new StaticByteRatings(size);
            else if (rating_type == RatingType.FLOAT)
                ratings = new StaticFloatRatings(size);
            else
                ratings = new StaticRatings(size);

            var split_chars = new char[]{ '\t', ' ', ',' };
            string line;

            while ( (line = reader.ReadLine()) != null )
            {
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(split_chars);

                if (tokens.Length < 3)
                    throw new IOException("Expected at least three columns: " + line);

                int user_id = user_mapping.ToInternalID(int.Parse(tokens[0]));
                int item_id = item_mapping.ToInternalID(int.Parse(tokens[1]));
                double rating = double.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            return ratings;
        }
Ejemplo n.º 36
0
        internal static List <T> Execute(string tableName, IEntityMapping <T> entityMapping)
        {
            StringBuilder query = new StringBuilder();

            query.Append($"SELECT * FROM {tableName}");

            return(SqlCommandExecuteQueryForSelect <T> .Execute(query.ToString(), entityMapping));
        }
Ejemplo n.º 37
0
 protected override Expression VisitEntity(EntityExpression entity)
 {
     if (this.entity == null)
     {
         this.entity = entity.Entity;
     }
     return(entity);
 }
Ejemplo n.º 38
0
 /// <summary>Read in implicit feedback data from a file</summary>
 /// <param name="filename">name of the file to be read from, "-" if STDIN</param>
 /// <param name="user_mapping">user <see cref="IEntityMapping"/> object</param>
 /// <param name="item_mapping">item <see cref="IEntityMapping"/> object</param>
 /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
 public static IPosOnlyFeedback Read(string filename, IEntityMapping user_mapping, IEntityMapping item_mapping)
 {
     if (filename.Equals("-"))
         return Read(Console.In, user_mapping, item_mapping);
     else
         using ( var reader = new StreamReader(filename) )
             return Read(reader, user_mapping, item_mapping);
 }
Ejemplo n.º 39
0
 public void Init()
 {
     for (int i = 0; i < _entityMappings.Count; i++)
     {
         IEntityMapping entityMapping = _entityMappings[i];
         entityMapping.Map();
     }
 }
Ejemplo n.º 40
0
        private IMemberMapping GetMemberMapping(Type type, string memberName, IEntityMapping entity)
        {
            MemberInfo member = type.GetMember(memberName).FirstOrDefault();

            ColumnAttribute column = member.GetCustomAttributes <ColumnAttribute>().FirstOrDefault();

            return(new MemberMapping(member, column, entity));
        }
Ejemplo n.º 41
0
        public virtual Expression BuildEntityExpression(IEntityMapping mapping, IList<EntityAssignment> assignments)
        {
            NewExpression newExpression;

            // handle cases where members are not directly assignable
            EntityAssignment[] readonlyMembers = assignments.Where(b => b.MemberMapping != null)
                .Where(b => (b.MemberMapping as MemberMapping).setter == null)
                .ToArray();
            ConstructorInfo[] cons = mapping.EntityType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
            bool hasNoArgConstructor = cons.Any(c => c.GetParameters().Length == 0);

            if (readonlyMembers.Length > 0 || !hasNoArgConstructor)
            {
                var consThatApply = cons
                                        .Select(c => this.BindConstructor(c, readonlyMembers))
                                        .Where(cbr => cbr != null && cbr.Remaining.Length == 0)
                                        .ToList();
                if (consThatApply.Count == 0)
                    throw new InvalidOperationException(string.Format(Res.ConstructTypeInvalid, mapping.EntityType));

                // just use the first one... (Note: need better algorithm. :-)
                if (readonlyMembers.Length == assignments.Count)
                    return consThatApply[0].Expression;
                var r = this.BindConstructor(consThatApply[0].Expression.Constructor, assignments);

                newExpression = r.Expression;
                assignments = r.Remaining;
            }
            else
                newExpression = Expression.New(mapping.EntityType);

            Expression result;
            if (assignments.Count > 0)
            {
                if (mapping.EntityType.IsInterface)
                    assignments = this.MapAssignments(assignments, mapping.EntityType).ToList();
                var memberBindings = new List<MemberBinding>();
                foreach (var a in assignments)
                {
                    try
                    {
                        memberBindings.Add(Expression.Bind(a.Member, a.Expression));
                    }
                    catch
                    {
                        throw;
                    }
                }
                result = Expression.MemberInit(newExpression, memberBindings.ToArray());
            }
            else
                result = newExpression;

            if (mapping.EntityType != mapping.EntityType)
                result = Expression.Convert(result, mapping.EntityType);
            return result;
        }
        public Uri SelectGraph(EntityId entityId, IEntityMapping entityMapping, IPropertyMapping predicate)
        {
            EntityId nonBlankId = entityId;
            if (nonBlankId is BlankId)
            {
                nonBlankId = ((BlankId)nonBlankId).RootEntityId;
            }

            return new Uri(System.Text.RegularExpressions.Regex.Replace((nonBlankId != null ? nonBlankId.Uri.AbsoluteUri : ((BlankId)entityId).Graph.AbsoluteUri), "((?<!data.)magi)", "data.magi"));
        }
Ejemplo n.º 43
0
        /// <summary>
        /// Sets the currently processed enitty type
        /// and updates inheritance cache
        /// </summary>
        public void Visit(IEntityMapping entityMapping)
        {
            if (!_classMappings.ContainsKey(entityMapping.EntityType))
            {
                _classMappings.Add(entityMapping.EntityType, new List<IClassMapping>());
            }

            AddAsChildOfParentTypes(entityMapping.EntityType);

            _currentClasses = _classMappings[entityMapping.EntityType];
        }
Ejemplo n.º 44
0
        /// <summary>Read in static rating data from a file</summary>
        /// <param name="filename">the name of the file to read from, "-" if STDIN</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="rating_type">the data type to be used for storing the ratings</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(string filename,
            IEntityMapping user_mapping, IEntityMapping item_mapping,
            RatingType rating_type)
        {
            int size = 0;
            using ( var reader = new StreamReader(filename) )
                while (reader.ReadLine() != null)
                    size++;

            using ( var reader = new StreamReader(filename) )
                return Read(reader, size, user_mapping, item_mapping, rating_type);
        }
Ejemplo n.º 45
0
        /// <summary>Write item predictions (scores) to a file</summary>
        /// <param name="recommender">the <see cref="IRecommender"/> to use for making the predictions</param>
        /// <param name="train">a user-wise <see cref="IPosOnlyFeedback"/> containing the items already observed</param>
        /// <param name="candidate_items">list of candidate items</param>
        /// <param name="num_predictions">number of items to return per user, -1 if there should be no limit</param>
        /// <param name="filename">the name of the file to write to</param>
        /// <param name="users">a list of users to make recommendations for</param>
        /// <param name="user_mapping">an <see cref="IEntityMapping"/> object for the user IDs</param>
        /// <param name="item_mapping">an <see cref="IEntityMapping"/> object for the item IDs</param>
        public static void WritePredictions(
			this IRecommender recommender,
			IPosOnlyFeedback train,
			System.Collections.Generic.IList<int> candidate_items,
			int num_predictions,
			string filename,
			System.Collections.Generic.IList<int> users = null,
			IEntityMapping user_mapping = null, IEntityMapping item_mapping = null)
        {
            using (var writer = new StreamWriter(filename))
                WritePredictions(recommender, train, candidate_items, num_predictions, writer, users, user_mapping, item_mapping);
        }
        public Uri SelectGraph(EntityId entityId, IEntityMapping entityMapping, IPropertyMapping predicate)
        {
            if (entityId is BlankId)
            {
                EntityId nonBlankId = ((BlankId)entityId).RootEntityId;
                if (nonBlankId != null)
                {
                    entityId = nonBlankId;
                }
            }

            return entityId.Uri;
        }
Ejemplo n.º 47
0
        /// <summary>Rate a given set of instances and write it to a file</summary>
        /// <param name="recommender">rating predictor</param>
        /// <param name="ratings">test cases</param>
        /// <param name="user_mapping">an <see cref="EntityMapping"/> object for the user IDs</param>
        /// <param name="item_mapping">an <see cref="EntityMapping"/> object for the item IDs</param>
        /// <param name="line_format">a format string specifying the line format; {0} is the user ID, {1} the item ID, {2} the rating</param>
        /// <param name="filename">the name of the file to write the predictions to</param>
        public static void WritePredictions(
			IRatingPredictor recommender,
			IRatings ratings,
			IEntityMapping user_mapping, IEntityMapping item_mapping,
		    string line_format,
			string filename)
        {
            if (filename.Equals("-"))
                WritePredictions(recommender, ratings, user_mapping, item_mapping, line_format, Console.Out);
            else
                using ( var writer = new StreamWriter(filename) )
                    WritePredictions(recommender, ratings, user_mapping, item_mapping, line_format, writer);
        }
Ejemplo n.º 48
0
 protected override string[] BuildSchemaScript(IEntityMapping[] mappings)
 {
     return new string[]{
         mappings
              .Where(p => p.Schema.HasValue()
                  && p.Schema.Trim().ToUpper() != "DBO"
                  && p.Schema.Trim().ToUpper() != "[DBO]")
              .Select(p => p.Schema.Trim().ToLower())
              .Distinct()
              .Select(p => string.Format("{0} CREATE SCHEMA {1}{0} ", Environment.NewLine, p))
              .ToCSV(";")
     };
 }
Ejemplo n.º 49
0
        // TODO as soon as we drop support for Mono 2.6, use default arguments
        /// <summary>Rate a given set of instances and write it to a TextWriter</summary>
        /// <param name="recommender">rating predictor</param>
        /// <param name="ratings">test cases</param>
        /// <param name="user_mapping">an <see cref="EntityMapping"/> object for the user IDs</param>
        /// <param name="item_mapping">an <see cref="EntityMapping"/> object for the item IDs</param>
        /// <param name="line_format">a format string specifying the line format; {0} is the user ID, {1} the item ID, {2} the rating</param>
        /// <param name="writer">the TextWriter to write the predictions to</param>
        public static void WritePredictions(
			IRatingPredictor recommender,
			IRatings ratings,
			IEntityMapping user_mapping, IEntityMapping item_mapping,
		    string line_format,
			TextWriter writer)
        {
            for (int index = 0; index < ratings.Count; index++)
                writer.WriteLine(line_format,
                                 user_mapping.ToOriginalID(ratings.Users[index]),
                                 item_mapping.ToOriginalID(ratings.Items[index]),
                                 recommender.Predict(ratings.Users[index], ratings.Items[index]).ToString(CultureInfo.InvariantCulture));
        }
Ejemplo n.º 50
0
        /// <summary>Read in rating data from a file</summary>
        /// <param name="filename">the name of the file to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(string filename, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null, bool ignore_first_line = false)
        {
            if (!(user_mapping is EntityMapping) && !(item_mapping is EntityMapping) && File.Exists(filename + ".bin.Ratings"))
                return (IRatings) FileSerializer.Deserialize(filename + ".bin.Ratings");

            return Wrap.FormatException<IRatings>(filename, delegate() {
                using ( var reader = new StreamReader(filename) )
                {
                    var ratings = (Ratings) Read(reader, user_mapping, item_mapping);
                    if (!(user_mapping is EntityMapping) && !(item_mapping is EntityMapping))
                        ratings.Serialize(filename + ".bin.Ratings");
                    return ratings;
                }
            });
        }
		private void BindPersistentClassCommonValues(IEntityMapping classMapping, PersistentClass model, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			// DISCRIMINATOR
			var discriminable = classMapping as IEntityDiscriminableMapping;
			model.DiscriminatorValue = (discriminable == null || string.IsNullOrEmpty(discriminable.DiscriminatorValue)) ? model.EntityName : discriminable.DiscriminatorValue;

			// DYNAMIC UPDATE
			model.DynamicUpdate = classMapping.DynamicUpdate;

			// DYNAMIC INSERT
			model.DynamicInsert = classMapping.DynamicInsert;

			// IMPORT
			// For entities, the EntityName is the key to find a persister
			// NH Different behavior: we are using the association between EntityName and its more certain implementation (AssemblyQualifiedName)
			// Dynamic entities have no class, reverts to EntityName. -AK
			string qualifiedName = model.MappedClass == null ? model.EntityName : model.MappedClass.AssemblyQualifiedName;
			mappings.AddImport(qualifiedName, model.EntityName);
			if (mappings.IsAutoImport && model.EntityName.IndexOf('.') > 0)
				mappings.AddImport(qualifiedName, StringHelper.Unqualify(model.EntityName));

			// BATCH SIZE
			if (classMapping.BatchSize.HasValue)
				model.BatchSize = classMapping.BatchSize.Value;

			// SELECT BEFORE UPDATE
			model.SelectBeforeUpdate = classMapping.SelectBeforeUpdate;

			// META ATTRIBUTES
			model.MetaAttributes = GetMetas(classMapping, inheritedMetas);

			// PERSISTER
			if(!string.IsNullOrEmpty(classMapping.Persister))
				model.EntityPersisterClass = ClassForNameChecked(classMapping.Persister, mappings,
				                                                 "could not instantiate persister class: {0}");

			// CUSTOM SQL
			HandleCustomSQL(classMapping, model);
			if (classMapping.SqlLoader != null)
				model.LoaderName = classMapping.SqlLoader.queryref;

			foreach (var synchronize in classMapping.Synchronize)
			{
				model.AddSynchronizedTable(synchronize.table);
			}

			model.IsAbstract = classMapping.IsAbstract;
		}
Ejemplo n.º 52
0
        /// <summary>Read in rating data which will be interpreted as implicit feedback data from a file</summary>
        /// <param name="filename">name of the file to be read from</param>
        /// <param name="rating_threshold">the minimum rating value needed to be accepted as positive feedback</param>
        /// <param name="user_mapping">user <see cref="IEntityMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IEntityMapping"/> object</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        public static IPosOnlyFeedback Read(string filename, float rating_threshold, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null, bool ignore_first_line = false)
        {
            string binary_filename = string.Format(CultureInfo.InvariantCulture, "{0}.bin.PosOnlyFeedbackThreshold-{1}", filename, rating_threshold);
            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
                return (IPosOnlyFeedback) FileSerializer.Deserialize(binary_filename);

            return Wrap.FormatException<IPosOnlyFeedback>(filename, delegate() {
                using ( var reader = new StreamReader(filename) )
                {
                    var feedback_data = (ISerializable) Read(reader, rating_threshold, user_mapping, item_mapping);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                        feedback_data.Serialize(binary_filename);
                    return (IPosOnlyFeedback) feedback_data;
                }
            });
        }
Ejemplo n.º 53
0
        /// <summary>Read in rating data from a file</summary>
        /// <param name="filename">the name of the file to read from, "-" if STDIN</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <returns>the rating data</returns>
        public static ITimedRatings Read(string filename, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null)
        {
            string binary_filename = filename + ".bin.TimedRatings";
            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
                return (ITimedRatings) FileSerializer.Deserialize(binary_filename);

            return Wrap.FormatException<ITimedRatings>(filename, delegate() {
                using ( var reader = new StreamReader(filename) )
                {
                    var ratings = (TimedRatings) Read(reader, user_mapping, item_mapping);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                        ratings.Serialize(binary_filename);
                    return ratings;
                }
            });
        }
Ejemplo n.º 54
0
        /// <summary>Read in implicit feedback data from a file</summary>
        /// <param name="filename">name of the file to be read from</param>
        /// <param name="user_mapping">user <see cref="IEntityMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IEntityMapping"/> object</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        public static IPosOnlyFeedback Read(string filename, IEntityMapping user_mapping = null, IEntityMapping item_mapping = null, bool ignore_first_line = false)
        {
            string binary_filename = filename + ".bin.PosOnlyFeedback";
            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
                return (IPosOnlyFeedback) FileSerializer.Deserialize(binary_filename);

            return Wrap.FormatException<IPosOnlyFeedback>(filename, delegate() {
                using ( var reader = new StreamReader(filename) )
                {
                    var feedback_data = (ISerializable) Read(reader, user_mapping, item_mapping);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                        feedback_data.Serialize(binary_filename);
                    return (IPosOnlyFeedback) feedback_data;
                }
            });
        }
Ejemplo n.º 55
0
        protected override List<ColumnAssignment> GetInsertColumnAssignments(Expression table, Expression instance, IEntityMapping entity, Func<IMemberMapping, bool> fnIncludeColumn)
        {
            var items = base.GetInsertColumnAssignments(table, instance, entity, fnIncludeColumn);
            var pk = entity.PrimaryKeys.FirstOrDefault(p => p.IsGenerated);
            if (pk != null)
            {
                var sequenceName = string.IsNullOrEmpty(pk.SequenceName) ? "NEXTID" : pk.SequenceName;

                var ca = new ColumnAssignment(
                                (ColumnExpression)this.GetMemberExpression(table, entity, pk.Member),
                                 new FunctionExpression(pk.MemberType, sequenceName + ".NEXTVAL", null)
                                );
                items.Add(ca);
            }
            return items;
        }
Ejemplo n.º 56
0
        /// <summary>Write item predictions (scores) to a TextWriter object</summary>
        /// <param name="recommender">the <see cref="IRecommender"/> to use for making the predictions</param>
        /// <param name="train">a user-wise <see cref="IPosOnlyFeedback"/> containing the items already observed</param>
        /// <param name="candidate_items">list of candidate items</param>
        /// <param name="num_predictions">number of items to return per user, -1 if there should be no limit</param>
        /// <param name="writer">the <see cref="TextWriter"/> to write to</param>
        /// <param name="users">a list of users to make recommendations for; if null, all users in train will be provided with recommendations</param>
        /// <param name="user_mapping">an <see cref="IEntityMapping"/> object for the user IDs</param>
        /// <param name="item_mapping">an <see cref="IEntityMapping"/> object for the item IDs</param>
        public static void WritePredictions(
			this IRecommender recommender,
			IPosOnlyFeedback train,
			System.Collections.Generic.IList<int> candidate_items,
			int num_predictions,
			TextWriter writer,
			System.Collections.Generic.IList<int> users = null,
			IEntityMapping user_mapping = null, IEntityMapping item_mapping = null)
        {
            if (users == null)
                users = new List<int>(train.AllUsers);

            foreach (int user_id in users)
            {
                var ignore_items = train.UserMatrix[user_id];
                WritePredictions(recommender, user_id, candidate_items, ignore_items, num_predictions, writer, user_mapping, item_mapping);
            }
        }
Ejemplo n.º 57
0
		protected void BindClass(IEntityMapping classMapping, PersistentClass model, IDictionary<string, MetaAttribute> inheritedMetas)
		{
			// handle the lazy attribute
			// go ahead and set the lazy here, since pojo.proxy can override it.
			model.IsLazy = classMapping.UseLazy.HasValue ? classMapping.UseLazy.Value : mappings.DefaultLazy;

			// transfer an explicitly defined entity name
			string entityName = classMapping.EntityName ??
								ClassForNameChecked(classMapping.Name, mappings, "persistent class {0} not found").FullName;
			if (entityName == null)
				throw new MappingException("Unable to determine entity name");
			model.EntityName = entityName;

			BindPocoRepresentation(classMapping, model);
			BindXmlRepresentation(classMapping, model);
			BindMapRepresentation(classMapping, model);

			BindPersistentClassCommonValues(classMapping, model, inheritedMetas);
		}
Ejemplo n.º 58
0
        /// <summary>Read in static rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="size">the number of ratings in the file</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="rating_type">the data type to be used for storing the ratings</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(
            TextReader reader, int size,
			IEntityMapping user_mapping = null, IEntityMapping item_mapping = null,
			RatingType rating_type = RatingType.FLOAT,
			bool ignore_first_line = false)
        {
            if (user_mapping == null)
                user_mapping = new IdentityMapping();
            if (item_mapping == null)
                item_mapping = new IdentityMapping();
            if (ignore_first_line)
                reader.ReadLine();

            IRatings ratings;
            if (rating_type == RatingType.BYTE)
                ratings = new StaticByteRatings(size);
            else if (rating_type == RatingType.FLOAT)
                ratings = new StaticRatings(size);
            else
                throw new FormatException(string.Format("Unknown rating type: {0}", rating_type));

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(Constants.SPLIT_CHARS);

                if (tokens.Length < 3)
                    throw new FormatException("Expected at least 3 columns: " + line);

                int user_id = user_mapping.ToInternalID(tokens[0]);
                int item_id = item_mapping.ToInternalID(tokens[1]);
                float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            ratings.InitScale();
            return ratings;
        }
Ejemplo n.º 59
0
        /// <summary>Read in rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(TextReader reader,	IEntityMapping user_mapping, IEntityMapping item_mapping)
        {
            var ratings = new Ratings();

            var separators = new string[] { "::" };
            string line;

            while ( (line = reader.ReadLine()) != null )
            {
                string[] tokens = line.Split(separators, StringSplitOptions.None);

                if (tokens.Length < 3)
                    throw new IOException("Expected at least three columns: " + line);

                int user_id = user_mapping.ToInternalID(int.Parse(tokens[0]));
                int item_id = item_mapping.ToInternalID(int.Parse(tokens[1]));
                double rating = double.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            return ratings;
        }
Ejemplo n.º 60
0
        /// <summary>Read binary attribute data from a StreamReader</summary>
        /// <remarks>
        /// The expected (sparse) line format is:
        /// ENTITY_ID tab/space/comma ATTRIBUTE_ID
        /// for the relations that hold.
        /// </remarks>
        /// <param name="reader">a StreamReader to be read from</param>
        /// <param name="mapping">the mapping object for the given entity type</param>
        /// <returns>the attribute data</returns>
        public static SparseBooleanMatrix Read(StreamReader reader, IEntityMapping mapping)
        {
            var matrix = new SparseBooleanMatrix();

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                // ignore empty lines
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(Constants.SPLIT_CHARS);

                if (tokens.Length != 2)
                    throw new FormatException("Expected exactly 2 columns: " + line);

                int entity_id = mapping.ToInternalID(tokens[0]);
                int attr_id   = int.Parse(tokens[1]);

                matrix[entity_id, attr_id] = true;
            }

            return matrix;
        }