Finite state machine with gamma distributed waiting times

Question: I have a state machine with positive and negative inputs. The time between positive inputs follows a gamma distribution ($X_+ \sim \Gamma(k_+, \theta_+)$), and the time between negative inputs follows a different gamma distribution ($X_- \sim \Gamma(k_-, \theta_-)$). Hence, the probabilities of receiving $K$ positive and negative inputs over some interval of time are exactly known for all $K$. The state machine is shown below:

The blue boxes represent achievable states in the machine, and the solid and dashed lines represent a positive and negative input, respectively. So for instance, if the machine is in state 3 and a positive input arrives, the machine yields a positive output and resets to state 2. If the machine then receives a negative input, it moves to state 1 without producing any output.
Is it possible to find the PMF for positive outputs? That is, what is the probability of getting $K$ positive outputs over the same interval of time for all $K$?
Comment: The problem with presenting a toy version of your real problem is that you may not be abstracting it in the way that will be most helpful for you. (Nonstatisticians are often surprised about what details are statistically important.) So [describe the real problem instead](http://arfer.net/w/statqgl).
Comment: (+1)  Are all waiting times are independent of each other, ie, the positive inputs and negative inputs form two independent renewal processes? (This, if a negative input occurs at time $t$, the time to next negative input is independent of all previous occurrences of positive and negative inputs? and the state?) This is most likely intended since no dependence is specified (but needs to be assumed since the description as written does not guarantee this)
Comment: Thanks. I don't know much about this domain, but it looks like you could probably represent the system as a Markov chain.
Comment: I have been looking at continuous time Markov chains, and it doesn't look like my state machine satisfies the Markov property since the time spent in each state does not follow an exponential distribution. Am I dead in the water, or is there something else I could try?
Comment: @JuhoKokkala, the answer is yes, they are independent of each other. If a negative input arrives at time t, the time to the next negative input does not depend on previous inputs or the current state.
Answer: Note that this is NOT an attempt to fully answer the problem, but to show how to overcome the lack of the Markov property for a special case that may not apply - one that is far too long to put in comments.
Unfortunately, as you have realized, this is not a Markov process, but a semi-Markov process.   If you a) have integer $k_+$ and $k_-$, and b) are willing to expand your state space, you can transform this to a Markov process using the fact that the Gamma distributions become Erlang distributions and the Erlang variates are a sum of i.i.d. Exponential variates with the same scale parameter as the original Erlang variate.
We can expand the state space to include two new variables, the "+ state" and "- state", which record "how far along" we are in generating the next positive or negative arrival.  For concreteness, assume $k_+ = 5$; the next positive arrival occurs when the fifth of five consecutive Exponential arrivals has occurred, so the "+ state" records how many positive arrivals have occurred since the last positive input.  The sequence of "+ state" values is $\{0, 1, 2, 3, 4, 0, 1, ...\}$; state $0$ can only transition to $0$ or $1$, state $4$ can only transition to $4$ or $0$, and so forth.
Your state space becomes $[\text{BoxID},+,-]$ recording which box the process is in, how many positive arrivals have occurred modulus $k_+$, and how many negative arrivals have occurred modulus $k_-$.
We now have two random variates - the time until the next "+ state" transition and the time until the next "- state" transition - both of which are Exponentially distributed.  As the minimum of two independent Exponential variates is itself Exponential, the time until the next transition (of any type) is Exponential with rate equal to the sum of the two component rates ($\theta_+ + \theta_-$ or $1/\theta_+ + 1/\theta_-$ depending on how your Gamma distributions are parameterized). The probability that the next transition is a "+ state" transition is just $\theta_+/(\theta_+ + \theta_-)$, or $1/\theta_+ / (1/\theta_+ + 1/\theta_-)$, again depending on how your Gamma distributions are parameterized.  Given that the time to the next transition now has an Exponential distribution, you have a CTMC (Continuous Time Markov Chain), which can be analyzed in standard ways.
For a concrete example, assume positive arrivals occur at a rate of $0.5/$unit of time and negative arrivals occur at the rate of $0.25/$unit of time.  The time until the next transition is Exponential with a rate of $0.75/$unit of time and the probability that the transition is triggered by a positive arrival is $0.5/(0.5+0.25) = 2/3$.
Now you have a much-enlarged state space, with each box in your initial diagram having $k_+k_-$ states interior to it, but at least you have the Markov property and can find the steady-state probabilities of being in box 3 and the states with "+ state" = $k_+-1$, i.e., one of the states from which you can experience a transition that results in a positive output.  Combining those steady state probabilities with the transition matrix and the mean time between transitions gives you the long run average rate of seeing a positive output.  You can also calculate the desired probability distribution using the steady state probabilities, the transition matrix, and the fact that the time between transitions has an Exponential distribution with known rate.
Comment: This is exactly what I needed. I have very large $k_+$ and $k_-$ (on the order of ten thousand), so the transition matrix will be quite large. The saving grace is that the matrix is also very sparse. Thank you very much!
Comment: Well with your shape parameters that large, the distinction between integer and non-integer values is moot!    Yes, sparsity will indeed be your saving grace...


Count data - Poisson or negative binomial - SPSS?

Question: I have count data of litter found across a beach. In one column there is the litter type (plastic bottles, glass bottles etc.) and in the other the quantities found, such as below:

You can see that sometimes the same litter type comes back several times (bottles - I can sum the quantities of bottles and make it one data entry if required). In some cases I have extreme counts reported (e.g. 50 towels). Usually, the quantities range from 3 to 10. The aim here is to observe any significant difference between groups. Not knowing if I should go for a Poisson or negative binomial, I was looking to see if the mean of my data was equal to the variance:

Seeing they are not equal, and that sometime I have extreme counts, should I go for a negative binomial regression? In addition, I've run a Poisson regression on the data, and the omnibus test resulted in a p-value <0,05. Is that wrong in the case of a negative binomial regression needed? Many thanks
Comment: Thanks for including a sample of your data. Honestly, it's not clear to me that a poisson or negbin model is appropriate for the sum of Litter Types, since this sum is across such disparate items. What additional information do you have about this information, e.g., why are there multiple entries for *plastic bottles*? Are multiple beaches or days of the week being aggregated? To me, these possible sources of variance need to be accounted for by any model.
Comment: Thanks for your input. There are multiple entries because each entry was inputted by one single person. As there were 150 participants, I have 150 entries with different litter types and corresponding quantities. The idea is to observe any differences in quantities amongst litter types. As for now there is no more info being aggregated.
Answer: The Case Studies for the GENLIN procedure include a discussion of Poisson vs. negative binomial models and how to test to see which to use. See here.


Erro ao executar o emulador do Android Studio

Question: Estou com um problema na hora de emular um device no android studio. Quando tento acontece o seguinte erro:

15:28:24 FileNotFoundException:
  /home/henrique/.android/avd/3_2_HVGA_slider_ADP1_API_17.ini (Arquivo
  ou diretório não encontrado)

Depois de ter criado a AVD não aparece e quando clico pra emular aparece para criar uma.
Comment: Já verificou se realmente existe esse arquivo ou diretório no endereço indicado?
Comment: Isso que não entendo. Só tem uma arquivo dentro de .android/
Comment: Isso que não entendo. Só tem uma arquivo dentro de .android/ um arquivo chamado uid.txt, com esse conteúdo: 23a02592-9afb-4694-b798-fccd0956879a
Comment: Tente criar novamente uma maquina virtual e verifique onde ela está sendo configurada.
Comment: Esse diretório avd é onde encontra todas as máquinas virtuais criadas.
Comment: No caso ter que desinstalar o AVD e reinstalar normalmente?
Comment: Você criou uma máquina virtual certo?! Então, quando você cria um avd ele gera esse arquivo `.ini`. Pelo que parece, a ide não está encontrando esse arquivo. Por padrão ele deveria estar dentro desta pasta avd. O que estou dizendo é pra você tentar criar uma outra  avd com um nome mais acessível e fazer uma busca no seu HD pra ver onde foi parar esse arquivo.
Comment: Há, certo. Farei isso. Muito obrigado pelas dicas. Informarei se deu certo.
Comment: Não deu certo. Aparentemente ele não chega a criar a AVD.
Answer: Pelo que percebemos, seu arquivo .ini não está dentro do diretório padrão como deveria estar. Então você tem que fazer uma verificação de onde realmente foi criado seu AVD (Android Virtual Devices).
Para mostrar os arquivos .ini e .img do AVD associado no disco, clique com o botão direito do mouse em um AVD e selecione Show on Disk. Ou clique em Menu  e selecione Show on Disk.
Você pode ler mais na própria documentação que diz como Criar e gerenciar dispositivos virtuais.
Comment: @henrique-nascimento no link desta resposta, mostra como é possível criar e gerenciar dispositivos virtuais.
Comment: Então na verdade ele deixa criar a AVD, mas no caso quando se vai para a tela de gerenciar dispositivos virtuais, não tem nada. E quando procurei no HD não encontrei nada também relacionado a avd que tinha criado.
Comment: @HenriqueNascimento O problema então está na criação de um AVD. Vi ali no canto superior que tem atualizações a fazer no seu Android Studio. Tente atualiza-lo para a versão mais recente que talvez deva corrigir esse problema.
Comment: Vou atualizar, se não der certo ou se der posto aqui. Mas provavelmente vou desinstalar, é a terceira vez que tento instalar no ubuntu, desde sexta e toda vez da algum tipo de erro. Vou reinstalar em outra oportunidade. Quando tive windows em dualboot com ubuntu.
Comment: @HenriqueNascimento eu já passei muita raiva com esses tipo de coisa. Hoje eu uso Windows, mas gostaria de estar usando Linux mas já tive uns problemas grandes de perder bastante tempo e perder projetos. Por toda ruindade do Windows, ainda uso por causa de Photoshop e CorelDraw que sou viciado. Nas épocas que usei UBUNTU, migrei para o Inkscape, mas nunca, nem chegou aos pés dessas duas ferramentas maravilhosas (Photoshop e CorelDraw) hehe. Mas boa sorte ai!
Comment: kkk sei come é. Eu criei uma pasta avd dentro do diretório .android/ ficando assim: .android/avd. Agora mostra as AVD's no gerenciado. Porem ainda ta com um erro:


Merging rows to map type based on max value in a column

Question: I am doing a small POC to ingest the user events(CSV file) from a website. Below is the sample input:

Input Schema:

Output should be in the format as below

The logic required is to group by the id column and merge the
name and value columns to a Map type where the name column represents the key
and the value column represent the value in the Map type. The value to be picked for each key in the Map is the one with the highest value in the timestamp column.
I was able to achieve some part where it needs to be grouped by id and extract maximum of the timestamp column.I am facing difficulty with selecting one value(from corresponding max timestamp) for each id) and merge with other names(using map).
Below is my code
import org.apache.spark.sql.functions._
import org.apache.spark.sql.expressions.Window
val schema = StructType(List(
    StructField("id", LongType, nullable = true),
    StructField("name", StringType, nullable = true),
    StructField("value", StringType, nullable = true),
    StructField("timestamp", LongType, nullable = true)))
val myDF = spark.read.schema(schema).option("header", "true").option("delimiter", ",").csv("wasbs:///HdiSamples/HdiSamples/SensorSampleData/hvac/tru.csv")
val df = myDF.toDF("id","name","value","timestamp")
//df.groupBy("id","name","value").agg(max("timestamp")).show()
val windowSpecAgg  = Window.partitionBy("id")
df.withColumn("max", max(col("timestamp")).over(windowSpecAgg)).where(col("timestamp") === col("max")).drop("max").show()

Comment: Please don't add sample data as image.
Answer: Use window function and filter out latest data by partitioning on "id","name"
later use map_from_arrays,to_json functions to recreate the desired json.
Example:
df.show()

//sample data
//+---+----+-------+---------+
//| id|name|  value|timestamp|
//+---+----+-------+---------+
//|  1|   A| Exited|     3201|
//|  1|   A|Running|     5648|
//|  1|   C| Exited|     3547|
//|  2|   C|Success|     3612|
//+---+----+-------+---------+

val windowSpecAgg  = Window.partitionBy("id","name").orderBy(desc("timestamp"))

df.withColumn("max", row_number().over(windowSpecAgg)).filter(col("max")===1).
drop("max").
groupBy("id").
agg(to_json(map_from_arrays(collect_list(col("name")),collect_list(col("value")))).as("settings")).
show(10,false)

//+---+----------------------------+
//|id |settings                    |
//+---+----------------------------+
//|1  |{"A":"Running","C":"Exited"}|
//|2  |{"C":"Success"}             |
//+---+----------------------------+

Answer: You can use ranking function - row_number() to get the latest records per partition.
val spark = SparkSession.builder().master("local[*]").getOrCreate()
spark.sparkContext.setLogLevel("ERROR")
import spark.implicits._

val df = Seq((1, "A", "Exited", 1546333201),
  (3, "B", "Failed", 1546334201),
  (2, "C", "Success", 1546333612),
  (3, "B", "Hold", 1546333444),
  (1, "A", "Running", 1546335648),
  (1, "C", "Exited", 1546333547)).toDF("id", "name", "value", "timestamp")

df.withColumn("rn",
  row_number().over(Window.partitionBy("id", "name").orderBy('timestamp.desc_nulls_last)))
  .where('rn === 1)
  .drop("rn")
  .groupBy("id")
  .agg(collect_list(map('name, 'value)).as("settings"))
  .show(false)
/*
+---+-------------------------------+
|id |settings                       |
+---+-------------------------------+
|1  |[[A -> Running], [C -> Exited]]|
|3  |[[B -> Failed]]                |
|2  |[[C -> Success]]               |
+---+-------------------------------+ */


Proving $\bigwedge^k(U\cap V) = \left(\bigwedge^kU\right) \bigcap \left(\bigwedge^k V\right)$ via the universal mapping property

Question: Let $V$ be a (finite dimensional) vector space, its exterior algebra of order $k$ is the vector space $\bigwedge^k V$ consisting of the formal sums of terms of the form $v_1 \wedge v_2 \wedge \dots \wedge v_k$, where each $v_i \in V$ (with a few additional properties regarding $\wedge$). There are quite a few ways to define $\bigwedge^k V$ rigorously, one of them is via the universal mapping property:

There exists a vector space $L$ and an alternating multilinear map $M\colon V^k \to L$ with the universal mapping property in the sense that for any alternating multilinear map $M'\colon V^k \to L'$, there exists a unique linear map $T\colon L\to L'$ such that $M' = T\circ M$. This space $L$ is unique up to isomorphism and we define $\bigwedge^k V := L$ and write $v_1 \wedge v_2 \wedge \dots \wedge v_k:= M(v_1,v_2,\dots,v_k)$.

From the above construction, all the usual properties of $\bigwedge^k V$ follow, e.g. if $\{ e_1, \dots , e_n \}$ is a basis for $V$, then $\{ e_{i_1} \wedge \dots \wedge e_{i_k} \}_{i_1<\dots<i_k }$ is a basis for $\bigwedge^k V$. If $U$ is a subspace of $V$, then $\bigwedge^k U$ can be canonically identified with a subspace of $\bigwedge^k V$ via the universal property (the inclusion map $i\colon U^k\to V^k$ composes with $M$ is an alternating multilinear map from $U^k$ into $\bigwedge^k V$).
Now, let $W$ be a vector space and $U,V$ be subspaces of $W$. I believe it is true that $\bigwedge^k(U\cap V) = \left(\bigwedge^kU\right) \bigcap \left(\bigwedge^k V\right)$, which should be straight forward to prove  by fixing a common basis for $U,V$ in $W$. However, I want to know if it can be demonstrated from the perspective of category theory using the universal mapping property. It seems that this would require some characterization of $U\cap V$ in terms of morphisms but my working knowledge of techniques  from category theory has mostly faded away at this point (not that there was much of it from the beginning). Any help is highly appreciated, especially if it's accompanied by a diagram :-)
Comment: @Thorgott it is not uncommon for left adjoints to preserve some limits, and in fact it comes up naturally quite often. For example, a good notion for a morphism between topoi is a left adjoint which preserves finite limits.
Comment: This is curious from the categorical point of view. Let's take the exterior algebra $\bigwedge^{\ast}$ rather than just one $\bigwedge^k$. This furnishes a functor from the category of vector space to the category of graded commutative algebras that is left adjoint to the forgetful functor. Intersections in these categories are pullbacks along the inclusions, so we're asking whether a left adjoint preserves pullbacks, which are limits. But, generically, it's the right adjoints that preserve limits.
Answer: Let me give some evidence that you should not expect there to be a purely "formal" proof using the universal property.  Namely, an analogous result is not true in a slightly more general setting.
Specifically, instead of vector spaces over a field, let us consider $\mathbb{Z}$-modules.  Let $W=\mathbb{Z}^2$ with standard basis $\{e_0,e_1\}$, let $U$ be the submodule generated by $2e_0$ and $e_1$, and let $V$ be the submodule generated by $e_0$ and $2e_1$.  Then $\bigwedge^2W$ is freely generated by $e_0\wedge e_1$, and the inclusion maps $U,V\to W$ induce injections on the second exterior powers that identify $\bigwedge^2 U$ with the submodule generated by $2e_0\wedge e_1$ and $\bigwedge^2 V$ with the submodule generated by $e_0\wedge 2e_1$.  But $e_0\wedge 2e_1=2e_0\wedge e_1$ in $\bigwedge^2 W$ so these are the same submodule.  On the other hand, $U\cap V$ is generated by $2e_0$ and $2e_1$, so $\bigwedge^2(U\cap V)$ is the submodule of $\bigwedge^2 W$ generated by $2e_0\wedge 2e_1=4e_0\wedge e_1$.  So in this case, $\bigwedge^2(U\cap V)$ is strictly smaller than $\bigwedge^2U\cap \bigwedge^2 V$ as submodules of $\bigwedge^2 W$.
So, any proof of this property is going to need to use something that is true about vector spaces but not true about $\mathbb{Z}$-modules (or even just finitely generated free $\mathbb{Z}$-modules).  Basically, this means you need to use bases, or at least semisimplicity (i.e., you need to use the fact that $W$ can be decomposed as a direct sum $W_0\oplus W_1\oplus W_2\oplus W_3$ where $U=W_0\oplus W_1$ and $V=W_0\oplus W_2$).
Comment: Very interesting. In any case, is it possible that there is some property intrinsic to the category of (finite dimensional) linear spaces over $\Bbb R$ that would perhaps enable the statement to be proved in a category-theory-ish way?
Comment: I just saw your edit. That does seem to address my question, thanks.
Answer: The important result is
$$\wedge^{\cdot}(V\oplus W)\simeq \wedge^{\cdot}(V)\otimes \wedge^{\cdot}(W)$$
valid for modules over every commutative ring.
However, it is possible to have    modules $U\subset V$, while $\wedge^2(U) \ne 0$, $\wedge^2(V)= 0$. Indeed, consider $U = (\frac{1}{n}\mathbb{Z}/\mathbb{Z})^{\oplus 2} \subset (\mathbb{Q}/\mathbb{Z})^{\oplus 2} = V$.
Comment: Sorry if this is a stupid question but could you please elaborate a bit on how your answer is related to my question?
Comment: For $\mathbb{Z}$ modules, rather than vector spaces, you can have $U\subset V$ but $\wedge^2(U) \not \subset \wedge^2(V)$.  So the argument for vector spaces has to include a bit more than the universality ... it's what the other answer says...
Comment: I see what you meant now, thank you.


What Database for extensive logfile analysis?

Question: The task is to filter and analyze a huge amount of logfiles (around 8TB) from a finished research project. The idea is to fill a database with the data to be able to run different analysis tasks later.
The values are stored comma separated. In principle the values are tuples of up to 5 values:
id, timestamp, type, v1, v2, v3, v4, v5

In a first try using MySQL I used one table with one log entry per row. So there is no direct relation between the log values. The downside here is slow querying of subsets.
Because there is no relation I looked into alternatives like NoSQL databases, and column based tables like hbase or cassandra seemed to be a perfect fit for this kind of data. But these systems are made for huge distributed systems, which we not have. In our case the analysis will run on a single machine or perhaps some VMs.
Which kind of database would fit this task? Is it worth to setup a single machine instance with hadoop+hbase... or is this all a bit over-sized?
What database would you choose to do high-performance logfile analysis?
EDIT: Maybe out of my question it is not clear that we cannot spend money for cloud services or new hardware. The Question is if there are benefits in using noSQL approaches instead of mySQL (especially for this data). If there are none, or if they are so small that the effort of setting up a noSQL system is not worth the benefit we can use our ESXi infrastructure and MySQL.
EDIT2: I'm still having the Problem here. I did further experiments with MySQL and just inserted a quarter of all available data. The insert is now running for over 2 days and is not yet finished. Currently there are 2,147,483,647 rows in my single table db. With indeces this takes 211,2 GiB of disk space. And this is just a quarter of all logging data...
A query of the form
SELECT * FROM `table` WHERE `timestamp`>=1342105200000 AND `timestamp`<=1342126800000 AND `logid`=123456 AND `unit`="UNIT40";

takes 761 seconds to complete, in this case returning one row.
There is a combined index on timestamp, logid, unit.
So I think this is not the way to go, because later in analysis I will have to get all entries in a time range and compare the datapoints.
I read bout MongoDB and Redis, but the problem with them is, that they are in Memory databases.
In the later analyzing process there will a very small amount of concurrent database access. In fact the analyzing will be run from one single machine.
I do not need redundancy. I would be able to regenerate the database in case of a failure.
When the database is once completely written, there would also be no need to update or add further row.
What do you think about alternatives like Redis, MongoDB and so on. When I get this right, i would need RAM in the dimension of my data...
Is this task even somehow possible with a single node system or with maybe two nodes?
Comment: If you need to do this but don't have the systems for it you could take a look at solutions like Amazon where you can use them for a specified amount of time. That gives you the power to do such big data analyses quite simple and affordable compared to investing in hardware.
Comment: Out of privacy and financial aspects, we can not use such ressources like amazon.
Comment: Ok clear, then the following important question: How much hardware do you have? It's 8TB of data, based on the systems you have we can give a more clear answer. What happens now is that you ask for performance advise but give no info about: systems, performance of them, availability of it (can you use it for days without other users) and about what kind of analysis you want to do. All missing parts which makes it impossible to give good advice. To be clear: You will need some serious memory to let 8TB of data be analyzed. But if you for example can split it our create aggregates it is easier.
Comment: There are several ESXi Servers which can be used, maybe one exclusively (8 Cores 24GB RAM).
The analysis will be something like "give me all log entries where id=x and type=y and timestamp in range of ..." and then we will be looking at the values to analyze project specific parameters.
The exact analyzing questions are to be defined yet. The date should be in a form where we easily can do such analyzes.
Answer: well i personally would prefer the faster solution, as you said you need a high-perfomance analysis. the problem is, if you have to setup a whole new system to do so and the performance-improvement would be minor in relation to the additional effort you'd need, then stay with SQL.
in our company, we have a quite small Database containing not even half a GB of Data on the VM. the problem now is, as soon as you use a VM, you will have major performance issues, when opening the Database on VM you can go for a coffee in the meantime ;)
But if the time until the Database is loaded to cache is not so important it doesn't matter. It all depends on how much faster you think the new System will be, and how much effort you will have to put in it, but as i said i'd prefer the faster solution if you have to go for "high-performance analysis"
Comment: The question is: what is the faster solution? I have no experience in noSQL approaches and the performance when not run on distributed systems. Maybe the effort of installing a new noSQL single node server isn't worth the small performance gain.
When saying "high-performance" I mean relatively fast querying in relation to the amount of data.
Comment: i have no experience concerning that either so i just said what i thought. you have to decide yourself either way, and in single node mode i guess both attempts will be quite slow. the faster solution for implementation will obviously be the sql attempt, seen from the point of performance it depends on how often you need to analyse the data. i think it is not reasonable to implement something you are completely new to just to run 10 times and then discard.  [...]
Comment: [...] the more often you analyse, the more time you win by having the faster solution, but you have to think of how long you will need to implement it. and from your description i think it is really some, "do once and never again" task.


I'm not sure why my QThread pattern is blocking

Question: I've seen many posts and articles on QThread and moving QObjects between QThreads but alas, its still causing me headaches. This is the pattern I'm trying to adopt:
#include "connectionthread.h"
#include <cassert>

ConnectionThread::ConnectionThread(ConnectionPtr const &connectionPtr) :
                               worker(NULL),
                               m_connectionPtr(connectionPtr)
{
    connect(this, SIGNAL(executeSignal()), this, SLOT(loginProcess()));
}

void
ConnectionThread::start()
{
    if(worker) {
        if(worker->isRunning()) {
            worker->quit();
        }
        delete worker;
    }
    worker = new QThread;
    connect(worker, SIGNAL(started()), this, SLOT(run()));
    worker->start();
}

void
ConnectionThread::run()
{
    emit executeSignal();
}

void
ConnectionThread::loginProcess()
{
    m_connectionPtr->Connect();
}

Now an instance of this is created in the main GUI thread, yet when loginProcess is finally called, it blocks until completion which causes my app's GUI to hang. Note, no difference is observed if I put the logic code directly into the run function and omit the signal like as follows:-
void
ConnectionThread::run()
{
    m_connectionPtr->Connect();
}

So I assumed that I need to move 'this' to the thread named worker, something like:
void
ConnectionThread::start()
{
    if(worker) {
        if(worker->isRunning()) {
            worker->quit();
        }
        delete worker;
    }
    worker = new QThread;
    this->moveToThread(worker);
    connect(worker, SIGNAL(started()), this, SLOT(run()));
    worker->start();
}

but this gives me
QObject: Cannot create children for a parent that is in a different thread.

I'm not sure why this is the case however since an instance of ConnectionThread is created and its start function is called from another thread. Let's call this other thread GuiThread. This means that GuiThread has control so should be able to transfer ownership of the ConnectionThread instance to the worker thread.
One final possibility which I haven't fully explored yet is the possibility of moving m_connectionPtr to the worker thread..
Any thoughts on the above pattern, how I might improve it, and generally how I can prevent it from blocking?
Comment: Is connection thread derived from anything? Is ConnectionPtr the class that does the job you want to be executed in another thread? Againg, is conncetionPtr derived from anything From the sample you have posted, it is not quite clear...
Comment: I think that crucial is if actually ConnectionThread object should not be moved to separate thread. I cannot see which actions you would like to delegate to worker. Now I don't see anything going on in worker.
Comment: ConnectionThread is derived from QObject. And ConnectionPtr, which is also derived from QObject does that job that I want carried out in another thread.
Answer: EDIT 1: The following was my proposed solution but it doesn't actually work as expected because the finished() signal is never being emitted by worker
EDIT 2: fixed the finished signal being triggered but I still can't move m_connectionPtr back to the main thread within the moveConnectionPtrBack. Gives error "QObject::moveToThread: Current thread (0x102900380) is not the object's thread (0x10493b740). Cannot move to target thread (0x102900380)"
So, I think I've figured out what to do: the solution it seems was to transfer thread ownership of the ConnectionPtr to the worker thread:
#include "connectionthread.h"

ConnectionThread::ConnectionThread(ConnectionPtr const &connectionPtr) :
                               worker(NULL),
                               m_connectionPtr(connectionPtr)
{
    // EDIT 2 added bit -- m_connectionPtr sends signal when work finished
    connect(m_connectionPtr.data(),
            SIGNAL(connectFinishedSignal()), this, SLOT(quitThread()));
}

void
ConnectionThread::start()
{
    if(worker) {
        if(worker->isRunning()) {
            worker->quit();
        }
        delete worker;
    }
    worker = new QThread;
    m_connectionPtr->moveToThread(worker);
    connect(worker, SIGNAL(started()), m_connectionPtr.data(), SLOT(Connect()));
    connect(worker, SIGNAL(finished()), this, SLOT(moveConnectionPtrBack()));
    worker->start();
}

void
ConnectionThread::moveConnectionPtrBack()
{
    // this call failing still
    m_connectionPtr->moveToThread(QApplication::instance()->thread());
}

// EDIT 2 added bit; quitting causes worker to send finished signal() which causes
// correct triggering of moveConnectionPtrBack() function
void
ConnectionThread::quitThread()
{
    worker->quit();
}

(Note m_connectionPtr is a shared ptr to a 'Connection' which itself is derived from a QObject but has no parent; likewise ConnectionThread is derived from a QObject but again, has no parent).
Since m_connectionPtr is also used by other threads in future, I also had to move it back again to the main thread as shown by the moveConnectionPtrBack slot.
Seems to do the trick but overall is not entirely bug-free.


Angularjs - multiplas abas no mesmo navegador

Question: Estou utilizando angularJS em minha aplicação Node. Quando tento abrir uma outra Aba também com minha aplicação no mesmo navegador ( ou abrir outra instância do mesmo navegador ) recebo esse erro:
s:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0(anonymous function) @ utils.js:14
angular.js:13236 SyntaxError: Unexpected token u in JSON at position 0
at Object.parse (native)
at new MyController (http://meuProjeto/my.controller.js:8:28)
at Object.invoke (http://meuProjeto/js/libs/angular-1.5.0/angular.js:4604:19)
at extend.instance (http://meuProjeto/js/libs/angular-1.5.0/angular.js:9855:34)
at nodeLinkFn (http://meuProjeto/js/libs/angular-1.5.0/angular.js:8927:34)
at compositeLinkFn (http://meuProjeto/js/libs/angular-1.5.0/angular.js:8226:13)
at publicLinkFn (http://meuProjeto/js/libs/angular-1.5.0/angular.js:8106:30)
at http://meuProjeto/js/libs/angular-1.5.0/angular.js:1696:27
at Scope.$eval (http://meuProjeto/js/libs/angular-1.5.0/angular.js:16820:28)
at Scope.$apply (http://meuProjeto/js/libs/angular-1.5.0/angular.js:16920:25)(anonymous function) @ angular.js:13236

no meu script utils.js na linha 14 tem o seguinte comando:
var param = JSON.parse(window.sessionStorage.param);

Sei que é bem genérico o que postei, porém sou novato em desenvolvimento WEB e estou postando isto torcendo para que alguém tenha passado pelo mesmo problema (ou por algo parecido) e possa me guiar! Não faço ideia de onde começar a procurar uma solução, postei por isso também.
Caso seja necessário posso adicionar alguma informação que seja relevante e eu não tenha colocado aqui.
Desde já, grato!
Answer: sessionStorage pertence exclusivamente à sessão do browser, que é vinculada à página.
Caso deseje compartilhar conteúdo entre duas ou mais tab/janelas, utilize localStorage.
Exemplo de implementação:
app.factory('userService', ['$rootScope', function ($rootScope) {
    var service = {
        model: {
            name: '',
            email: ''
        },

        SaveState: function () {
            sessionStorage.userService = angular.toJson(service.model);
        },

        RestoreState: function () {
            service.model = angular.fromJson(sessionStorage.userService);
        }
    }

    $rootScope.$on("savestate", service.SaveState);
    $rootScope.$on("restorestate", service.RestoreState);

    return service;
}]);

(fonte: https://stackoverflow.com/questions/18247130/how-to-store-the-data-to-local-storage)
Abaixo uma lista de serviços que podem lhe ajudar:

https://github.com/grevory/angular-local-storage
http://ngmodules.org/modules/ngStorage
Comment: Fico feliz que tenha funcionado. Sinta-se à vontade para perguntar se possuíres outras dúvidas!
Comment: Obrigado pela resposta e pela explicação OnoSendai . Irei testar a sua resposta e assim que terminar irei marcá-la como correta!
Comment: Nos links que você forneceu, encontrei um deles explicando o uso do sessionStorage e do localStorage. Troquei como você disse o sessionStorage pelo localStorage e aparentemente está tudo ok! Obrigado pela resposta!


why is there the error "No plugin found for prefix 'help' in the current project" when I write mvn help:effective-pom in the command prompt?

Question: I'm reading a tutorial about maven and it should be mentioned that I know nothing about it. I have done first and second chapter successfully which was about it's setup. but in chapter 3 which is "Maven Pom" I have a problem. In last paragraph it says to make a "pom.xml" file in any directory you wish and write this in it:
 <project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.companyname.project-group</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>

</project>

and then it says  that an easy way to look at the default configurations of the super POM is by running the following command: mvn help:effective-pom in the command prompt.
But when I try this, I get this error:
No plugin found for prefix 'help' in the current project

This is the tutorial I mentioned above: maven tutorial
Why there is no plugin help in my repository folder?! and how I can get it?
This is the output of mvn --version in the cmd:
Apache Maven 3.2.3
Maven home: C:\apache-maven-3.2.3
Java version: 1.6.0_13
Java home: C:\Program Files\Java\jdk1.6.0_13
Default locale: en_US, platform encoding: Cp1252
OS name: "windows Vista",version:"6.8"

This is the rezult of mvn -X help:effective-pom(first ten lines)
[INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5: Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.5
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-install-plugin:2.4: Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-install-plugin:jar:2.4
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-resources-plugin:2.6: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-surefire-plugin/2.12.4/maven-surefire-plugin-2.12.4.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-surefire-plugin:2.12.4: Plugin org.apache.maven.plugins:maven-surefire-plugin:2.12.4 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-surefire-plugin:jar:2.12.4
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.1/maven-compiler-plugin-3.1.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-compiler-plugin:3.1: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-compiler-plugin:jar:3.1
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-jar-plugin/2.4/maven-jar-plugin-2.4.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-jar-plugin:2.4: Plugin org.apache.maven.plugins:maven-jar-plugin:2.4 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-jar-plugin:jar:2.4
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-deploy-plugin/2.7/maven-deploy-plugin-2.7.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-deploy-plugin:2.7: Plugin org.apache.maven.plugins:maven-deploy-plugin:2.7 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-deploy-plugin:jar:2.7
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-site-plugin/3.3/maven-site-plugin-3.3.pom

Comment: Does your file actually say ``?  If so, change `>` to `>`.
Comment: I corrected that but the Error changed to "No plugin found for prefix 'help' in the current project" @ajb
Comment: Can you check what `mvn --version` says?
Comment: Maven home: C:\apache-maven-3.2.3
Java version:1.6.0_13
OS name: "windows vista"
Comment: seems like effective-pom has been deprecated as per "http://maven.apache.org/plugins/maven-help-plugin/examples/describe-configuration.html" ,,,, you must have been using on cutting edge maven build, it would be interesting to know the output of mvn --version
Comment: The output of mvn --version is: Apach-maven-3.2.3
java version:1.6.0_13
OS name:"window vista"
Comment: @shakiba Please make full screenshot of the output....@Xinus: effective-pom is **not** deprecated.
Comment: I can't add an image because of my reputation, but the exact out put is
Maven home: C:\apache-maven-3.2.3
Apache Maven 3.2.3(33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4;)
Java version: 1.6.0_13, vendor: sun Mirosystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_13\jre
Default locale: de_DE, platform encoding: Cp1252
OS name: "Windows Vista", version: "6.0", family: "window"
@khmarbaise
Comment: Try to delete your local repository ${HOME}/.m2/repository and retry `mvn help:effective-pom`...
Comment: I retried but there was no change in the rezult, It seems that it fails to download! @khmarbaise
Comment: Ok..than we are the root cause of the problem. Download problems..which you have to solve first..Are you beding a proxy ?
Comment: No I don't use a proxy, I didn't get what should I download?!@khmarbaise
Comment: @Shakiba: mvn needs you to be connected to the internet while you run any mvn command, Please check if any of earlier console traces suggest any connection failures while running this command. you can always do 'mvn help:effective-pom >> log.txt', After running this you can check log.txt to see any problems
Comment: Had the same issue. It seems my console did not have enough right to modify necessary directory. Re-ran from admin mode and working fine.
Answer: Background Information
When you execute mvn help:effecitve-pom, help is a prefix which represents a specific plugin while effective-pom is the goal from that plugin that should be executed.
The first thing Maven does is try to lookup the full plugin based on the given prefix (so in your case the prefix help). Normally it does this by checking the /org/apache/maven/plugins/maven-metadata.xml file in your local repository (by default your local repository is stored under %HOME%/.m2/repository).
If this file doesn't exist then it tries to check it remotely (again default configuration would use maven central), and will then cache this file in your local repository with the following name: maven-metadata-<remote-repo-id>.xml (so for maven central it would be: maven-metadata-central.xml). If at this point it is unable to access this file remotely then it wont be able to find the plugin and will fail the build. This is exactly what you are experiencing.
Your Issue
There are two main reasons why it wont be able to resolve the plugin:

the local files in your repository are corrupt in some way
maven is unable to access the remote repository (so basically a problem with your internet connection).

You can use the debug flag -X when executing your maven build to receive more information about what is going on. For you executing the following:
mvn -X help:effective-pom

should provide information about how it is trying to resolve the plugin prefix. For example if I execute this command I see the following:
[DEBUG] Resolving plugin prefix help from [org.apache.maven.plugins, org.codehaus.mojo]
[DEBUG] Could not find metadata org.apache.maven.plugins/maven-metadata.xml in local (/home/user/.m2/repository)
[DEBUG] Skipped remote update check for org.apache.maven.plugins/maven-metadata.xml, locally cached metadata up-to-date.

My locally cached file from the remote repository is up to date so it will just use that.
Do either /org/apache/maven/plugins/maven-metadata.xml or /org/apache/maven/plugins/maven-metadata-central.xml exist in your local repository? If they don't then most likely you have an internet connection problem. If they do exist then they might be corrupt. What is the result of running with the -X flag, what information does it provide?
After all this usually the simplest solution is to delete your local repository (by default %HOME%/.m2/repository, but running maven with the -X flag will tell you exactly where it is), and execute maven again. By deleting the local repository you force maven to download everything again.
Comment: @shakiba: You probably are behind a firewall or proxy of some kind. You need to configure Maven to talk to it. http://maven.apache.org/guides/mini/guide-proxies.html
Comment: it is the rezult of mvn -X help:effective-pom(first 5 lines) @DB5 [INFO] Scanning for projects...
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom

[WARNING] Failed to retrieve plugin descriptor for org.apache.maven.plugins:maven-clean-plugin:2.5: Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-clean-plugin:jar:2.5
Comment: what happens when you delete your local repository and run the command again? Actually where does maven think your local repository is located?
Comment: when i delete local repository and run the command again the same rezult happens. how can I understand "where does maven think local repository is located"?? I just know local repository is located in .m2
Comment: but I don't use any proxy and I tried it even when my firewall turned off but the same thing happened. @AaronDigulla
Answer: The command should work. If it doesn't, then that means you made a mistake in the setup of Maven or it printed more errors before that, namely that it could download plugins from Maven Central.
Delete the folder %HOME%/.m2/repository/ and try again. Now look closely for any problems in the output of Maven.
If it still doesn't work, show us the content of %HOME%/.m2/repository/org/apache/maven/plugins/maven-help-plugin/
Comment: there is no "maven-help-plugin" ! but the set up has no problem because when I try mvn --version, the output is correctly the version of my maven. I don't know why it hasn't installed the help plugin? @Aaron Digulla
Comment: In fact, I don't know what should I have do to have the help plugin in the repository? @Aaron Digulla
Comment: The error message means that Maven can't download the plugins. That means there is something wrong with your Internet connection (which doesn't seem to be the case since you could download Maven itself) or there is something wrong with `https`. Try to upgrade your version of Java. That might help.
Answer: Like in a similar topic in stackoverflow, just use a plugin directly in the pom, for example:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <testSource>1.8</testSource>
                <testTarget>1.8</testTarget>
                <compilerArgument>-Xlint:deprecation</compilerArgument>
            </configuration>
        </plugin>
    </plugins>
</build>

Answer: If you are using a mirror of central you might need to define the prefix in  the plugin metadata file for your mirror.
The file
.m2/repository/org/apache/maven/plugins/maven-metadata-<mirror id>.xml
must contain
<plugin>
   <name>Maven Help Plugin</name>
   <prefix>help</prefix>
   <artifactId>maven-help-plugin</artifactId>
</plugin>

Comment: While the above is true; if you change the file manually it will simply get overriden with the file coming from the mirror. You have to fix it at the mirror. In may case the mirror is an Artifactory virtual repo. That virtual repo included an actual repo where the metadata did not have the 'help' prefix. By deleting the metadata file from that repo it all worked as expected.


Finding the host node of a paragraph

Question: I'm trying to find all the absolute URLs in a site (in wysiwyg content, url fields, config) to convert them into relative URLs later via manual administration. All the URLs in question are within the same Drupal site. I've managed to use PHPMyAdmin to find and record readily-accessible entities that contain absolute urls (nodes - /node/NID/edit, menu items - /admin/structure/menu/item/MLID/edit, etc.). However, I'm having a difficult time with Paragraphs.

The id of a paragraph field row is for a specific instance of the paragraph.
Without knowledge of the field hosting the paragraph, the instance could be anywhere.
The paragraph could be nested in another paragraph.

How would one go about solving this issue? How do I find the host node of paragraphs containing an absolute link?
Comment: The host entity (node or another paragraph) is stored in the paragraph `$paragraph->getParentEntity()`
Comment: I think what are looking for is something like the [Reverse Entity Reference module](https://www.drupal.org/project/reverse_entity_reference)
Comment: Can you elaborate a bit why exactly you'd recommend this module, and how exactly it will help OP to solve his issue?
Comment: Paragraphs are attached to parent entities as entity references ... so to find out which parent entities reference a particular paragraph you will need to do a reverse reference lookup (e.g. SELECT all entites WHERE reference field = $paragraph_entity_id) ... this functionality is exactly what is provided by the Reverse Entity Reference module...
Comment: @bleen Paragraphs uses a different field type (entity_reference_revisions), does the reverse ER module work with that do you know?
Comment: https://drupalusbcandmore.blot.im/let-s-learn-recursive-cte-sql-via-paragraphs is helpful here.
Answer: It might be a bit easier to use the API rather than SQL for this. I'd be tempted to load all nodes, loop through all their fields and check values that way. If you hit a paragraph field, recurse and check its fields too.
This is a very rough example function, but it should get you going:
function entityHasFieldWithAbsUrl(\Drupal\Core\Entity\ContentEntityInterface $entity, array $types) {
  foreach ($entity->getFields() as $key => $field) {
    $field_type = $field->getFieldDefinition()->getType();
    $field_settings = $field->getSettings();
    if ($field_type == 'entity_reference_revisions' && $field_settings['target_type'] == 'paragraph') {
      foreach ($field as $item) {
        if ($item->entity instanceof \Drupal\paragraphs\ParagraphInterface) {
          if (entityHasFieldWithAbsUrl($item->entity, $types)) {
            return TRUE;
          }
        }
      }
    }
    elseif (in_array($field_type, $types)) {
      foreach ($field as $item) {
        // Change this to something better...
        if (strpos($item->value, 'https://www.example.com/') !== FALSE) {
          return TRUE;
        }
      }
    }
  }

  return FALSE;
}

And you'd use it something like this:
$nodes_with_abs_urls = [];
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple();
$types = ['text', 'text_long', 'text_with_summary', 'string'];
foreach ($nodes as $node) {
  if (entityHasFieldWithAbsUrl($node, $types)) {
    $nodes_with_abs_urls[] = $node->id();
  }
}

You probably need to tweak the types to suit your installation, but it should work at a basic level.
Answer: So what I ended up doing was searching for the URLs in PHPMyAdmin, picked out all the tables that belong to paragraph fields (they were prefixed paragraph__) and gathered all their entity_ids. Then using a temporary Drush command, I loaded all of these paragraphs and for each, recursively loaded $paragraph->getParentEntity() until it hit a desired entity type (in this case, nodes). Then I recorded the node ids.
function mymodule_drush_command() {
  $items = [];
  $items['mycommand'] = [
    'description' => 'Find the nids of the pids',
    'arguments' => [],
    'drupal dependencies' => ['mymodule'],
    'aliases' => [],
  ];
  return $items;
}

function drush_mymodule_mycommand() {
  // Could be improved by retrieving ids via query.
  $pids = [/* paragraph entity_ids */];

  // For each, recursively search ancestors.
  $nids = array_unique(array_filter(array_map(function($pid) {
    return _mymodule_find_node(\Drupal\paragraphs\Entity\Paragraph::load($pid));
  }, $pids)));

  // Print results
  drush_print_r(array_map(function($nid) {
    return '/node/' . $nid . '/edit';
  }, $nids));
}

function _mymodule_find_node($entity){
  // Could be improved by handling more than just nodes
  if ($entity instanceof \Drupal\node\NodeInterface) return $entity->id();
  // If still a paragraph, climb
  if ($entity instanceof \Drupal\paragraphs\ParagraphInterface) return _mymodule_find_node($entity->getParentEntity());
  // Otherwise, this is an entity we didn't want.
  return null;
}

Bulk of this operation could have been automated. It could have been improved to grab the entity ids via a query instead of PHPMyAdmin and handle different root types like blocks, terms, etc. It could also be more performant by not loading every single paragraph. But the above did what I needed at the moment, so I left it as is.
Comment: I really don't understand what you are trying to do here:
You could simply get all bundles that containt Paragraph reference fields, and then query all the bundles to retrieve the nids..
But in general: I don't really understand why this is handy to you.
Comment: @ssibal Then I'll refer you to the first part of my question: [_"I'm trying to find all the absolute URLs in a site (in wysiwyg content, url fields, config) to convert them into relative URLs later via manual administration."_](https://drupal.stackexchange.com/q/269353/57572). Absolute links aren't ideal, especially if you move sites across domains/environments. _"You could simply get all bundles that containt Paragraph reference fields, and then query all the bundles to retrieve the nids."_ - paragraphs can reference paragraphs. You cannot assume a node references a paragraph and ends there.

Finding the host node of a paragraph


How to refresh Angular application after build library?

Question: I have an Angular 8 application that includes a library and an application.

My-Workspace

projects

my-lib (library)
my-lib-test (application)

And I have run a command in terminal ng build --watch=true. This watches any changes in library and build it immediately.
I added library to my-lib-test project. And I run command ng-serve my-lib-test. But when I changed the library, my application does not refresh in browser.
Why?
Comment: See [this](https://medium.com/@ntkolev/angular-6-build-watch-multiple-dependent-libraries-in-one-shell-562a5131788c) article
Answer: Maybe you need to use:

npm install

For updating every libraries and packages.


How do I show that $P(B\cap C)\ge0.1$ Given the information $P(A)=0.25, P(B)=0.5, P(C)=0.6, P(A\cup B) =0.55, P(A\cap C)=0.1$

Question: How do I show that $P(B\cap C)\ge0.1$ Given the information $P(A)=0.25, P(B)=0.5, P(C)=0.6, P(A\cup B) =0.55, P(A\cap C)=0.1$
I've tried to use $P(A\cap C)= -P( B\cup C) +P(A) + P(C)$ but get that $P(B \cap C) \le 0.9$ I've also tried to approach it by saying that $P(A\cup B \cup C) \le 1)$ But have had no successes, can I have something to help me.
Comment: The condition $\mathbb{P}(A)$ is not useful if you just calculate $\mathbb{P}(B\cap C)$. Please see the solution by @trancelocation.
Answer: $$P(B \cup C) = P(B) + P(C) - P(B \cap C) \leq 1 \Rightarrow P(B \cap C) \geq P(B) + P(C) - 1$$


Build libgit2sharp on Ubuntu

Question: I'm trying to build v0.19 of libgit2sharp on my Ubuntu 14 machine using Mono 3.6.0. However when I call ./build.libgit2sharp.sh I get lots of messages like the one below.
/home/chris/projects/libgit2sharp/CI-build.msbuild: error:
LibGit2Sharp.Tests.CloneFixture.CanCloneBarely(url: "http://github.com/libgit2/TestGitRepository"): LibGit2Sharp.LibGit2SharpException : An error was raised by libgit2. Category = Net (Error).
Only relative redirects are supported

The error message is always the same "Only relative redirects are supported".
I have been reading on the issue tracker that the version of Mono that libgit2sharp is built with can be an issue, is 3.6.0 okay?

Turns out I was wrong, the above error is from when I was trying to build v0.14 (i forgot to switch branch). My error on v0.19 is:
/home/chris/projects/libgit2sharp/CI/build.msbuild: error : LibGit2Sharp.Tests.BlameFixture.CanBlameFromADifferentCommit: Assert.Throws() Failure Expected: LibGit2Sharp.LibGit2SharpException Actual:   System.EntryPointNotFoundException: git_blame_file

I get a lot more errors like that. Full log is here.
Comment: Are you sure that's 0.19?  Error messages haven't been formatted like that since 0.16.  It was changed at https://github.com/libgit2/libgit2sharp/pull/669.
Comment: Doh your right, i was fiddling around and switched to 0.14 branch. However the build still fails with this message:
`/home/chris/projects/libgit2sharp/CI/build.msbuild: error : LibGit2Sharp.Tests.BlameFixture.CanBlameFromADifferentCommit: Assert.Throws() Failure
Expected: LibGit2Sharp.LibGit2SharpException
Actual:   System.EntryPointNotFoundException: git_blame_file`

I get alot more errors like that. Full log is [here](http://pastebin.com/g7PY7Ci9).
Comment: EntryPointNotFoundException -> The error message hints at a wrong version of libgit2 being loaded by LibGit2Sharp. the .sh scripts takes care of building the correct one for you, but maybe have you still the version from v0.14 reachable from the PATH?
Comment: Doh, got it fixed. Git submodules are a pain. I had checked out v0.19 but forgot to update the submodules. Wonder if its worth adding a git submodule update to the build script?
Answer: The version of mono isn't an issue. The network code is not implemented in C# anyhow.
For various reasons, the libgit2 HTTP layer only supports relative redirects at the moment. As you're using http as the scheme, GitHub first needs to tell you to use HTTPS which has to be an absolute redirect.
If you use HTTPS to connect to GitHub, it will work correctly (which you should be using for all of it in any case).
Comment: But I'm not doing this. I'm just calling build.libgit2sharp.sh which runs some unit tests that come with the library that are failing with that error. I find it hard to believe that those unit tests have been added but don't actually work?

Build libgit2sharp on Ubuntu


What is this natural trumpet?

Question: In this picture of the London Gabrieli Brass Ensemble found here, one sees what I suppose are natural brass instruments.

Does the trumpet in the lower right corner have a specific name? It has a different form than the baroque trumpet that looks like this (from this site):
Comment: Blackadder?   He must have a cunning plan
Comment: If it's helpful, that trumpeter is David Blackadder, principal trumpet of the Orchestra of the Age of Enlightenment.
Comment: He played at the wedding of Prince Harry and Meghan Markle.
Answer: It's a demilune trumpet. There are lots of images on google.

Item 72 from the Natural Trumpets catalogue of the Bate Musical Instrument collection.

Demilune trumpet  c.1840 This trumpet is made of brass. It has a
  central tuning slide. It is also curved probably to make hand stopping
  easier. It was made by Dubois & Cie.

If you look carefully you'll see Prof. Blackadder's trumpet has an extra loop just after the mouthpiece, so this picture isn't the same instrument he's playing.
Comment: Demilune - half moon. Apposite naming!
Comment: The "extra loop" is a crook that the player will remove to change the key of the instrument. See https://collections.ed.ac.uk/stcecilias/record/96059

What is this natural trumpet?


Retrieving dropdown options out of a database

Question: I want to create a dropdown filled with numbers. These numbers need to be retrieved from a database. The connection to the database and the supposed display code are as follows:
The Function to retrieve the options for the dropdown
    <?php
    function dropdown_menu() {

    global $wpdb;
    /* Query the database */
    $query = $wpdb->prepare('SELECT * FROM LID ORDER BY LidID', 'LID', 'LidID');
    $results = $wpdb->get_results($query);

    /* Check for $results */
    if(!empty($results)) :

    /* Loop through the $results and add each as a dropdown option */
    global $options;
    $options = '';
    foreach($results as $result) :

    echo ("<option value=\"<?php echo $item->LidID ?>\"> <?php echo $item->LidID ?> </option>");

    endforeach;
    endif;
    }
    ?>

The statement which includes the aforementioned function into a form:

                Lid ID (verplicht)
                <select name="LidID" id="LidID" value="<?php echo $item->LidID ?>" aria-required="true"> <?php dropdown_menu(); ?> </select>

The problem I'm having, is that it creates a dropdown list with the corresponding amount of records of "LidID" in the database, but they all show up blank. I've already been toying around with it, and I think I know what the problem is, but I don't know how to solve it. I presume the problem lies within the echo-statement;
    echo ("<option value=\"<?php echo $item->LidID ?>\"> <?php echo $item->LidID ?> </option>");

when I remove the second LidID ?> and replace it with, for example, a 5, it shows a list of fives. For some reason it doesn't recognize the statement I wrote. I've tried to switch out both the $item->LidID in the echo statement into $result->LidID, but that doesn't seem to have any impact. Does anyone have the slightest idea of what is going wrong here?
Oh yeah, I don't know if it's relevent but it's a plug-in for wordpress.
Answer: You're right, that is the issue. You are already in PHP when echoing, so you don't need to open more PHP tags. The other issues is $item doesn't exist. Another issue is you're not using MySQLi correctly, you only use prepare if you have a variable, you don't so don't use it. You also don't use get_results in that context. Try this:
function dropdown_menu($default) {
    global $wpdb;
    /* Query the database */
    $query = 'SELECT * FROM LID ORDER BY LidID, LID, LidID';
    $results = $wpdb->query($query);
    while ($item = $results->fetch_assoc()):
        echo '<option value="'.$item->LidID.'"'.($item->LidID == $default ? ' selected="selected"' : '').'>'.$item->LidID.'</option>';
    endwhile;
}

Then when calling the function

    <select name="LidID" id="LidID" aria-required="true"> <?php dropdown_menu($item->LidID); ?> </select>

Comment: doesn't seem to do the trick either, they still show up empty
Comment: haha, I see you just edited your post as I wanted to reply again. I replaced the $item with $result and now it works. Thanks for the help!
Comment: You're whole function was screwed up. I've updated my answer to show what it should look like.
Comment: I noticed. However, it tells me there is an unexpected ',' on line 26, which is the line '$query = 'SELECT ... etc'. I tried to remove everything after 'ORDER BY lidID', but that messed up my complete form.
Comment: That's because you've got quotation marks around the columns. I've removed them from my answer.
Comment: got it. Another side-question, I tried to declare the 'default value' of the dropdown to be set to $item->LidID. This value doesn't have anything to do with the function you've rewritten for me, it's been transferred to this page via the URL. It doesn't seem to work however, as it sets the default value to 0 instead of the value I've given it. Is there a way to work around that?
Comment: You can't use `value` with a `select` input. You have to set the `option` as `selected`. I've updated my answer to include this.
Comment: and everything works perfectly now. A lot of thanks dude. Thanks for all the patience as well, I'm still all new to php so I've probably made some pretty basic mistakes as well. Have a nice day :)


prevent double tap on android WebView from reaching html but let tap reach html

Question:
 I'm trying to create a hybrid app using android webView.
Here is my problem: I have a webView and a touchListener on webView. I want tap events go down to html but prevent
doubleTap from reaching html which is inside webview.
and this is my code:
webView.setOnTouchListener { v, event ->
    //returning true here prevents all kind of touches from reaching html
    true
 }

above code prevents all touches from reaching html.
I want something like this:
webView.setOnTouchListener { v, event ->
        if (doubleTap) {
            //return true and prevent doubleTap from reaching html
            true
        } else (!doubleTap) {
            //return false and let html recieve tap or any other gesture!
            false
        }
    }

By the way, I don't want to handle this with JavaScript or anything inside my html page!
(tell my if the code is very short and you need more information)
Thank You All!
Answer: If you would like to identify double taps when they occur, I would suggest you use the GestureDetector.
It has a specific listener for double taps called GestureDetector.OnDoubleTapListener.
An example of how to use the GestureDetector is below (taken from the documentation):
class MainActivity :
    Activity(),
    GestureDetector.OnGestureListener,
    GestureDetector.OnDoubleTapListener {

private lateinit var mDetector: GestureDetectorCompat

// Called when the activity is first created.
public override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    // Instantiate the gesture detector with the
    // application context and an implementation of
    // GestureDetector.OnGestureListener
    mDetector = GestureDetectorCompat(this, this)
    // Set the gesture detector as the double tap
    // listener.
    mDetector.setOnDoubleTapListener(this)
}

override fun onTouchEvent(event: MotionEvent): Boolean {
    return if (mDetector.onTouchEvent(event)) {
        true
    } else {
        super.onTouchEvent(event)
    }
}

override fun onLongPress(event: MotionEvent) {
    Log.d(DEBUG_TAG, "onLongPress: $event")
}

override fun onDoubleTap(event: MotionEvent): Boolean {  // <----
    Log.d(DEBUG_TAG, "onDoubleTap: $event")
    return true
}

override fun onDoubleTapEvent(event: MotionEvent): Boolean {  // <-----
    Log.d(DEBUG_TAG, "onDoubleTapEvent: $event")
    return true
  }

}


Checking whether a particular group has an efficient, faithful representation as a matrix group

Question: There are cryptographic protocols being developed for non-abelian groups. For some protocols it is necessary to know whether the group has an efficient representation as a matrix group (say, a matrix group over a field $\mathbb{F}$).
What should I do to find out whether a semidirect product of finite groups can be represented as a matrix group efficiently?
Particularly, say semidirect products of the form $(\mathbb{Z}_p \times \mathbb{Z}_p) \rtimes_{\phi} \mathbb{Z}_q$, where $p,q$ are distinct primes, $(\mathbb{Z}_5 \times \mathbb{Z}_5) \rtimes_{\theta} \mathbb{Z}_3$. How can I check whether they have efficient, faithful representations as matrix groups (provided that I know $\phi,\theta$)?
Thanks a lot in advance.
Comment: How do you define efficient? All representations of the particular groups you are talking about have dimension 1 (not faithful) or q (faithful or not depending on the action phi).
Comment: For such groups, the minimum degree of a faithful matrix representation is likely to be 2 (when abelian) or q (when Zq acts faithfully) or q+1 (when Zq acts faithfully on only one Zp and centralizes the other). A matrix representation is built from irreducible matrix representations, and for the groups you've given, the dimensions are either 1 or q.
Comment: Well, they don't need to be computed at all. There is a generic presentation of them. The elements of the base group act as diagonal matrices with various pth roots of unity along the diagonal, and the elements of the acting group are permutation matrices, with $q$th roots of unity as the entries.

I guess what I mean is there is a constant time algorithm to determine the (i,j)th entry of the matrices of the generators.
Comment: Degree of matrix representation is the size. A 2x2 matrix has degree 2. Base group is Zp x Zp. Acting group is Zq. David Craven gave the pattern for the degree q representation. A degree 1, or 2, or q+1 representation is also computable in the same (very fast) way.
Comment: Thank you @DavidA.Craven I think in those cryptographic protocols they mean that representations that can be computed in polynomial time. So, efficiently computed.
Comment: Thank you very much @JackSchmidt can you also please tell me what is meant by degree of matrix representation?
Comment: Oh, is it? :) Can you please explain a bit more @DavidA.Craven base group and the acting group means, $\mathbb{Z}_p \times \mathbb{Z}_p$ and $\mathbb{Z}_q$?
Comment: Okay, thank you very very much @JackSchmidt
Comment: and @DavidA.Craven
Answer: The semidirect products under discussion all embed into ${\rm GL}(3,p)$, so they all have exceedingly efficient faithful matrix representations of degree $3$ over the field
${\mathbb F}_p$.
Comment: Using modular representations. Sneaky.
Comment: @DavidA.Craven Not really, my immediate reaction to the question was to look for representations over finite fields, given that the poster seemed to want this for computational work. And the field ${\mathbb F}$ was left unpsecified in the question.


Best way to make an image classification model with dynamic image sizes

Question: I'm working on a project where I need an image classification system, so I've decided to learn Tensorflow, and, after a week of study i've the following model:
model = Sequential([
  layers.Rescaling(1./255, input_shape=(250, 250, 3)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Dropout(0.2),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(8)
])

as you probably have noticed is the model shown in the official tutorial in the Tensorflow's website.
However i've a problem, my users, can actually upload photo of different ratio (a 1:1 for avatar and 16:9/10:9 for thumbnail) but i don't want to make 3 (or more) different network for each ratio.
So what's best and why?

Simply scaling and deforming the image to match the size (and ratio) in the input shape
Change the input shape in 720p and add black strip to the smaller image and resize (with eventually black strip) the larger image
Read the image as 1D array and append to the end a bunch of zero until the input shape is filled

p.s. (If you can, if the best is the 3rd option, can you show me how to modify the model?)
Thank you for you time!
Comment: Crop and downscale/upscale. Pytorch supports it out of the box
Comment: @ArayKarjauv Hi, sorry for being late with my response. First off, thank you for answering. Down/up scaling the image (and by logic changing it's ratio) doesn't impact negatively on the AI when does the recognition (I mean more false positive/negaive)? Thank you for your time.
Answer: The solutions you suggest are valid except for the last one. Since you are using 2d CNN you cannot use 1d input.
In fact, your model already has a Rescaling preprocessing layer that will normalize the input to the range [0,1] and resize it to 250x250.
Scaling can negatively affect accuracy (especially upscaling), but it can be mitigated by training the model with data augmentation - cropping, rescaling, padding (adding black pixels) - whatever you will use at the inference step for your users.
Cropping may also cut out important parts of the image, so it depends on your task. For example, if it's face recognition, faces are usually centered, so cropping will work. You can also detect an object and crop it.
If you cannot retrain you model, I would choose to crop and downscale for large images and upscale to the full width/height for small ones
Comment: Thank you very much for your answer!


minima function is positive defined

Question: Is a function: C(x, y) = min(x, y) a positive definite function?
I think it is not positive - a defined function. If values x and y are negative the f (x, y) <0
Comment: When asking a question you should properly define your terms. So why don't you start by spelling out in detail what "positive definite function" means. Maybe this already helps to answer the question yourself.
Answer: Well, if you mean $C(x,x)\geq 0$ with equality iff $x=0$, then certainly it isn't positive definite. Pick $x=-1$ then you habe $C(x,x)=-1<0$ a contradiction.
Comment: You recognised yourself that the OP did not properly define his terms. It is much better to ask him for clarification istead of providing a (trivial?) answer based on some (arbitrary?) assumption what he may have meant.

minima function is positive defined


Updating core to a specific version with Composer

Question: My site hasn't been updated for two months, a few new updates has been released.
My site's core version is 8.5.6.
Before running composer update -with-dependencies I read the release notes to the latest version of core (8.6.2), they say:

Sites on 8.5.x should update immediately to Drupal 8.5.8 instead, and
  plan to update to the latest 8.6.x release before May 2019.

How do I update the old core to Drupal 8.5.8 instead?
How do I specify that version in CLI, what command?
Eventually I want to update it to the latest version.
Comment: `composer require drupal/core:8.5.8 --update-with-dependencies`, or some slight variation. If you get complaints about conflicting symfony component, add `symfony/config` to your command. If you have `webflo/drupal-core-strict` installed, also add that
Answer: I was in the same position. I used :
composer require drupal/core:^8.5.10

And it worked. Cheers.
Answer: If your project uses drupal/core-recommended you can execute this command to update your site to a specific release :
composer require drupal/core-recommended:9.2.16 drupal/core-composer-scaffold:9.2.16 drupal/core-project-message:9.2.16 --update-with-all-dependencies


ADF test - interpreting the results

Question: I have a time series $x$ and I want to run the ADF test to check stationarity in the sense of unit root.
The series has lenght 60 so I apply lags from 1 to 11 as suggested by the literature (Schwert 1989).
In matlab I run:
[h,p]=adftest(x,'lags',2:11)
The output is:

h =     0     0     0     0     0     0     0     0     0     0
p =    0.7158    0.7889    0.6778    0.6989    0.6574    0.6658    0.6934  0.7161    0.6883    0.6855

How can I interpreter this result? The fact that h is zero shall be interpreted as It might not be stationary in the sense of the unit root. But the p-value is very high.
So can I conclude something or not?
Answer: Well, I am also looking for answers related to the Dickey Fuller test but I know this much that a high p-value does indeed support the fact that your series is non-stationary. h=0 means that the test failed to reject the null hypothesis and your p-values also say exactly that.
Answer: h=0 here (in MATLAB) means that the ADF test fails to reject the null hypothesis of having a unit root.
You can conclude that the high $p$-values indicate that you cannot reject the unit root null hypothesis.

ADF test - interpreting the results


Python asyncio e threading

Question: Estou estudando o módulo asyncio do Python e existe a função run_coroutine_threadsafe que deve ser executado numa thread diferente daquela que está o loop de eventos. Segue meu script:
#!usr/bin/python3
# -*- coding: utf-8 -*-

import asyncio
import threading

def target():
    print('Iniciou a thread')
    #asyncio.set_event_loop(None)
    #loop = asyncio.new_event_loop()
    asyncio.run_coroutine_threadsafe(blah(), loop)
    print('Finalizou a thread')

async def blah():
    print('Thread atual:', threading.current_thread().name)

async def main():
    thread = threading.Thread(target=target, args=(), kwargs={}, name='Minha Thread')
    thread.setDaemon(True)
    thread.start()
    thread.join()
    print('finalizou')

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    try:
        loop.run_until_complete(main())
    finally:
        loop.close()

# Output
Iniciou a thread
Finalizou a thread
finalizando

O problema é que a corotina blah nunca é invocada pelo função run_coroutine_threadsafe mas não consigo encontrar o motivo, tentei até mesmo instanciar um novo loop de eventos (como podem ver nas linhas comentadas) mas nem mesmo assim o script funciona. O que estou errando?
Comment: Não houve saída no terminou.
Comment: Troquei os `print` pelo `sys.stdout.write` mas nem mesmo assim funcionou.
Comment: Devo ter me enganado sobre isso. Desculpe-me.
Comment: Que isso eu agradeço por estar tentando me ajudar.
Comment: Pelo o que entendi, lendo por cima a documentação, você não precisa criar uma *thread* para isso, a própria função `run_coroutine_threadsafe` irá executar em outra *thread*.
Comment: Eu também achei que sim, mas quando executo fazendo a chamada de `run_coroutine_threadsafe` da `MainThread` o resultado impresso é `"MainThread"`, ele não cria outra `thread`, pesquisando encontrei na documentação uma função `run_in_executor` que faz a criação de outra `thread`.
Answer: O problema é que você está chamando loop.close() antes de ele ter a oportunidade de rodar a rotina blah.
A rotina blah está agendada para execução no loop, porém a thread principal (que tem o loop) está presa em main()... Ela nunca cedeu controle ao loop para que ele rode outras rotinas, e quando main() termina o loop é fechado.
Por exemplo, colocando 1 segundo de espera antes de terminar o loop:
...
thread.join()
await asyncio.sleep(1)
print('finalizou')
...

Coloque esse await sleep em main() entre o join() e o print como no exemplo e vai ter o resultado que você espera. Pois o await devolve o controle para o loop.
Um outro exemplo é usar um evento para sinalizar o fim do programa:
import asyncio
import threading

def target(e):
    print('Iniciou a thread')
    asyncio.run_coroutine_threadsafe(blah(e), loop)
    print('Finalizou a thread')

async def blah(e):
    print('Thread atual:', threading.current_thread().name)
    e.set() # sinaliza o final da rotina, autorizando o loop a terminar

async def main():
    e = asyncio.Event()
    thread = threading.Thread(target=target, args=(e,), kwargs={}, name='Minha Thread')
    thread.setDaemon(True)
    thread.start()
    await e.wait() # devolve o controle ao loop e espera que
                   # a rotina sinalize o fim do programa
    print('finalizou')

Dessa forma antes de terminar o loop, esperamos pela execução da rotina blah usando um evento asyncio.Event().
Comment: A espera da finalização da `thread` com o método `join()` ocorre somente se o controle for retornado ao `event loop`?
Comment: Quer dizer que mesmo que eu agende uma `corotina` numa `thread` diferente se o `event loop` for o mesmo preciso ceder o controle ao loop de eventos para que a mesma seja executada?
Comment: @ThiagoLuizS Primeira pergunta: não, você pode retirar o `join()` que vai funcionar do mesmo jeito. O que você não pode fazer é terminar o loop... O seu loop tem que ficar ativo, sempre esperando alguma coisa - no exemplo foi o `sleep` mas num programa real o loop provavelmente fica a maior parte do tempo esperando por io.
Comment: @ThiagoLuizS segunda pergunta: Você usa `run_coroutine_threadsafe`  para agendar uma corotina que será executada na thread principal, quando o loop tiver tempo pra isso. O erro do seu código foi somente ter finalizado o loop - geralmente o loop fica esperando algo acontecer, até terminar tudo que o programa tem que fazer.  Quando não tiver mais o que esperar, aí sim você chama `loop.close()`.
Comment: Se `run_coroutine_threadsafe` é executada na `MainThread` qual o motivo do mesmo ser `threadsafe`?
Comment: @ThiagoLuizS rotinas assíncronas só rodam na thread principal, onde o loop está - se você tem outras threads que precisam iniciar rotinas assíncronas, precisa usar isso para agendar a execução de algo na thread principal.
Comment: Tenho que chamar esse método da `MainThread` e como será executado em outra `thread` ?
Comment: @ThiagoLuizS `run_coroutine_threadsafe` pode ser executado de qualquer thread. A corotina que você passar, vai rodar no loop, ou seja, vai rodar na main thread.
Comment: Então `run_coroutine_threadsafe` é invocado em outra `thread` fazendo com que uma `corotina` rode no `event loop` da `MainThread` ?
Comment: @ThiagoLuizS sim, como objetos assíncronos e objetos de corotina não são *threadsafe*, você precisa usar `run_coroutine_threadsafe` toda vez que precisar rodar algo no loop, porque você está em outra thread e as corotinas precisam ser executados na thread principal onde o loop está. Para isso que existe essa função `run_coroutine_threadsafe` - para permitir rodar tarefas assíncronas a partir de uma thread que não é a mesma onde o loop está.
Comment: Entendi. Muitíssimo obrigado por tudo.

Python asyncio e threading


Invalid Regex is accepted by Java. Is this a Java bug or a missed interpretation of expectations

Question: This pattern is not a valid regex according to several websites
groovy:000> java.util.regex.Pattern.compile("^*");
===> ^*

But the same expression in node correctly understands this:
$ node
> new RegExp('^*')
SyntaxError: Invalid regular expression: /^*/: Nothing to repeat

Who's right here? Java, node/internet? Or, am I just expecting something from the Java libs that I shouldn't
Comment: In `^*` you are using a quantifier `*` for the anchor `^` that asserts the start of the string. I don't think that is valid syntax because the anchor itself does not capture a character but asserts a position..
Comment: `(^)*` is accepted, btw. And Perl also accepts it: `echo "oo" | perl -ne "s/^*/F/g; print;"`
Answer: I'd say the links to the regex test tools are wrong (in the PCRE sense of it). I think this is so because of JS implementations handle these matches differently (see: https://github.com/gskinner/regexr/issues/28)
Note that both regexr and regex101 accept ^()* and (^)*. Also, Perl v5.18.2 has no issue with it: running echo "ubar" | perl -ne "s/^*/F/; print;" from my terminal results in no warnings or errors and will print Fubar.
This is what the PCRE specification say:

It is possible to construct infinite loops by following a subpattern that can match no characters with a quantifier that has no upper limit, for example:
(a?)*
Earlier versions of Perl and PCRE used to give an error at compile time for such patterns. However, because there are cases where this can be useful, such patterns are now accepted, but if any repetition of the subpattern does in fact match no characters, the loop is forcibly broken.
-- https://www.pcre.org/original/doc/html/pcrepattern.html

So, matching infinite amounts of zero-width matches, like ^* does, is accepted by the specs.


Angular-gettext translation service doesn't appear to be working.

Question: I'm having a really hard time understanding what I'm doing wrong and I'm hoping someone can help. I need to build a small app just to practice with angular-gettext. I followed the tutorial here: http://lostechies.com/gabrielschenker/2014/02/11/angularjspart-12-multi-language-support/
My project, however, will not translate. I don't get any errors in the console. I have all of the necessary dependencies and everything appears to be working correctly (grunt, bower, etc.), so it must be in the actual code. Probably a curly brace or something.
Here's my app.js file:
var app = angular.module('app', ['gettext']);
app.controller("TestCtrl", function($scope){
    app.run(function(gettextCatalog){
        gettextCatalog.currentLanguage = 'de';
        gettextCatalog.debug = true;
    });

});

Index.html:
<!DOCTYPE html>

    <title>Translation Sample</title>
    <link rel="stylesheet" type="text/css"
            href="bower_components/bootstrap/dist/css/bootstrap.css"/>

Welcome to my app

We want to test globalization and localization

Gruntfile.js:
module.exports = function(grunt){
    grunt.loadNpmTasks('grunt-angular-gettext');

    grunt.initConfig({
        nggettext_extract:{
            pot: {
                files: {
                    'po/template.pot': ['**/*.html']
                }
            }
        },
        nggettext_compile:{
            all: {
                files: {
                    'translations.js': ['po/*.po']
                }
            }
        }
    })
};

Translations.js:
angular.module('gettext').run(['gettextCatalog', function (gettextCatalog) {
    gettextCatalog.setStrings('de', {
        "We want to test globalization and localization":"Wir wollen testen, Globalisierung und Lokalisierung",
        "Welcome to my app":"Willkommen auf meiner App"
    });
}]);

I know this is long. Thanks for reading and thanks in advance for any suggestions.
Answer: This might be an angular-gettext bug: your strings are loaded after you set the language. With recent changes in angular.js, this might break.
I'll push a fix for that.
Update: angular-gettext 1.1.2 was released with a fix for this (try it out). Note that in recent versions the use of currentLanguage has been deprecated. You should use gettextCatalog.setCurrentLanguage('de') instead.
https://angular-gettext.rocketeer.be/dev-guide/api/angular-gettext/
Comment: Thank you so much for responding (and for the fix)! I thought my post got lost in the 'nobody cares' dump. I haven't tried this out yet, but thanks again!
Comment: Never give up hope :-)


Change webserver's IP address depending on the hosted domain TLD

Question: I'm looking to build a CMS to manage several websites. Some will have a Spanish content and will be accessible via the domain domain.es, others will be in french, via the domain domain.fr, etc.
Since they will all sit on the same machine, this means they may be penalized by Google since it will geolocalize the hosting server's IP address to find out its location.
Here is my question: is it possible to have a different IP address for each domain, so that search engines geolocalizing the IP address will place the server in Spain when parsing domain.esand that the server is in France when parsing domain.fr? If it is possible, what is this technique's name ?
Answer: Your problem is not associating each domain with a unique IP. That is very easy, and is precisely what DNS is designed to do.
Your actual problem is finding (or building, more likely) the service that allows IP addresses from around the globe to be routed to a single server. That is something that is not only technically complex and un-orthodox, but will also cause horrible performance.
You'll either need to take the SEO hit (which I believe you may be over-emphasizing) or build a distributed system such that you can have multiple servers, one in each region you're servicing.
Another possibility for you to research is using a CDN like Akamai or Amazon Cloudfront. With these, user requests will automatically be directed to the closest CDN node to them.
Answer: Yes, it's possible. You could set up web servers in each country. You could have multiple IPs on the same server, with the same back end.
However I think you've got this wrong. I don't think the country where a server is hosted is that relevant any more, it's more about the domain name and acceptable speed. If you put all the content on one domain you'd probably get benefits from having a lot of useful, relevant content on one domain, if you spread it out you're diluting your SEO efforts.
In general, for SEO, aim to provide useful, unique content that is of value for users, is easily accessible, and is fast. Google will likely see it as good content.


How fast of an internet connection do I need for a smooth, seamless remote desktop experience?

Question: I'm considering moving a lot of my development software onto a hosted server and getting a really lightweight laptop to do work on from anywhere by connecting to the server remotely through RDP.
What kind of internet connection speeds do I need in order to make this painless/seemless?
Anything else I should take into consideration when setting this up?
Comment: As others have said, its mostly about latency, not so much the bandwidth. Read "It's the Latency, Stupid", it'll help understand why: http://rescomp.stanford.edu/~cheshire/rants/Latency.html
Answer: You should be concerned with two factors:

Latency: How long does it take for data to get from point A to point B? Latency of 50ms to 100ms or less is what you should look for. Note that you won't be able to control latency but anything over 100ms is likely to exhibit lag in the RDP session.
Capacity: How much bandwidth do you need? If you run multiple applications in the remote session, run the session in full screen and/or spanned monitors, if you configure the RDP client to use 24 bit or 32 bit color, if you enable resource redirection (local printers, drives, etc) then you should plan on having bandwidth of 100Kbps for the RDP session itself. This doesn't account for any other bandwidth needs that you have that are unrelated to the RDP session.

100Kbps is the number I've come up with in my RDP performance tests. A lot of people are going to disagree with me, but a heavily used RDP session (resource redirection, full screen or spanned, many applications running concurrently requiring lots of screen redraws) averaged about 100Kbps over an 8 hour period in my tests. That being said, I'd rather have more capacity than I need then less.
Comment: Glad to help...
Answer: It's less about the speed of the connection and more about the latency of the connection. I use a similar arrangement for consulting and monitoring from a central data center-based environment. Anything under 50ms is more than acceptable. Ideally, you'll have a connection speed greater than 1.5-megabits/second. If using raw RDP, setting a lower color depth and resolution will help. On your hosted system, disable themes and use a basic desktop background.
One thing that's helped me when I'm farther away or have higher-latency links is using an RDP accelerator. I use a single instance of Ericom Blaze, which employs compression and other techniques, for this.
Comment: Do you find this an effective way to do your consulting? I currently have an external hard drive with a VM on it for each client. It's been great because I can customize the dev environment, join their domain, etc. I would like to offload this to a server if I could though . . .
Comment: It depends. I have site-to-site VPNs established from my data center to client sites. I still work off of a full laptop, though. The data center presence is really just a nice way to have a "home base".
Comment: Do you host the data center?
Comment: My servers in a co-location facility.
Answer: Sorry if it's an obvious answer but you should also consider the security of your new hosted server versus one which you have previously been running locally and have more control over.
You would want to be able to connect yourself but you obviously don't want anyone else to be able to connect!
Multiple redundant backups of your hosted server would also be necessary.
Answer: 100ms and 512Kbits works fine for me.
Also rdp-clients have some settings to be faster on bad connections.
Comment: any recommendations of a client that could offer some settings for slow connections? especially Linux and android based?


Getting ID from array (PHP)

Question: I have an array like this:
Array
(
    [0] => Array
        (
            [0] => Sales
            [1] => Offices
            [2] => Products
        )
    [1] => Array
        (
            [0] => Cars
            [1] => Trucks
            [2] => Management
        )
)

All those 'Cars' 'Trucks' etc are links Cars etc. Now I would need to get that ID number of from those links? But Im pretty new to PHP and I have no idea how to get it. Foreach loop and reset function or something similar?
This is on drupal and ubercart if those info is needed.
Thanks.
Answer: Like this:
foreach($yourarry as $arr2){
    foreach($arr2 as $id=>$text){
        echo $id;
        echo $text;
    }
}

Comment: at this point $text = Cars. You'll need to explode() or regex to get the ID.
Comment: Thanks, got so far but having some trouble with xplode(). I noticed that it actually print some html too. Basically the $text is something like this: Cars (and the html for it: Cars/. How can I get that ID #32 from that?
Answer: I think you're looking for a foreach loop with a preg_match and explode() to extract the ID:
$regex = '/href="([^"]+)/i';
foreach ($arr as $item) {
  foreach ($item as $html) {
    if (preg_match($regex, $html, $matches)) {
      $id = end(explode('/', $matches[1]));
      // For the string 'Cars/' $id is equal to 32
    }
  }
}

Comment: Thank you Clive. This is excactly what I was looking. Got the ID now :)
Answer:
a href="/mysite/catalog/63"

...implies that 63 is the id for Cars - but there is neither a key called id in your sample array nor a value of 63 - you can't write code to find a value which is not there.
If you want to extract the number from the link text, then have a look at the various string parsing functions, the easiest functions to use would be strtok, explode or preg_match.

Getting ID from array (PHP)


Yarn Run Build Fails , TypeError: _module.default.createRequireFromPath is not a function

Question: When I run yarn run build it fails with below error, I'm new to this env how should I debug this error.
<code>yarn run build
yarn run v1.22.10
warning ../../../package.json: No license field
$ yarn clean && yarn prep && yarn bundle && yarn combine-ivm-loc-files
warning ../../../package.json: No license field
$ rimraf Resources && rimraf ItemsViewMobileStrings
warning ../../../package.json: No license field
$ node mkdirp.js Resources
warning ../../../package.json: No license field
$ haul bundle --entry-file index.mobile.js --platform android --minify true --dev false --bundle-output Resources/main.android.bundle --assets-dest Resources
error ▶ TypeError: _module.default.createRequireFromPath is not a function
</code>
Comment: Looks like some export problem. Could you please share how `createRequireFromPath` is exported from the module?
Answer: You should have something like this
<code>"license": "ISC",
</code>
in your package.json file.


Find the curvature of a curve at the given point

Question: Edit: I've revised this to clear up any confusion. The $T(0), N(0)$, etc were just to help me.
Suppose we have a space curve $r(t)=<3t,\cos t,\sin t>$. I'm asked to find the curvature at $P(0,1,0)$.
So, I do the following:
$r'(t)=<3,-sin(t),cos(t)>$
$||r'(t)||=\sqrt{(3^2)+(-sin(t))^2+(cos(t))^2}=\sqrt{10}$
$T(t)=\frac{r'(t)}{||r'(t)||} = \frac{<3,-sin(t),cos(t)}{\sqrt{10}}= \frac{1}{\sqrt10}(<3,-sin(t),cos(t)>)$
$T'(t)=\frac{1}{\sqrt10}(<0,-cos(t),-sin(t)>)$
$||T'(t)||=\sqrt{(\frac{1}{\sqrt10})^2+(0)^2+(-sin(t))^2+(cos(t))^2}=\sqrt{\frac{11}{10}}$
$k(curvature)=\frac{||T'(t)||}{||r'(t)||}=\frac{<0,\frac{-cos(t)}{\sqrt{10}},\frac{-sin(t)}{\sqrt{10}}>}{\sqrt{10}} = <0,\frac{-cos(t)}{10},\frac{-sin(t)}{10}>$
My problem is that I'm not really sure how to do it at $P(0,1,0)$. Usually it's just at $t=2$ or something similar. Sorry, it may seem like a silly question, but I really can't see where to plug in all 3 points. Do you just multiply $\frac{1}{10}*P(0,1,0)?$
Comment: When $t=0$ then $r(r)=P$.
Comment: Where do you get stuck when finding $T'(0)$ and $r'(0)$?
Comment: You first find the value of $t$ for which $r(t) = P$, then you calculate the curvature for that value of $t$. Or is that not what you're asking?
Comment: Sorry, but I'm not really sure what you mean =[ If I follow the formula I get 1/sqrt(10) = k, but how does that tie into the point P(0,1,0)?
Comment: I had a typo.  $r(0) = (0,1,0) = P$.
Comment: Sorry, I may be a little unclear here. What I mean is, given that curve above...I solved for T,N, and B. Now I need to find the curvature, but at P(0,1,0). If my curvature, k, is 1/sqrt(10) once I solved it...how do you plug in the rest of the points to find the curvature at that point in particular?
Answer: Your curve is $r(t)=(3t,\cos(t),\sin(t))$. It takes a number $\mathbb{R}$ (like time) and "maps" it to $\mathbb{R}^3$ (i.e. 3D space). Think of it as the curve of an object traveling in space, say a missile or something. At time $t$, it is at point in space $r(t)$. So, if you ask for the curvature of a point $P\in\mathbb{R}^3$, it may not even make sense if the curve does not pass through that point. You first need to find when the missile passes through $P$, if it does at all.
In this case, where $P=(0,1,0)$, notice something special: when $t=0$, we have $r(0)=(0,1,0)$. So, the time when $r$ passes through $P$ is right at the start, at time $t=0$. So all the question is asking is what the curvature is at $t=0$. (I.e. what is $\kappa(0)$.)