protected override IRelation PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); var dto = Database.FirstOrDefault <RelationDto>(sql); if (dto == null) { return(null); } var relationType = _relationTypeRepository.Get(dto.RelationType); if (relationType == null) { throw new Exception(string.Format("RelationType with Id: {0} doesn't exist", dto.RelationType)); } var factory = new RelationFactory(relationType); var entity = factory.BuildEntity(dto); //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 ((TracksChangesEntityBase)entity).ResetDirtyProperties(false); return(entity); }
protected override Relation PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); var dto = Database.FirstOrDefault <RelationDto>(sql); if (dto == null) { return(null); } var relationType = _relationTypeRepository.Get(dto.RelationType); if (relationType == null) { throw new Exception(string.Format("RelationType with Id: {0} doesn't exist", dto.RelationType)); } var factory = new RelationFactory(relationType); var entity = factory.BuildEntity(dto); entity.ResetDirtyProperties(); return(entity); }
public void TestFieldRelations2() { var cls = Class(Type("Outer.Inner.MainClass")); cls.Fields.AddRange(List( new FieldInfo(Modifier.Public, "field1", Type("List", Type("External.F1"))), new FieldInfo(Modifier.Protected | Modifier.Readonly, "field2", Type("List", Type("Dictionary", Type("string"), Type("Inner2.F2")))), new FieldInfo(Modifier.Private | Modifier.Const, "field3", Type("External.Outer.Inner.F3")), new FieldInfo(Modifier.Internal, "field4", Type("F4")) )); var relations = RelationFactory.CreateFromClasses(new[] { cls, ClassFully("External", Type("F1")), Class(Type("Inner2.F2")), ClassFully("External", Type("Outer.Inner.F3")), ClassFully("Internal", Type("Outer.Inner.F4")) }); AreEqualRelations(relations, Relation("Outer.Inner.MainClass", "F1", Association), Relation("Outer.Inner.MainClass", "Inner2.F2", Association), Relation("Outer.Inner.MainClass", "Outer.Inner.F3", Association), Relation("Outer.Inner.MainClass", "Outer.Inner.F4", Association)); }
public void TestMethodRelations() { var cls = Class(Type("MainClass")); cls.Methods.AddRange(List( new MethodInfo(Modifier.Public, "Method1", Type("void"), null), new MethodInfo(Modifier.Protected | Modifier.Abstract, "Method2", Type("R2"), List(Arg(Type("List", Type("Dictionary", Type("string"), Type("A2"))), "arg"))), new MethodInfo(Modifier.Private | Modifier.Static, "Method3", Type("R3"), List(Arg(Type("A3a"), "argA"), Arg(Type("A3b"), "argB"))) )); var relations = RelationFactory.CreateFromClasses(new[] { cls, Class(Type("R2")), Class(Type("A2")), Class(Type("R3")), Class(Type("A3a")), }); AreEqualRelations(relations, Relation("MainClass", "R2", Dependency), Relation("MainClass", "A2", Dependency), Relation("MainClass", "R3", Dependency), Relation("MainClass", "A3a", Dependency)); }
public void TestMethodRelations2() { var cls = Class(Type("Outer.Inner.MainClass")); cls.Methods.AddRange(List( new MethodInfo(Modifier.Public, "Method1", Type("External.R1"), null), new MethodInfo(Modifier.Protected | Modifier.Abstract, "Method2", Type("Inner2.R2"), List(Arg(Type("List", Type("Dictionary", Type("string"), Type("Outer2.Inner.A2"))), "arg"))), new MethodInfo(Modifier.Private | Modifier.Static, "Method3", Type("R3"), List(Arg(Type("External.Outer.Inner.A3a"), "argA"), Arg(Type("External2.Outer.Inner.A3b"), "argB"))) )); var relations = RelationFactory.CreateFromClasses(new[] { cls, ClassFully("External", Type("R1")), Class(Type("Inner2.R2")), Class(Type("Outer2.Inner.A2")), ClassFully("Internal", Type("Outer.Inner.R3")), ClassFully("External", Type("Outer.Inner.A3a")), ClassFully("External", Type("Outer.Inner.A3b")), }); // Note that relation of 'A3b' is not contained AreEqualRelations(relations, Relation("Outer.Inner.MainClass", "R1", Dependency), Relation("Outer.Inner.MainClass", "Inner2.R2", Dependency), Relation("Outer.Inner.MainClass", "Outer2.Inner.A2", Dependency), Relation("Outer.Inner.MainClass", "Outer.Inner.R3", Dependency), Relation("Outer.Inner.MainClass", "Outer.Inner.A3a", Dependency)); }
public void SaveBulk(IEnumerable <ReadOnlyRelation> relations) { foreach (var hasIdentityGroup in relations.GroupBy(r => r.HasIdentity)) { if (hasIdentityGroup.Key) { // Do updates, we can't really do a bulk update so this is still a 1 by 1 operation // however we can bulk populate the object types. It might be possible to bulk update // with SQL but would be pretty ugly and we're not really too worried about that for perf, // it's the bulk inserts we care about. foreach (var relation in hasIdentityGroup) { var dto = RelationFactory.BuildDto(relation); Database.Update(dto); } } else { // Do bulk inserts var dtos = hasIdentityGroup.Select(RelationFactory.BuildDto); Database.InsertBulk(dtos); } } }
private static IRelation DtoToEntity(RelationDto dto, IRelationType relationType) { var entity = RelationFactory.BuildEntity(dto, relationType); // reset dirty initial properties (U4-1946) entity.ResetDirtyProperties(false); return(entity); }
private static IRelation DtoToEntity(RelationDto dto, RelationFactory factory) { var entity = factory.BuildEntity(dto); // reset dirty initial properties (U4-1946) ((BeingDirtyBase)entity).ResetDirtyProperties(false); return(entity); }
private static IRelation DtoToEntity(RelationDto dto, RelationFactory factory) { var entity = factory.BuildEntity(dto); //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 ((TracksChangesEntityBase)entity).ResetDirtyProperties(false); return(entity); }
protected override void PersistUpdatedItem(IRelation entity) { ((EntityBase)entity).UpdatingEntity(); var factory = new RelationFactory(entity.RelationType); var dto = factory.BuildDto(entity); Database.Update(dto); entity.ResetDirtyProperties(); }
protected override void PersistUpdatedItem(IRelation entity) { entity.UpdatingEntity(); var dto = RelationFactory.BuildDto(entity); Database.Update(dto); PopulateObjectTypes(entity); entity.ResetDirtyProperties(); }
protected override void PersistNewItem(IRelation entity) { ((EntityBase)entity).AddingEntity(); var factory = new RelationFactory(entity.RelationType); var dto = factory.BuildDto(entity); var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; entity.ResetDirtyProperties(); }
protected override void PersistNewItem(IRelation entity) { entity.AddingEntity(); var dto = RelationFactory.BuildDto(entity); var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; PopulateObjectTypes(entity); entity.ResetDirtyProperties(); }
public void TestInheritances() { var relations = RelationFactory.CreateFromClasses(new[] { Class(Type("ClassA"), List(Type("Base1"), Type("IF1"), Type("IF2"), Type("IF3"))), Class(Type("Base1")), Interface(Type("IF1")), Interface(Type("IF2")), }); // Note that relation of 'IF3' is not contained AreEqualRelations(relations, Relation("ClassA", "Base1", Generalization), Relation("ClassA", "IF1", Realization), Relation("ClassA", "IF2", Realization)); }
/// <summary> /// Tests whether a class diagram generated from <paramref name="inputCode"/> contains all <paramref name="expectedLines"/>. /// (It's order does not matter) /// <para>This test method does not confirm that generated diagram constains unexpected lines.</para> /// <para>It searches expected texts with ignoring space characters.</para> /// </summary> /// <param name="parser">Parser to parse <paramref name="inputCode"/></param> /// <param name="inputCode">Input source code text to generate a class diagram</param> /// <param name="expectedLines">Text lines which is expected to be included in a generated class diagram</param> /// <param name="expectedIgnoreLines">Text lines which is expected to be included as commented line in a generated class diagram</param> /// <param name="accessFilter">Access level filter</param> /// <param name="excludedClasses">Class names to be excluded in a diagram (actually they are included as commented lines in a diagram)</param> private static void TestcaseGenerate(ISourceCodeParser parser, string inputCode, IEnumerable <string> expectedLines, IEnumerable <string> expectedIgnoreLines = null, Modifier accessFilter = Modifiers.AllAccessLevels, IEnumerable <string> excludedClasses = null) { var title = "test_title"; var classes = parser.Parse(inputCode); var relations = RelationFactory.CreateFromClasses(classes); var diag = PumlClassDiagramGenerator.Generate(title, classes, relations, accessFilter, excludedClasses); diag = Regex.Replace(diag, "\\s+", string.Empty); var emptyDiag = PumlClassDiagramGenerator.Generate(title, null, null); var emptyDiagLines = emptyDiag.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => Regex.Replace(s, "\\s+", string.Empty)); var allExpLines = expectedLines .Concat(emptyDiagLines) // To check common lines .Concat(expectedIgnoreLines?.Select(s => CommentSymbol + s) ?? Enumerable.Empty <string>()) .Select(s => Regex.Replace(s, "\\s+", string.Empty)); foreach (var line in allExpLines) { // It is OK, if one of patterns generated from a expected line matches a part of input code. var patterns = CreatePatterns(line); var matchedPattern = patterns.Where(p => diag.IndexOf(p) >= 0).FirstOrDefault(); if (matchedPattern == null) { Console.WriteLine(diag); AssertEx.Fail($"Expected '{line}' is contained in a generated class diagram" + $", but actual diagram does not contain it."); } diag = diag.Remove(diag.IndexOf(matchedPattern), matchedPattern.Length); } // Remove comment symbol attached to bracket. diag = diag.Replace($"{CommentSymbol}{{", "{").Replace($"{CommentSymbol}}}", "}"); // If all texts are checked, diag is expected to contain only symbols. Regex.IsMatch(diag, "^[\\s{}]*$").IsTrue($"Unexpected lines are contained {diag}"); }
public void Save(IEnumerable <IRelation> relations) { foreach (var hasIdentityGroup in relations.GroupBy(r => r.HasIdentity)) { if (hasIdentityGroup.Key) { // Do updates, we can't really do a bulk update so this is still a 1 by 1 operation // however we can bulk populate the object types. It might be possible to bulk update // with SQL but would be pretty ugly and we're not really too worried about that for perf, // it's the bulk inserts we care about. var asArray = hasIdentityGroup.ToArray(); foreach (var relation in hasIdentityGroup) { relation.UpdatingEntity(); var dto = RelationFactory.BuildDto(relation); Database.Update(dto); } PopulateObjectTypes(asArray); } else { // Do bulk inserts var entitiesAndDtos = hasIdentityGroup.ToDictionary( r => // key = entity { r.AddingEntity(); return(r); }, RelationFactory.BuildDto); // value = DTO foreach (var dto in entitiesAndDtos.Values) { Database.Insert(dto); } // All dtos now have IDs assigned foreach (var de in entitiesAndDtos) { // re-assign ID to the entity de.Key.Id = de.Value.Id; } PopulateObjectTypes(entitiesAndDtos.Keys.ToArray()); } } }
private IEnumerable <IRelation> DtosToEntities(IEnumerable <RelationDto> dtos) { // in most cases, the relation type will be the same for all of them, // plus we've ordered the relations by type, so try to allocate as few // factories as possible - bearing in mind that relation types are cached RelationFactory factory = null; var relationTypeId = -1; return(dtos.Select(x => { if (relationTypeId != x.RelationType) { factory = new RelationFactory(_relationTypeRepository.Get(relationTypeId = x.RelationType)); } return DtoToEntity(x, factory); }).ToList()); }
public void TestInheritances2() { var relations = RelationFactory.CreateFromClasses(new[] { Class(Type("Outer.Inner.ClassA"), List(Type("Base1"), Type("IF1"), Type("Inner2.IF2"), Type("IF3"))), ClassFully("External", Type("Base1")), Interface(Type("Outer.Inner.IF1")), Interface(Type("Outer.Inner2.IF2")), InterfaceFully("Internal", Type("Outer.Inner.IF3")) }); AreEqualRelations(relations, Relation("Outer.Inner.ClassA", "Base1", Generalization), Relation("Outer.Inner.ClassA", "Outer.Inner.IF1", Realization), Relation("Outer.Inner.ClassA", "Outer.Inner2.IF2", Realization), Relation("Outer.Inner.ClassA", "Outer.Inner.IF3", Realization)); }
/// <summary> /// 计算当前用户对当前牌允许的操作,不包含胡牌 /// </summary> /// <param name="user"></param> /// <param name="currCard"></param> /// <returns></returns> public static IRelation[] CardOperations(GameUser user, CardPlay currCard) { var isCurrUser = currCard.User.UserName.Equals(user.UserName); var isNext = !isCurrUser && currCard.User.NextUser.UserName.Equals(user.UserName); // 从固定规则中解析出跑、清 IRelation outputRelation; var relation = RelationFactory.BuildRelationsByRelations(user.SelfRelations.ToArray(), out outputRelation, currCard.Card, currCard.IsUserPay, isCurrUser); if (relation == null) { relation = RelationFactory.BuildRelationsByRelations(user.Relations.ToArray(), out outputRelation, currCard.Card, currCard.IsUserPay, isCurrUser); } if (relation != null) { return(new IRelation[] { relation }); } // 解析出其他规则 Card[] outputCards; List <Card> tempCards = new List <Card>(); tempCards.AddRange(user.Cards); tempCards.Add(currCard.Card); // 当前用户,解析出吃或提 if (isCurrUser) { return(RelationFactory.BuildVariableRelationsBySelf(tempCards.ToArray(), out outputCards, currCard.Card)); } // 其他用户 else { // 不是下家,只能碰 if (!isNext) { return(RelationFactory.BuildCustomVariableRelations(tempCards.ToArray(), new Card[] { currCard.Card }, out outputCards, ParserDefines.SameThreeParser)); } // 下家,可以碰、吃 return(RelationFactory.BuildVariableRelations(tempCards.ToArray(), out outputCards, currCard.Card)); } }
/// <summary> /// 初始化用户手中的牌,提取出固定规则的牌 /// </summary> /// <param name="cards"></param> /// <returns>返回Item1:剩余的牌,Item2:手中的固定规则,Item3:牌桌上的规定规则</returns> public static Tuple <Card[], IRelation[], IRelation[]> InitUserCards(params Card[] cards) { var relations = RelationFactory.BuildFixedRelations(cards, out Card[] outputCards); return(new Tuple <Card[], IRelation[], IRelation[]>( (from card in cards where !outputCards.Contains(card) select card).ToArray(), (from relation in relations where relation.RelationType == RelationTypes.LLL_Self || relation.RelationType == RelationTypes.SSS_Self select relation).ToArray(), (from relation in relations where relation.RelationType == RelationTypes.LLLL_Self || relation.RelationType == RelationTypes.SSSS_Self select relation).ToArray() )); }
private IEnumerable <IRelation> DtosToEntities(IEnumerable <RelationDto> dtos) { // in most cases, the relation type will be the same for all of them, // plus we've ordered the relations by type, so try to allocate as few // factories as possible - bearing in mind that relation types are cached RelationFactory factory = null; var relationTypeId = -1; // the ToList() here is important because we are using _relationTypeRepository and we // cannot wait until the result is actually enumerated to do so, because that would // mean we kinda capture the current unit of work and reuse it after it's been disposed return(dtos.Select(x => { if (relationTypeId != x.RelationType) { factory = new RelationFactory(_relationTypeRepository.Get(relationTypeId = x.RelationType)); } return DtoToEntity(x, factory); }).ToList()); }
public void TestNestedClasses() { var root = Class(Type("Root")); var inner1 = Class(Type("In1")); var inner11 = Class(Type("In1-1")); inner11.InnerClasses.Add(Class(Type("In1-1-1"))); inner1.InnerClasses.Add(inner11); inner1.InnerClasses.Add(Class(Type("In1-2"))); inner1.InnerClasses.Add(Class(Type("In1-3"))); root.InnerClasses.Add(inner1); root.InnerClasses.Add(Class(Type("In2"))); var relations = RelationFactory.CreateFromClasses(new[] { root }); AreEqualRelations(relations, Relation("In1", "Root", Nested), Relation("In2", "Root", Nested), Relation("In1-1", "In1", Nested), Relation("In1-2", "In1", Nested), Relation("In1-3", "In1", Nested), Relation("In1-1-1", "In1-1", Nested)); }
/// <summary> /// Generates a class diagram. /// </summary> /// <param name="title">Title of class diagram</param> /// <param name="filePaths">Source code paths</param> /// <param name="parser">Parser to parse source codes</param> /// <param name="accessLevel">Access level of members written to a class diagram</param> /// <param name="excludedClasses">A collection of class names not to be written to a class diagram</param> /// <returns>Class diagram described in PlantUML</returns> private static string GenerateClassDiagram(string title, IEnumerable <string> filePaths, ISourceCodeParser parser, Modifier accessLevel, IEnumerable <string> excludedClasses) { var classes = new List <ClassInfo>(); foreach (var file in filePaths) { try { var content = File.ReadAllText(file); classes.AddRange(parser.Parse(content)); } catch { Console.Error.WriteLine($"Skipped (could not open file) : {file}"); } } Console.WriteLine($"Parsed classes ... {classes.Count}"); var relations = RelationFactory.CreateFromClasses(classes); return(PumlClassDiagramGenerator.Generate(title ?? string.Empty, classes, relations, accessLevel, excludedClasses)); }
protected override IRelation PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { id }); var dto = Database.Fetch <RelationDto>(SqlSyntax.SelectTop(sql, 1)).FirstOrDefault(); if (dto == null) { return(null); } var relationType = _relationTypeRepository.Get(dto.RelationType); if (relationType == null) { throw new Exception(string.Format("RelationType with Id: {0} doesn't exist", dto.RelationType)); } var factory = new RelationFactory(relationType); return(DtoToEntity(dto, factory)); }
public void TestGenericRelations() { var cls = Class(Type("MainClass", Type("T")), List(Type("BaseClass", Type("T")), Type("IF", Type("T")))); cls.Methods.Add(new MethodInfo(Modifier.Public, "Method", Type("R", Type("T"), Type("T")), List(Arg(Type("A1", Type("T")), "arg1"), Arg(Type("A2", Type("T")), "arg2")))); cls.Fields.Add(new FieldInfo(Modifier.Private, "field", Type("F1", Type("T"), Type("T")))); var relations = RelationFactory.CreateFromClasses(new[] { cls, Class(Type("BaseClass", Type("X"))), Interface(Type("IF", Type("X"), Type("Y"))), Class(Type("R", Type("X"), Type("Y"))), Class(Type("A1", Type("X"))), Class(Type("A2", Type("X"), Type("Y"))), Class(Type("F1", Type("X"), Type("Y"))), }); AreEqualRelations(relations, Relation("MainClass`1", "BaseClass`1", Generalization), Relation("MainClass`1", "R`2", Dependency), Relation("MainClass`1", "A1`1", Dependency), Relation("MainClass`1", "F1`2", Association)); }
public void TestBuildFixedRelations() { var source = CardSource(21); foreach (var relation in RelationFactory.BuildFixedRelations(source, out Card[] outputCards))
public RelationVisualCardViewModel() : this(RelationFactory.CreateNew()) { }