Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/30/09 10:47:00 (15 years ago)
Author:
gkronber
Message:

GP Refactoring: #713

  • added project GP.Operators
  • moved operators from plugin GP to plugin GP.Operators
  • deleted unused constraints
  • removed dependency of GP plugins on Constraints plugin
  • moved StructID functions into directory Symbols
  • deleted unused class FunView
  • implemented add and remove functionality for the FunctionLibraryView
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/GP-Refactoring-713/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs

    r2210 r2212  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.ComponentModel;
    25 using System.Drawing;
    26 using System.Data;
    2724using System.Linq;
    28 using System.Text;
    2925using System.Windows.Forms;
    3026using HeuristicLab.Core;
    31 using System.Diagnostics;
    32 using HeuristicLab.Data;
    33 using HeuristicLab.Operators;
    34 using HeuristicLab.Random;
    3527using HeuristicLab.GP.Interfaces;
    3628
    3729namespace HeuristicLab.GP {
    3830  public partial class FunctionLibraryEditor : EditorBase {
     31    private ChooseItemDialog chooseFunctionDialog;
    3932    public FunctionLibrary FunctionLibrary {
    4033      get { return (FunctionLibrary)Item; }
     
    4639      InitializeComponent();
    4740      FunctionLibrary = library;
    48 
    49       mutationVariableView.Enabled = false;
    50       initVariableView.Enabled = false;
    51 
    52       foreach (IFunction fun in library.Functions) {
    53         if (fun.Manipulator != null) {
    54           ListViewItem item = new ListViewItem();
    55           item.Text = fun.Name;
    56           item.Name = fun.Name;
    57           item.Tag = fun;
    58           mutationListView.Items.Add(item);
    59         }
    60         if (fun.Initializer != null) {
    61           ListViewItem item = new ListViewItem();
    62           item.Name = fun.Name;
    63           item.Text = fun.Name;
    64           item.Tag = fun;
    65           initListView.Items.Add(item);
    66         }
    67       }
    6841    }
    6942
     
    7548    protected override void UpdateControls() {
    7649      base.UpdateControls();
     50      foreach (IFunction fun in FunctionLibrary.Functions) {
     51        if (fun.Manipulator != null) {
     52          mutationListView.Items.Add(CreateListViewItem(fun));
     53        }
     54        if (fun.Initializer != null) {
     55          initListView.Items.Add(CreateListViewItem(fun));
     56        }
     57      }
    7758    }
    7859
    79     //private void GPOperatorLibraryView_OperatorAdded(object sender, EventArgs e) {
    80     //  IOperator op = ((OperatorEventArgs)e).op;
    81     //  if(op.GetVariable(FunctionBase.MANIPULATION) != null) {
    82     //    ListViewItem operatorMutationItem = new ListViewItem();
    83     //    operatorMutationItem.Name = op.Name;
    84     //    operatorMutationItem.Text = op.Name;
    85     //    operatorMutationItem.Tag = op;
    86     //    mutationListView.Items.Add(operatorMutationItem);
    87     //  }
    88 
    89     //  if(op.GetVariable(FunctionBase.INITIALIZATION) != null) {
    90     //    ListViewItem operatorInitItem = new ListViewItem();
    91     //    operatorInitItem.Name = op.Name;
    92     //    operatorInitItem.Text = op.Name;
    93     //    operatorInitItem.Tag = op;
    94     //    initListView.Items.Add(operatorInitItem);
    95     //  }
    96 
    97     //  op.NameChanged += new EventHandler(op_NameChanged);
    98     //  Refresh();
    99     //}
    100 
    101     //private void op_NameChanged(object sender, EventArgs e) {
    102     //  IOperator srcOp = (IOperator)sender;
    103     //  foreach(ListViewItem item in mutationListView.Items) {
    104     //    if(item.Tag == srcOp) {
    105     //      item.Name = srcOp.Name;
    106     //      item.Text = srcOp.Name;
    107     //      break;
    108     //    }
    109     //  }
    110     //  foreach(ListViewItem item in initListView.Items) {
    111     //    if(item.Tag == srcOp) {
    112     //      item.Name = srcOp.Name;
    113     //      item.Text = srcOp.Name;
    114     //      break;
    115     //    }
    116     //  }
    117     //}
    118 
    119 
    120     //private void GPOperatorGroup_OperatorRemoved(object sender, EventArgs e) {
    121     //  IOperator op = ((OperatorEventArgs)e).op;
    122 
    123     //  foreach(ListViewItem item in mutationListView.Items) {
    124     //    if(item.Tag == op) {
    125     //      mutationListView.Items.Remove(item);
    126     //      break;
    127     //    }
    128     //  }
    129 
    130     //  foreach(ListViewItem item in initListView.Items) {
    131     //    if(item.Tag == op) {
    132     //      initListView.Items.Remove(item);
    133     //      break;
    134     //    }
    135     //  }
    136     //}
    137 
    13860    private void mutationListView_SelectedIndexChanged(object sender, EventArgs e) {
    139       //if(mutationListView.SelectedItems.Count>0 && mutationListView.SelectedItems[0].Tag != null) {
    140       //  IVariable variable = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
    141       //  mutationVariableView.Enabled = true;
    142       //  mutationVariableView.Variable = variable;
    143       //} else {
    144       //  mutationVariableView.Enabled = false;
    145       //}
     61      if (mutationListView.SelectedItems.Count > 0 && mutationListView.SelectedItems[0].Tag != null) {
     62        IOperator manipulator = ((IFunction)mutationListView.SelectedItems[0].Tag).Manipulator;
     63        mutationVariableView.Enabled = true;
     64        mutationVariableView.Variable = new Variable("Manipulator", manipulator);
     65      } else {
     66        mutationVariableView.Enabled = false;
     67      }
    14668    }
    14769
    14870    private void initListView_SelectedIndexChanged(object sender, EventArgs e) {
    149       //if(initListView.SelectedItems.Count>0 && initListView.SelectedItems[0].Tag != null) {
    150       //  IVariable variable = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
    151       //  initVariableView.Enabled = true;
    152       //  initVariableView.Variable = variable;
    153       //} else {
    154       //  initVariableView.Enabled = false;
    155       //}
     71      if (initListView.SelectedItems.Count > 0 && initListView.SelectedItems[0].Tag != null) {
     72        IOperator initializer = ((IFunction)initListView.SelectedItems[0].Tag).Initializer;
     73        initVariableView.Enabled = true;
     74        initVariableView.Variable = new Variable("Initializer", initializer);
     75      } else {
     76        initVariableView.Enabled = false;
     77      }
     78    }
     79
     80    private void addButton_Click(object sender, EventArgs e) {
     81      if (chooseFunctionDialog == null) chooseFunctionDialog = new ChooseItemDialog(typeof(IFunction));
     82      if (chooseFunctionDialog.ShowDialog(this) == DialogResult.OK) {
     83        FunctionLibrary.AddFunction((IFunction)chooseFunctionDialog.Item);
     84        functionsListView.Items.Add(CreateListViewItem((IFunction)chooseFunctionDialog.Item));
     85        functionsListView.Sort();
     86      }
     87    }
     88
     89    private void removeButton_Click(object sender, EventArgs e) {
     90      // delete from the end of the list
     91      IEnumerable<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x);
     92      foreach (int selectedIndex in removeIndices) {
     93        FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag);
     94        functionsListView.Items.RemoveAt(selectedIndex);
     95      }
     96    }
     97
     98    private void functionsListView_SelectedIndexChanged(object sender, EventArgs e) {
     99      removeButton.Enabled = functionsListView.SelectedIndices.Count > 0;
     100    }
     101
     102    private ListViewItem CreateListViewItem(IFunction function) {
     103      ListViewItem item = new ListViewItem();
     104      item.Name = function.Name;
     105      item.Text = function.Name;
     106      item.Tag = function;
     107      return item;
    156108    }
    157109  }
Note: See TracChangeset for help on using the changeset viewer.