Strategies to deal with problems when downcasting looks like the best fit - haskell

Usually, use sum types is pretty straightforward and works very well in many cases
data LifeForm = Grass | Sheep | Wolf
but when I want open my "LifeForm" on my problems I fall in heterogeneus containers, Data.Dynamic, Data.Typeable and so on (here a silly example using a simple String to discover the "type" at runtime).
I'll like extend my "LifeForm" in a manner that new "life forms" can interact and restrict between them with their own and new properties (classes).
Typically downcast at runtime is the simplest way in many languages.
I'm looking for useful strategies I could use to extend (without change the "core") containers when their elements may know or not optional properties of the others (eg. class Vegetable a, class Runner a, ...).
How do you deal with it?
Are Data.MultiConstrainedDynamic and others the best way?
Is preferable try to avoid this extensibility?
Thk!
UPDATED to clarify "What's not clear at all is what you wish you could do with it. "
Once I've my "lifeforms core":
I like add the "wind" defining a new class AffectedByTheWind a, now any lifeform could be affected by the wind (for every "world step" the "Wind life form" will check all AffectedByTheWind).
I like add the Dodo bird instancing class Fly a, class IsMeat a, class Might a, ... and automatically other "life forms" can interact with it.
I like add a "watcher life form" (a "spirit form") reporting how many life forms can run (instance class Runner a).
I like add a "behavior life form" (a "element form") that check how many IsMeat forms exists and reduce their vital energy.
...
All of this can be achieved using sum types or fixed type class hierarchy but is not extensible in the "expression problem" sense.

Related

How to automatically bind template types in Enterprise Architect when a class is realizing a generic interface

I have defined a generic interface using Enterprise Architect (see figure below).
I would now like to specify the following realization:
class AircraftsTypesRepository implements Repository<AircraftTypes, Integer>
Is there a way for EA to automatically bind types and method signatures to the generic types I specified in the base interface. In other words, I would like to show in the diagram that for the AircraftTypesRepository class, T and K and bound to T=AircraftTypes, and K=Integer. I would also like to see this reflected in the interface methods
I thought about this and (as there's no native support) would suggest to script that. There are plenty of ways, so I'd take a KISS one. The Realize relation could be adorned with tagged values named Bind<val> or so where <val> is the name of a template parameter (in your example T or K). These TVs should then be defined as RefGUID which allows them to link to an EA element. Creating these TVs should be one script which looks into the templated class. You find the template definition in the table t_xref with
SELECT description FROM t_xref
WHERE client = `<GUID of element>` AND type = `elment property`
This will contain something like
#ELEMENT;GUID={5EC3D8DF-BC37-4529-8F36-0D9BA363955D};Name=E;Type=ClassifierTemplateParameter;Pos=0;#ENDELEMENT;;
(I created an example with just T but you will decode it easily, I guess.)
Now that you have the tagged value(s) set in the Realize you can run a second script to synch the definition ("just" look for textually identical types). Later you could alter the TVs and re-synch again (AFAIK there's not hook for TVs being altered so that needs to be triggered manually).
This is not a complete solution but just a suggestion which leaves open quite some field for experimentation (and failure).

How to create abstract class in MagicDraw

I'm newbie in MagicDraw and I'd like to know how to specify a class as {abstract}.
I know about de property "Is abstract" in the Specification of Class, but I'd like that it appears in the header.
The place where you set isAbstract in MagicDraw is in the specification window for the class. To open that window, either right-click on the class or press enter while it is selected. The window will look like this:
You didn't specifically ask for more information, but I'll provide it in case you find it helpful.
The model you want to create will look like this:
Notice that Abstract Class is written in italics to indicate it is abstract. Also notice that {complete, disjoint} is specified for the generalization set. (Just FYI, {complete} is also known as a covering axiom.)
Beware that if you do not specify {complete}, you're creating a conflict with the isAbstract meta-property. The reason there's a conflict is that in UML, the default is {incomplete}, which means that you are allowed to create an instance of the super-class without it also being an instance of one of the sub-classes. That conflicts with isAbstract.

Haskell Types vs. Typeclasses in architecture

Suppose I have an application whose core data are Projects, Document Sets ("DocSets"), and Documents ("Docs"). Docs are not restricted to any particular content-type; they could be spreadsheets, images, HTML, etc., and can have functions/behaviors other documents don't have. Similarly, I might have multiple kinds of DocSets depending on the type of behavior I need. Maybe this DocSet presents the documents in a flat list, maybe that one associates a status with each document, and has some cool rendering trick based off of status.
The code might look like this if I wasn't using typeclasses:
data Project = Project {
projName :: Text,
projDocSets :: [DocSet]
}
data DocSet = DocSet {
dsName :: Text,
dsDocs :: [Doc]
}
data Doc = Doc {
docTitle :: Text,
docType :: Text,
docContents :: ByteString
}
The problem I see with this is while it allows there to be multiple kinds of Docs (via the docType field), it essentially uses manual type checking, which doesn't feel right (in my head). I could have data constructors for PagedTextDoc, ContinuousTextDoc, SpreadsheetDoc, ImageDoc, etc. That seems like poor modularity to me.
EDIT: My "poor modularity" comment relies on knowledge that wasn't communicated very well. Each Doc has some common behaviors
((de-)serialization, being grouped into a DocSet, a title, rendering,
others as the application evolves), and some unique behaviors
(internal representation in an easy-to-edit format, saving incremental
edits, partial rendering to HTML assuming application is a web app).
Even if that weren't the case, it seems to me like the app would be
more modular if I could just create a new module, define my document
and what you can do with it, and bang, document is now supported. That
way, I define my paginated document over here, my spreadsheet over
there, and the two don't care about each other at all.
Maybe I could use a typeclass for Docs?
data DocSet = DocSet {
dsName :: Text,
dsDocs :: [Doc]
}
class Doc d where
docTitle :: d -> Text
docType :: d -> Text
docContents :: d -> ByteString
The declaration for dsDocs now doesn't typecheck; lists only work for one particular concrete type, vs. any member of a typeclass. The DocSet definitely needs to be able to store/reference multiple kinds of documents.
I've tried to do some searching, but frankly, my Google skills completely fail here. Am I thinking about the data structures correctly? Is the approach with all of (Proj, DocSet, Doc) as a single data constructor each really the best way of handling this, or am I missing something about the way Haskell handles polymorphism?
Well, the class-based proposal doesn't even work. Classes aren't types. You can't have [Doc] if Doc is a class.
The usual approach here is to determine what it is you're modeling, exactly.
Do you want a bunch of things that behave uniformly, and can easily be packaged up together? Then put that behavior into a type. This is usually done by creating a record that holds a function for each kind of behavior you want.
On the other hand, do you have a bunch of things that require very different handling? In that case, put the data into a bunch of types and let the type checker (and exhaustiveness checker) guide you along the way to make sure you're always handling the right thing in the right place.
I don't recommend using classes for this at all. Classes are (mostly) good only for the situation where you can write type-polymorphic code that is meaningful when there is a concrete type, but you don't need to know what the type is. (This is a slight exaggeration, but it's close enough to the truth to use it as a first-approximation heuristic.)
Suppose I have an application whose core data are Projects, Document
Sets ("DocSets"), and Documents ("Docs"). Docs are not restricted to
any particular content-type; they could be spreadsheets, images, HTML,
etc., and can have functions/behaviors other documents don't have.
It sounds like you don't know that types can have multiple constructors (sum types). Instead of your Doc do:
data Doc = PagedTextDoc {docTitle :: Text, docContents :: ByteString}
| SpreadsheetDoc {docTitle :: Text, docContents :: ByteString}
... etc
Similarly, I might have multiple kinds of DocSets depending on the
type of behavior I need. Maybe this DocSet presents the documents in a
flat list, maybe that one associates a status with each document, and
has some cool rendering trick based off of status.
data DocSet = ListDocset Text [Doc] | KoolDocset [(Status, Doc)] | ...etc

Any modifications i should do on this class diagram?

I'm working on a class diagram of a simple project.
Here's a basic description of it:
"usuario" is the 'user class'. From it, i can have "membro_coral, "Membro_comissao" and "Administrador". "membro_coral" can post some news on the web site, but it goes through an avaliation to see if it can be posted or not. "Membro_coral" has a "perfil" associated with it.
There's the "evento" class. It's a class for mapping events. Users can give their disponibilities to attend to some events. And "escalacoes_disponibilidades" is the junction table.
I've added Three classes corresponding to the "Perfil" values. Some people are "membro_comissao", some people are "membro_coral" and actualy, only one is the "Administrador".
Is this class diagram right? Any modifications i should do?
EDIT: i've update my class diagram. I've added the "Status_Noticia" class. It's associated with noticia. I've done this in order to improve the statemachine diagram of "Noticia" (there's an use case which an user can send news to de displayed, but it goes through an avaliation process).
It's mostly correct. There are some minor observations however:
Membro_comissao, Membro_coral and Administrator are kind of users, right? If so, you should do one of the following: a) derive all 3 classes form user, or b) create a new class (enumerator) TipoUsuario, with those 3 values. Which option to implement depends on if there are some special features that make the 3 different one from another (some different attributes, methods or maybe associations (see the next point)
You said: " "Membro_coral" has a "perfil" associated with it.". Than this class should be associated with PErfil and not otherwise. This could be a reason to apply the option a) from before. This association should be a normal assoc. and not a composition
So, the main decision you must take is how to model user types.

How to really define what a "helper" is?

How can we really say "Here it is, the class I just developed is a helper!"
Would the class have some special criteria? I have a class in Java that exporta my JTables into CVS Files. Is it a helper? I also use a class to validate my forms. Is it a helper?
I hear this term often, but I realize that I don't really know what is it.
I don't think there is a 'textbook' definition of what a helper is, but I think most people would define a helper Class as one that makes no sense on its own - in other words, a Class whose whole purpose of existence it to 'help' make some operation on another class easier to do.
For example, if you create a class that follows the 'Adapter' pattern (i.e. it wraps an object of type 'foo' and lets it be used where normally you can only use type 'bar',) I think that adapter class could loosely be called a 'helper' class as well.
A helper class is a class that is used only by another class to complete a function. Thus, the helper has no right or need to be public, and is only created as being private.
For example, Private Class B helps public class A complete a set of tasks, A is calling on B and is of no use on its own.
For example (just a quick write-up, not tested)
private int newInt(String str)
{
test = Integer.parseInt(str);
return test;
}
and it could be called by:
public int transformation(String str)
{
return newInt(str);
}
This of course would be a class of its own, which would have to be called in your main class.
From some of the quick research I did, and my own personal experience when dealing with helper classes (.Net and Java), a helper class, is a class that provides functionality that is not actually relevant to the code one is developing, but rather provides some boilerplate work, such as casting, converting datatypes, or performing some common mathematical functions, for instance.
Such code can be generally reused in other projects, to further facilitate work being done.
Not everything helps should be a helper, a class wouldn't even exist if it didn't 'help' something. Usually helper classes offer shortcuts, like sql helpers. I think they should be something spesific and contain a group of relevant shortcut functions.
I think a "helper" class is a class which has been created only to support another classes on their functions.
There are a lot of cases where you need to create a "helper" class to put on it some methods that they do some functions. Some examples would be a class that have methods to calculate holidays on a year or transform some fields or formats in another.
Another example could be when you have to develop an app that it has a progress bar. You can create a "helper" class only for manage the progress bar (setting texts, calculating times, providing estimations...).
If you divide your app on layers (GUI, Business, DAO, etc.), it's interesting to have some "helper" or "utility" class (or classes) to do some tasks that you need to do but that they don't belong to GUI, business or DAO (Data Access Objects) layers explicitly. In this case, you could have an utility class for each layer and put inside the auxiliary methods that you need.

Resources