org.hwo.pulscounter/src/org/hwo/pulscounter/simplescript/SimpleScript.java

350 lines
7.4 KiB
Java

package org.hwo.pulscounter.simplescript;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;
import org.hwo.i18n.Messages;
import org.hwo.models.TableMapper.TableColumn;
public class SimpleScript {
private UUID id;
private String name;
private String description;
private ArrayList<SimpleScriptElement> simpleScriptElements;
public SimpleScript(){
id = UUID.randomUUID();
name = "SimpleScript";
description = "-";
simpleScriptElements = new ArrayList<>();
simpleScriptElements.add(new SimpleScriptElement(0));
}
public SimpleScript(UUID uuid,String name,String description,SimpleScriptElement elements[]){
this.id = uuid;
this.name = name;
this.description = description;
this.simpleScriptElements = new ArrayList<>();
this.simpleScriptElements.addAll(Arrays.asList(elements));
}
public SimpleScript(UUID uuid,String name,String description,Integer[] elementCodes){
this.id = uuid;
this.name = name;
this.description = description;
this.simpleScriptElements = new ArrayList<>();
for (Integer code: elementCodes){
this.simpleScriptElements.add(new SimpleScriptElement(code));
}
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
@TableColumn(label="name",order=10,width=120)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@TableColumn(label="Bezeichnung",order=20)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public ArrayList<SimpleScriptElement> getSimpleScriptElements() {
return simpleScriptElements;
}
public int[] getSimpleScriptElementsAsInt(){
int[] sse = new int[simpleScriptElements.size()];
for (int i=0;i<sse.length;i++){
sse[i] = simpleScriptElements.get(i).getCode();
}
return sse;
}
public Integer[] getSimpleScriptElementsAsInteger(){
Integer[] sse = new Integer[simpleScriptElements.size()];
for (int i=0;i<sse.length;i++){
sse[i] = simpleScriptElements.get(i).getCode();
}
return sse;
}
public void setSimpleScriptElements(SimpleScriptElement[] elements){
this.simpleScriptElements.clear();
this.simpleScriptElements.addAll(Arrays.asList(elements));
}
public void setSimpleScriptElements(int[] elements){
this.simpleScriptElements.clear();
for (int e: elements){
simpleScriptElements.add(new SimpleScriptElement(e));
}
}
public void setSimpleScriptElements(Integer[] elements){
this.simpleScriptElements.clear();
for (int e: elements){
simpleScriptElements.add(new SimpleScriptElement(e));
}
}
public enum ScriptOperation {
SET_ZERO (0x00,"ARITHEMTIK = 0"),
SET_ONE (0x01,"ARITHEMTIK = 1"),
SET_A (0x02,"ARITHEMTIK = A"),
SET_B (0x03,"ARITHEMTIK = B"),
A_PLUS_B (0x04,"ARITHEMTIK = A + B"),
A_MINUS_B (0x05,"ARITHEMTIK = A - B"),
A_MUL_B (0x06,"ARITHEMTIK = A * B"),
A_DIV_B (0x07,"ARITHEMTIK = A / B"),
A_AND_B (0x08,"LOGIK = A and B"),
A_OR_B (0x09,"LOGIK = A or B"),
A_XOR_B (0x0A,"LOGIK = A xor B"),
/* 0x0b..0x0f: frei */
SET_NOT_A (0x10,"LOGIK = not A"),
SET_NOT_B (0x11,"LOGIK = not B"),
A_PLUS_ONE (0x12,"ARITHEMTIK = A + 1"),
A_MINUS_ONE (0x13,"ARITHEMTIK = A - 1"),
B_PLUS_ONE (0x14,"ARITHEMTIK = B + 1"),
B_MINUS_ONE (0x15,"ARITHEMTIK = B - 1")
/* 0x16..0x1f: frei */
;
ScriptOperation(int n,String desc){
this.n = n;
this.desc = desc;
}
static ScriptOperation operations[] = {
SET_ZERO, // 0x00
SET_ONE,
SET_A,
SET_B,
A_PLUS_B, // 0x04
A_MINUS_B,
A_MUL_B,
A_DIV_B,
A_AND_B, // 0x08
A_OR_B,
A_XOR_B,
null,
null, // 0x0c
null,
null,
null,
SET_NOT_A, // 0x10
SET_NOT_B,
A_PLUS_ONE,
A_MINUS_ONE,
B_PLUS_ONE, // 0x14
B_MINUS_ONE,
null,
null,
null, // 0x18
null,
null,
null,
null, // 0x1C
null,
null,
null
};
public static ScriptOperation get(int code){
return operations[code];
}
private int n;
private String desc;
public int getCode(){
return this.n;
}
public String getDesc() {
return Messages.getString(desc);
}
@Override
public String toString() {
return Messages.getString(desc);
}
}
public enum ScriptCondition {
NEVER (0x00,"Niemals"),
ALWAYS (0x01,"Immer"),
A_EQ_ZERO (0x02,"A == 0"),
A_NEQ_ZERO (0x03,"A != 0"),
A_EQ_B (0x04,"A == B"),
A_NEQ_B (0x05,"A != B"),
A_LT_B (0x06,"A < B"),
A_GT_B (0x07,"A > B")
;
static ScriptCondition scriptConditions[] = {
NEVER,
ALWAYS,
A_EQ_ZERO,
A_NEQ_ZERO,
A_EQ_B,
A_NEQ_B,
A_LT_B,
A_GT_B
};
static ScriptCondition get(int code){
return scriptConditions[code];
}
private int code;
private String desc;
ScriptCondition(int code,String desc){
this.code = code;
this.desc = desc;
}
public int getCode(){
return this.code;
}
public String getDesc() {
return Messages.getString(desc);
}
@Override
public String toString() {
return Messages.getString(desc);
}
}
public static class SimpleScriptElement{
private int code;
public SimpleScriptElement(){
code = 0;
}
public SimpleScriptElement(int code){
this.code = code;
}
public SimpleScriptElement(int a,int b,int z,ScriptCondition scriptCondition,ScriptOperation scriptOperation){
setA(a);
setB(b);
setZ(z);
setScriptCondition(scriptCondition);
setScriptOperation(scriptOperation);
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@TableColumn(label="A",order=0,width=120)
public String getHumanA(){
return SimpleScriptAddress.toString(getA());
}
public int getA(){
return code & 0xff;
}
@TableColumn(label="B",order=10,width=120)
public String getHumanB(){
return SimpleScriptAddress.toString(getB());
}
public int getB(){
return (code >> 8) & 0xff;
}
@TableColumn(label="Z",order=90,width=120)
public String getHumanZ(){
return SimpleScriptAddress.toString(getZ());
}
public int getZ(){
return (code >> 16) & 0xff;
}
public int getCondition(){
return (code >> 24) & 0x07;
}
public int getOperation(){
return (code >> 27) & 0x1f;
}
public void setA(int a){
code &= ~0xff;
code |= a & 0xff;
}
public void setB(int b){
code &= ~( 0xff << 8 );
code |= (b & 0xff) << 8;
}
public void setZ(int z){
code &= ~( 0xff << 16 );
code |= (z & 0xff) << 16;
}
public void setCondition(int condition){
code &= ~( 0x07 << 24 );
code |= (condition & 0x07) << 24;
}
public void setOperation(int operation){
code &= ~( 0x1f << 27 );
code |= (operation & 0x1F) << 27;
}
@TableColumn(label="Operation",order=50,width=140)
public ScriptOperation getScriptOperation(){
return ScriptOperation.get(getOperation());
}
public void setScriptOperation(ScriptOperation scriptOperation){
if (scriptOperation == null){
setOperation(0);
} else {
setOperation(scriptOperation.getCode());
}
}
@TableColumn(label="Bedingung",order=40,width=120)
public ScriptCondition getScriptCondition(){
return ScriptCondition.get(getCondition());
}
public void setScriptCondition(ScriptCondition scriptCondition){
if (scriptCondition == null){
setCondition(0);
} else {
setCondition(scriptCondition.getCode());
}
}
}
}