Skip to content

Commit

Permalink
fixed #294 change the way mapvalue and valuemap is handled+obtained i…
Browse files Browse the repository at this point in the history
…n compass such that they now are subject to the delay mechanism
  • Loading branch information
lausdahl committed Aug 28, 2014
1 parent 0eca55c commit 1898115
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import org.overture.ast.statements.PStm;
import org.overture.interpreter.eval.DelegateExpressionEvaluator;
import org.overture.interpreter.runtime.Context;
import org.overture.interpreter.runtime.ObjectContext;
import org.overture.interpreter.runtime.ValueException;
import org.overture.interpreter.runtime.VdmRuntime;
import org.overture.interpreter.runtime.VdmRuntimeError;
import org.overture.interpreter.values.MapValue;
import org.overture.interpreter.values.NameValuePair;
import org.overture.interpreter.values.NameValuePairList;
import org.overture.interpreter.values.OperationValue;
Expand All @@ -27,6 +29,7 @@
import org.overture.interpreter.values.SetValue;
import org.overture.interpreter.values.Value;
import org.overture.interpreter.values.ValueList;
import org.overture.interpreter.values.ValueMap;
import org.overture.interpreter.values.ValueSet;

import eu.compassresearch.ast.analysis.QuestionAnswerCMLAdaptor;
Expand All @@ -51,6 +54,8 @@
import eu.compassresearch.core.interpreter.api.values.LatticeTopValue;
import eu.compassresearch.core.interpreter.api.values.NameValue;
import eu.compassresearch.core.interpreter.api.values.RenamingValue;
import eu.compassresearch.core.interpreter.runtime.DelayedWriteContext;
import eu.compassresearch.core.interpreter.runtime.DelayedWriteObjectValue;

public class CmlExpressionVisitor extends
QuestionAnswerCMLAdaptor<Context, Value>
Expand Down Expand Up @@ -120,14 +125,56 @@ public Value caseAApplyExp(AApplyExp node, Context ctxt)
{
try
{
Value object = node.getRoot().apply(VdmRuntime.getExpressionEvaluator(), ctxt).deref();
final Value objectRefed = node.getRoot().apply(VdmRuntime.getExpressionEvaluator(), ctxt);
Value object = objectRefed.deref();
if (object instanceof OperationValue)
{
return StatementInspectionVisitor.invokeOperation(node.getLocation(),node, node.getArgs(), ctxt, (OperationValue)object, this);
} else
} else if(object instanceof MapValue)
{
return super.caseAApplyExp(node, ctxt);
Context lCtxt = ctxt;
if(ctxt instanceof ObjectContext)
{
DelayedWriteContext delayedCtxt = null;
Context tmp = lCtxt;

while (tmp != null)
{
if (tmp instanceof DelayedWriteContext)
{
if (!((DelayedWriteContext) tmp).isDisabled())
{
delayedCtxt = (DelayedWriteContext) tmp;

break;
}
}
tmp = tmp.outer;
}

if(delayedCtxt !=null)
{
//we have to wrap the object context
lCtxt =new ObjectContext(ctxt.assistantFactory, ctxt.location, ctxt.title, ctxt.outer, new DelayedWriteObjectValue(((ObjectContext)lCtxt).self, delayedCtxt));
}
}

Value arg = node.getArgs().get(0).apply(VdmRuntime.getExpressionEvaluator(), ctxt);
// MapValue mv = (MapValue) object;
// return mv.lookup(arg, ctxt);
ValueMap mval = objectRefed.mapValue(lCtxt);

Value v = mval.get(arg);

if (v == null)
{
throw new ValueException(4061, "No such key value in map: " + arg, ctxt);
}

return v;
//return super.caseAApplyExp(node, lCtxt);
}
return super.caseAApplyExp(node, ctxt);
} catch (ValueException e)
{
return VdmRuntimeError.abort(node.getLocation(), e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.overture.interpreter.values;

import java.util.Map.Entry;

import org.overture.ast.analysis.AnalysisException;
import org.overture.ast.intf.lex.ILexLocation;
import org.overture.ast.lex.Dialect;
Expand Down Expand Up @@ -28,7 +30,13 @@ public class DelayedUpdatableWrapper extends UpdatableValue
ILexLocation newLoc = null;
Context newContext = null;

protected DelayedUpdatableWrapper(Value value, ValueListenerList listeners, PType type)
/**
* Value maps obtained through {@link DelayedUpdatableWrapper#mapValue(Context)}
*/
ValueMap obtainedMaps = null;

protected DelayedUpdatableWrapper(Value value, ValueListenerList listeners,
PType type)
{
super(value, listeners, type);
this.original = value;
Expand All @@ -38,17 +46,15 @@ public DelayedUpdatableWrapper(UpdatableValue val)
{
this(val, val.listeners, val.restrictedTo);
}

protected DelayedUpdatableWrapper(Value value, ValueListenerList listeners, PType type,Value original, ILexLocation newLoc,Context newContext)

protected DelayedUpdatableWrapper(Value value, ValueListenerList listeners,
PType type, Value original, ILexLocation newLoc, Context newContext)
{
super(value, listeners, type);
this.original = original;
this.newLoc = newLoc;
this.newContext = newContext;
}




@Override
public void set(ILexLocation location, Value newval, Context ctxt)
Expand Down Expand Up @@ -86,25 +92,59 @@ public void set(ILexLocation location, Value newval, Context ctxt)
listeners.changedValue(location, value, ctxt);
}

/* Useful for debugging, but we don't want to ship with this on. */
/* Useful for debugging, but we don't want to ship with this on. */
// System.err.println("Setting value(" + toShortString(10) + ") to "
// + newval);
// + newval);
}

/**
* Write transactional value to the original value
*
* @throws ValueException
* @throws AnalysisException
*/
public void set() throws ValueException, AnalysisException
{
original.set(newLoc, value, newContext);
}


@Override
public synchronized ValueMap mapValue(Context ctxt) throws ValueException
{
ValueMap m = null;
if (obtainedMaps == null)
{
m = super.mapValue(ctxt);
} else
{
return obtainedMaps;
}
if (m != null)
{
ValueMap mWrapper = new ValueMap();

for (Entry<Value, Value> entry : m.entrySet())
{
if (entry.getValue() instanceof UpdatableValue)
{
mWrapper.put(entry.getKey(), new DelayedUpdatableWrapper((UpdatableValue) entry.getValue()));
} else
{
mWrapper.put(entry.getKey(), entry.getValue());
}
}
obtainedMaps = mWrapper;
this.value = new MapValue(obtainedMaps);
return obtainedMaps;
}

return m;
}

@Override
public synchronized Object clone()
{
return new DelayedUpdatableWrapper((Value)value.clone(), listeners, restrictedTo,original,newLoc,newContext);
return new DelayedUpdatableWrapper((Value) value.clone(), listeners, restrictedTo, original, newLoc, newContext);
}

}
19 changes: 19 additions & 0 deletions core/interpreter/src/test/resources/issues/issue-294.cml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
channels
ping : nat
pair : nat * nat

process TEST = begin
state
simple : map nat to nat := {1 |-> 0}
actions
TOP = A(1) ||| A(2)
A = val x : nat @
pair!x!(simple(1)) ->
simple(1) := x;
pair!x!(simple(1)) ->
Skip
@ ping!(simple(1)) ->
TOP;
ping!(simple(1)) ->
Skip
end
9 changes: 9 additions & 0 deletions core/interpreter/src/test/resources/issues/issue-294.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version='1.0' encoding='UTF-8'?>
<testResult>
<exception></exception>
<interpreterState>FINISHED</interpreterState>
<trace>
<events>ping.0,pair.(\d).0,(pair.\1.\1,pair.(\d).0,pair.\3.\3,|pair.(\d).0,(pair.\1.\1,pair.\4.\4,|pair.\4.\4,pair.\1.\1,))
ping.0</events>
</trace>
</testResult>

0 comments on commit 1898115

Please sign in to comment.