0

I am currently writing a program using Weka that builds a model (using one of the various model types included in Weka). The part I'm having trouble with is that I want to allow the program to use multiple types for the same function. For example, if I choose to use a J48 decision tree, I need to build and call functions on a variable of type J48. On the other hand, if I want to use a BayesNet I need to build and call the same functions on a variable of type BayesNet. This model needs to be used at different points of the program, and so a simple if-else chain won't do.

Essentially what I need is the equivalent of a void pointer in C++. Something that can take on various types that can all perform a similar function. I'm tried using the generic Object class but found it was not able to be casted to the correct type (I got "incompatible type" errors).

So, the short version of my question is this: How do Java programmers solve the problem that C++ programmers would solve with void pointers?

Thanks, -Morag

Morag
  • 49
  • 5

3 Answers3

3

Do you mean, something like this?

public void doStuff(MyType<?> someObject) { }  

? stands for any type and represents unbounded wildcard.

Just beware, that this wildcard has certain limitations. For example, you can't add anything to List<?>.

See Official Tutorial or AngelikaLanger Generics FAQ for more information.

jFrenetic
  • 5,384
  • 5
  • 42
  • 67
0

I am currently writing a program using Weka that builds a model (using one of the various model types included in Weka). The part I'm having trouble with is that I want to allow the program to use multiple types for the same function. For example, if I choose to use a J48 decision tree, I need to build and call functions on a variable of type J48. On the other hand, if I want to use a BayesNet I need to build and call the same functions on a variable of type BayesNet. This model needs to be used at different points of the program, and so a simple if-else chain won't do.

Actually what you really need is a polymorphic behavior. Please check this post I answered explaining a bit about it: Polymorphism

After understanding the idea behind polymorphism, go to something deeper and look for Factory Pattern. Here are good resources for Java patterns:

Source Making Patterns

Wikipedia - Factory Pattern

Community
  • 1
  • 1
axcdnt
  • 14,004
  • 7
  • 26
  • 31
0

You can use the superclass Classifier as the type declaration for any classifier object, even though each object will be a subclass of Classifier such as J48. It works because they all implement the functions of the superclass, and so the function calls are defined even though you do not necessarily know which specific type of Classifier you are using. Axcdnt's answer has a link to more details about polymorphism. Anyway, I have a working program with the following code snippits; they do exactly what it sounds like you need.

Classifier Clfs = null;

    try {
        if (modelType.equals("J48")) {
            Clfs = new J48();
        } else if (modelType.equals("MLP")) {
            Clfs = new MultilayerPerceptron(); 
        } else if (modelType.equals("IB3")) {
            Clfs = new IBk(3);
        } else if (modelType.equals("RF")) {
            Clfs = new RandomForest(); 
        } else if (modelType.equals("NB")) {
            Clfs = new NaiveBayes();
//...

and elsewhere, after writing the classifier to a file I call

Classifier cls = (Classifier) weka.core.SerializationHelper.read(target);
prediction = cls.distributionForInstance(data.instance(0));

(target is a string containing a file path to load the model from) As you can see, it does not matter what type of classifier I'm using except at creation time, when I must specify. N.B. there will still be some limitations, e.g. j48 cannot predict numeric classes.

kaz
  • 675
  • 2
  • 5
  • 13