I want to modelate the producer consumer problem, with an infinite buffer that recive generate data from productor, and stores them by a priority level: High, Medium, Low.
Then, the consumer extract them and process then,asking only for one per time. The buffer will give the oldest one with highest priority. How can I do with use? I have modeled this:
model producerconsumer
class Productor
attributes
datos : String
` prioridad: Prioridad
end
class priority < Productor
attributes
High: String
Medium: String
Low: String
end
class buffer
attributes
data : String
time: int
operations
count(time):int
end
class consumer
attributes
data: String
operations:
extract(data)
process():data
end
-- associations
association send between
Productor [1] role input
buffer [1] role output
end
association send between
buffer [1] role input
consumer [1] role output
end
Related
I have doubts about where I should check my invariants...
For example, I have a Question aggregate with the following invariant:
The question's text must be not null
The question's text must have a length between 100 and 500 characters
I have read that the best place to check the invariants is in aggregate's constructor, but I have also read it would be recommended push domain logic in value objects, for example.
A possible aggregate's implementation could be:
public class Question {
private final id: QuestionId;
private final text: QuestionText
public Question(id: String, text: String) {
// verify invariants
ensureLengthText(text); // this method verifies the invariant
this.id = new QuestionId(id);
this.text = new QuestionText(text);
}
}
QuestionId and QuestionText are value objects.
In this case, in the aggregate we can see the invariant explicitly.
But, if we push that invariant logic in QuestionText value object, in the aggregate we will not see that invariant... then, what would be the best approach?
If you can easily enforce an invariant (e.g. "Question texts cannot be shorter than 100 characters nor longer than 500 characters") in a value object, I recommend doing so. Question inherits that invariant (and any and all others from QuestionText) by saying that text is a QuestionText.
I have a constructor that receives multiple parameters so it is a line a bit long. I'm working on the UML diagram for the class but I'm not sure if I could break the line and continue on the next one so that the class box does not take that much room. Probably this is something that has been asked before, but I haven't found a good reference on what to do in this case.
public Product(String name, int code, int price, int quantity,
int minQuantity, int maxQuantity) {
super();
this.name = name;
this.code = code;
this.price = price;
this.quantity = quantity;
this.minimumQuantity = minQuantity;
this.maximumQuantity = maxQuantity;
}
The UML specification does not specify whether wrapping the text on multiple lines is allowed or not. Because it's not forbidden, I would say it is allowed.
Personally, I would not show long parameter lists in class diagrams. A class diagram should primarily give an overview of classes and their relationships. To publish the details of the operations, it is usually better to generate textual documentation from the source code.
9.6.4 of UML 2.5.1 (page 118) states
The parameter list may be suppressed.
or even better, you may suppress the complete list of operations.
9.2.4.1 of UML 2.5.1 (page 101) states
Any compartment may be suppressed.
I have to use Alloy in a Requirements Analysis and Specification Document for a university project. I started with the easy stuff, only signatures and no facts. These are the signatures I use:
abstract sig Date{
year: one Int,
month: one Int,
day: one Int
}
abstract sig Time{
h: one Int,
m: one Int,
s: one Int
}
abstract sig Double{
leftPart: one Int,
rightPart: one Int
}
abstract sig Bool{
value: one String
}
sig DateBirth extends Date{}
sig DateRide extends Date{}
sig DateExpiry extends Date{}
abstract sig User {
email: one String,
name: one String,
surname: one String,
gender: one Bool,
address: one String,
password: one String,
dateOfBirth: one DateBirth,
IDRide: set Ride
}
sig TaxiDriver extends User{
taxiLicense: one String,
personalLicense: one String,
IBAN: one String,
positionInQueue: lone Int,
IDTaxi: set Taxi
}
sig Client extends User{
}
sig Zone {
numberOfZone: one Int,
vertexNorthWest: one Double,
vertexNorthEast: one Double,
vertexSouthWest: one Double,
vertexSouthEast: one Double,
currentQueue: set TaxiDriver
}
sig Taxi {
IDTaxi: one String,
plate: one String,
availablePlaces: one Int,
}
sig Ride {
IDRide: one String,
origin: one String,
destination: one String,
dateOfRide: one DateRide,
timeOfDeparture: one Time,
timeOfArrival: one Time,
price: one Double,
numberOfPeople: one Int,
accepted: one Bool,
userEmail: set User
}
sig Credit_Card {
number: Double,
owner: String,
expiryDate: DateExpiry,
ownerEmail: one Client
}
Then, I added the predicate "show" to veify whether the it is consistent or not:
pred Show{}
run Show for 10
After running "Execute" on Alloy Analyzer 4.2 this is the message I get:
Executing "Run Show for 10"
Solver=sat4j Bitwidth=4 MaxSeq=7 SkolemDepth=1 Symmetry=20
21067 vars. 3840 primary vars. 37164 clauses. 376ms.
Instance. found. Predicate is consistent. 375ms.
No problems, right? But then, when I click on "Show" there are no instances of the signature "User" (and its child signatures) shown on the display, while all the others are there. I tried to click on "Next" a gazillion times to try to see if maybe I could find a model in which they were shown, but there weren't any.
Any idea/suggestion? Thanks!
It's probably because of the use of String. As far as I know, String is a reserved word in Alloy, but it is not really implemented at this point. Try to remove the String fields or replace them with something else.
On a more general note, Alloy is not so much about modelling real data (ints, bools and strings), but more about modelling structure, i.e. relationships between entities. For the analysis of structure, you usually don't need concrete data types.
The purpose of building an Alloy model is to capture the essence of a design or system and explore subtle properties. You don't want to include all the details you'd find in a database schema. Your model has lots of implementation details too, such as the ids (which aren't needed since they're implicit in the object identities), and the use of strings instead of conceptual types -- destination, eg, should have a type such as "Location".
So I'd recommend that you start again, and think first about what kinds of questions you'd like this model to answer.
Thanks to everyone, removing strings solved the problem.
However, my "distorted" vision about Alloy's purpose was due to the fact that we were asked to use it but we weren't given a real explanation on how to use it, in most examples all details were written. I guess I'll have to try and study it a bit more!
I was wondering if when calling reduceByKey in apache spark streaming the order of the records in the stream were guarantied. Basically part of the computation I do has to get the last value.
Here's an example:
JavaPairDStream< String, Double > pairs; // ...
pairs.reduceByKey( new Function2<Double, Double, Double>() {
#Override public Double call(Double first, Double second) throws Exception {
return second;
}
});
No, it isn't. The intention of Map Reduce is to parallize tasks and when parallized you cannot guarantee order. The previous results might get shuffled on the way to the reduce processor. Note that the reduce processor won't wait for all results to arrive, he justs grabs two values and starts reducing.
Once created, the distributed dataset (distData) can be operated on in parallel. For example, we might call distData.reduce((a, b) => a + b) to add up the elements of the array.
I have a memory address pool with 1024 addresses. There are 16 threads running inside a program which access these memory locations doing either read or write operations. The output of this program is in the form of a series of quadruples whose defn is like this
Quadruple q1 : (Thread no, Memory address, read/write , time)
e.g q1 = (12,578,r,2t), q2= (16,578,w,6t)
I want to design a program which takes the stream of quadruples as input and reports all the conflicts which occur if more than 2 threads try to access the same memory resource inside an interval of 5t secs with at least one write operation.
I have several solutions in mind but I am not sure if they are the best ones to address this problem. I am looking for a solution from a design and data structure perspective.
So the basic problem here is collision detection. I would generally look for a solution where elements are added to some kind of associative collection. As a new element is about to be added, you need to be able to tell whether the collection already contains a similar element, indicating a collision. Here you would seem to need a collection type that allows for duplicate elements, such as the STL multimap. The Quadraple (quadruple?) would obviously be the value type in the associative collection, and the key type would contain the data necessary to determine whether two elements represent a collision, i.e. memory address and time. In order to use a standard associative collection like STL multimap, you need to define some ordering on the keys by defining operator< for the key type (I'm assuming C++ here, you didn't specify). You define a collision as two elements where the memory location is identical and the time values differ by less than some threshold amount. The ordering of the key type has to be such that two keys that represent a collision come out as equivalent under the ordering. Equivalence under the < operator is expressed as a < b is false and b < a is false as well, so the ordering might be defined by this operator:
bool operator<( Key const& a, Key const& b ) {
if ( a.address == b.address ) {
if ( abs(a.time - b.time) < threshold ) {
return false;
}
return a.time < b.time;
}
return a.address < b.address;
}
There is a problem with this design, due to the fact that two keys may be equivalent under < without being equal. This means that two different but similar Quadraples, i.e. two values that collide with one another, would be stored under the same key in the collection. You could use a simpler definition of the ordering
bool operator<( Key const& a, Key const& b ) {
if ( a.address == b.address ) {
return a.time < b.time;
}
return a.address < b.address;
}
Under this ordering definition, colliding elements end up adjacent in an ordered associative container (but under different keys), so you'd be able to find them easily in a post-processing step after they have all been added to the collection.