source: trunk/CrypPlugins/Factorizer/Factorizer.cs @ 1507

Last change on this file since 1507 was 1259, checked in by Matthäus Wander, 12 years ago

shortened license text to boilerplate notice in all source files

File size: 6.1 KB
Line 
1/*
2   Copyright 2008 Timo Eckhardt, University of Siegen
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using Cryptool.PluginBase.Tool;
22using Cryptool.PluginBase;
23using Cryptool.PluginBase.IO;
24using Primes.Bignum;
25
26namespace Factorizer
27{
28  [Author("Timo Eckhardt", "T-Eckhardt@gmx.de", "Uni Siegen", "http://www.uni-siegen.de")]
29  [PluginInfo(false, "Factorizer", "Factorizer", null, "Factorizer/icon.png")]
30  public class Factorizer : IThroughput
31  {
32    public Factorizer()
33    {
34      m_Settings = new FactorizerSettings();
35    }
36    #region IPlugin Members
37
38    public event Cryptool.PluginBase.StatusChangedEventHandler OnPluginStatusChanged;
39    private void FireOnPluginStatusChangedEvent()
40    {
41      if (OnPluginStatusChanged != null) OnPluginStatusChanged(this, new StatusEventArgs(0));
42    }
43
44    public event Cryptool.PluginBase.GuiLogNotificationEventHandler OnGuiLogNotificationOccured;
45    private void FireOnGuiLogNotificationOccuredEvent(string message, NotificationLevel lvl)
46    {
47      if (OnGuiLogNotificationOccured != null) OnGuiLogNotificationOccured(this, new GuiLogEventArgs(message, this, lvl));
48    }
49    private void FireOnGuiLogNotificationOccuredEventError(string message)
50    {
51      FireOnGuiLogNotificationOccuredEvent(message, NotificationLevel.Error);
52    }
53
54    public event Cryptool.PluginBase.PluginProgressChangedEventHandler OnPluginProgressChanged;
55    private void FireOnPluginProgressChangedEvent(string message, NotificationLevel lvl)
56    {
57      if (OnPluginProgressChanged != null) OnPluginProgressChanged(this, new PluginProgressEventArgs(0, 0));
58    }
59
60    private FactorizerSettings m_Settings;
61    public Cryptool.PluginBase.ISettings Settings
62    {
63      get { return m_Settings; }
64    }
65
66    public System.Windows.Controls.UserControl Presentation
67    {
68      get { return null; }
69    }
70
71    public System.Windows.Controls.UserControl QuickWatchPresentation
72    {
73      get { return null; }
74    }
75
76    public void PreExecution()
77    {
78    }
79
80    public void Execute()
81    {
82      if (m_Settings.HasErrors)
83      {
84        foreach (string message in m_Settings.Errors)
85        {
86          FireOnGuiLogNotificationOccuredEventError(message);
87        }
88      }
89      else
90      {
91        if (m_Input != null)
92        {
93          if (m_Input.IsProbablePrime(10))
94          {
95            Factor = m_Input.ToString();
96            Remainder = PrimesBigInteger.One.ToString();
97          }
98          else
99          {
100            PrimesBigInteger i = PrimesBigInteger.Two;
101            while (i.Multiply(i).CompareTo(m_Input) <= 0)
102            {
103              if (m_Input.Mod(i).CompareTo(PrimesBigInteger.Zero) == 0)
104              {
105                Factor = i.ToString();
106                Remainder = m_Input.Divide(i).ToString();
107                return;
108              }
109              i = i.NextProbablePrime();
110            }
111          }
112        }
113        else
114        {
115          FireOnGuiLogNotificationOccuredEventError("No input given");
116        }
117      }
118    }
119
120    public void PostExecution()
121    {
122    }
123
124    public void Pause()
125    {
126    }
127
128    public void Stop()
129    {
130    }
131
132    public void Initialize()
133    {
134    }
135
136    public void Dispose()
137    {
138    }
139
140    #endregion
141
142    #region INotifyPropertyChanged Members
143
144    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
145    private void FirePropertyChangedEvent(string propertyName)
146    {
147      if (PropertyChanged != null) PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
148    }
149
150    #endregion
151
152    #region Properties
153    private PrimesBigInteger m_Input = null;
154    private string m_InputString;
155    [PropertyInfo(Direction.InputData, "String input", "A string that represents a natural number", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
156    public string InputString
157    {
158      get { return m_InputString; }
159      set {
160        if (!string.IsNullOrEmpty(value))
161        {
162          m_InputString = value;
163          try
164          {
165            m_Input = new PrimesBigInteger(m_InputString);
166            if (m_Input.CompareTo(PrimesBigInteger.Zero) <= 0)
167            {
168              m_Input = null;
169              throw new Exception();
170            }
171
172          }
173          catch 
174          {
175            FireOnGuiLogNotificationOccuredEventError("Input has to be a natural number.");
176          }
177          FirePropertyChangedEvent("InputString");
178        }
179        else
180        {
181          FireOnGuiLogNotificationOccuredEventError("Input has to be a natural number.");
182          m_Input = null;
183        }
184      }
185    }
186    private string m_Factor;
187
188    [PropertyInfo(Direction.OutputData, "A prime factor", "A string that represents a factor that is a prime. ", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
189    public string Factor
190    {
191      get { return m_Factor; }
192      set {
193        if (!string.IsNullOrEmpty(value))
194        {
195          m_Factor = value;
196          FirePropertyChangedEvent("Factor");
197        }
198      }
199    }
200    private string m_Remainder;
201
202    [PropertyInfo(Direction.OutputData, "Remainder", "Remainder", "", true, false, DisplayLevel.Beginner, QuickWatchFormat.Text, null)]
203    public string Remainder
204    {
205      get { return m_Remainder; }
206      set {
207        if (!string.IsNullOrEmpty(value))
208        {
209          m_Remainder = value;
210          FirePropertyChangedEvent("Remainder");
211        }
212      }
213    }
214    #endregion
215  }
216}
Note: See TracBrowser for help on using the repository browser.