Search in sources :

Example 1 with Value

use of jkind.lustre.values.Value in project AGREE by loonwerks.

the class TcgXmlWriter method writeSignal.

private void writeSignal(int k, Signal<Value> signal) throws Exception {
    String name = escapeXml(signal.getName());
    // TODO: fix this with the type map!
    Type type;
    if (signal.getValues().isEmpty()) {
        throw new TcgException("Unable to assertain signal type in XmlWriter");
    } else {
        Map.Entry<Integer, Value> entry = signal.getValues().entrySet().iterator().next();
        Value v = entry.getValue();
        if (v instanceof IntegerValue) {
            type = NamedType.INT;
        } else if (v instanceof RealValue) {
            type = NamedType.REAL;
        } else if (v instanceof BooleanValue) {
            type = NamedType.BOOL;
        } else {
            throw new TcgException("Unexpected signal type in XmlWriter");
        }
    }
    out.println("      <Signal name=\"" + name + "\" type=\"" + type + "\">");
    for (int i = 0; i < k; i++) {
        out.println("        <Value time=\"" + i + "\">" + formatValue(signal.getValue(i)) + "</Value>");
    }
    out.println("      </Signal>");
}
Also used : RealValue(jkind.lustre.values.RealValue) Type(jkind.lustre.Type) NamedType(jkind.lustre.NamedType) IntegerValue(jkind.lustre.values.IntegerValue) BooleanValue(jkind.lustre.values.BooleanValue) RealValue(jkind.lustre.values.RealValue) IntegerValue(jkind.lustre.values.IntegerValue) Value(jkind.lustre.values.Value) BooleanValue(jkind.lustre.values.BooleanValue) Map(java.util.Map) TcgException(com.rockwellcollins.atc.tcg.TcgException)

Example 2 with Value

use of jkind.lustre.values.Value in project AGREE by loonwerks.

the class CounterexampleLoaderHelper method simulateCounterexample.

private void simulateCounterexample(final Counterexample cex, final int startStepIndex, final Map<String, Object> signalNameToSimStateElementMap, final AGREESimulationEngine simulationEngine, final SimulationService simulationService, final SimulationUIService simulationUIService) {
    // For each frame in the counter example, step through the simulation and constrain the arguments to match those of the counter example
    for (int stepIndex = startStepIndex; stepIndex < cex.getLength(); stepIndex++) {
        simulationEngine.resetInputConstraints();
        // Constraint simulation variables based on the counterexample
        for (final Entry<String, Value> sv : cex.getStep(stepIndex).entrySet()) {
            final String signalName = sv.getKey();
            final Object stateElement = signalNameToSimStateElementMap.get(signalName);
            // Throw exception if the state element corresponding to the signal in the counterexample was not found unless the signal corresponds to a clock or a specification statement
            if (stateElement == null) {
            // TODO: Cleanup
            // Ignore variables that do not have state elements. Need a way to verify that they can be safely ignored. Clocks may be ignored if they aren't
            // used. Variables for SpecStatements are okay to ignore but we don't have a way of getting the reference in all cases.
            // In the case of a monolithic analysis, the references for all layers are not contained in the reference map
            /*
					final String renamedClockIdSuffix = agreeSimulationEngine.getSimulationProgram().getAgreeRenaming().forceRename(AgreeASTBuilder.clockIDSuffix);
					final boolean isClock = signalName.endsWith(renamedClockIdSuffix);
					final EObject ref = refMap.get(signalName);
					if(!isClock && !(ref instanceof SpecStatement)) {
						//throw new RuntimeException("Unable to find state element for signal '" + signalName + "'");
					}
					*/
            } else {
                // Don't include hidden variables
                if (!simulationEngine.getCurrentState().isElementHidden(stateElement)) {
                    // Constrain the next value of the variable to match the value contained in the counter example
                    final InputConstraint ic = lustreValueToInputConstraint(sv.getValue());
                    simulationEngine.setInputConstraint(stateElement, ic);
                }
            }
        }
        simulationEngine.stepForward();
    }
    simulationEngine.queueNotification(notification -> {
        // Check that the number of simulated frames matches the expected number. It should match the length of the counter example
        if (cex.getLength() == notification.getEngineState().getNumberOfFrames()) {
            simulationEngine.resetInputConstraints();
        } else {
            // Determine whether there are unsatisfied properties that are halting the simulation
            boolean hasUnsatisfiedProperty = false;
            for (final SimulationProperty simProp : simulationEngine.getSimulationProgram().getSimulationProperties()) {
                if (notification.getEngineState().getPropertyStatus(simProp) == AGREEPropertyStatus.UNSATISFIED_ERROR) {
                    hasUnsatisfiedProperty = true;
                }
            }
            if (hasUnsatisfiedProperty) {
                Display.getDefault().syncExec(() -> {
                    final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                    final IWorkbenchPage activePage = window.getActivePage();
                    if (activePage != null) {
                        try {
                            activePage.showView(UIConstants.PROPERTIES_VIEW_ID);
                        } catch (final PartInitException e) {
                        // Ignore
                        }
                    }
                    final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
                    final String errorMsg = "One or more properties could not be satisfied. Disable one or more properties from the Simulation Properties view and select Retry to continue.";
                    class RetryDialog extends MessageDialog {

                        public RetryDialog() {
                            super(shell, "Unable to Simulate Counterexample", null, errorMsg, MessageDialog.ERROR, new String[] { "Retry", "End Simulation" }, 0);
                            setShellStyle(SWT.CLOSE | SWT.MODELESS | SWT.BORDER | SWT.TITLE);
                            setBlockOnOpen(false);
                        }

                        @Override
                        protected void buttonPressed(final int buttonId) {
                            switch(buttonId) {
                                case // Retry
                                0:
                                    // Try to resume the simulation
                                    simulationEngine.stepBackward();
                                    simulateCounterexample(cex, notification.getEngineState().getNumberOfFrames() - 1, signalNameToSimStateElementMap, simulationEngine, simulationService, simulationUIService);
                                    break;
                                case // End Simulation
                                1:
                                    simulationService.dispose(simulationEngine);
                                    break;
                            }
                            super.buttonPressed(buttonId);
                        }
                    }
                    final MessageDialog dlg = new RetryDialog();
                    dlg.open();
                });
            } else {
                final StatusAdapter statusAdapter = new StatusAdapter(new Status(IStatus.WARNING, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Simulation halted before every step in the counterexample was simulated.", new RuntimeException("The number of simulated frames does not match the length of the counterexample.")));
                statusAdapter.setProperty(IStatusAdapterConstants.TITLE_PROPERTY, "Unable to Simulate Counterexample");
                StatusManager.getManager().handle(statusAdapter, StatusManager.SHOW);
            }
        }
    });
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) AGREEPropertyStatus(edu.uah.rsesc.aadlsimulator.agree.engine.AGREEPropertyStatus) Status(org.eclipse.core.runtime.Status) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) StatusAdapter(org.eclipse.ui.statushandlers.StatusAdapter) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint) Shell(org.eclipse.swt.widgets.Shell) RealValue(jkind.lustre.values.RealValue) IntegerValue(jkind.lustre.values.IntegerValue) Value(jkind.lustre.values.Value) BooleanValue(jkind.lustre.values.BooleanValue) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) EObject(org.eclipse.emf.ecore.EObject) SimulationProperty(edu.uah.rsesc.aadlsimulator.agree.SimulationProperty) PartInitException(org.eclipse.ui.PartInitException) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) InputConstraint(edu.uah.rsesc.aadlsimulator.xtext.inputConstraint.InputConstraint)

Example 3 with Value

use of jkind.lustre.values.Value in project AGREE by loonwerks.

the class FrameAssertionHelper method addNextFrameAssertions.

/**
 * Adds the frame specific assertions for the next frame to an assertion collection. Does not add assertions related to property enablement.
 * @param program
 * @param lastResults
 * @param assertions is the collection to which assertions will be added.
 */
public static void addNextFrameAssertions(final SimulationProgram program, final SimulationFrameResults lastResults, final Collection<Expr> assertions) {
    if (lastResults == null) {
        for (final Expr c : program.getInitialConstraints()) {
            assertions.add(c);
        }
    } else {
        for (final CarryVariable cv : program.getCarryVariables()) {
            final Value value = lastResults.getValue(cv.getOutputVariableExpression().toString());
            if (value == null) {
                throw new RuntimeException("Unable to get value for: " + cv.getOutputVariableExpression().toString());
            }
            final Expr valueExpression = valueToExpr(value);
            assertions.add(new BinaryExpr(cv.getInputVariableExpression(), BinaryOp.EQUAL, valueExpression));
        }
    }
}
Also used : BoolExpr(jkind.lustre.BoolExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealExpr(jkind.lustre.RealExpr) TupleExpr(jkind.lustre.TupleExpr) ArrayExpr(jkind.lustre.ArrayExpr) Expr(jkind.lustre.Expr) IntExpr(jkind.lustre.IntExpr) IdExpr(jkind.lustre.IdExpr) BinaryExpr(jkind.lustre.BinaryExpr) RealValue(jkind.lustre.values.RealValue) TupleValue(jkind.lustre.values.TupleValue) Value(jkind.lustre.values.Value) EnumValue(jkind.lustre.values.EnumValue) ArrayValue(jkind.lustre.values.ArrayValue) BooleanValue(jkind.lustre.values.BooleanValue) IntegerValue(jkind.lustre.values.IntegerValue) CarryVariable(edu.uah.rsesc.aadlsimulator.agree.CarryVariable)

Example 4 with Value

use of jkind.lustre.values.Value in project AGREE by loonwerks.

the class TcgXmlReader method getSignal.

private Signal<Value> getSignal(Element signalElement) {
    String name = signalElement.getAttribute("name");
    String type = signalElement.getAttribute("type");
    if (type.contains("subrange ")) {
        type = "int";
    }
    Signal<Value> signal = new Signal<>(name);
    for (Element valueElement : getElements(signalElement, "Value")) {
        int time = Integer.parseInt(valueElement.getAttribute(getTimeAttribute()));
        signal.putValue(time, getValue(valueElement, type));
    }
    return signal;
}
Also used : Signal(jkind.results.Signal) Element(org.w3c.dom.Element) Value(jkind.lustre.values.Value)

Example 5 with Value

use of jkind.lustre.values.Value in project AGREE by loonwerks.

the class SymbolicValue method applyBinaryOp.

public SymbolicValue applyBinaryOp(final BinaryOp op, SymbolicValue value) {
    final Value newCoefficient;
    final Variable newVariable;
    final Value newConstant;
    if (op == BinaryOp.EQUAL) {
        // Equality comparison is only supported when the variables are the same
        if (variable == value.variable) {
            boolean equal = Objects.equals(coefficient, value.coefficient) && Objects.equals(variable, value.variable) && Objects.equals(constant, value.constant);
            return new SymbolicValue(null, null, equal ? BooleanValue.TRUE : BooleanValue.FALSE);
        } else {
            return null;
        }
    } else if (op == BinaryOp.LESS || op == BinaryOp.LESSEQUAL || op == BinaryOp.GREATER || op == BinaryOp.GREATEREQUAL) {
        // Comparison operators are only supported when the symbolic value are constant values
        if (variable == null && value.variable == null && constant != null && value.constant != null) {
            final Value result = constant.applyBinaryOp(op, value.constant);
            if (result instanceof BooleanValue) {
                return new SymbolicValue(null, null, (BooleanValue) result);
            }
        }
        return null;
    } else if (op == BinaryOp.PLUS || op == BinaryOp.MINUS) {
        if (variable == null && value.variable == null) {
            newCoefficient = null;
            newVariable = null;
        } else if (variable != null && value.variable != null) {
            if (variable == value.variable) {
                newCoefficient = coefficient.applyBinaryOp(op, value.coefficient);
                newVariable = variable;
            } else {
                // Unsupported
                return null;
            }
        } else {
            if (variable == null) {
                // value.variable != null
                if (op == BinaryOp.PLUS) {
                    newCoefficient = value.coefficient;
                } else {
                    newCoefficient = value.coefficient.applyUnaryOp(UnaryOp.NEGATIVE);
                }
                newVariable = value.variable;
            } else {
                newCoefficient = coefficient;
                newVariable = variable;
            }
        }
        if (constant == null && value.constant == null) {
            newConstant = null;
        } else if (constant != null && value.constant != null) {
            newConstant = constant.applyBinaryOp(op, value.constant);
        } else {
            if (constant == null) {
                // value.constant != null
                if (op == BinaryOp.PLUS) {
                    newConstant = value.constant;
                } else {
                    newConstant = value.constant.applyUnaryOp(UnaryOp.NEGATIVE);
                }
            } else {
                newConstant = constant;
            }
        }
    } else if (op == BinaryOp.MULTIPLY || op == BinaryOp.DIVIDE) {
        if (variable == null && value.variable == null) {
            newCoefficient = null;
            newVariable = null;
        } else if (variable != null && value.variable != null) {
            // At most one may have a variable id
            return null;
        } else {
            if (variable == null) {
                // value.variable != null
                newCoefficient = value.coefficient.applyBinaryOp(op, constant);
                newVariable = value.variable;
            } else {
                newCoefficient = coefficient.applyBinaryOp(op, value.constant);
                newVariable = variable;
            }
        }
        // Since multiplying values which both contain coefficients are not allowed, at least one value must have a constant
        if (constant != null && value.constant != null) {
            newConstant = constant.applyBinaryOp(op, value.constant);
        } else {
            newConstant = null;
        }
    } else if (op == BinaryOp.AND) {
        if (variable == null && value.variable == null) {
            newCoefficient = null;
            newVariable = null;
            newConstant = constant.applyBinaryOp(op, value.constant);
        } else if (variable != null && value.variable != null) {
            if (variable == value.variable && coefficient.equals(value.coefficient)) {
                newCoefficient = coefficient;
                newVariable = variable;
                newConstant = null;
            } else {
                // Unsupported
                return null;
            }
        } else {
            if (variable == null) {
                // value.variable != null
                if (constant == BooleanValue.TRUE) {
                    newCoefficient = value.coefficient;
                    newVariable = value.variable;
                    newConstant = null;
                } else {
                    newCoefficient = null;
                    newVariable = null;
                    newConstant = BooleanValue.FALSE;
                }
            } else {
                if (value.constant == BooleanValue.TRUE) {
                    newCoefficient = coefficient;
                    newVariable = variable;
                    newConstant = null;
                } else {
                    newCoefficient = null;
                    newVariable = null;
                    newConstant = BooleanValue.FALSE;
                }
            }
        }
    } else if (op == BinaryOp.OR) {
        if (variable == null && value.variable == null) {
            newCoefficient = null;
            newVariable = null;
            newConstant = constant.applyBinaryOp(op, value.constant);
        } else if (variable != null && value.variable != null) {
            if (variable == value.variable && coefficient.equals(value.coefficient)) {
                newCoefficient = coefficient;
                newVariable = variable;
                newConstant = null;
            } else {
                // Unsupported
                return null;
            }
        } else {
            if (variable == null) {
                // value.variable != null
                if (constant == BooleanValue.TRUE) {
                    newCoefficient = null;
                    newVariable = null;
                    newConstant = BooleanValue.TRUE;
                } else {
                    newCoefficient = value.coefficient;
                    newVariable = value.variable;
                    newConstant = null;
                }
            } else {
                if (value.constant == BooleanValue.TRUE) {
                    newCoefficient = null;
                    newVariable = null;
                    newConstant = BooleanValue.TRUE;
                } else {
                    newCoefficient = coefficient;
                    newVariable = variable;
                    newConstant = null;
                }
            }
        }
    } else {
        return null;
    }
    // This will occur if unsupported operations are performed on the coefficient.
    if ((newCoefficient == null) != (newVariable == null)) {
        return null;
    }
    return new SymbolicValue(newCoefficient, newVariable, newConstant);
}
Also used : BooleanValue(jkind.lustre.values.BooleanValue) Value(jkind.lustre.values.Value) BooleanValue(jkind.lustre.values.BooleanValue)

Aggregations

Value (jkind.lustre.values.Value)17 BooleanValue (jkind.lustre.values.BooleanValue)11 IntegerValue (jkind.lustre.values.IntegerValue)9 RealValue (jkind.lustre.values.RealValue)9 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 Signal (jkind.results.Signal)6 EObject (org.eclipse.emf.ecore.EObject)6 List (java.util.List)4 BinaryExpr (jkind.lustre.BinaryExpr)4 BoolExpr (jkind.lustre.BoolExpr)4 IntExpr (jkind.lustre.IntExpr)4 RealExpr (jkind.lustre.RealExpr)4 ArrayValue (jkind.lustre.values.ArrayValue)4 EnumValue (jkind.lustre.values.EnumValue)4 TupleValue (jkind.lustre.values.TupleValue)4 Counterexample (jkind.results.Counterexample)4 Layout (jkind.results.layout.Layout)4 AgreeSubclause (com.rockwellcollins.atc.agree.agree.AgreeSubclause)3 AgreeUtils (com.rockwellcollins.atc.agree.analysis.AgreeUtils)3