diff options
Diffstat (limited to 'rba.model.core/src/rba/core/generator/util')
9 files changed, 639 insertions, 0 deletions
diff --git a/rba.model.core/src/rba/core/generator/util/EObjectDecorator.java b/rba.model.core/src/rba/core/generator/util/EObjectDecorator.java new file mode 100644 index 0000000..86a20d7 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/EObjectDecorator.java @@ -0,0 +1,118 @@ +package rba.core.generator.util; + +import java.lang.reflect.InvocationTargetException; + +import org.eclipse.emf.common.notify.Adapter; +import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.TreeIterator; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EOperation; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.resource.Resource; + +abstract public class EObjectDecorator implements EObject { + private EObject object; + + protected EObjectDecorator(EObject o) { + object = o; + } + + @Override + public EList<Adapter> eAdapters() { + return object.eAdapters(); + } + + @Override + public boolean eDeliver() { + return object.eDeliver(); + } + + @Override + public void eSetDeliver(boolean deliver) { + object.eSetDeliver(deliver); + } + + @Override + public void eNotify(Notification notification) { + object.eNotify(notification); + } + + @Override + public EClass eClass() { + return object.eClass(); + } + + @Override + public Resource eResource() { + return object.eResource(); + } + + @Override + public EObject eContainer() { + return object.eContainer(); + } + + @Override + public EStructuralFeature eContainingFeature() { + return object.eContainingFeature(); + } + + @Override + public EReference eContainmentFeature() { + return object.eContainmentFeature(); + } + + @Override + public EList<EObject> eContents() { + return object.eContents(); + } + + @Override + public TreeIterator<EObject> eAllContents() { + return object.eAllContents(); + } + + @Override + public boolean eIsProxy() { + return object.eIsProxy(); + } + + @Override + public EList<EObject> eCrossReferences() { + return object.eCrossReferences(); + } + + @Override + public Object eGet(EStructuralFeature feature) { + return object.eGet(feature); + } + + @Override + public Object eGet(EStructuralFeature feature, boolean resolve) { + return object.eGet(feature, resolve); + } + + @Override + public void eSet(EStructuralFeature feature, Object newValue) { + object.eSet(feature, newValue); + } + + @Override + public boolean eIsSet(EStructuralFeature feature) { + return object.eIsSet(feature); + } + + @Override + public void eUnset(EStructuralFeature feature) { + object.eUnset(feature); + } + + @Override + public Object eInvoke(EOperation operation, EList<?> arguments) throws InvocationTargetException { + return object.eInvoke(operation, arguments); + } + +} diff --git a/rba.model.core/src/rba/core/generator/util/value/ComparisonAndEvaluator.java b/rba.model.core/src/rba/core/generator/util/value/ComparisonAndEvaluator.java new file mode 100644 index 0000000..0973ab9 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/ComparisonAndEvaluator.java @@ -0,0 +1,49 @@ +package rba.core.generator.util.value; + +import org.eclipse.emf.common.util.EList; + +import rba.core.ComparisonAnd; +import rba.core.ComparisonOperator; +import rba.core.Expression; +import rba.core.GreaterThanOperator; +import rba.core.LetStatement; +import rba.core.MuchGreaterThanOperator; +import rba.core.RuleObject; + +public class ComparisonAndEvaluator<T extends RuleObject> extends ComparisonOperatorEvaluator<T> implements ComparisonAnd { + + public ComparisonAndEvaluator(ComparisonAnd o) { + super(o); + } + + @Override + public boolean evaluate(ValueSortedList<T> list) { + // TODO Auto-generated method stub + return false; + } + + @Override + public int suggestIndex(ValueSortedList<T> list) { + int max = 9999; + int min = -1; + + for (Expression operand : operator.getOperand()) { + if (operand instanceof GreaterThanOperator || operand instanceof MuchGreaterThanOperator) { + int i = list.createEvaluator((ComparisonOperator) operand).suggestIndex(list); + if (i > min && i >= 0) { + min = i; + } + } else { + throw new RuntimeException("Unsupported expression [" + operator.getExpression() + "]"); + } + } + return Math.min(min, max); + } + + @Override + public EList<LetStatement> getLetStatements() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/rba.model.core/src/rba/core/generator/util/value/ComparisonOperatorEvaluator.java b/rba.model.core/src/rba/core/generator/util/value/ComparisonOperatorEvaluator.java new file mode 100644 index 0000000..548183f --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/ComparisonOperatorEvaluator.java @@ -0,0 +1,48 @@ +package rba.core.generator.util.value; + +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.ecore.EObject; + +import rba.core.ComparisonOperator; +import rba.core.Expression; +import rba.core.RuleObject; + +public abstract class ComparisonOperatorEvaluator<T extends RuleObject> extends ExpressionEvaluator<T> implements ComparisonOperator { + + protected ComparisonOperator operator; + + protected T subject; + + protected ComparisonOperatorEvaluator(ComparisonOperator o) { + super(o); + operator = o; + EObject container = o.eContainer(); + while (!(container instanceof RuleObject)) { + container = container.eContainer(); + if (container == null) + break; + } + if (container != null) { + subject = (T) container; + } + } + + abstract public boolean evaluate(ValueSortedList<T> list); + + /** + * @param list + * @return -1 if it has no suggestion 9999 implies MAX group (last index) + */ + abstract public int suggestIndex(ValueSortedList<T> list); + + @Override + public EList<Expression> getOperand() { + return operator.getOperand(); + } + + @Override + public String getSymbol() { + return operator.getSymbol(); + } + +} diff --git a/rba.model.core/src/rba/core/generator/util/value/EqualToOperatorEvaluator.java b/rba.model.core/src/rba/core/generator/util/value/EqualToOperatorEvaluator.java new file mode 100644 index 0000000..bb20773 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/EqualToOperatorEvaluator.java @@ -0,0 +1,51 @@ +package rba.core.generator.util.value; + +import org.eclipse.emf.common.util.EList; + +import rba.core.ComparisonOperator; +import rba.core.EqualToOperator; +import rba.core.Expression; +import rba.core.LetStatement; +import rba.core.ObjectReference; +import rba.core.RuleObject; +import rba.core.ThatOfOperator; +import rba.core.ValueExpression; + +public class EqualToOperatorEvaluator<T extends RuleObject> extends ComparisonOperatorEvaluator<T> implements EqualToOperator { + + protected EqualToOperatorEvaluator(ComparisonOperator o) { + super(o); + } + + @Override + public boolean evaluate(ValueSortedList<T> list) { + // TODO Auto-generated method stub + return false; + } + + @Override + public int suggestIndex(ValueSortedList<T> list) { + Expression operand = operator.getOperand().get(0); + if (operand instanceof ThatOfOperator) { + Expression target = ((ThatOfOperator) operand).getOperand().get(0); + + if (target instanceof ObjectReference) { + T object = (T) ((ObjectReference) target).getRefObject(); + return list.getGroupIndexOf(object); + } + + } else if (operand instanceof ValueExpression) { + int i = list.getGroupIndexOf(((ValueExpression) operand).getExpression()); + return i; + } + + throw new RuntimeException("Unsupported expression [" + operator.getExpression() + "]"); + } + + @Override + public EList<LetStatement> getLetStatements() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/rba.model.core/src/rba/core/generator/util/value/ExpressionEvaluator.java b/rba.model.core/src/rba/core/generator/util/value/ExpressionEvaluator.java new file mode 100644 index 0000000..75bf881 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/ExpressionEvaluator.java @@ -0,0 +1,56 @@ +package rba.core.generator.util.value; + +import rba.core.Expression; +import rba.core.ExpressionType; +import rba.core.generator.util.EObjectDecorator; + +abstract public class ExpressionEvaluator<T> extends EObjectDecorator implements Expression { + + private Expression expression; + + protected ExpressionEvaluator(Expression o) { + super(o); + expression = o; + } + + @Override + public ExpressionType getType() { + return expression.getType(); + } + + @Override + public boolean isSetType() { + return expression.isSetType(); + } + + @Override + public String getExpression() { + return expression.getExpression(); + } + + @Override + public void setExpression(String value) { + expression.setExpression(value); + } + + @Override + public void unsetExpression() { + expression.unsetExpression(); + } + + @Override + public boolean isSetExpression() { + return expression.isSetExpression(); + } + + @Override + public String getExpressionText() { + return expression.getExpressionText(); + } + + @Override + public ExpressionType getUnderlyingType() { + return expression.getUnderlyingType(); + } + +} diff --git a/rba.model.core/src/rba/core/generator/util/value/GreaterThanOperatorEvaluator.java b/rba.model.core/src/rba/core/generator/util/value/GreaterThanOperatorEvaluator.java new file mode 100644 index 0000000..4b3506d --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/GreaterThanOperatorEvaluator.java @@ -0,0 +1,62 @@ +package rba.core.generator.util.value; + +import org.eclipse.emf.common.util.EList; + +import rba.core.Expression; +import rba.core.GreaterThanOperator; +import rba.core.LetStatement; +import rba.core.ObjectReference; +import rba.core.RuleObject; +import rba.core.ThatOfOperator; +import rba.core.ValueExpression; + +public class GreaterThanOperatorEvaluator<T extends RuleObject> extends ComparisonOperatorEvaluator<T> implements GreaterThanOperator { + + protected GreaterThanOperatorEvaluator(GreaterThanOperator o) { + super(o); + } + + @Override + public boolean evaluate(ValueSortedList<T> list) { + Expression operand = operator.getOperand().get(0); + if (operand instanceof ObjectReference) { + T object = (T) ((ObjectReference) operand).getRefObject(); + return (list.getGroupIndexOf(subject) > list.getGroupIndexOf(object)); + } + + return false; + } + + @Override + public int suggestIndex(ValueSortedList<T> list) { + Expression operand = operator.getOperand().get(0); + if (operand instanceof ThatOfOperator) { + Expression target = ((ThatOfOperator) operand).getOperand().get(0); + + if (target instanceof ObjectReference) { + T object = (T) ((ObjectReference) target).getRefObject(); + if (list.getGroupIndexOf(subject) > list.getGroupIndexOf(object)) { + return -1; + } else { + return list.getGroupIndexOf(object) + 1; + } + } + + } else if (operand instanceof ValueExpression) { + int i = ((ValueExpression) operand).getExpressionValue(); + if (list.getGroupIndexOf(subject) > i) { + return -1; + } else { + return i + 1; + } + } + + throw new RuntimeException("Unsupported expression [" + operator.getExpression() + "]"); + } + + @Override + public EList<LetStatement> getLetStatements() { + // TODO Auto-generated method stub + return null; + } +} diff --git a/rba.model.core/src/rba/core/generator/util/value/MuchGreaterThanOperatorEvaluator.java b/rba.model.core/src/rba/core/generator/util/value/MuchGreaterThanOperatorEvaluator.java new file mode 100644 index 0000000..2c222c2 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/MuchGreaterThanOperatorEvaluator.java @@ -0,0 +1,65 @@ +package rba.core.generator.util.value; + +import org.eclipse.emf.common.util.EList; + +import rba.core.Expression; +import rba.core.LetStatement; +import rba.core.MuchGreaterThanOperator; +import rba.core.ObjectReference; +import rba.core.RuleObject; +import rba.core.ThatOfOperator; +import rba.core.ValueExpression; + +public class MuchGreaterThanOperatorEvaluator<T extends RuleObject> extends ComparisonOperatorEvaluator<T> implements MuchGreaterThanOperator { + + static int MUCH_GAP = 10; + + public MuchGreaterThanOperatorEvaluator(MuchGreaterThanOperator o) { + super(o); + } + + @Override + public boolean evaluate(ValueSortedList<T> list) { + Expression operand = operator.getOperand().get(0); + if (operand instanceof ObjectReference) { + T object = (T) ((ObjectReference) operand).getRefObject(); + return (list.getGroupIndexOf(subject) >= list.getGroupIndexOf(object) + MUCH_GAP); + } + + return false; + } + + @Override + public int suggestIndex(ValueSortedList<T> list) { + Expression operand = operator.getOperand().get(0); + if (operand instanceof ThatOfOperator) { + Expression target = ((ThatOfOperator) operand).getOperand().get(0); + + if (target instanceof ObjectReference) { + T object = (T) ((ObjectReference) target).getRefObject(); + if (list.getGroupIndexOf(subject) >= list.getGroupIndexOf(object) + MUCH_GAP) { + return -1; + } else { + return list.getGroupIndexOf(object) + MUCH_GAP; + } + } + + } else if (operand instanceof ValueExpression) { + int i = ((ValueExpression) operand).getExpressionValue(); + if (list.getGroupIndexOf(subject) > i) { + return -1; + } else { + return i + 1; + } + } + + throw new RuntimeException("Unsupported expression [" + operator.getExpression() + "]"); + } + + @Override + public EList<LetStatement> getLetStatements() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/rba.model.core/src/rba/core/generator/util/value/ValueGroup.java b/rba.model.core/src/rba/core/generator/util/value/ValueGroup.java new file mode 100644 index 0000000..de62c20 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/ValueGroup.java @@ -0,0 +1,35 @@ +package rba.core.generator.util.value; + +import java.util.ArrayList; +import java.util.Collection; + +public class ValueGroup<T> { + private String name; + + private Collection<T> member; + + public ValueGroup(String name) { + this.name = name; + member = new ArrayList<T>(); + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void add(T e) { + member.add(e); + } + + public void remove(T e) { + member.remove(e); + } + + public Collection<T> getMember() { + return member; + } +} diff --git a/rba.model.core/src/rba/core/generator/util/value/ValueSortedList.java b/rba.model.core/src/rba/core/generator/util/value/ValueSortedList.java new file mode 100644 index 0000000..f16f950 --- /dev/null +++ b/rba.model.core/src/rba/core/generator/util/value/ValueSortedList.java @@ -0,0 +1,155 @@ +package rba.core.generator.util.value; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import rba.core.ComparisonAnd; +import rba.core.ComparisonOperator; +import rba.core.EqualToOperator; +import rba.core.Expression; +import rba.core.GreaterThanOperator; +import rba.core.MuchGreaterThanOperator; +import rba.core.RuleObject; +import rba.core.ValueExpression; + +abstract public class ValueSortedList<T extends RuleObject> { + private List<ValueGroup<T>> list; + + private Collection<T> unsorted; + + public ValueSortedList(Collection<T> originalList) { + list = new ArrayList<ValueGroup<T>>(); + list.add(new ValueGroup<T>("MIN")); + list.add(new ValueGroup<T>("1")); + list.add(new ValueGroup<T>("MAX")); + unsorted = new ArrayList<T>(originalList); + sort(); + } + + public void add(T o) { + unsorted.add(o); + sort(); + } + + public void addAll(Collection<T> list) { + unsorted.addAll(list); + sort(); + } + + public void sort() { + list.get(0).getMember().addAll(unsorted); + unsorted.clear(); + boolean trysort = true; + int retry = 0; + while (trysort) { + trysort = false; + retry = retry + 1; + if (retry > 100) { + throw new RuntimeException("Could not sort value [" + list + "]"); + } + for (T i : sortedList()) { + int currentIndex = this.getGroupIndexOf(i); + Expression e = this.getValueExpression(i); + if (e == null) + continue; + if (e instanceof ValueExpression) { + int index = this.getGroupIndexOf(e.getExpression()); + if (index == -1) { + index = ((ValueExpression) e).getExpressionValue(); + if (index >= list.size() - 1) { + list.get(currentIndex).remove(i); + addGroupAt(index, i); + trysort = true; + continue; + } + } + if (index != currentIndex) { + list.get(currentIndex).remove(i); + list.get(index).add(i); + trysort = true; + } else { + } + } else if (e instanceof ComparisonOperator) { + ComparisonOperatorEvaluator<T> evaluator = createEvaluator((ComparisonOperator) e); + int index = evaluator.suggestIndex(this); + if (index < 0) + continue; + if (index >= list.size() - 1) { + list.get(currentIndex).remove(i); + addGroupAt(index, i); + trysort = true; + } else if (index != currentIndex) { + list.get(currentIndex).remove(i); + list.get(index).add(i); + trysort = true; + } else { + } + } else { + throw new RuntimeException("Unsupported expression [" + e.getExpression() + "]"); + } + } + } + } + + private void addGroupAt(int index, T e) { + + if (index >= list.size() - 1) { + for (int i = list.size() - 1; i <= index; ++i) { + ValueGroup<T> newGroup = new ValueGroup<T>(Integer.toString(i)); + list.add(i, newGroup); + } + } + + list.get(index).add(e); + + } + + public List<T> sortedList() { + List<T> result = new ArrayList<T>(); + for (ValueGroup<T> i : list) { + result.addAll(i.getMember()); + } + return result; + } + + public List<ValueGroup<T>> getGroupList() { + return list; + } + + abstract protected Expression getValueExpression(T e); + + public int getGroupIndexOf(T e) { + for (ValueGroup<T> i : list) { + if (i.getMember().contains(e)) { + return list.indexOf(i); + } + } + return -1; + } + + public int getGroupIndexOf(String groupName) { + for (ValueGroup<T> i : list) { + if (i.getName().contentEquals(groupName)) { + return list.indexOf(i); + } + } + return -1; + } + + public ComparisonOperatorEvaluator<T> createEvaluator(ComparisonOperator e) { + ComparisonOperatorEvaluator<T> evaluator; + if (e instanceof GreaterThanOperator) { + evaluator = new GreaterThanOperatorEvaluator<T>((GreaterThanOperator) e); + } else if (e instanceof MuchGreaterThanOperator) { + evaluator = new MuchGreaterThanOperatorEvaluator<T>((MuchGreaterThanOperator) e); + } else if (e instanceof ComparisonAnd) { + evaluator = new ComparisonAndEvaluator<T>((ComparisonAnd) e); + } else if (e instanceof EqualToOperator) { + evaluator = new EqualToOperatorEvaluator<T>((EqualToOperator) e); + } else { + throw new RuntimeException("Unsupported expression [" + e.getExpression() + "]"); + } + return evaluator; + } +} |