plugin based architectures elvira: problems and solutions andrés r. masegosa department of computer...

23
Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras, June 2008

Upload: rodger-melvin-george

Post on 19-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesElvira: Problems and SolutionsAndrés R. Masegosa

Department of Computer Science and A.I.

Univerisity of Granada

Puerto Lumbreras, June 2008

Page 2: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Why extends is evil?[javaworld.com]

James Gosling (Java’s inventor):“If you could do Java over again, what would you

change?”

“I’d leave out classes”

The real problem is the inheritance.

“You should avoid implementation inheritance whenever possible”:

Interfaces versus Classes

Page 3: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Interfaces versus ClassesThe flexibility Issue

“You start programming before you fully specified the problem”: Incorporate newly discovered requirements into the existing code

as painlessly as possible

No Interface Use Interface CollectionLinkedList

Interface CollectionHashSet

Page 4: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Interfaces versus ClassesThe NodeList Case: Unsorted and Sorted NodeList

Firstly, a linear time for searching in a NodeList is acceptable.

But… gene expression data appears (thousand of variables), so we need logarithmic access time (sorted).

Sorted NodeList inclusion suppose a complexity introduction:

“One class has to manage the two versions”

Solution: NodeList as Interface SimpleNodeList manage the simple version. SortedNodeList manage the sorted version. No changes in the rest of the code (except at the constructors!!!!!!!).

Page 5: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

CouplingConfiguration and ContinuousConfiguration

Configuration of Continuous Variables seems a subclass of Configuration.

ContinuousConfiguration inherits from Configuration.

Hard to change the implementation: HashTable or Ordered version

(There aren’t interfaces)

Page 6: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

CouplingConfiguration and ContinuousConfiguration: Problems Some methods are valid for Configuration and not for Continuous

Configuration, but they are active and can’t be used: public int getValue(FiniteStates var)

void setValue(FiniteStates var, int value)

Some methods are implemented in Configuration and not overwritten in ContinuousConfiguration, but they can’t be used:

public isCompatible(Configuration conf)

void double hashCode()

All of them provokes runtime errors not compiling time errors. As more more funcionalities are added as stronger these problems becames.

Page 7: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

CouplingConfiguration and ContinuousConfiguration: Solution Configuration represents a set of assigments

or instatations of a set of random variables.

Everything is Discrete: FiniteStates, int…

X1 X2 X3 X4 Vector

1 0 0 1 Vector

Interface Configuration

void setVars(Collection Vars) void setValues(Collection Values) void setVar(FiniteStates var) void setValue(FiniteStates var, int v) FiniteStates getVar(int pos) int getValue(int pos)

Class SimpleConfigurationimplements

Page 8: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

CouplingConfiguration and ContinuousConfiguration: Solution A new Interface is defined:

Incompatible methods are removed:void int getValue(FiniteState node)

Not defined methods are removed:void int possibleValues()

Interface Configuration

void setVars(Vector Vars) void setValues(Vector Values) void setVar(FiniteStates var) void setValue(FiniteStates var, int v) FiniteStates getVar(int pos) int getValue(FiniteStates var) int possibleValues()

Interface ContinuousConfiguration

void setVars(Collection Vars) void setValues(Collection Values) void setVar(Node var) void setValue(Node var, int v) Node getVar(int pos) double getValue(Node var)

Page 9: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

CouplingConfiguration and ContinuousConfiguration: Solution

X6 X7 X8 X9 Vector Continuous

1.5 1.3 1.2 1.5 Vector Doubles

Class SimpleContConfigurationimplements

Configuration conf = new SimpleConfiguration()

Interface ContinuousConfiguration

• void setVars(Collection Vars)• void setValues(Collection Values)• void setVar(Node var)• void setValue(Node var, int v)• Node getVar(int pos)• double getValue(Node var)

void setVar(Node var) { if (var.isDiscrete()) conf.setVar(var) else varCont.insert(var)}

double getValue(Node var) { if (var.isDiscrete()) return (double) conf.getValue(var) else return valCont.elementAt(index(Var);}

Page 10: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

CouplingConfiguration and ContinuousConfiguration: Solution Advantages:

It is easier to change the underlying implementation.

We can even have several implementations: Configuration using HashTable. Configuration using Ordered List.

We would only need to change the constructor. Disadvantages:

Introduction of some duplicity in source code.

Page 11: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Agglomeration of FuncionalitiesDataBaseCases Class DataBaseCases manages the input, storage and

output of the data. Complexity of the class (without inheritance):

8 different constructors. >80 methods

Very different methods: divideIntoTrainAndTest Classification getPotentialTable Learning logLikelihood Clustering

Confusion in the class and for the programmer.

Page 12: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Agglomeration of FuncionalitiesNode Class Node represents a generic random variable. Complexity of the class (without inheritance):

35 different fields. >90 methods

Very different methods: getChildren GUI, Inference, Learning getNeighbourAt Inference getVisited Learning, Classification

Again, confusion in the class and for the programmer.

Page 13: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Agglomeration of FuncionalitiesNodeList Class NodeList represents a list of nodes. Complexity of the class (without inheritance):

7 different constructors. >50 methods

Very different methods: subSetsOfSize, randomOrder Learning getNodeString GUI isIncluded Inference

Again, more confusion in the class and for the programmer.

Page 14: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Agglomeration of Funcionalities Strange Classes There are many classes that are only used in a

mininum part of the code: NodeQueue ProbabilityTree ContinuousCaseListMem Learning RelationList Inference

Many different classes creates confusion and delay development times.

Very differents requirments among some modules: learning, inference, classification, influence diagrams, gui…

Page 15: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Agglomeration of FuncionalitiesConfusion in Concepts What’s a Node is?

Node in an Aciclic Graph. Node in a Decision Tree. A simple random variable of a data base.

Tracking an important error: NodeList copy():

copy.insertNode(node.copy()) OR copy.insertNode(node) node.copy() removes parents and child information. node without copy made a shallows copy of NodeList.

Solution: Create two different methods but complexity is added to the code.

Page 16: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesIntensive use of Interfaces

Based on several principles: Hollywood’s: “Don’t call us, we’ll call you’ Inversion of Control:

When Library Methods calls User Methods

Basically, you firstly define what you need and after you find out how you can implement it.

It is very generalized among important software programms: Eclipse Almost an standard for plugin development. Firefox, Thunderbird, Microsoft Outlook, IE Explorer… Some research tools: Bioclipse (biological data analysis), Spamato

(spam filtering), GumTree (scientific experiment platform),…

Page 17: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesPlugin Definition

What is a plugin? .jar File + plugin.xml

… and the key concepts: Extension Point:

An Interface + XML When a plugin wants to allow other plugins to extend or

customize some funcionalities. Extensions:

A class that implements the interface of some extension point

Page 18: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesPlugin Definition: An example

Let’s see an example:

Classifier and Evaluation would be the extension point:

public interface Classifier { void learn (DataBaseCases training, int c); Vector classify (Configuration instance, int c); }

Cla

ssif

ier

Classification PluginTreeClassifier Plugin

Weka Plugin

Eva

luat

ion

Evaluation Plugin

public interface Evaluated { String objectName (); double evaluate (DataBaseCases database, int c);}

Page 19: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesPlugin Definition: An example Classification

How to use an special data base for classification:

public interface ClassifierImprove { public void learn (DBClassifiy training); public Vector classify (Configuration instance); }

public interface DBClassifiy { DataBaseCases getData() int getClassIndex() void setData (DataBaseCases db) void setClassIndex (int index)}

public class ExtensionDBClassifiy { DataBasesCases case=null; int classIndex=-1 DataBaseCases getData() int getClassIndex() void setData (DataBaseCases db) void setClassIndex (int index)}

public interface EvaluatedImprove { String objectName (); double evaluate (DBClassifiy database);}

Page 20: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesPlugin Definition: An example Classification

The solid line indicates a common import…

Cla

ssif

ier

TreeClassifier Plugin

Weka Plugin

Eva

luat

ion

ClassifierImprove

Evaluation Plugin

Evaluation Improved

DBClassify

Classification Plugin

Elvira Core Plugin

Configuration

SimpleConfigurationHashConfiguration

Page 21: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesAdavantages

You can specify what you need before it is implemented.

You reduce the coupling among classes with Interfaces.

You allow external plugins to create their own classes (DBClassify) without introduce complexity in the basic plugins.

You can have different implementations of a core funcionality (i.e. Configuration normal or Hashtable or Ordered) at the same time. You just change the extension

Very flexible and easily extended by third-parties.

Page 22: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

Plugin Based ArchitecturesDisadvantages

Suppose a change in our programming habits.

There will be appear duplicated source code.

It could be hard to decide what is included in one plugin and what is included in another plugin.

It could be hard to decide what is a simple class and what is an interface or an extension point.

Introduce some complexity in the source code.

We would need to choose a plugin framework: eclipse, java plugin-framework…

Page 23: Plugin Based Architectures Elvira: Problems and Solutions Andrés R. Masegosa Department of Computer Science and A.I. Univerisity of Granada Puerto Lumbreras,

ENDAny questions?