Skip to content

Commit

Permalink
- added channel subscription expressions
Browse files Browse the repository at this point in the history
 - added filters and transforms for expressions
 - removed old client lib (once for testing)
 - removed old gson lib
  • Loading branch information
matsfunk committed Apr 5, 2019
1 parent 788919c commit f7b58b8
Show file tree
Hide file tree
Showing 12 changed files with 898 additions and 18 deletions.
530 changes: 530 additions & 0 deletions client/test/ClientSubscriptionTest.java

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions server/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<classpathentry kind="lib" path="test/lib/oocsi.jar"/>
<classpathentry kind="lib" path="libs/javaosc-core-0.3.jar"/>
<classpathentry kind="lib" path="libs/gson-2.8.2.jar"/>
<classpathentry kind="lib" path="libs/EvalEx/EvalEx-2.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion server/build/makejar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
<attribute name="Rsrc-Main-Class" value="nl.tue.id.oocsi.server.OOCSIServer"/>
<attribute name="Class-Path" value="."/>
<attribute name="Rsrc-Class-Path" value="./ gson-2.2.4.jar javaosc-core-0.3.jar"/>
<attribute name="Rsrc-Class-Path" value="./ gson-2.8.2.jar javaosc-core-0.3.jar EvalEx/EvalEx-2.1.jar"/>
</manifest>
<zipfileset src="jar-in-jar-loader.zip"/>
<fileset dir="../bin">
<exclude name="**/*ServerCommandLineParsing*"/>
<exclude name="**/*ClientSignoffTest*"/>
<exclude name="**/*MessageParsingTest*"/>
</fileset>
<fileset dir="../libs"/>
</jar>
Expand Down
Binary file modified server/dist/OOCSI_server.jar
Binary file not shown.
Binary file added server/libs/EvalEx/EvalEx-2.1.jar
Binary file not shown.
23 changes: 23 additions & 0 deletions server/libs/EvalEx/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright 2012-2017 Udo Klimaschewski

http://about.me/udo.klimaschewski
http://UdoJava.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Binary file removed server/libs/gson-2.2.4.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion server/src/nl/tue/id/oocsi/server/OOCSIServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class OOCSIServer extends Server {

// constants
public static final String VERSION = "1.12";
public static final String VERSION = "1.13";

// defaults for different services
private static int maxClients = 100;
Expand Down
292 changes: 292 additions & 0 deletions server/src/nl/tue/id/oocsi/server/model/FunctionClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
package nl.tue.id.oocsi.server.model;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.udojava.evalex.AbstractFunction;
import com.udojava.evalex.AbstractLazyFunction;
import com.udojava.evalex.Expression;
import com.udojava.evalex.Expression.LazyNumber;

import nl.tue.id.oocsi.server.protocol.Message;

public class FunctionClient extends Client {

private String functionString;
private Client delegate;

// reference: https://github.com/uklimaschewski/EvalEx
private List<String> filterExpression = new LinkedList<String>();
private List<String> transformExpression = new LinkedList<String>();

private WindowFunction sumFct = new SumOverWindowFunction();
private WindowFunction meanFct = new MeanOverWindowFunction();
private WindowFunction stdevFct = new StandardDeviationOverWindowFunction();

public FunctionClient(Client delegateClient, String token, String functionString, ChangeListener presence) {
super(token, presence);
this.functionString = functionString;
this.delegate = delegateClient;

initFunctions(functionString);
}

private void initFunctions(String functionString) {
final Pattern filterPattern = Pattern.compile("filter\\((.*)\\)");
final Pattern transformPattern = Pattern.compile("transform\\((.*)\\)");

// functions are separated by ';'
String[] functions = functionString.split(";");
for (String fct : functions) {
Matcher filterMatcher = filterPattern.matcher(fct);
if (filterMatcher.find()) {
// init filter expression
filterExpression.add(filterMatcher.group(1));
continue;
}

Matcher transformMatcher = transformPattern.matcher(fct);
if (transformMatcher.find()) {
// init transform expression
transformExpression.add(transformMatcher.group());
continue;
}
}
}

@Override
public void send(Message message) {

// filtering checks
for (String expression : filterExpression) {
// apply expression
try {
final Expression e = loadExpression(expression, message, true);
if (expression.contains("sum("))
e.addFunction(sumFct);
if (expression.contains("mean("))
e.addFunction(meanFct);
if (expression.contains("stdev("))
e.addFunction(stdevFct);
BigDecimal bd = e.eval();
if (bd.intValue() == 0) {
return;
}
} catch (Exception ex) {
// default behavior: filter out on error
return;
}
}

// transformation
for (String expression : transformExpression) {
try {
Expression e = loadExpression(expression, message, false);
if (expression.contains("sum("))
e.addFunction(sumFct);
if (expression.contains("mean("))
e.addFunction(meanFct);
if (expression.contains("stdev("))
e.addFunction(stdevFct);
e.addLazyFunction(new AbstractTransform(message));
e.eval();
} catch (Exception ex) {
// System.out.println("problem");
}
}

message.recipient += "[" + functionString + "]";
delegate.send(message);
}

/**
* create an expression based on <code>message</code> parameters and a String <code>expression</code>
*
* @param expression
* @param message
* @param abortOnMissing
* @return
*/
private static Expression loadExpression(final String expression, Message message, boolean abortOnMissing) {
final Expression e = new Expression(expression);
List<String> vars = e.getUsedVariables();
for (String key : vars) {
Object value = message.data.get(key);
if (value != null) {
e.and(key, value.toString());
} else if (!abortOnMissing) {
e.and(key, new BigDecimal(0));
}
}
return e;
}

@Override
public String getName() {
return delegate.getName();
}

@Override
public void disconnect() {
delegate.disconnect();
}

@Override
public boolean isConnected() {
return delegate.isConnected();
}

@Override
public void ping() {
delegate.ping();
}

abstract class WindowFunction extends AbstractFunction {

private Queue<BigDecimal> queue = new ConcurrentLinkedQueue<BigDecimal>();
protected int queueLength = -1;

protected WindowFunction(String name, int numParams) {
super(name, numParams);
}

@Override
public BigDecimal eval(List<BigDecimal> parameters) {

// not initialized?
if (queueLength == -1) {
// initialize!
queueLength = Math.min(parameters.get(1).intValue(), 50);
}

// make space
while (queue.size() >= queueLength) {
queue.poll();
}

// insert element
BigDecimal value = parameters.get(0);
queue.offer(value);

// return the added element
return evalQueue(queue);
}

abstract public BigDecimal evalQueue(Queue<BigDecimal> queue);

}

// summary statistics over windows

class SumOverWindowFunction extends WindowFunction {

protected SumOverWindowFunction() {
super("sum", 2);
}

@Override
public BigDecimal evalQueue(Queue<BigDecimal> queue) {
BigDecimal result = new BigDecimal(0);
for (BigDecimal number : queue) {
result = result.add(number);
}
return result;
}
}

class MeanOverWindowFunction extends WindowFunction {

protected MeanOverWindowFunction() {
super("mean", 2);
}

@Override
public BigDecimal evalQueue(Queue<BigDecimal> queue) {
return new BigDecimal(mean(queue));
}

/**
* calculate the mean of all values in queue
*
* @param queue
* @return
*/
private double mean(Queue<BigDecimal> queue) {
double result = 0;
for (BigDecimal number : queue) {
result += number.doubleValue() / queueLength;
}
return result;
}
}

class StandardDeviationOverWindowFunction extends WindowFunction {

protected StandardDeviationOverWindowFunction() {
super("stdev", 2);
}

@Override
public BigDecimal evalQueue(Queue<BigDecimal> queue) {
double result = 0;
double mean = mean(queue);
for (BigDecimal number : queue) {
result += Math.pow(number.doubleValue() - mean, 2);
}
return new BigDecimal(Math.sqrt(result / queueLength));
}

/**
* calculate the mean of all values in queue
*
* @param queue
* @return
*/
private double mean(Queue<BigDecimal> queue) {
double result = 0;
for (BigDecimal number : queue) {
result += number.doubleValue() / queueLength;
}
return result;
}
}

class AbstractTransform extends AbstractLazyFunction {

private Message message;

protected AbstractTransform(Message message) {
super("transform", 2);
this.message = message;
}

@Override
public LazyNumber lazyEval(List<LazyNumber> parameters) {

String key = parameters.get(0).getString();
String expression = parameters.get(1).getString();

if (key == null || key.length() == 0 || expression == null || expression.length() == 0) {
return parameters.get(0);
}

// compute transformation
try {
final Expression e = loadExpression(expression, message, true);
BigDecimal bd = e.eval();

// store in message
message.data.put(key, bd.floatValue());
} catch (Exception ex) {
// do nothing
}

return parameters.get(0);
}
}
}
Loading

0 comments on commit f7b58b8

Please sign in to comment.