How can an Alloy constraint put a set inside its subset? - alloy

The following Alloy code says that each hotel room has a set of keys:
sig Key {}
sig Room {
keys: set Key
}
The keys relation needs to be constrained. As it stands, it permits instances such as this: key K1 is used in a bunch of rooms. Ouch! We don't want that. We want each key to be used only with one room. Here's a graphic illustrating the universe of valid instances (and the subset of instances that we actually want to allow):
The set of instances that we actually want is nicely expressed with this Alloy code:
Room lone -> Key
The instances for that Alloy code is depicted in the above graphic by the small circle.
So, how do we constrain keys? One answer is this: create an Alloy fact which says this:
keys in Room lone -> Key
Think about what that is graphically saying. It is saying that the big circle must be inside the little circle (see below). Isn't that weird? How can a circle be inside its sub-circle? Can someone give me some intuition about this please? It seems odd.

if you only have sig Room {keys: set Key} without any additional facts/constraints, the domain for the keys relation is the big circle;
you can decide to add some constraints (like keys in Room lone -> Key) exactly for the purpose of shrinking the domain for the keys relation (so that it becomes the small circle).
So the right way to think about it is not that the big circle must be inside the small circle (?!); rather, think of it as using the small circle instead of the big circle as the domain (set of all valid values) for keys.

Related

What is the difference between st_isvalid and st_issimple?

In Postgis there are two very similar functions. One is st_isValid, the other one is st_isSimple. I'd like to understand the difference between both for Polygons. For the st_isValid we have:
Some of the rules of polygon validity feel obvious, and others feel arbitrary (and in fact, are arbitrary).
Polygon rings must close.
Rings that define holes should be inside rings that define exterior boundaries.
Rings may not self-intersect (they may neither touch nor cross one another).
Rings may not touch other rings, except at a point.
For the st_isSimple we've got:
Returns true if this Geometry has no anomalous geometric points, such as self intersection or self tangency. For more information on the OGC's definition of geometry simplicity and validity, refer to "Ensuring OpenGIS compliancy of geometries"
Does it mean that any valid polygon is automatically simple?
Both functions check for similar OGC definition compliancy of geometries, but are defined for different geometries (by dimension);
By OGC definition
a [Multi]LineString can (should) be simple
a [Multi]Polygon can (should) be valid
This implies that
a simple [Multi]LineString is always considered valid
a valid [Multi]Polygon is always considered simple (as in, it must have at least one simple closed LineString ring)
thus the answer is yes.
Strictly speaking, using the inherent checks of the OGC defined functionality on the 'wrong' geometry type is useless.
PostGIS, however, liberally extends the functionality of ST_IsValid to use the correct checks for all geometry types.

How to represent 2 classes with references to each other in UML?

I am trying to represent my program in a UML diagram. I have 2 classes as below:
Mesh.h
class Mesh
{
public:
Mesh();
~Mesh();
VertexArrayObject* vao;
};
VertexArrayObject.h
class VertexArrayObject
{
public:
VertexArrayObject(Mesh* mesh);
~VertexArrayObject();
Mesh* mesh;
};
I'd imagine it'd be drawn like this:
However that doesn't look right at all. How is it best to represent a relationship where both classes have references to each other using UML?
No. That's plain wrong. Composite aggregation can only be on one side. (Imagine the car/wheel example: each aggregating the other is nonsense.) Remove the diamonds and you're done.
You can go further and put dots instead of the diamonds. That will mean that both sides have attributes referring the class at the other side. See this answer.
This depends a lot on what the logical construct is you are wanting to represent. You could specify the relationship between Mesh and Vertex in many ways and get the message across; what I understand from the model you have posted is that you think composition may be an important aspect of the relationship, e.g. Vertex is a composite part of Mesh (or vice versa).
In UML you can’t really have a bidirectional composition since this leads to a logical paradox — I.e. you cant simultaneously have two classes that are at the same time both composite parts of each other. If composition is important then you will need to choose which is composed of which.
Given my (poor) understanding of 3D (I guess that’s what your program is about), I would assume that a Mesh includes a collection of Verticies, so the composition would run from the Vertex type to the Mesh type with the solid diamond at the Mesh end. You might also want to add multiplicities (so that it is obvious that multiple Vertex may exist within the Mesh) and also consider whether the relationship is composite or a shared aggregate (can a Vertex exist outside a Mesh? If so a shared aggregate (white diamond) is needed).
You probably don’t need to represent the relationship from Mesh to Vertex, as this is an implementation detail that may be safely abstracted away in UML. The fact that you’re using a pointer to an array is probably not relevant to the logic that Mesh is composed of many Vertex.
That said, if you do want to specify what you have in code precisely then this can be done in UML. I would recommend abstracting out the Array of Vertex from Vertex — there are three logical types in your code — Mesh, Vertex and VertexArray. You might illustrate Mesh having a basic association with VertexArray (with an arrow to identify direction of reference), and this in turn being composed of Vertex (black diamond at VertexArray end).
Of course, I am assuming that VertexArray represents an array of Vertex objects, not a single Vertex!

To Have An ID or Not To Have An ID - Regarding Value Object

Let's say two domain objects: Product and ProductVariety (with data such as color, size etc). The relationship between these two is one-to-many. Conceptually saying in the domain driven design, the ProductVariaty should be a value object, which is not the same object once its data is changed. From the implementation point of view, however, it is better to have some sort identification for the ProductVariaty so that we know which ProductVariety is selected and so on. Is an only solution to convert it to an entity class?
The following is a code segment to illustrate this situation.
#Embeddable
class ProductVariety {...}
#Entity
class Product {
#ElementCollection
private Set<ProductVariety> varities;
...
}
Conceptually saying in the domain driven design, the ProductVariaty should be a value object, which is not the same object once its data is changed
That's not quite the right spelling. In almost all cases (many nines), Value Object should be immutable; its data never changes.
Is an only solution to convert it to an entity class?
"It depends".
There's nothing conceptually wrong with having an identifier be part of the immutable state of the object. For example, PANTONE 5395 C is an Identifier (value type) that is unique to a particular Color (value type).
However, for an identifier like PANTONE 5395 C to have value, it needs to be semantically stable. Changing the mapping of the identifier to the actual color spectrum elements destroys the meaning of previous messages about color. If the identifier is "wrong", then the proper thing to do is deprecate the identifier and nominate a replacement.
Put simply, you can't repaint the house by taking the label off the old paint can and putting it on a new one.
In that case, there's no great advantage to using the identifier vs the entire value object. But its not wrong to do so, either.
On the other hand, if you are really modeling a mapping, and you want to follow changes that happen over time -- that's pretty much the definition of an entity right there.
What it really depends on is "cost to the business". What are the trade offs involved, within the context of the problem you are trying to solve?
Note: if you really do find yourself in circumstances where you are considering something like this, be sure to document your cost benefit analysis, so that the next developer that comes along has a trail of breadcrumbs to work from.

uml exercise Texas'Hold'Em Poker

I'm really a beginner in dealing in Java, suprise, and we have have to make an UML class chart for a situation. I'm totally unsure about my UML-diagram and I wanted to ask, if someone with a little bit more expierience could take a look. Thank you.
This is the situation:
For poker in the Texas Hold'em variant following information is given:
For a pokergame you need between two and twelve players and a card deck, that has 52 cards. Each card has a label (e.g., "King"), a value (2 to 14) and a color (e.g., Cross). Further specified for a poker game are the sum of all inserts (the pot, e.g., 450), and five (common) cards from the deck.
For each player the name, the credit (e.g., 7592) and the current hand, two cards from the card deck and a value (e.g., "Full House?").
For each player it is also noted whether he has the dealer position or not.
Task: Design an UML class chart for the game without functionalities (the classes to be used are indicated by bold). For the classes, type the necessary instance variables, including their (Java-compliant) data types. Draw all the relationships between the classes. Pay attention to the information given above multiplicities.
This is my solution:
What is meant by functionalities? Did I do the relations right? Do I need methods?
I tried to get the information from the text into a diagram:
You could replace the enumerations by plain attributes of type 'String'. I didn't fill out all values of HandValue (the three dots are not valid UML).
It is not clear to me whether a hand always has a value. If it has, then replace multiplicity 0..1 by 1.
If I understand the text correctly, a hand has only two cards, but this seems odd to me, I think it should be five.
You could add a composition diamond on the association between CardDeck and Card, but it is not clear from the text whether this is appropriate.
You could add a constraint, that the commonCards of a PokerGame should be a subset of the cards of the cardDeck of the PokerGame; also, that the cards of the hand of each of the players of the PokerGame are a subset of the cards of the cardDeck of the PokerGame.
You could draw an open arrowhead at the end of each association (not a triangle arrowhead; this would mean generalization), i.e. at the side where I have mentioned the multiplicity and the instance variable name.
You could specify multiplicities at the other ends of the associations, but these multiplicities are not mentioned in the text.
I didn't specify any visibilities (public/private/protected), because these are not specified in the text either.
You should not so much care about aggregation. This does not add much value to a design (but only in rare cases). Firstly your arrows are wrong. They represent generalization in UML. You need simple associations. Just leave away the arrows (which need to be unfilled open triangles) except you want to express navigability to be just in one direction (which in most cases is also nothing you need absolutely). What you should do is to use roles at the ends of the associations rather then putting typed properties in the classes. Further you should follow conventions that say class names start with an upper case letter (though I'm not really familiar with Java).
The above is a partial model to show what I mean. Note that I have added a dealer association. This assures that there is exactly one dealer in the game. The {subsets players} constraint tells that the dealer must be one of the players (thanks to JimL. for the hint). Using just the flag could lead to multiple dealers with the flag set. There should be a constraint that tells isDealer is true only for the one that is linked with the dealer association.

property owned by an association vs. property owned by a class?

What is the differences between property owned by an association and a property owned by a class in UML?
is there a simple example helps me understand the differences?
The difference is more conceptual than anything else. If you have a property attached to an association, then you will have an association class, with the desired property.
Here is an example of a mailman sending letters to clients (the attribute weight is bound to the association):
The difference is very concrete, but traditionally ignored.
A is associated to B. A,B are classes.
If B end of the association is "association owned" and navigable, it means, that you can easily reach instance of B from A, either by reference, or by some method(s). It should be shown by arrow.
If B end is "classifier owned", you know a bit more. it means, that A has an attribute, that is a direct reference. (No functions or reference counting here). It should be shown by arrow and "dot"- a small black filled circle.
If you are going to B by a.smth.smthelse.b, it is arrow, but surely no dot.
If you are going to B by a.b, it is arrow and dot.
If you haven't decided yet, it is arrow again.
Traditionally modellers show arrows only. But it is not a good tradition, and is against UML standard. Diagrams are more useful if we decide as much as possible on them, not in code.
If both ends are navigable, both arrows disappear and you may see only dot(s).
The association lines with cross with dot on one side, or arrow on one side and only dot on the other are senseless.

Resources