答:
I had a similar case where I wanted to distinguish different meanings of some integer values, and forbid implicit conversions between them.  I wrote a generic class like this:

```<code>template <typename T, typename Meaning>
struct Explicit
{
  //! Default constructor does not initialize the value.
  Explicit()
  { }

  //! Construction from a fundamental value.
  Explicit(T value)
    : value(value)
  { }

  //! Implicit conversion back to the fundamental data type.
  inline operator T () const { return value; }

  //! The actual fundamental value.
  T value;
};
</code>```

Of course if you want to be even more safe, you can make the T constructor explicit as well.  The Meaning is then used like this:

```<code>typedef Explicit<int, struct EntityIDTag> EntityID;
typedef Explicit<int, struct ModelIDTag> ModelID;
</code>```

Title: C++ strongly typed typedef

Body:
I've been trying to think of a way of declaring strongly typed typedefs, to catch a certain class of bugs in the compilation stage. It's often the case that I'll typedef an int into several types of ids, or a vector to position or velocity:

```<code>typedef int EntityID;
typedef int ModelID;
typedef Vector3 Position;
typedef Vector3 Velocity;
</code>```

This can make the intent of code more clear, but after a long night of coding one might make silly mistakes like comparing different kinds of ids, or adding a position to a velocity perhaps.

```<code>EntityID eID;
ModelID mID;

if ( eID == mID ) // <- Compiler sees nothing wrong
{ /*bug*/ }

Position p;
Velocity v;

Position newP = p + v; // bug, meant p + v*s but compiler sees nothing wrong
</code>```

Unfortunately, suggestions I've found for strongly typed typedefs include using boost, which at least for me isn't a possibility (I do have c++11 at least). So after a bit of thinking, I came upon this idea, and wanted to run it by someone.

First, you declare the base type as a template. The template parameter isn't used for anything in the definition, however:

```<code>template < typename T >
class IDType
{
    unsigned int m_id;

    public:
        IDType( unsigned int const& i_id ): m_id {i_id} {};
        friend bool operator==<T>( IDType<T> const& i_lhs, IDType<T> const& i_rhs );
};
</code>```

Friend functions actually need to be forward declared before the class definition, which  requires a forward declaration of the template class.

We then define all the members for the base type, just remembering that it's a template class.

Finally, when we want to use it, we typedef it as:

```<code>class EntityT;
typedef IDType<EntityT> EntityID;
class ModelT;
typedef IDType<ModelT> ModelID;
</code>```

The types are now entirely separate. Functions that take an EntityID will throw a compiler error if you try to feed them a ModelID instead, for example. Aside from having to declare the base types as templates, with the issues that entails, it's also fairly compact.

I was hoping anyone had comments or critiques about this idea?

One issue that came to mind while writing this, in the case of positions and velocities for example, would be that I can't convert between types as freely as before. Where before multiplying a vector by a scalar would give another vector, so I could do:

```<code>typedef float Time;
typedef Vector3 Position;
typedef Vector3 Velocity;

Time t = 1.0f;
Position p = { 0.0f };
Velocity v = { 1.0f, 0.0f, 0.0f };

Position newP = p + v*t;
</code>```

With my strongly typed typedef I'd have to tell the compiler that multypling a Velocity by a Time results in a Position.

```<code>class TimeT;
typedef Float<TimeT> Time;
class PositionT;
typedef Vector3<PositionT> Position;
class VelocityT;
typedef Vector3<VelocityT> Velocity;

Time t = 1.0f;
Position p = { 0.0f };
Velocity v = { 1.0f, 0.0f, 0.0f };

Position newP = p + v*t; // Compiler error
</code>```

To solve this, I think I'd have to specialize every conversion explicitly, which can be kind of a bother. On the other hand, this limitation can help prevent other kinds of errors (say, multiplying a Velocity by a Distance, perhaps, which wouldn't make sense in this domain). So I'm torn, and wondering if people have any opinions on my original issue, or my approach to solving it.

Body:
I had a similar case where I wanted to distinguish different meanings of some integer values, and forbid implicit conversions between them.  I wrote a generic class like this:

template <typename T, typename Meaning>
struct Explicit
{
  //! Default constructor does not initialize the value.
  Explicit()
  { }

  //! Construction from a fundamental value.
  Explicit(T value)
    : value(value)
  { }

  //! Implicit conversion back to the fundamental data type.
  inline operator T () const { return value; }

  //! The actual fundamental value.
  T value;
};

Of course if you want to be even more safe, you can make the T constructor explicit as well.  The Meaning is then used like this:

typedef Explicit<int, struct EntityIDTag> EntityID;
typedef Explicit<int, struct ModelIDTag> ModelID;


问:
Find points whose pairwise distances approximate a given distance matrix
Problem. I have a symmetric distance matrix with entries between zero and one, like this one:

```<code>D = ( 0.0 0.4 0.0 0.5 )
    ( 0.4 0.0 0.2 1.0 )
    ( 0.0 0.2 0.0 0.7 )
    ( 0.5 1.0 0.7 0.0 )
</code>```

I would like to find points in the plane that have (approximately) the pairwise distances given in D. I understand that this will usually not be possible with strictly correct distances, so I would be happy with a "good" approximation.

My matrices are smallish, no more than 10x10, so performance is not an issue.

Question. Does anyone know of an algorithm to do this?

Background. I have sets of probability densities between which I calculate Hellinger distances, which I would like to visualize as above. Each set contains no more than 10 densities (see above), but I have a couple of hundred sets.

What I did so far.

I did consider posting at math.SE, but looking at what gets tagged as "geometry" there, it seems like this kind of computational geometry question would be more on-topic here. If the community thinks this should be migrated, please go ahead.

This looks like a straightforward problem in computational geometry, and I would assume that anyone involved in clustering might be interested in such a visualization, but I haven't been able to google anything.

One simple approach would be to randomly plonk down points and perturb them until the distance matrix is close to D, e.g., using Simulated Annealing, or run a Genetic Algorithm. I have to admit that I haven't tried that yet, hoping for a smarter way.

One specific operationalization of a "good" approximation in the sense above is Problem 4 in the Open Problems section here, with k=2. Now, while finding an algorithm that is guaranteed to find the minimum l1-distance between D and the resulting distance matrix may be an open question, it still seems possible that there at least is some approximation to this optimal solution. If I don't get an answer here, I'll mail the gentleman who posed that problem and ask whether he knows of any approximation algorithm (and post any answer I get to that here).

答:
There seems to be a very simple solution here. (edited)

Set the coordinates of each point to be (x_i, y_i, z_i)
Form a system of quadratic equations according to the distance matrix.
For each equation adds a delta value
so we have something like
(x_a - x_b)^2 + (y_a - y_b)^2 + (z_a - z_b)^2 = D_ab + delta_ab.
Move D_ab to the other side and you have only delta_ab
Square each side so you have delta_ab always non negative.

Sum all the squared deltas and you have a multi-variable function. (almost miss this part)

The goal now is to minimize the sum of all those deltas.
This can easily be done with gradient descent.

Let me know if there is anything wrong with this approach.

ContentLicense: CC BY-SA 3.0


问:
Understanding branching strategy/workflow correctly
I'm using svn without branches (trunk-only) for a very long time at my workplace. I had discovered most or all of the issues related to projects which do not have any branching strategy. Unlikely this is not going to change at my workplace but for my private projects.

For my private projects which most includes coworkers and working together at the same time on different features I like to have an robust branching strategy with supports long-term releases powered by git.

I find out that the Atlassian Toolchain (JIRA, Stash and Bamboo) helped me most and it also recommending me an branching strategy which I like to verify for the team needs.

The branching strategy was taken directly from Atlassian Stash recommendation with a small modification to the hotfix branch tree. All hotfixes should also merged into mainline.

The branching strategy in words

mainline (also known as master with git or trunk with svn) contains the "state of the art" developing release. Everything here was successfully checked with various automated tests (through Bamboo) and looks like everything is working. It is not proven as working because of possible missing tests. It is ready to use but not recommended for production.

feature covers all new features which are not completely finished. Once a feature is finished it will be merged into mainline. Sample branch: feature/ISSUE-2-A-nice-Feature

bugfix fixes non-critical bugs which can wait for the next normal release. Sample branch: bugfix/ISSUE-1-Some-typos

production owns the latest release.

hotfix fixes critical bugs which have to be release urgent to mainline, production and all affected long-term releasees. Sample branch: hotfix/ISSUE-3-Check-your-math

release is for long-term maintenance. Sample branches: release/1.0, release/1.1 release/1.0-rc1

I am not an expert so please provide me feedback. Which problems might appear? Which parts are missing or slowing down the productivity?

答:
http://nvie.com/posts/a-successful-git-branching-model/ outlines a comparable branching model which the writer claims to have been using successfully on a number of projects.

Key points:

two long-lived branches, master and development, with the former for production-ready code, and the latter being the integration branch.

branches for feature, release and hotfix. These exist just for the duration of the relevant work, unlike the long-lived branches mentioned above.

feature branches come off development and are merged back into it when complete. These typically exist in developer repositories only.

release branches come off development and are merged back into development and master. The goal being to prevent big breaking changes to master causing problems for an imminent release. Hence these branches are typically created just before a release is due.

hotfix branches come off master and are merged back into master and development. These are intended to address critical production issues.


答:
The shortest answer: No single branching strategy is going to be optimal for every project, as such the strategy you have above will be perfect for some projects you have, and useless for others.

The various factors you need to keep in mind are (among others, if any more are suggested i will happily edit them into this answer):

Does the project have unique releases, or is it continuously released?

Is the project software distributed to customers, or made available as a service on your own hardware?

If the software has unique releases, do newer releases always obsolete older releases, or will older releases also need to be maintained?

How experienced are your contributors? Are they all highly experienced developers or will you need to make concessions to non-technical personnel like designers?

How much time am i willing to invest in keeping the project history clean and tidy?

The strategy you described above is optimal for the case of:

The project has unique releases.

The project code is distributed to customers.

Older releases will need to be maintained.

! It does not describe the way merging is handled, i.e. rebase-first, no-ff, existence of merge commit, etc. and as such is ambiguous on the point of contributor experience.

! Since it does not address merging, it is also ambiguous about the point of time investment.

That said, a much more useful question to ask is in this form: "My project is structured like [this], what is the optimal branching strategy for this, what are the drawbacks it has?"

Title: Understanding branching strategy/workflow correctly

Body:
I'm using svn without branches (trunk-only) for a very long time at my workplace. I had discovered most or all of the issues related to projects which do not have any branching strategy. Unlikely this is not going to change at my workplace but for my private projects.

For my private projects which most includes coworkers and working together at the same time on different features I like to have an robust branching strategy with supports long-term releases powered by git.

I find out that the Atlassian Toolchain (JIRA, Stash and Bamboo) helped me most and it also recommending me an branching strategy which I like to verify for the team needs.

The branching strategy was taken directly from Atlassian Stash recommendation with a small modification to the hotfix branch tree. All hotfixes should also merged into mainline.

The branching strategy in words

mainline (also known as master with git or trunk with svn) contains the "state of the art" developing release. Everything here was successfully checked with various automated tests (through Bamboo) and looks like everything is working. It is not proven as working because of possible missing tests. It is ready to use but not recommended for production.

feature covers all new features which are not completely finished. Once a feature is finished it will be merged into mainline. Sample branch: feature/ISSUE-2-A-nice-Feature

bugfix fixes non-critical bugs which can wait for the next normal release. Sample branch: bugfix/ISSUE-1-Some-typos

production owns the latest release.

hotfix fixes critical bugs which have to be release urgent to mainline, production and all affected long-term releasees. Sample branch: hotfix/ISSUE-3-Check-your-math

release is for long-term maintenance. Sample branches: release/1.0, release/1.1 release/1.0-rc1

I am not an expert so please provide me feedback. Which problems might appear? Which parts are missing or slowing down the productivity?


问:
is it valid that a state machine can have more than one possible state for some transition?
I have a requirement for a workflow which I am trying to model as a state machine, I see that there is more than one outcome of a given transition(or activity).

Is it valid for a state machine to have more than one possible states, but only one state will be true at a given time?

Note: This is my first attempt to model a state machine.

Eg. might be:

s1->t1->s2

s1->t1->s3

s1->t1->s4

where s1, s2, s3, s4 are states and t1 is transition/activity.

A fictitious real world example might be:

For a human, there can be two states: hungry, not hungry
A basket can have only one item from: apple, orange.

So, to model it we will have:

hungry->pick from basket->apple found

hungry->pick from basket->orange found

apple found->eat->not hungry

orange found->take juice out of it and then drink-> not hungry

答:
The thing to recognize is you don't have to make all the states exactly model a real-world state, and you don't have to make all the transitions exactly model a real-world action.

For your example, you can fix it in one of two ways:  create a separate transition for each possible outcome, or create an intermediate state.  Creating a separate transition looks like this:

hungry -> pick orange from basket -> have orange

hungry -> pick apple from basket -> have apple

Here, you basically don't follow the transition until you've determined what fruit you picked.  It's non-deterministic in the real world, but in your model you discover which event occurred before you update your state machine.

The intermediate state fix looks like this:

hungry -> pick item from basket -> have item

have item -> identify item as orange -> have orange

have item -> identify item as apple -> have apple

Here, you can follow the first transition without knowing what fruit you've picked, but then you immediately identify it and make another transition.  This more closely models the real-world actions, but recognizes that picking and identifying are two separate actions, even though they occur almost simultaneously so humans don't really think of them as separate.

Which method you choose depends on the implementation.  You might want to try implementing both and see which comes out cleaner in your situation.


答:
This is the standard problem known as "Multidimensional Scaling".

There are lots of varieties, so I recommend reading the wikipedia page on the subject (it's not so long), but in your case, it looks like plain PCA (Principal component analysis) is the way to go.

In particular, a (squared) distance matrix D is closely related to B, the product of the transposed position matrix X with itself:

B = -0.5 * J D J where J = I - 1/n (i.e. 1-1/n on the diagonal, -1/n elsewhere)

B = X^T X (by construction)

PCA will give you the eigenvalues and eigenvectors such that

B = U^T L U

with the columns of U being the eigenvectors, and the diagonal of L being the eigenvalues.

Then it's just a question of multiplication:

X = L^(1/2) U

If you're not used to math this may sound a little scary, but PCA is almost certainly available as a library whatever platform you're on, and then it's just a question of a multiplying a diagonal matrix with matrix, which is easy to do manually if necessary.  PCA will reconstruct a full-dimensional space, so if you want just the 2 or 3 most significant dimensions, simply crop the resultant matrix.  More significant dimensions are those with the highest eigenvalue; most PCA algorithms list them in order, so you can just take the top few rows.

There are various documents online that explain this in more detail; alas wikipedia falls short here.  See, for instance: Wickelmaier, F. (2003). An introduction to MDS. Reports from the Sound Quality Research Unit (SQRU), No. 7 pages 9-11 for more info.

The above is a general pattern you might apply in many languages.  To verify this works, I tested it using Eigen which is the fastest linear algebra library I know of that can express this kind of thing simply.

I'll walk you through it:

```<code>#include <iostream>
#include <Eigen/Core>
#include <Eigen/Eigenvalues>

using namespace Eigen;
using namespace std;

#define DBG(s) (cout<< #s<<":\n"<<(s)<<"\n\n" )

int main(int argc, char* argv[])
{
</code>```

The above is just boilerplate referring to Eigen and simple console output function.

```<code>    auto const n = 4;//4 points.

    MatrixXd points(3, n);
    points <<
        0.0,   4.0,  2.0,   3.0,
        3.0,   1.0,  0.0,  -4.0,
        0.01, -0.1, -0.05, -0.01;

    MatrixXd D(n, n);
    for (int i = 0; i < n; i++)
        D.col(i) = (points.colwise() - points.col(i)).colwise().squaredNorm().transpose();

    DBG(D);
    //      0 20.0121 13.0036 58.0004
    //20.0121       0  5.0025 26.0081
    //13.0036  5.0025       0 17.0016
    //58.0004 26.0081 17.0016       0
</code>```

I'm initializing the input (squared) distance matrix D with real distances to make sure they sort of make sense.  Notice that the third dimensions values are all really small - I intend to make a 2D reconstruction and I'd like data that's not quite perfect :-).

```<code>    MatrixXd J = MatrixXd::Identity(n, n) - MatrixXd::Constant(n, n, 1.0 / 4.0);
    DBG(J);
    // 0.75 -0.25 -0.25 -0.25
    //-0.25  0.75 -0.25 -0.25
    //-0.25 -0.25  0.75 -0.25
    //-0.25 -0.25 -0.25  0.75

    MatrixXd B = -0.5 * J * D * J;
    DBG(B);
    //  14.0648 -0.940469  0.561906  -13.6862
    //-0.940469   4.06641 -0.436719  -2.68922
    // 0.561906 -0.436719 0.0626563 -0.187844
    // -13.6862  -2.68922 -0.187844   16.5633
</code>```

Note that B has a non-zero diagonal (you worried about this in the comments below).

```<code>    SelfAdjointEigenSolver<MatrixXd> eigendecomp(B);
    DBG(eigendecomp.eigenvalues());
    //-1.77725e-015
    //  0.000568306
    //      5.61749
    //       29.139
</code>```

Note that Eigen returns eigenvalues in ascending order; also note that the first eigenvalue is negative.  This is a problem, because we're going to take the square root.  Of course, this is just a numeric artifact; since our input data was 3D, it makes sense that the least-significant dimention of a 4D representation is just noise.  It's tiny too.  In general, you need to make sure that you pick the right number of dimensions to use, because too few and you'll introduce error; too many and you'll run into negative eigenvalues.  Note that if any of these negative eigenvalues were large, that would indicate your input is bad, such as that it violates the triangle inequality that A to C via B is always longer than A to C directly.

Your input above has such a violation: points 0 and 2 are the same (have 0 distance), yet point 0 is at distance 0.4 from point 1, but point 2 is at distance 0.2.  Clearly regardless of dimensionality or where you put the points, this is impossible.  That's going to cause negative eigenvalues that aren't just numerical noise.  There are various hacks to mitigate that, but let's get back on track and use those eigenvalues:

```<code>    MatrixXd X = eigendecomp.eigenvalues().bottomRows(2).cwiseSqrt().asDiagonal()
        * eigendecomp.eigenvectors().transpose().bottomRows(2);
</code>```

That's just the wordy way of saying X = L^(1/2) U while only considering the most significant 2 dimensions.

```<code>    DBG(X.transpose() * X);
    //  14.0647 -0.940508  0.562078  -13.6863
    //-0.940508   4.06638 -0.436623  -2.68925
    // 0.562078 -0.436623 0.0622356 -0.187691
    // -13.6863  -2.68925 -0.187691   16.5632
</code>```

Right, so we can verify that the decomposition works: this is quite similar to B. It's not exactly the same because we chopped off two dimensions, after all.

```<code>    DBG(X);
    //-0.999577   1.99533 -0.232164 -0.763594
    //  3.61463  0.291588 0.0912992  -3.99751
</code>```

These points turn out not to be very similar to the original points we started from.  That's not actually surprising; after all, distances are invariant under rotation and translation, so you'll find that these points are zero-centered, and they're arbitrarily rotated compared to the points we started with.  You could actually reconstruct the rotation matrix effectively used with another PCA, but let's not :-).

```<code>    MatrixXd D2(n, n);
    for (int i = 0; i < n; i++)
        D2.col(i) = (X.colwise() - X.col(i)).colwise().squaredNorm().transpose();
    DBG(D2);
    //      0 20.0121 13.0028 58.0004
    //20.0121       0 5.00187 26.0081
    //13.0028 5.00187       0 17.0008
    //58.0004 26.0081 17.0008       0

    return 0;
}
</code>```

As a final verification, note that the distances these new points generate are pretty much identical to the distances we started with.  Numerical inaccuracies and more importantly our 3D -> 2D reduction mean it's not completely perfect, but you should be able to see that D is almost the same as D2.

Note for when your data really isn't very clean and/or doesn't actually represent Euclidean distances: MDS (using PCA) solves the optimization problem of minimizing the squared error of the distance.  That's all fine and dandy, but notice that errors in large distances are (over-)emphasized here (12 - 02 = 1, but 112 - 102 = 21, so MDS will try 21 times as hard to fix the second error).  If your distances aren't perfect, PCA will try to make the "most significant" i.e. largest distance fit best.  This is often actually exactly the wrong thing to do; usually, you care more about local structure than global structure.  IF your distance matrix is inaccurate and you cannot reconstruct a good overall position matrix, you might want to look at techniques that at least reconstruct local neighbourhoods best.  State of the art there (AFAIK) is t-SNE: you won't get an absolutely accurate map from that technique, but you will get the right things close to each other, at least.

Good luck - sounds like a fun problem!

Title: Find points whose pairwise distances approximate a given distance matrix

Body:
Problem. I have a symmetric distance matrix with entries between zero and one, like this one:

```<code>D = ( 0.0 0.4 0.0 0.5 )
    ( 0.4 0.0 0.2 1.0 )
    ( 0.0 0.2 0.0 0.7 )
    ( 0.5 1.0 0.7 0.0 )
</code>```

I would like to find points in the plane that have (approximately) the pairwise distances given in D. I understand that this will usually not be possible with strictly correct distances, so I would be happy with a "good" approximation.

My matrices are smallish, no more than 10x10, so performance is not an issue.

Question. Does anyone know of an algorithm to do this?

Background. I have sets of probability densities between which I calculate Hellinger distances, which I would like to visualize as above. Each set contains no more than 10 densities (see above), but I have a couple of hundred sets.

What I did so far.

I did consider posting at math.SE, but looking at what gets tagged as "geometry" there, it seems like this kind of computational geometry question would be more on-topic here. If the community thinks this should be migrated, please go ahead.

This looks like a straightforward problem in computational geometry, and I would assume that anyone involved in clustering might be interested in such a visualization, but I haven't been able to google anything.

One simple approach would be to randomly plonk down points and perturb them until the distance matrix is close to D, e.g., using Simulated Annealing, or run a Genetic Algorithm. I have to admit that I haven't tried that yet, hoping for a smarter way.

One specific operationalization of a "good" approximation in the sense above is Problem 4 in the Open Problems section here, with k=2. Now, while finding an algorithm that is guaranteed to find the minimum l1-distance between D and the resulting distance matrix may be an open question, it still seems possible that there at least is some approximation to this optimal solution. If I don't get an answer here, I'll mail the gentleman who posed that problem and ask whether he knows of any approximation algorithm (and post any answer I get to that here).

Body:
This is the standard problem known as "Multidimensional Scaling".

There are lots of varieties, so I recommend reading the wikipedia page on the subject (it's not so long), but in your case, it looks like plain PCA (Principal component analysis) is the way to go.

In particular, a (squared) distance matrix D is closely related to B, the product of the transposed position matrix X with itself:

B = -0.5 * J D J where J = I - 1/n (i.e. 1-1/n on the diagonal, -1/n elsewhere)

B = X^T X (by construction)

PCA will give you the eigenvalues and eigenvectors such that

B = U^T L U

with the columns of U being the eigenvectors, and the diagonal of L being the eigenvalues.

Then it's just a question of multiplication:

X = L^(1/2) U

If you're not used to math this may sound a little scary, but PCA is almost certainly available as a library whatever platform you're on, and then it's just a question of a multiplying a diagonal matrix with matrix, which is easy to do manually if necessary.  PCA will reconstruct a full-dimensional space, so if you want just the 2 or 3 most significant dimensions, simply crop the resultant matrix.  More significant dimensions are those with the highest eigenvalue; most PCA algorithms list them in order, so you can just take the top few rows.

There are various documents online that explain this in more detail; alas wikipedia falls short here.  See, for instance: Wickelmaier, F. (2003). An introduction to MDS. Reports from the Sound Quality Research Unit (SQRU), No. 7 pages 9-11 for more info.

The above is a general pattern you might apply in many languages.  To verify this works, I tested it using Eigen which is the fastest linear algebra library I know of that can express this kind of thing simply.

I'll walk you through it:

#include <iostream>
#include <Eigen/Core>
#include <Eigen/Eigenvalues>

using namespace Eigen;
using namespace std;

#define DBG(s) (cout<< #s<<":\n"<<(s)<<"\n\n" )

int main(int argc, char* argv[])
{

The above is just boilerplate referring to Eigen and simple console output function.

    auto const n = 4;//4 points.

    MatrixXd points(3, n);
    points <<
        0.0,   4.0,  2.0,   3.0,
        3.0,   1.0,  0.0,  -4.0,
        0.01, -0.1, -0.05, -0.01;

    MatrixXd D(n, n);
    for (int i = 0; i < n; i++)
        D.col(i) = (points.colwise() - points.col(i)).colwise().squaredNorm().transpose();

    DBG(D);
    //      0 20.0121 13.0036 58.0004
    //20.0121       0  5.0025 26.0081
    //13.0036  5.0025       0 17.0016
    //58.0004 26.0081 17.0016       0

I'm initializing the input (squared) distance matrix D with real distances to make sure they sort of make sense.  Notice that the third dimensions values are all really small - I intend to make a 2D reconstruction and I'd like data that's not quite perfect :-).

    MatrixXd J = MatrixXd::Identity(n, n) - MatrixXd::Constant(n, n, 1.0 / 4.0);
    DBG(J);
    // 0.75 -0.25 -0.25 -0.25
    //-0.25  0.75 -0.25 -0.25
    //-0.25 -0.25  0.75 -0.25
    //-0.25 -0.25 -0.25  0.75

    MatrixXd B = -0.5 * J * D * J;
    DBG(B);
    //  14.0648 -0.940469  0.561906  -13.6862
    //-0.940469   4.06641 -0.436719  -2.68922
    // 0.561906 -0.436719 0.0626563 -0.187844
    // -13.6862  -2.68922 -0.187844   16.5633

Note that B has a non-zero diagonal (you worried about this in the comments below).

    SelfAdjointEigenSolver<MatrixXd> eigendecomp(B);
    DBG(eigendecomp.eigenvalues());
    //-1.77725e-015
    //  0.000568306
    //      5.61749
    //       29.139

Note that Eigen returns eigenvalues in ascending order; also note that the first eigenvalue is negative.  This is a problem, because we're going to take the square root.  Of course, this is just a numeric artifact; since our input data was 3D, it makes sense that the least-significant dimention of a 4D representation is just noise.  It's tiny too.  In general, you need to make sure that you pick the right number of dimensions to use, because too few and you'll introduce error; too many and you'll run into negative eigenvalues.  Note that if any of these negative eigenvalues were large, that would indicate your input is bad, such as that it violates the triangle inequality that A to C via B is always longer than A to C directly.

Your input above has such a violation: points 0 and 2 are the same (have 0 distance), yet point 0 is at distance 0.4 from point 1, but point 2 is at distance 0.2.  Clearly regardless of dimensionality or where you put the points, this is impossible.  That's going to cause negative eigenvalues that aren't just numerical noise.  There are various hacks to mitigate that, but let's get back on track and use those eigenvalues:

    MatrixXd X = eigendecomp.eigenvalues().bottomRows(2).cwiseSqrt().asDiagonal()
        * eigendecomp.eigenvectors().transpose().bottomRows(2);

That's just the wordy way of saying X = L^(1/2) U while only considering the most significant 2 dimensions.

    DBG(X.transpose() * X);
    //  14.0647 -0.940508  0.562078  -13.6863
    //-0.940508   4.06638 -0.436623  -2.68925
    // 0.562078 -0.436623 0.0622356 -0.187691
    // -13.6863  -2.68925 -0.187691   16.5632

Right, so we can verify that the decomposition works: this is quite similar to B. It's not exactly the same because we chopped off two dimensions, after all.

    DBG(X);
    //-0.999577   1.99533 -0.232164 -0.763594
    //  3.61463  0.291588 0.0912992  -3.99751

These points turn out not to be very similar to the original points we started from.  That's not actually surprising; after all, distances are invariant under rotation and translation, so you'll find that these points are zero-centered, and they're arbitrarily rotated compared to the points we started with.  You could actually reconstruct the rotation matrix effectively used with another PCA, but let's not :-).

    MatrixXd D2(n, n);
    for (int i = 0; i < n; i++)
        D2.col(i) = (X.colwise() - X.col(i)).colwise().squaredNorm().transpose();
    DBG(D2);
    //      0 20.0121 13.0028 58.0004
    //20.0121       0 5.00187 26.0081
    //13.0028 5.00187       0 17.0008
    //58.0004 26.0081 17.0008       0

    return 0;
}

As a final verification, note that the distances these new points generate are pretty much identical to the distances we started with.  Numerical inaccuracies and more importantly our 3D -> 2D reduction mean it's not completely perfect, but you should be able to see that D is almost the same as D2.

Note for when your data really isn't very clean and/or doesn't actually represent Euclidean distances: MDS (using PCA) solves the optimization problem of minimizing the squared error of the distance.  That's all fine and dandy, but notice that errors in large distances are (over-)emphasized here (12 - 02 = 1, but 112 - 102 = 21, so MDS will try 21 times as hard to fix the second error).  If your distances aren't perfect, PCA will try to make the "most significant" i.e. largest distance fit best.  This is often actually exactly the wrong thing to do; usually, you care more about local structure than global structure.  IF your distance matrix is inaccurate and you cannot reconstruct a good overall position matrix, you might want to look at techniques that at least reconstruct local neighbourhoods best.  State of the art there (AFAIK) is t-SNE: you won't get an absolutely accurate map from that technique, but you will get the right things close to each other, at least.

Good luck - sounds like a fun problem!

ContentLicense: CC BY-SA 4.0


问:
How to design database having multiple interrelated entities
I am designing a new system which is more of a help system for core applications in banks or healthcare sector. Given the nature of the system this is not a  heavy transaction oriented system but more of read intensive.

Now within this application I have multiple entities which are related to each other.
For e.g. Assume the following entities in the system

User

Training

Regulations

Now each of these entities have M:N Relationship with each other.

Assuming the usage of a standard RDBMS, the design may involve many relationship tables each containing the relationships one other entity ("User_Training", "User_Regulations", "Training_Regulations"). This design is limiting since I have more than 3 entities in the system and maintaining the relationship graph is difficult this way.

The most frequently used operation is "given an entity get me all the related entities" . I need to design the database where this operation is relatively inexpensive.

What are the different recommendations for modelling this kind of database.

答:
Normalization has many benefits to RDBMS, but it also has some draw-backs. Performance for multiple and complex relations can be hindered when selecting data.

It seems like the main purpose of this app is to provide reporting and not a lot of data alterations by the typical user. You can keep your current structure to handle all the data entry/setup of the various entities by some administrator of the information, but that doesn't mean those getting the help information have to query these exact same structures.

Depending on how much your help items are altered, you can very easily prebuild denormalized reporting tables. They are basically the results of your queries without running the same query over and over with each user request. Each type of request would have its own table. The table gets rebuilt/updated/refreshed when help items are changed. You'll have to code this.

There are plenty of other caching tools and some are built into different database products. This can get more costly and complicated. I would start with this. Maybe you can just pick one and see how it works. Whether your app is using the results of the actual run of the query or just a simple query of a prebuilt table, it's working with the same data, so very few changes need to be made in your app as you migrate more and more of these to some prebuilt table.


问:
Why is String Templating Better Than String Concatenation from an Engineering Perspective?
I once read (I think it was in "Programming Pearls") that one should use templates instead of building the string through the use of concatenation.

For example, consider the template below (using C# razor library)

```<code><in a properties file>
Browser Capabilities
Type = @Model.Type
Name = @Model.Browser
Version = @Model.Version
Supports Frames = @Model.Frames
Supports Tables = @Model.Tables
Supports Cookies = @Model.Cookies
Supports VBScript = @Model.VBScript
Supports Java Applets = @Model.JavaApplets
Supports ActiveX Controls = @Model.ActiveXControls
</code>```

and later, in a separate code file

```<code>private void Button1_Click(object sender, System.EventArgs e)
{
    BrowserInfoTemplate = Properties.Resources.browserInfoTemplate; // see above
    string browserInfo = RazorEngine.Razor.Parse(BrowserInfoTemplate, browser);
    ...
}
</code>```

From a software engineering perspective, how is this better than an equivalent string concatentation, like below:

```<code>private void Button1_Click(object sender, System.EventArgs e)
{
    System.Web.HttpBrowserCapabilities browser = Request.Browser;
    string s = "Browser Capabilities\n"
        + "Type = "                    + browser.Type + "\n"
        + "Name = "                    + browser.Browser + "\n"
        + "Version = "                 + browser.Version + "\n"
        + "Supports Frames = "         + browser.Frames + "\n"
        + "Supports Tables = "         + browser.Tables + "\n"
        + "Supports Cookies = "        + browser.Cookies + "\n"
        + "Supports VBScript = "       + browser.VBScript + "\n"
        + "Supports JavaScript = "     +
            browser.EcmaScriptVersion.ToString() + "\n"
        + "Supports Java Applets = "   + browser.JavaApplets + "\n"
        + "Supports ActiveX Controls = " + browser.ActiveXControls
              + "\n"
    ...
}
</code>```

答:
The engineering principles at play here are readability and DRY.

The primary driver of readability is length. Shorter code is more readable (for readers of adequate skill level), so the first case reads better.

A secondary driver is clutter. The absence of punctuation and extraneous characters favours the first case.

The DRY point here is that symbols such as browser and \n are concealed in the abstraction in case 1, and repeated in case 2. If either of those needed to be changed there would be less places to change in case 1 than case 2.

Set against this is WYSIWYG. In the first case there is something missing between the code you see and how it is put to work, where in the second case all the machinery is plain to see.

[At the opinion level, I would strongly favour case 1; others would not. But that would make this an opinion-based question and it might get closed, so we wouldn't want to venture there.]

OwnerDisplayName: stephen


问:
Is this method pure?
I have the following extension method:

```<code>    public static IEnumerable<T> Apply<T>(
        [NotNull] this IEnumerable<T> source,
        [NotNull] Action<T> action)
        where T : class
    {
        source.CheckArgumentNull("source");
        action.CheckArgumentNull("action");
        return source.ApplyIterator(action);
    }

    private static IEnumerable<T> ApplyIterator<T>(this IEnumerable<T> source, Action<T> action)
        where T : class
    {
        foreach (var item in source)
        {
            action(item);
            yield return item;
        }
    }
</code>```

It just applies an action to each item of the sequence before returning it.

I was wondering if I should apply the Pure attribute (from Resharper annotations) to this method, and I can see arguments for and against it.

Pros:

strictly speaking, it is pure; just calling it on a sequence doesn't alter the sequence (it returns a new sequence) or make any observable state change

calling it without using the result is clearly a mistake, since it has no effect unless the sequence is enumerated, so I'd like Resharper to warn me if I do that.

Cons:

even though the Apply method itself is pure, enumerating the resulting sequence will make observable state changes (which is the point of the method). For instance, items.Apply(i => i.Count++) will change the values of the items every time it's enumerated. So applying the Pure attribute is probably misleading...

What do you think? Should I apply the attribute or not?

答:
I disagree with both Euphoric and Robert Harvey's answers. Absolutely that is a pure function; the problem is that

It just applies an action to each item of the sequence before returning it.

is very unclear what the first "it" means. If "it" means one of those functions, then that's not right; neither of those functions do that; the MoveNext of the enumerator of the sequence does that, and it "returns" the item via the Current property, not by returning it.

Those sequences are enumerated lazily, not eagerly so it is certainly not the case that the action is applied before the sequence is returned by Apply. The action is applied after the sequence is returned, if MoveNext is called on an enumerator.

As you note, these functions take an action and a sequence and return a sequence; the output depends on the input, and no side effects are produced, so these are pure functions..

Now, if you create an enumerator of the resulting sequence and then call MoveNext on that iterator then the MoveNext method is not pure, because it calls the action and produces a side effect. But we already knew that MoveNext was not pure because it mutates the enumerator!

Now, as for your question, should you apply the attribute: I would not apply the attribute because I would not write this method in the first place. If I want to apply an action to a sequence then I write

```<code>foreach(var item in sequence) action(item);
</code>```

which is nicely clear.

Title: Is this method pure?

Body:
I disagree with both Euphoric and Robert Harvey's answers. Absolutely that is a pure function; the problem is that

It just applies an action to each item of the sequence before returning it.

is very unclear what the first "it" means. If "it" means one of those functions, then that's not right; neither of those functions do that; the MoveNext of the enumerator of the sequence does that, and it "returns" the item via the Current property, not by returning it.

Those sequences are enumerated lazily, not eagerly so it is certainly not the case that the action is applied before the sequence is returned by Apply. The action is applied after the sequence is returned, if MoveNext is called on an enumerator.

As you note, these functions take an action and a sequence and return a sequence; the output depends on the input, and no side effects are produced, so these are pure functions..

Now, if you create an enumerator of the resulting sequence and then call MoveNext on that iterator then the MoveNext method is not pure, because it calls the action and produces a side effect. But we already knew that MoveNext was not pure because it mutates the enumerator!

Now, as for your question, should you apply the attribute: I would not apply the attribute because I would not write this method in the first place. If I want to apply an action to a sequence then I write

foreach(var item in sequence) action(item);

which is nicely clear.


问:
Liskov substitution principle with abstract parent class
Does Liskov substitution principle apply to inheritance hierarchies where the parent is an abstract class the same way if the parent is a concrete class?

The Wikipedia page list several conditions that have to be met before a hierarchy is deemed to be correct. However, I have read in a blog post that one way to make things easier to conform to LSP is to use abstract parent instead of a concrete class.

How does the choice of the parent type (abstract vs concrete) impacts the LSP? Is it better to have an abstract base class whenever possible?

答:
You can answer questions like this by going back to first principles and asking "what is the Liskov substitution principle intended to accomplish?"  And the answer to that is that code that works correctly with the superclass should also work correctly with all subclasses.  (For CS theory purposes there is a more specific mathematical definition but for the workaday programmer that's likely to only be concern if you are creating a programming language).

So, why would an abstract class be easier?  There really is no situation where using a concrete class means you can't possibly follow the Liskov substitution principle in your subclasses.  After all, the superclass defines a contract for the subclasses either way.  The fact that it also contains one possible implementation of the contract is neither here nor there.

However, one might make the argument that psychologically it's easier to focus on a contract when making an abstract class, and perhaps a concrete class might take an implementation detail and accidentally make that part of the contract.  It seems sort of plausible without being really convincing.

As far as never using a concrete class - perhaps a good rule of thumb is to be a bit skeptical of any design philosophy that says you should never use a standard, not totally broken feature of a language.  In this case, the chance that you might, maybe, be more likely to make a mistake designing the contract of a concrete class vs. an abstract one - well that would be at best a minor factor in making that decision.


问:
Best practice for managing dynamic HTML modules?
I've been building web apps that add and remove lots of dynamic content and even structure within the page, and I'm not impressed by the method I'm using to do it.

When I want to add a section or module into a position in the interface, I'm storing the html in the code, and I don't like that:

```<code>if (rank == "moderator") {
   $("#header").append('<div class="mod_controls">' +
      // content, using + to implement line breaks
   '</div>');
}
</code>```

This seems like such a bad programming practice.. There must be a better way. I thought of building a function to convert a JSON structure to html, but it seems like overkill.

For the functionality of the apps I'm using:

Node.js

JS

JQuery

AJAX

Is there some common way to store HTML modules externally for AJAX importation?

答:
As you're already using jQuery, you might like the .load() method:
 "Load data from the server and place the returned HTML into the matched element."

If you incorporate AngularJS in the future you'll find there are many ways to include HTML (ngInclude, ngRoute w/templates) but my favorite are "directives" as you can easily couple associated JS & HTML together in a modular fashion.


问:
Drag-n-drop programming - would it fly?
All programming languages I know of are written - i.e. typed out as lengths of text one way or another. But I wonder if there's any programming language where you can just drag-n-drop the whole program; to get a loop, you select this box over here and drag it to that section of the "code" over there, and so on. And if there isn't one like this, would it fly if one was invented?

Personally I don't believe it would be such a good idea, but I'd like to hear what you think.

答: Prograph was a cool language that was all drag and drop. Also, Wikipedia has an article with a good sized list of visual languages.</p>

ContentLicense: CC BY-SA 2.5

Title: Drag-n-drop programming - would it fly?


问:
With agile, what do when a user story doesn't get completed in an iteration?
Do you just move the story to a new iteration, or delay the next iteration?

It gets a bit tricky when you use something like Jira when you have to copy or move around stories between iterations as it effects calculations of velocity/points etc.

Is there a best-practice?

答:
So there are two ways you do it

FIRST  - Split the stories and assign and divide  points to old and new split story  - this means estimate the remaining work on the story

For example - An 8 pointer story was 'unfinished' with only 3 point of work completed and team estimates that 5 points is needed for the pending/remaining work - Then update the 'unfinished' story to 3 points and the new split story to 5 points.... makes sense?

SECOND - Split the story and zero out the points from the old story and update the new split sory with the full points of the old story - this is the best practice scrum teams follow.
The credit is given only when the full story is completed and velocity also can be captured properly by averaging out once some iterations are done.

Second option is what we follow

OwnerDisplayName: codecompleting


问:
Use adapter pattern for coupled classes
I need (for unit testing purposes) to create adapters for external library classes.ExchangeService and ContactsFolder are Microsoft's implementations in its' EWS library.
So I created my adapters that implement my interfaces, but it seems that contactsFolder has a dependency for ExchangeService in its' constructor.
The problem is that I cannot instantiate ContactsFolderAdapter without somehow accessing the actual ExchangeService instance (I see only ExchangeServiceAdapter in scope).

Is there a better pattern for this that retains the adapter classes? Or should I "infect" ExchangeServiceAdapter with some kind of GetActualObject method?

答:
I think the adapter pattern is the right one here.  What I don't see is your interfaces having more than one concrete implementation which is the advantage of the pattern in this context.  Assuming that you only want to use IContactsFolder and IExchangeService in your unit tests (Microsoft is responsible for testing their classes), you need to create a mock or stub implementation of each interface that returns predictable dummy data. There are a lot of .NET frameworks for object mocking that will automatically implement these interfaces for you.

If there is some logic you are trying to test that exists in ExchangeServiceAdapter and ContactsFolderAdapter, then that logic needs to be extracted out into a class that uses the IContactsFolder or IExchangeService interfaces.  Adapters should do nothing but translate one interface into another.

OwnerDisplayName: kaiseroskilo

Title: Use adapter pattern for coupled classes

OwnerDisplayName: Jason Nesbitt


问:
How often should saving to disk occur in an automatically saving text editor?
I am developing a simple text editor and would like the application to save the text automatically. In other words, the user would never have to press a save button.

I have seen other applications that do this, and was wondering how often is it safe to write files to disk? From a user experience standpoint, it seems that the more frequently this happens the better, but I am worried about performance and possible disk wear (especially on writes to SSDs). It seems like the operating system disk caching might help avoid these problems, but I also don't know if its safe to rely on that for an application like this.

I was planning on writing the whole document to disk at each save, but this just seems terribly inefficient if the OS ends up writing it to disk to frequently, but relying on program unload may lose data in the case of a crash.

Does anyone have any experience dealing with this that might be able to help?

答: I wouldn't over-analyze this to the point that you're worrying about hardware.  A basic approach would be to maintain a 'modified' flag.  Then automatically save after the program is idle for some number of seconds - but only if the text has been modified.  Once the text is saved, clear the modified flag, and repeat the process.  </p>


问:
Software monetization that is not evil
I have a free open-source project with around 800K downloads to date.
I've been contacted by some monetization companies from time to time and turned them down, since I didn't want toolbar malware associated with my software.
I was wondering however, is there a non-evil way to monetize software ?

Here are the options as I know them:

Add a donation button.

I don't feel comfortable with that as I really don't need "donations" - I'm paid quite well.

Donating users may feel entitled to support etc.  (see the second to last bullet)

Add ads inside your application.

 In the web that may be acceptable, but in a desktop program it looks incredibly lame.

Charge a small amount for each download.

This model works well in the mobile world, but I suspect no one will go for it on the desktop.

It doesn't mix well with open source, though I suppose I could charge only for the binaries (most users won't go to the hassle of compiling the sources).

People may expect support etc. after having explicitly paid (see next bullet).

Make money off a service / community / support associated with the program.

This is one route I definitely don't want to take, I don't want any sort of hassle beyond coding. I assure you, the program is top notch (albeit simple) and I'm not aware of any bugs as of yet (there are support forums and blog comments where users may report them). It is also very simple, documented, and discoverable so I do think I have a case for supplying it "as is".

Add affiliate suggestions to your installer.

If you use a monetization company, you lose control over what they propose. Unless you can establish some sort of strong trust with the company to supply quality suggestions (I sincerely doubt it), I can't have that.

Choosing your own affiliate (e.g. directly suggesting Google Toolbar) is possibly the only viable solution to my mind. Problem is, where do I find a solid affiliate that could actually give value to the user rather than infect his computer with crapware? I thought maybe Babylon (not the toolbar of course, I hate toolbars)?

答:
You could always follow sublime text and do what I call the npr model of funding. Basically every couple of saves remind people that they could buy a license if they feel the software is worth it and remind them that this software requires some of your time to make. </p>

<p>This has the advantage of still leaving your software free for use and you can set up a reasonable license fee that those who feel they use your stuff enough can pay. </p>

Title: Software monetization that is not evil

Body:
You could always follow sublime text and do what I call the npr model of funding. Basically every couple of saves remind people that they could buy a license if they feel the software is worth it and remind them that this software requires some of your time to make.

This has the advantage of still leaving your software free for use and you can set up a reasonable license fee that those who feel they use your stuff enough can pay.


答:
There are a lot of factors that go into the determine what frequency makes sense. You have already mentioned some of them. It's highly unlikely that you will fine a single value that makes sense for all cases.

What you should do it make the value configurable. The application doesn't care how frequently a save happens, just that it happens. Once you have this in place, you can test it and try to tune the value. Since you are dealing with human interaction, the delay between required number of disc writes will seem like an eternity to the computer.

Even after you have arrived at what you feel is a good default, you should still leave the value as configurable. You might have a user that has a different preference than you.

Title: How often should saving to disk occur in an automatically saving text editor?

Body:
I am developing a simple text editor and would like the application to save the text automatically. In other words, the user would never have to press a save button.

I have seen other applications that do this, and was wondering how often is it safe to write files to disk? From a user experience standpoint, it seems that the more frequently this happens the better, but I am worried about performance and possible disk wear (especially on writes to SSDs). It seems like the operating system disk caching might help avoid these problems, but I also don't know if its safe to rely on that for an application like this.

I was planning on writing the whole document to disk at each save, but this just seems terribly inefficient if the OS ends up writing it to disk to frequently, but relying on program unload may lose data in the case of a crash.

Does anyone have any experience dealing with this that might be able to help?


答:
I was planning on writing the whole document to disk at each save, but this just seems terribly inefficient if the OS ends up writing it to disk to frequently, but relying on program unload may lose data in the case of a crash

What many editors do (v.g. Word) is just writting the entire document to another, temporary location and, once the file is written to disk, just change the name to the default one. After an application crash, if I find the temp file, I can check if it is consistent and proceed accordingly. If the temp file was damaged, I still have the original one.

That said, the idea of overwritting my previous document version without my consent is a terrible one, IM(ns)HO. First, because it is not the behavior users expect. And second, because it forbids them from deciding if the changes done to the document are worth saving or not. At worst, you auto-save feature will run after a user selected some (or all) of the document for applying an style and inadvertently pushed "Del".


问:
Do cross reference database tables have a place in domain driven design?
First some background. Let's say we have a system where a customer is placing an order in a web interface. The items that customer is ordering can priced in various ways. Sometimes including the cost of delivery and sometimes not at all. That pricing effectively depends on a variety of factors including the vendor's own pricing model, that vendor's individual contracts with customers as well as that vendor's contracts with its own suppliers. Let's assume that once a customer places an order for a particular item and chooses a contract if any, the method of delivery can be determined by variables on those contracts. Those delivery methods also live in their own table in the database and have various properties consumed downstream.

It makes sense that a cross reference or lookup table would store that information. That table would be loaded into the domain and could then be used to apply the appropriate delivery method while processing the order.

Does this make sense in the context of domain driven design? Or is my thinking too relational? Is this logic that should be built into it's own class/method (I mean beyond apply the cross reference table data)?

答:
Tables rarely belong in a domain model. Your domain is what changes most frequently, and thus should preferably be persistence ignorant.

It sounds like you might be missing a concept in your domain. I'd recommend following your last path of thought by encapsulating this logic in its own class/method. However, if you're doing domain-driven design, remember that it's more about modelling than coding. You should bring this up with your domain expert, and identify what kind of role this concept plays in your domain.


问:
Naming in Security Protocols: Alice, Bob and Eve
Among computer scientists and programmers, there's the common habit of naming people in the context of security protocols e.g. Alice, Bob or Eve. Descriptions of more elaborate attack vector sometimes refer to Charlie (as does this XKCD strip), but is there a convention for additional participants?

答:
Alice and Bob were the first two described in Applied Cryptography.  These are two people communicating and used a placeholder names.  Keeping with a convention makes it easier for people to remember what role they play in communication.

Beyond Alice and Bob, the first letter of the name typically implies the role of the individual in the communication.

C is sometimes a third person, but other times C is a Cracker.

D is often a fourth person in communication.

E can either be a fifth person, but often E has Evil intent.  Eve in particular is an Eavedropper.

F is a sixth person... and so on.

M becomes a Malicious attacker (as opposed to Eve, who just wants to eavesdrop).

O is an Opponent, similar to M, but not necessarily malicious.

P needs to have something Proven and V needs to have something Verified.

S is for Sybil which is a book about the treatment of a woman named Sybil Dorsett who had dissociative identity disorder.  In the context of security names, Sybil is a particular type of attacker who uses many identities often in combination with a system that uses reputation.

T is Trusted.

W can either be a Warden who guards Alice and Bob or a Whistleblower with insider information.

Further reading:

Alice and Bob (Wikipedia)

Metasyntatic variable (Wikipedia)

Placeholder name (computing specific) (Wikipedia)


问:
How is intermediate data organized in MapReduce?
From what I understand, each mapper outputs an intermediate file. The intermediate data (data contained in each intermediate file) is then sorted by key.

Then, a reducer is assigned a key by the master. The reducer reads from the intermediate file containing the key and then calls reduce using the data it has read.

But in detail, how is the intermediate data organized? Can a data corresponding to a key be held in multiple intermediate files? What happens when there is too much data corresponding to one key to be held by a single file?

In short, how do intermediate partitions differ from intermediate files and how are these differences dealt with in the implementation?

答:
You might have missed one step. MapReduce works like this:

```<code>Map -> Shuffle -> Sort -> Reduce
</code>```

More details about how the Shuffle & Sort step works: https://www.inkling.com/read/hadoop-definitive-guide-tom-white-3rd/chapter-6/shuffle-and-sort

But in detail, how is the intermediate data organized?

Its just the output of the mapper. The actual shuffle and sort is a separate step.

Can a data corresponding to a key be held in multiple intermediate files?

Yes, but each reducer is guaranteed to get all data corresponding to a key due to the sort step, even though different mappers can produce data corresponding to a key.

What happens when there is too much data corresponding to one key to be held
  by a single file?

Would not actually matter too much. Each mapper gets a limited subset of the input, and the output will correspond in size to input for all mappers.

Body:
You might have missed one step. MapReduce works like this:

Map -> Shuffle -> Sort -> Reduce

More details about how the Shuffle & Sort step works: https://www.inkling.com/read/hadoop-definitive-guide-tom-white-3rd/chapter-6/shuffle-and-sort

But in detail, how is the intermediate data organized?

Its just the output of the mapper. The actual shuffle and sort is a separate step.

Can a data corresponding to a key be held in multiple intermediate files?

Yes, but each reducer is guaranteed to get all data corresponding to a key due to the sort step, even though different mappers can produce data corresponding to a key.

What happens when there is too much data corresponding to one key to be held
  by a single file?

Would not actually matter too much. Each mapper gets a limited subset of the input, and the output will correspond in size to input for all mappers.


问:
How to solve linear recurrences involving two functions?
Actually I came across a question in Dynamic Programming where we need to find the number of ways to tile a 2 X N area with tiles of given dimensions..
Here is the problem statement

Now after a bit of recurrence solving I came out with these.

F(n) = F(n-1) + F(n-2) + 2G(n-1), and

G(n) = G(n-1) + F(n-1)

I know how to solve LR model where one function is there.For large N as is the case in the above problem we can do the matrix exponentiation and achieve O(k^3log(N)) time where k is the minimum number such that for all k>m F(n) does not depend on F(n-k). The method of solving linear recurrence with matrix exponentiation as it is given in that blog.

Now for the LR involving two functions can anyone suggest an approach feasible enough for large N.

答:
Same method works: find a matrix that translates (F(n-1) F(n-2) G(n-1)) into (F(n) F(n-1) G(n)). A ((1 1 2) (1 0 0) (1 0 1)) matrix would do it.

Explanation

That's how matrix-by-vector multiplication works. A first element of a resulting vector is an inner product of a first row and an initial vector:

```<code>1*F(n-1) + 1*F(n-2) + 2*G(n-1)
</code>```

which according to recurrence is F(n). Same goes for the second and third.

Body:
Same method works: find a matrix that translates (F(n-1) F(n-2) G(n-1)) into (F(n) F(n-1) G(n)). A ((1 1 2) (1 0 0) (1 0 1)) matrix would do it.

Explanation

That's how matrix-by-vector multiplication works. A first element of a resulting vector is an inner product of a first row and an initial vector:

1*F(n-1) + 1*F(n-2) + 2*G(n-1)

which according to recurrence is F(n). Same goes for the second and third.


答:
Donationware for charity

If users feel that your software provided them real value, they'll donate some amount. It is hard to know how much value your (free) software may bring to someone until they have the mechanism to show you. I've seen web forum and podcast fans donate hundreds of dollars because the information/community helped them where other methods/sources failed. Use disclaimers or clear messaging that donations != support or upgrades.

If you choose to give the donations to charity, no evil is done. In fact, quite the opposite IMO. You could post up the feedback from the beneficiaries as part of updating your site, which may drive adoption. Win-win.

Of course, if your code is open source, other people could go ahead and do this - does your licence prohibit them? Paint.net had this kind of issue sometime ago.

Title: Software monetization that is not evil

Body:
I have a free open-source project with around 800K downloads to date.
I've been contacted by some monetization companies from time to time and turned them down, since I didn't want toolbar malware associated with my software.
I was wondering however, is there a non-evil way to monetize software ?

Here are the options as I know them:

Add a donation button.

I don't feel comfortable with that as I really don't need "donations" - I'm paid quite well.

Donating users may feel entitled to support etc.  (see the second to last bullet)

Add ads inside your application.

 In the web that may be acceptable, but in a desktop program it looks incredibly lame.

Charge a small amount for each download.

This model works well in the mobile world, but I suspect no one will go for it on the desktop.

It doesn't mix well with open source, though I suppose I could charge only for the binaries (most users won't go to the hassle of compiling the sources).

People may expect support etc. after having explicitly paid (see next bullet).

Make money off a service / community / support associated with the program.

This is one route I definitely don't want to take, I don't want any sort of hassle beyond coding. I assure you, the program is top notch (albeit simple) and I'm not aware of any bugs as of yet (there are support forums and blog comments where users may report them). It is also very simple, documented, and discoverable so I do think I have a case for supplying it "as is".

Add affiliate suggestions to your installer.

If you use a monetization company, you lose control over what they propose. Unless you can establish some sort of strong trust with the company to supply quality suggestions (I sincerely doubt it), I can't have that.

Choosing your own affiliate (e.g. directly suggesting Google Toolbar) is possibly the only viable solution to my mind. Problem is, where do I find a solid affiliate that could actually give value to the user rather than infect his computer with crapware? I thought maybe Babylon (not the toolbar of course, I hate toolbars)?


问:
How to present a stable data model in a public API that allows internal data structures to be changed without breaking the public view of the data?
I am in the process of developing an application that allows users to write C# scripts. These scripts allow users to call selected methods and to access and manipulate data in a document. This works well, however, in the development version, scripts access the document's (internal) data structures directly. This means that if we were to change the internal data model/structure, there is a good chance that someone's script will no longer compile. We obviously want to prevent this breaking change from happening, but still want to allow the user to write sensible C# code (whilst not restricting how we develop our internal data model as a result). We therefore need to decouple our scripting API and its data structures from our internal methods and data structures.

We've a few ideas as to how we might allow the user to access a what is effectively a stable public version of the document's internal data*, but I wanted to throw the question out there to someone who might have some real experience of this problem. NB our internal document's data structure is quite complex and it could be quite difficult to wrap. We know we want to expose as little as possible in our public API, especially as once it's out there, it's out there for good.

Can anyone help?

How do scripting languages / APIs decouple their public API and data structures from their internal data structures?

Is there no real alternative to having to write a complex interaction layer?

If we need to do this, what's a good approach or pattern for wrapping complex data structures that include nested objects, including collections? I've looked at the API facade pattern, which looks like it's trying to address these kinds of issues, but are there alternatives?

*One idea is to build a data facade that is kept stable across versions of our application. The facade exposes a set of facade data objects that are used in the script code. These maintain backwards compatibility and wrap access to our internal document's data model.

答: The solution is to think very hard about the object model presented, and create one that is adaptable to future changes (extensible). Adding properties is not going to break anything. It is removing data types, renaming them, or removing properties, that will break existing clients. In these cases, you either have to keep all existing members for backward compatibility, or provide conversion scripts that automatically upgrade the user's scripts.</p>


问:
Do hiring managers have a hard time accepting developers who have a "business look alike" personal app but are NOT entrepreneurs?
Directly post graduation from University, I decided to build my own web app (Ease My Day) while waiting to get a job as a software Engineer.

The reasons to build this app:

Gain solid hands on software experience before hitting the job scene

Providing a solution to a common problem

Not sitting doing nothing while searching for jobs

The app is Not an entrepreneurial tryout nor a business to be sold. Still throughout interviews I noticed that at the rate of 4 of each 5 interviews I pass through the app is being confused with a business and I am asked the same questions:

Why did you build the business? Why do you want to stop the app? Do you want to sell the app?

Knowing that I didn't build a business nor make any income from this application.

Do candidates who take initiatives and like to craft their own apps on the side cause a red flag on the hiring manager's radar?

UPDATE
requested by @Kate Gregory

This is how I describe it on my resume:

Personal Project: Developer of "Ease My Day" SaaS application, Montreal, Qc.

  Link: http://www.easemyday.com

  Facebook: http://www.facebook.com/EaseMyDay

"Ease My Day" BETA's main purpose is helping with daily nutrition/health monitoring.

A list of more than 6000 foods with their nutrition facts is available. Users can add their personal foods and grow the food database.

  It allows users to specify their goal: lose weight, gain weight, no goal just staying healthy.

  It allows users to calculate their daily needs (calories, protein, carbohydrates, fat etc..) using the app's built in calculator. Users can manually input those values if they already know their needs.

  Users can monitor their food intakes by: meals, days and/or weeks.

  It is deployed on Amazon AWS ( EC2 instance with RDS )

"Ease My Day" solved a daunting task; it is a speed efficient platform that turns daily health monitoring - a notably time consuming task in the past - to an enjoyable activity that can be finalized in just few seconds. It is currently used in several countries around the world.

Languages/technology: .... HERE I explain the languages/technologies/Software Engineering approaches used ....

答:
Every project is built in steps:

I have a concept or an idea. This is not very valuable: everyone has thousands of great ideas, and keeping the idea in your head without doing anything with it won't make the world better.

Example: a chat for cats and dogs would be so great!

I draft the concept/idea on paper. This step is important, because something purely abstract and speculative becomes a bit more concrete. It doesn't mean that it is doable, but at least it is described.

Example: a 20 pages draft explaining how cats and dogs can talk to each other online, while they are unable to use a keyboard or a mouse. It also contains my personal drawings of the thing which will be fixed on the head of the animal and plugged in the PC.

I do a prototype. Great, now, I know that it's also doable.

Example: I put the experimental instrument I created on the heads of two cats, and they were able to communicate. Sadly, one cat was burned, and the other one became crazy. Don't really care; in front of my house, there are other cats to make experiments on.

I realize a semi-working version I can use. It's not a commercial product, but it can be used by a person who is fully aware of the constraints of the product.

Example: the instrument is working fine, and it doesn't put cats on fire any longer. They can eventually become crazy if the instrument is installed incorrectly on their head. I explained the concept to my colleague. Our cats talked to each other for two hours, but then his cat jumped out of the window; don't know why, but I opened a bug and closed it as "Can't reproduce", since mine remains happy and fat.

I end up with a working product which is used by me and eventually a few other people.

Example: cats and dogs are talking together for hours every evening for the last two weeks. We're ten colleagues using it, and everybody appreciate it. Jeff's cat don't even want to go out, and spend the whole day in front of her PC. Jeff thinks about buying her a dedicated PC. Sadly, Kate's dog started biting people and was euthanized. I hardly doubt this is related to my product.

I ship a stable product to a limited number of people.

Example: we have over three hundred pets registered. The product was so successful that I finally created a company, Cat&Dog Chat Ltd. Thanks to the earnings, I even bought a new PC to Jeff's cat and hired two geeks. I may consider leaving my current job. I heard that there was a collective cat suicide in a building nearby. I hope it's not related to my product.

The product is a commercial success and achieved to become popular.

Example: we have literally thousands of pets here, it's so exciting. I hired twenty other people. The software was ported on MacOS and also works on most popular smartphone platforms. The product is very stable, and there is practically no bug reports concerning major bugs. I also prepare a new version of the product, which will enable other pets, especially birds, to talk online as well. Sadly, an old woman  filed a complaint after her six cats jumped out of the window one by one after spending ten hours in front of a PC; it's time to hire a lawyer.

Few projects achieve the latest step. Most remain at the first step. Many are between the first and the last. There is nothing wrong to target one of the intermediary steps.

Each step is more and more challenging, and also teaches you more and more things. For example, at the last step, you have to have a lawyer, an accountant, salesmen, marketing people, etc. You may have been an excellent technician, but you should also be able to sell, to market, to defend your interests in court and to pay taxes.

The fact that the interviewers ask you questions about the business side of your projects is understandable. If you were interested by the technical aspects only, that's perfectly fine (for a software engineer). On the other hand, if you had successfully built a full-grade commercial product which was actually sold, it's even better, because it shows that:

Your concept was commercially viable,

You were able to do it technically,

And you were able to convince other people to actually use it and pay for it.

Does it cause a red flag that you've done only the technical part? Not at all. If they need to hire a businessman but they tell that they search for a software engineer, they are doing it wrong. So no, it's not a bad thing; just be very clear about your motives: you don't sell this because:

You don't want to.

Perfectly understandable. You may hate talking to customers, or doing accounting. This is not your job.

You are interested in technical aspects only.

Perfectly understandable. You want to focus on the things you will need the most during your career.

not because:

You tried and failed.

Depending on how a failure is presented, this can become very negative.

You don't even try, telling yourself that you'll never succeed.

Nobody wants to hire a person who don't believe in him and in what he does, so even if the candidate knows exactly why the project is not viable commercially, he should omit talking about that under the risk of giving a negative impression anyway.


问:
Cloning existing software for commercial purposes - legal implications
I have been asked to clone some existing software for a company. Basically its an old 16 bit DOS console app, which was supplied free of charge in I believe the late 80's. Having replaced the machine that needs to run it with a box running Win7 x64 they can't get it to work. It crashes every couple of minutes under DOSbox.

The company that supplied it appears to no longer exist - if they did the company asking me to do this would almost certainly know about it. Its undetermined whether they have gone entirely or are just trading under a different name. If the latter they seem to have withdrawn from the market related to this product (because again, niche area, we should know about everyone there).

What is the status to this with regards to copyright etc.? The main concern for the company involved is they want an identical interface to what they already have so I would have to clone this entirely. Having no source code / indication of the underlying mechanisms these would be written from scratch.

Is an interface covered by copyright? / Does that still hold 30 years later? What is the assumed license when none at all is provided?

Under UK law would I be under any serious risk were I to take on the project? How would this pan out if I then decided to sell the software on to other companies?

答:
What is the status to this with regards to copyright etc.? The main concern for the company involved is they want an identical interface to what they already have so I would have to clone this entirely. Having no source code / indication of the underlying mechanisms these would be written from scratch.

Is an interface covered by copyright? / Does that still hold 30 years later? What is the assumed license when none at all is provided?

What is the status to this with regards to copyright etc.?

Legal questions are best asked of someone familiar with the ins and outs of the various aspects of intellectual property law in your jurisdiction.  Asking random people on the internet is not legal advice and shouldn't be construed as legal advice.  Everything that I'm going to say below is based on the US understanding of legal issues by a layman - the US because thats were most of the software court cases have been.  You're in the UK - talk with a local legal expert.

Copyright protects the specifics works.  The actual source code.  The graphics resources used in creating the application.  The music that the app plays.  The text in the help pages in the app.

The look and feel of an an application sits on some confusing ground.  Icons can be copyrighted.  In Lotus vs Borland the menu was at issue.  The outcome of the case was that the implementation is subject to copyright, and the icons in a menu may be copyrighted, but the operations and mechanics are not copyrightable.

Neither are they patentable (one might get a design patent on various aspects of how things look... but thats very unlikely for an old 16 bit DOS app).  And they aren't able to be trademarked either (one might be able to trademark specific words in the menu such as "About Frobnostic..." where Frobnostic is the trademarked name of the app).

You are starting out from scratch, doing a clean room design (or nearly so).  You are probably in the clear.


答:
Well, you got lucky.  I practice IP law.  Yet to hedge, I practice patent law, not copyrights, and this is a copyrights issue.  (The USPTO would no grant patents on software until recently, and even so any patents from the 80s have long sense lapsed and are rendered unenforceable.)

So the short answer is it depends.  The mere fact that someone wrote that software means there is an inherent copyright.  Filing a copyright at the Library of Congress just establishes a date for litigation later, but the mere act of creating original work renders a work copywritten, so long as it is minimally creative and tangibly recorded.  A copyright from the 80s IS still enforceable.  Copyrights have much longer terms than patents and do not require the maintenance fees patents do.

Honestly, based on what you just told us, it appears you would be infringing as you've met the three prong test of copying, similarity, and appropriation.  The question then becomes is your version of the software fair use.  ... this is a very tough question to answer.  I would guess you are ok because you will on some levels be transforming the main work and (most importantly) you won't be harming the market for the original work.  The doctorine of fair use will likely protect you from any unfavorable judgment if you get sued, so lucky you again.  Read up on fair use here, as this is one of the documents I used to brush up on my copyright law (sorry, I'm not going to do the leg work to look up the statutes and case law and decode them for you, lol).
http://www.kentlaw.edu/perritt/courses/seminar/papers%202009%20fall/Susan%20Estes%20Seminar%20paper%20Final.pdf

Lastly, THIS IS NOT LEGAL ADVICE; I AM NOT ACTING AS YOUR ATTORNEY. This is general advice and I decline any liability based upon any actions you take in furtherance of this information.  I have not researched this further than what I remember from school, and I haven't really looked back since.  Write you code at your own risk.  :)  After all, why would I risk getting sued for malpractice without you paying me when I'm trying to be helpful to a stranger discussing subject matter I haven't studied in 10 years?  :)

Title: Cloning existing software for commercial purposes - legal implications

Body:
I have been asked to clone some existing software for a company. Basically its an old 16 bit DOS console app, which was supplied free of charge in I believe the late 80's. Having replaced the machine that needs to run it with a box running Win7 x64 they can't get it to work. It crashes every couple of minutes under DOSbox.

The company that supplied it appears to no longer exist - if they did the company asking me to do this would almost certainly know about it. Its undetermined whether they have gone entirely or are just trading under a different name. If the latter they seem to have withdrawn from the market related to this product (because again, niche area, we should know about everyone there).

What is the status to this with regards to copyright etc.? The main concern for the company involved is they want an identical interface to what they already have so I would have to clone this entirely. Having no source code / indication of the underlying mechanisms these would be written from scratch.

Is an interface covered by copyright? / Does that still hold 30 years later? What is the assumed license when none at all is provided?

Under UK law would I be under any serious risk were I to take on the project? How would this pan out if I then decided to sell the software on to other companies?


答:
You can check the details on various sources on Hadoop, but since you asked here.
I think you want to have a summarization. I would summarize the answers to your two questions as follows:

How is intermediate data organized?
There are 2 kinds of intermediate data, intermediate data on the mapper and intermediate data on the reducer. Intermediate data on the mapper is map from key to value. Intermediate data on reducer are ALL values of certain set of keys, partitioned by the key. If you want even more details than this then it goes into the file system organization, than I would redirect you to read the source code of Hadoop. I am not sure you need to know that, you might but I am not you so I don't know your need.

What happens when there is too much data about a key to be held by a single file?
Then you keep them in multiple files BUT when you call the reducer on a certain key, you must be able to get access to all of them (all values of any key)

Hopefully, I don't miss anything here. If I do, would be very glad to be corrected.

Title: How is intermediate data organized in MapReduce?

Body:
From what I understand, each mapper outputs an intermediate file. The intermediate data (data contained in each intermediate file) is then sorted by key.

Then, a reducer is assigned a key by the master. The reducer reads from the intermediate file containing the key and then calls reduce using the data it has read.

But in detail, how is the intermediate data organized? Can a data corresponding to a key be held in multiple intermediate files? What happens when there is too much data corresponding to one key to be held by a single file?

In short, how do intermediate partitions differ from intermediate files and how are these differences dealt with in the implementation?


答: Here is an alternative approach: why not build your text editor like a transactional database? Instead of writing the whole document, make a backup copy once a document is loaded or saved (as a "reference document") and create a transaction log, where only the key strokes and menu commands are recorded. Write these commands continously to disk (which can be done quick, immediately and in the background), so in case of a system crash, you can always reconstructed the last working state by loading the last reference doc and re-apply all commands from the trans log.</p>


问:
Programmatically syncing with remote servers
My application generates text files that need to be synced with remote servers, which may be windows or linux. Sync has to happen without user's intervention.

I tried with rsync but windows doesn't come with rsync by default. Also it is not possible to supply password in the command line for rsync.

Currently I'm going with ftp. But that seems like an inefficient way.

Is there a way to rsync without user intervention? What are the ways to sync with a remote server programmatically? App is on nodejs.

答:
Synching entire files has the tendency to create more traffic than you want. It may be a better option to do this on a finer-grained level, using your own RPC service.

For example, you could have a look at Apache Thrift. It basically allows you to create a remote service to do next to anything what you want. It works cross-language, cross-platform out of the box, designed to be efficient and based on a very simple IDL.

While creating your own service may involve some work, you are no longer bound to transferring entire files (but you can of course do that as well), you will also be able to extend the service in the future easily.

If you still want to exchange whole files, consider compressing them beforehand, and using services like dropbox, or the like. Depending on the "disclosure-level" of your data, you may additionally want to encrypt them during transfer.


问:
Why isn't there a python compiler to native machine code?
As I understand, the cause of the speed difference between compiled languages and python is, that the first compiles code all way to the native machine's code, whereas python compiles to python bytecode, to be interpreted by the PVM. I see that this way python codes can be used on multiple operation system (at least in most cases), however I do not understand, why is not there an additional (and optional) compiler for python, which compiles the same way as traditional compilers. This would leave to the programmer to chose, which is more important to them; multiplatform executability or performance on native machine.
In general; why are not there any languages which could be behave both as compiled and interpreted?</p>

答:
No. The reason why there are speed differences between languages like Python and C++ is because statically-typed languages give the compiler tons of information about the structure of the program and its data which allows it to optimize both computations and memory access. Because C++ knows that variable is of type int, it can determine the optimal way to manipulate that variable even before the program is run. In Python on the other hand, the runtime doesn't know what value is in a variable right until the line is reached by the interpreter. This is extremely important for structures, where in C++, the compiler can easily tell the size of the structure and every location of its fields within memory during compilation. This gives it huge power in predicting how the data might be used and lets it optimize according to those predictions. No such thing is possible for languages like Python.

To effectively compile languages like Python you would need to:

Ensure that the structure of data is static during the execution of the program. This is problematic because Python has eval and metaclasses. Both which make it possible to change the structure of the program based on the input of the program. This is one of the things that give Python such expressive power.

Infer the types of all variables, structures and classes from the source code itself. While it is possible to some degree, the static type system and algorithm would be so complex it would be almost impossible to implement in a usable way. You could do it for a subset of the language, but definitely not for the whole set of language features.


问:
Mimicking a bluetooth disconnection
I've written a program to control a bluetooth device. I'm trying to test cases when the bluetooth disconnects, i.e. if its out of range.

Physically taking the device out of range is one possibility, but its quite cumbersome and I have to go outside my office to achieve this.

What can I do to trigger a disconnection? Is there, for example, an interferer I can setup, say with an Android phone, that would make the connection drop? Or limit the Bluetooth transmit power? Any other possibilities?

答:
Here's how I have approached a similar case in a Bluetooth application. The advantage is that you can test your application logic without actually using a device or having to setup a communication at all. This also means you can regression test much easier and faster.

Separate concerns

Implement a comms module that handles the Bluetooth communication, i.e. connecting, sending, receiving data, disconnecting. This module translates each of these events into application-level commands & responses and passes these on to a logic module that processes them. This way, logic handles all the application logic, while comms handles the communication.

Build a simulator for the comms module, a.k.a driver. The simulator should act as the comms module would, i.e. generate and consume commands & responses. But since it is independent of any actual communication, you can control exactly what it does, and when, while the logic module thinks it actually talks to to real comms module.

Build unit tests that use the driver to create a controlled environment to test your logic module.

For example, one unit test could be like so (pseudo-code):

```<code>def test_disconnect():
   # create a new driver and attach it to the logic module
   driver = Driver()
   logic.attach(driver)
   # make a connection, then disconnect
   driver.connect()
   driver.disconnect()
   # check if logic has processed the disconnect
   # (assuming this produces an observable state)
   assert(logic.isDisconnected)
</code>```

Title: Mimicking a bluetooth disconnection

Body:
Here's how I have approached a similar case in a Bluetooth application. The advantage is that you can test your application logic without actually using a device or having to setup a communication at all. This also means you can regression test much easier and faster.

Separate concerns

Implement a comms module that handles the Bluetooth communication, i.e. connecting, sending, receiving data, disconnecting. This module translates each of these events into application-level commands & responses and passes these on to a logic module that processes them. This way, logic handles all the application logic, while comms handles the communication.

Build a simulator for the comms module, a.k.a driver. The simulator should act as the comms module would, i.e. generate and consume commands & responses. But since it is independent of any actual communication, you can control exactly what it does, and when, while the logic module thinks it actually talks to to real comms module.

Build unit tests that use the driver to create a controlled environment to test your logic module.

For example, one unit test could be like so (pseudo-code):

def test_disconnect():
   # create a new driver and attach it to the logic module
   driver = Driver()
   logic.attach(driver)
   # make a connection, then disconnect
   driver.connect()
   driver.disconnect()
   # check if logic has processed the disconnect
   # (assuming this produces an observable state)
   assert(logic.isDisconnected)


问:
In which object should I implement wait()/notify()?
I'm working in an Android project with multithreading. Basically I have to wait to the server to respond before sending more data.

The data sending task is delimited by the flag boolean hasServerResponded so the Thread will loop infinitely without doing anything until the flag becomes true.

Since this boolean isn't declared as volatile (yet), and also looping without doing anything  wastes resources, I thought maybe I should use AtomicBoolean and also implement wait() / notify() mechanism.

Should I use the AtomicBoolean object notify() and wait() methods or should I create a lock Object?

答:
the typical way to implement this is:

```<code>void waitForResponce() throws InterruptedException{
    synchronized(this){
        while(!responded)wait();
    }
}

private boolean responded=false;

void signalResponce(){
    synchronized(this){
        responded=true;
    }
}
</code>```

the while loop is to guard against spurious wakeups and the synchronized surrounds the while to guard against race conditions

if you which to allow resets of the signal it gets tougher

however in android there is an event loop and async tasks that you can leverage which has callback to the ui thread build in

OwnerDisplayName: Christopher Francisco

Body:
the typical way to implement this is:

void waitForResponce() throws InterruptedException{
    synchronized(this){
        while(!responded)wait();
    }
}

private boolean responded=false;

void signalResponce(){
    synchronized(this){
        responded=true;
    }
}

the while loop is to guard against spurious wakeups and the synchronized surrounds the while to guard against race conditions

if you which to allow resets of the signal it gets tougher

however in android there is an event loop and async tasks that you can leverage which has callback to the ui thread build in