问题
I am currently working on a project in which I need to use FP-Growth algorithm. I know Weka is a handy tool for it. However, I am using C# for coding (due to some other libraries I need). So, I converted weka.jar
to weka.dll
using IKVM.NET
. Below is a code snippet that i have written:
FPGrowth FPMiner = new FPGrowth();
FPMiner.buildAssociations(dataset);
AssociationRules rules = FPMiner.getAssociationRules();
List<AssociationRule> rule = rules.getRules();
This gives me an error as:
Cannot implicitly convert type 'java.util.List' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)
So, I added a cast to the last line as:
List<AssociationRule> rule = (System.Collections.Generic.List<AssociationRule>)rules.getRules();
The error goes away but I get an exception when I run my code, saying:
System.InvalidCastException was unhandled Message=Unable to cast object of type 'java.util.ArrayList' to type System.Collections.Generic.List`1[weka.associations.AssociationRule]'. Source=WindowsFormsApplication1
The stacktrace goes as:
StackTrace:
at DetectGroup.Form1.GenerateARFF() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 279
at DetectGroup.Form1.findNearestNeighbours() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 236
at DetectGroup.Form1.findSelectedTraj() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 165
at DetectGroup.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Form1.cs:line 404
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at DetectGroup.Program.Main() in C:\Users\user\Documents\Visual Studio 2010\Projects\DetectGroup\DetectGroup\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart() InnerException:
I am unable to figure out what to do now. I have tried searching stuff but haven't yet got the solution. I understand that the error is because getRules()
returns java.util.List
while I am trying to use it as System.Collections.Generic.List
. What can I do to avoid it? Any help would be great!
Also, is there any data mining library (like Weka) available in C#?
Thank you!
回答1:
I am answering my own question. I used this link to solve the problem I was facing. Thanks @SecretSquirrel(see the comments) and @Jon Iles (see the answer I've linked).
来源:https://stackoverflow.com/questions/18313100/using-weka-in-c-unable-to-cast-object-of-type-java-util-arraylist-to-type-s