Changeset 13422 for branches/ProblemRefactoring
- Timestamp:
- 11/30/15 15:12:34 (9 years ago)
- Location:
- branches/ProblemRefactoring
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Core.Views/3.3/TypeSelector.cs
r12722 r13422 257 257 } 258 258 259 private Type[] genericTypeArguments = null; 259 260 protected virtual void UpdateTypeParameters() { 260 261 typeParametersListView.Items.Clear(); … … 262 263 typeParametersGroupBox.Enabled = false; 263 264 typeParametersSplitContainer.Panel2Collapsed = true; 265 genericTypeArguments = null; 264 266 } else { 265 267 typeParametersGroupBox.Enabled = true; … … 267 269 setTypeParameterButton.Enabled = false; 268 270 269 foreach (Type param in SelectedType.GetGenericArguments()) { 271 272 genericTypeArguments = SelectedType.GetGenericArguments(); 273 274 foreach (Type param in genericTypeArguments) { 270 275 if (param.IsGenericParameter) { 271 276 ListViewItem item = new ListViewItem(); … … 301 306 if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) { 302 307 Type selected = typeSelectorDialog.TypeSelector.SelectedType; 303 Type[] parameters = SelectedType.GetGenericArguments();304 parameters[param.GenericParameterPosition] = selected;305 SelectedType = SelectedType.GetGenericTypeDefinition().MakeGenericType(parameters);308 genericTypeArguments[param.GenericParameterPosition] = selected; 309 if (genericTypeArguments.All(p => !p.IsGenericParameter)) 310 SelectedType = SelectedType.GetGenericTypeDefinition().MakeGenericType(genericTypeArguments); 306 311 307 312 typeParametersListView.SelectedItems[0].Text = param.Name + ": " + selected.GetPrettyName(); -
branches/ProblemRefactoring/HeuristicLab.Core.Views/3.3/TypeSelectorDialog.cs
r12012 r13422 58 58 } 59 59 protected virtual void typeSelector_SelectedTypeChanged(object sender, EventArgs e) { 60 okButton.Enabled = typeSelector.SelectedType != null; 60 var selectedType = typeSelector.SelectedType; 61 okButton.Enabled = selectedType != null && !selectedType.IsGenericTypeDefinition; 61 62 } 62 63 protected virtual void TypesTreeView_DoubleClick(object sender, System.EventArgs e) { -
branches/ProblemRefactoring/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs
r12012 r13422 131 131 let t = assemblyType.BuildType(type) 132 132 where t != null 133 where t.Is SubTypeOf(type)133 where t.IsAssignableTo(type) 134 134 where !t.IsNonDiscoverableType() 135 135 where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType) -
branches/ProblemRefactoring/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs
r12012 r13422 264 264 let t = assemblyType.BuildType(type) 265 265 where t != null 266 where t.Is SubTypeOf(type)266 where t.IsAssignableTo(type) 267 267 where !t.IsNonDiscoverableType() 268 268 where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType) -
branches/ProblemRefactoring/HeuristicLab.PluginInfrastructure/3.3/TypeExtensions.cs
r12012 r13422 76 76 } 77 77 78 internal static bool Is SubTypeOf(this Type subType, Type baseType) {78 internal static bool IsAssignableTo(this Type subType, Type baseType) { 79 79 if (baseType.IsAssignableFrom(subType)) return true; 80 81 //check generics 80 82 if (!baseType.IsGenericType) return false; 83 if (RecursiveCheckGenericTypes(baseType, subType)) return true; 81 84 82 if (RecursiveCheckGenericTypes(baseType, subType)) return true;85 //check generic interfaces 83 86 IEnumerable<Type> implementedInterfaces = subType.GetInterfaces().Where(t => t.IsGenericType); 84 foreach (var implementedInterface in implementedInterfaces.Where(i => i.IsGenericType)) { 85 if (baseType.CheckGenericTypes(implementedInterface)) return true; 86 } 87 88 return false; 87 return implementedInterfaces.Any(implementedInterface => baseType.CheckGenericTypes(implementedInterface)); 89 88 } 90 89 91 90 private static bool RecursiveCheckGenericTypes(Type baseType, Type subType) { 92 91 if (!baseType.IsGenericType) return false; 93 if (!subType.IsGenericType) return false; 94 if (baseType.CheckGenericTypes(subType)) return true; 92 if (subType.IsGenericType && baseType.CheckGenericTypes(subType)) return true; 95 93 if (subType.BaseType == null) return false; 96 94 … … 102 100 var subTypeGenericTypeDefinition = subType.GetGenericTypeDefinition(); 103 101 if (baseTypeGenericTypeDefinition != subTypeGenericTypeDefinition) return false; 102 104 103 var baseTypeGenericArguments = baseType.GetGenericArguments(); 105 104 var subTypeGenericArguments = subType.GetGenericArguments(); … … 109 108 var subTypeGenericArgument = subTypeGenericArguments[i]; 110 109 111 if (baseTypeGenericArgument.IsGenericParameter ^ subTypeGenericArgument.IsGenericParameter) return false; 112 if (baseTypeGenericArgument == subTypeGenericArgument) continue; 113 if (!baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) return false; 110 //no generic parameters => concrete types => check for type equality (ignore co- and contravariance) 111 //for example List<int> is only a List<int>, IParameter<IItem> is not a base type of IParameter<DoubleValue> 112 if (!baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) { 113 if (baseTypeGenericArgument == subTypeGenericArgument) continue; 114 return false; 115 } 114 116 115 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint) && 116 !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) return false; 117 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) && 118 !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) return false; 119 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint) && 120 !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) return false; 117 //baseTypeGenericArgument is a concrete type and the subTypeGenericArgument is a generic parameter 118 //for example List<int> is not a base type of List<T> 119 if (!baseTypeGenericArgument.IsGenericParameter && subTypeGenericArgument.IsGenericParameter) return false; 121 120 122 foreach (var baseTypeGenericParameterConstraint in baseTypeGenericArgument.GetGenericParameterConstraints()) { 123 if (!subTypeGenericArgument.GetGenericParameterConstraints().Any(t => baseTypeGenericParameterConstraint.IsAssignableFrom(t))) return false; 121 //baseTypeGenericArugment is a generic parameter and the subTypeGenericArgument is a concrete type => check type constraints 122 //for example IParameter<T> is a base type of IParameter<IItem> if all generic contraints on T are fulfilled 123 if (baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) { 124 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint) && 125 subTypeGenericArgument.IsValueType) return false; 126 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) && 127 subTypeGenericArgument.GetConstructor(Type.EmptyTypes) == null) return false; 128 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) { 129 if (!subTypeGenericArgument.IsValueType) return false; 130 if (subTypeGenericArgument.IsGenericType && subTypeGenericArgument.GetGenericTypeDefinition() == typeof(Nullable<>)) 131 return false; 132 } 133 134 //not assignable if the subTypeGenericArgument is not assignable to all of the constraints of the base type 135 if (baseTypeGenericArgument.GetGenericParameterConstraints().Any(baseTypeGenericParameterConstraint => 136 !baseTypeGenericParameterConstraint.IsAssignableFrom(subTypeGenericArgument))) 137 return false; 138 } 139 140 //both generic arguments are generic parameters => check type constraints 141 //for example IParameter<T> is a base type of IParameter<T> 142 if (baseTypeGenericArgument.IsGenericParameter && subTypeGenericArgument.IsGenericParameter) { 143 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint) && 144 !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) return false; 145 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) && 146 !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) return false; 147 if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint) && 148 !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) return false; 149 150 //not assignable if any of the constraints is not assignable to the constraints of the base type 151 if (baseTypeGenericArgument.GetGenericParameterConstraints().Any(baseTypeGenericParameterConstraint => !subTypeGenericArgument.GetGenericParameterConstraints().Any(t => baseTypeGenericParameterConstraint.IsAssignableFrom(t)))) { 152 return false; 153 } 124 154 } 125 155 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblem.cs
r13390 r13422 34 34 [Creatable(CreatableAttribute.Categories.Problems, Priority = 120)] 35 35 [StorableClass] 36 public abstractclass MultiObjectiveProgrammableProblem<TEncoding, TSolution> : MultiObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem36 public class MultiObjectiveProgrammableProblem<TEncoding, TSolution> : MultiObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem 37 37 where TEncoding : class, IEncoding<TSolution> 38 38 where TSolution : class, ISolution { … … 66 66 RegisterEvents(); 67 67 } 68 69 public override IDeepCloneable Clone(Cloner cloner) { 70 return new MultiObjectiveProgrammableProblem<TEncoding, TSolution>(this, cloner); 71 } 72 68 73 public MultiObjectiveProgrammableProblem() 69 74 : base() { … … 71 76 new MultiObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name })); 72 77 ProblemScript.Encoding = (TEncoding)Encoding.Clone(); 78 79 var codeTemplate = ScriptTemplates.MultiObjectiveProblem_Template; 80 codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace); 81 codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name); 82 codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TSolution).Name); 83 ProblemScript.Code = codeTemplate; 84 73 85 RegisterEvents(); 74 86 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r13390 r13422 34 34 namespace HeuristicLab.Problems.Programmable { 35 35 [Item("Programmable Problem (single-objective)", "Represents a single-objective problem that can be programmed with a script.")] 36 [Creatable(CreatableAttribute.Categories.Problems, Priority = 110)] 36 37 [StorableClass] 37 public abstractclass SingleObjectiveProgrammableProblem<TEncoding, TSolution> : SingleObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem38 public class SingleObjectiveProgrammableProblem<TEncoding, TSolution> : SingleObjectiveProblem<TEncoding, TSolution>, IProgrammableItem, IProgrammableProblem 38 39 where TEncoding : class, IEncoding<TSolution> 39 40 where TSolution : class, ISolution { … … 67 68 RegisterEvents(); 68 69 } 70 71 public override IDeepCloneable Clone(Cloner cloner) { 72 return new SingleObjectiveProgrammableProblem<TEncoding, TSolution>(this, cloner); 73 } 74 69 75 public SingleObjectiveProgrammableProblem() 70 76 : base() { 71 Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.", 72 new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name })); 77 Parameters.Add(new FixedValueParameter<SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>>("ProblemScript", "Defines the problem.", new SingleObjectiveProblemDefinitionScript<TEncoding, TSolution>() { Name = Name })); 73 78 ProblemScript.Encoding = (TEncoding)Encoding.Clone(); 79 80 var codeTemplate = ScriptTemplates.SingleObjectiveProblem_Template; 81 codeTemplate = codeTemplate.Replace(ENCODING_NAMESPACE, typeof(TEncoding).Namespace); 82 codeTemplate = codeTemplate.Replace(ENCODING_CLASS, typeof(TEncoding).Name); 83 codeTemplate = codeTemplate.Replace(SOLUTION_CLASS, typeof(TSolution).Name); 84 ProblemScript.Code = codeTemplate; 85 74 86 Operators.Add(new BestScopeSolutionAnalyzer()); 75 87 RegisterEvents(); -
branches/ProblemRefactoring/HeuristicLab.Tests/HeuristicLab.PluginInfraStructure-3.3/TypeExtensionsTest.cs
r12012 r13422 36 36 [TestProperty("Time", "short")] 37 37 public void IsSubTypeOfTest() { 38 Assert.IsTrue(typeof(int).Is SubTypeOf(typeof(object)));39 Assert.IsTrue(typeof(IntValue).Is SubTypeOf(typeof(IItem)));40 Assert.IsTrue(typeof(List<int>).Is SubTypeOf(typeof(object)));38 Assert.IsTrue(typeof(int).IsAssignableTo(typeof(object))); 39 Assert.IsTrue(typeof(IntValue).IsAssignableTo(typeof(IItem))); 40 Assert.IsTrue(typeof(List<int>).IsAssignableTo(typeof(object))); 41 41 42 Assert.IsTrue(typeof(List<int>).IsSubTypeOf(typeof(IList))); 43 Assert.IsTrue(typeof(List<>).IsSubTypeOf(typeof(IList))); 44 Assert.IsFalse(typeof(NamedItemCollection<>).IsSubTypeOf(typeof(ICollection<IItem>))); 45 Assert.IsFalse(typeof(NamedItemCollection<>).IsSubTypeOf(typeof(ICollection<NamedItem>))); 42 Assert.IsTrue(typeof(List<int>).IsAssignableTo(typeof(IList))); 43 Assert.IsTrue(typeof(List<>).IsAssignableTo(typeof(IList))); 44 Assert.IsFalse(typeof(NamedItemCollection<>).IsAssignableTo(typeof(ICollection<IItem>))); 45 Assert.IsFalse(typeof(NamedItemCollection<>).IsAssignableTo(typeof(ICollection<NamedItem>))); 46 47 //new tests 46 48 47 49 48 Assert.IsTrue(typeof(List<IItem>).Is SubTypeOf(typeof(IList<IItem>)));49 Assert.IsFalse(typeof(IList<IntValue>).Is SubTypeOf(typeof(IList<IItem>)));50 Assert.IsTrue(typeof(List<IItem>).Is SubTypeOf(typeof(IList<IItem>)));51 Assert.IsFalse(typeof(ItemList<>).Is SubTypeOf(typeof(IList<IItem>)));52 Assert.IsFalse(typeof(ItemList<>).Is SubTypeOf(typeof(List<IItem>)));50 Assert.IsTrue(typeof(List<IItem>).IsAssignableTo(typeof(IList<IItem>))); 51 Assert.IsFalse(typeof(IList<IntValue>).IsAssignableTo(typeof(IList<IItem>))); 52 Assert.IsTrue(typeof(List<IItem>).IsAssignableTo(typeof(IList<IItem>))); 53 Assert.IsFalse(typeof(ItemList<>).IsAssignableTo(typeof(IList<IItem>))); 54 Assert.IsFalse(typeof(ItemList<>).IsAssignableTo(typeof(List<IItem>))); 53 55 54 Assert.Is False(typeof(List<int>).IsSubTypeOf(typeof(List<>)));55 Assert.IsTrue(typeof(List<>).Is SubTypeOf(typeof(IList<>)));56 Assert.IsTrue(typeof(ItemList<>).Is SubTypeOf(typeof(IList<>)));57 Assert.IsTrue(typeof(NamedItemCollection<>).Is SubTypeOf(typeof(IItemCollection<>)));58 Assert.Is False(typeof(List<IntValue>).IsSubTypeOf(typeof(IList<>)));56 Assert.IsTrue(typeof(List<int>).IsAssignableTo(typeof(List<>))); 57 Assert.IsTrue(typeof(List<>).IsAssignableTo(typeof(IList<>))); 58 Assert.IsTrue(typeof(ItemList<>).IsAssignableTo(typeof(IList<>))); 59 Assert.IsTrue(typeof(NamedItemCollection<>).IsAssignableTo(typeof(IItemCollection<>))); 60 Assert.IsTrue(typeof(List<IntValue>).IsAssignableTo(typeof(IList<>))); 59 61 } 60 62
Note: See TracChangeset
for help on using the changeset viewer.