use of jkind.api.results.JKindResult in project AGREE by loonwerks.
the class VerifyHandler method createVerification.
private AnalysisResult createVerification(String resultName, ComponentInstance compInst, Program lustreProgram, AgreeProgram agreeProgram, AnalysisType analysisType) {
// ///////
// Logger logger = Logger.getLogger("MyLog");
// FileHandler fh;
// try {
// This block configure the logger with handler and formatter
// fh = new FileHandler("/home/thomas/zzz.log");
// logger.addHandler(fh);
// SimpleFormatter formatter = new SimpleFormatter();
// fh.setFormatter(formatter);
// logger.info("Agree Program:");
// AgreeASTPrettyprinter pp = new AgreeASTPrettyprinter();
// pp.visit(agreeProgram);
// logger.info(pp.toString());
// } catch (SecurityException ex) {
// ex.printStackTrace();
// }
// catch (IOException ex) {
// ex.printStackTrace();
// }
// //////////////
AgreeAutomaterRegistry aAReg = (AgreeAutomaterRegistry) ExtensionRegistry.getRegistry(ExtensionRegistry.AGREE_AUTOMATER_EXT_ID);
List<AgreeAutomater> automaters = aAReg.getAgreeAutomaters();
AgreeRenaming renaming = new AgreeRenaming();
AgreeLayout layout = new AgreeLayout();
Node mainNode = null;
for (Node node : lustreProgram.nodes) {
if (node.id.equals(lustreProgram.main)) {
mainNode = node;
break;
}
}
if (mainNode == null) {
throw new AgreeException("Could not find main lustre node after translation");
}
List<String> properties = new ArrayList<>();
RenamingVisitor.addRenamings(lustreProgram, renaming, compInst, layout);
addProperties(renaming, properties, mainNode, agreeProgram);
for (AgreeAutomater aa : automaters) {
renaming = aa.rename(renaming);
layout = aa.transformLayout(layout);
}
JKindResult result;
switch(analysisType) {
case Consistency:
result = new ConsistencyResult(resultName, mainNode.properties, Collections.singletonList(true), renaming);
break;
case Realizability:
result = new JRealizabilityResult(resultName, renaming);
break;
case AssumeGuarantee:
final Set<String> invertedProperties = renaming.getInvertedProperties();
List<Boolean> invertedPropertyMask = mainNode.properties.stream().map(p -> invertedProperties.contains(p)).collect(Collectors.toList());
result = new JKindResult(resultName, properties, invertedPropertyMask, renaming);
break;
default:
throw new AgreeException("Unhandled Analysis Type");
}
queue.add(result);
ComponentImplementation compImpl = AgreeUtils.getInstanceImplementation(compInst);
linker.setProgram(result, lustreProgram);
linker.setComponent(result, compImpl);
linker.setContract(result, getContract(compImpl));
linker.setLayout(result, layout);
linker.setReferenceMap(result, renaming.getRefMap());
linker.setLog(result, AgreeLogger.getLog());
linker.setRenaming(result, renaming);
// System.out.println(program);
return result;
}
use of jkind.api.results.JKindResult in project AGREE by loonwerks.
the class VerifyHandler method doAnalysis.
protected IStatus doAnalysis(final Element root, final IProgressMonitor globalMonitor) {
Thread analysisThread = new Thread() {
@Override
public void run() {
// Record the analysis start time and get model hashcode for
// saving to property analysis log, if necessary
String modelHash = "";
long startTime = 0;
if (Activator.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.PREF_PROP_LOG)) {
try {
modelHash = AgreeFileUtil.getModelHashcode(root);
startTime = System.currentTimeMillis();
} catch (Exception e) {
System.out.println(e.getMessage());
return;
}
}
try {
activateTerminateHandlers(globalMonitor);
KindApi api = PreferencesUtil.getKindApi();
KindApi consistApi = PreferencesUtil.getConsistencyApi();
JRealizabilityApi realApi = PreferencesUtil.getJRealizabilityApi();
while (!queue.isEmpty() && !globalMonitor.isCanceled()) {
JKindResult result = queue.peek();
NullProgressMonitor subMonitor = new NullProgressMonitor();
monitorRef.set(subMonitor);
Program program = linker.getProgram(result);
if (api instanceof JKindApi) {
String resultName = result.getName();
String adviceFileName = rerunAdviceMap.get(resultName);
if (adviceFileName == null) {
adviceFileName = "agree_advice" + adviceCount++;
rerunAdviceMap.put(resultName, adviceFileName);
} else {
((JKindApi) api).setReadAdviceFile(adviceFileName);
}
((JKindApi) api).setWriteAdviceFile(adviceFileName);
}
try {
if (result instanceof ConsistencyResult) {
consistApi.execute(program, result, subMonitor);
} else if (result instanceof JRealizabilityResult) {
realApi.execute(program, (JRealizabilityResult) result, subMonitor);
} else {
api.execute(program, result, subMonitor);
}
} catch (JKindException e) {
System.out.println("******** JKindException Text ********");
e.printStackTrace(System.out);
// System.out.println("******** JKind Output ********");
// System.out.println(result.getText());
// System.out.println("******** Agree Lustre ********");
// System.out.println(program);
String errStr = e.getMessage();
int l = Math.min(errStr.length(), 300);
System.out.println(e.getMessage().substring(0, l));
// >>>>>>> origin/develop
break;
}
// Print to property analysis log, if necessary
if (Activator.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.PREF_PROP_LOG)) {
AgreeFileUtil.printLog(result, startTime, modelHash);
}
queue.remove();
}
while (!queue.isEmpty()) {
queue.remove().cancel();
}
} finally {
deactivateTerminateHandlers();
enableRerunHandler(root);
}
}
};
analysisThread.start();
return Status.OK_STATUS;
}
use of jkind.api.results.JKindResult in project AGREE by loonwerks.
the class AgreeMenuListener method addViewTraceabilityConsole.
private IAction addViewTraceabilityConsole(String text, IMenuManager manager, AnalysisResult result) {
return new Action(text) {
@Override
public void run() {
Map<String, EObject> tempRefMap = linker.getReferenceMap(result.getParent());
if (tempRefMap == null) {
tempRefMap = linker.getReferenceMap(result);
}
final Map<String, EObject> refMap = tempRefMap;
final MessageConsole console = findConsole("Traceability");
final Renaming renaming = linker.getRenaming(result);
showConsole(console);
console.clearConsole();
console.addPatternMatchListener(new AgreePatternListener(refMap));
new Thread(() -> {
try {
MessageConsoleStream out = console.newMessageStream();
printHLine(out, 2);
out.println("Traceability for Valid Contract Guarantees");
printHLine(out, 2);
out.println("");
List<PropertyResult> allProperties = new ArrayList<PropertyResult>(((JKindResult) result).getPropertyResults());
if (!allProperties.isEmpty()) {
for (PropertyResult prop : allProperties) {
if (prop.getStatus().equals(jkind.api.results.Status.VALID)) {
if (renaming instanceof AgreeRenaming) {
writeIvcResult(prop, console, (AgreeRenaming) renaming);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
}
};
}
use of jkind.api.results.JKindResult in project AGREE by loonwerks.
the class AgreeFileUtil method parsePropertyResult.
/**
* Converts an AGREE analysis result to a simple format used for saving AGREE analyses.
* @param result - The AGREE result.
* @return AgreePropertyResult
*/
private static AgreeLogResult parsePropertyResult(AnalysisResult result) {
AgreeLogResult agreeResult = new AgreeLogResult();
agreeResult.setName(result.getName());
if (result instanceof PropertyResult) {
PropertyResult propResult = (PropertyResult) result;
agreeResult.setResult(propResult.getStatus().toString());
} else if (result instanceof JKindResult) {
JKindResult jkindResult = (JKindResult) result;
ArrayList<AgreeLogResult> propResults = new ArrayList<>();
agreeResult.setResult(jkindResult.getMultiStatus().getOverallStatus().toString());
for (PropertyResult propResult : jkindResult.getPropertyResults()) {
propResults.add(parsePropertyResult(propResult));
}
agreeResult.setAnalyses(propResults);
}
return agreeResult;
}
use of jkind.api.results.JKindResult in project AGREE by loonwerks.
the class Simulation method executeFrame.
private SimulationFrameResults executeFrame(final List<Expr> assertions, final Set<SimulationProperty> disabledProperties) throws InterruptedException {
assert assertions != null;
// Build the final constrained lustre specification for the frame.
final ProgramBuilder programBuilder = new ProgramBuilder(program.getLustreProgram());
programBuilder.clearNodes();
final NodeBuilder nodeBuilder = new NodeBuilder(program.getLustreProgram().getMainNode());
// Add assignments for the sim assertions signal
// Actual assertions are not used because they can result in an inconsistent Lustre program which will prevent
// the set of support from being generated when using yices.
Expr prevSimAssertionExpr = new BoolExpr(true);
for (int assertionIndex = 0; assertionIndex < assertions.size(); assertionIndex++) {
final String simAssertionSignalId = CreateSimulationGuarantee.SIMULATION_ASSERTIONS_ID + assertionIndex;
final IdExpr simAssertionExpr = new IdExpr(simAssertionSignalId);
nodeBuilder.addLocal(new VarDecl(simAssertionSignalId, NamedType.BOOL));
nodeBuilder.addEquation(new Equation(simAssertionExpr, new BinaryExpr(prevSimAssertionExpr, BinaryOp.AND, assertions.get(assertionIndex))));
prevSimAssertionExpr = simAssertionExpr;
}
nodeBuilder.addEquation(new Equation(new IdExpr(CreateSimulationGuarantee.SIMULATION_ASSERTIONS_ID), prevSimAssertionExpr));
// Add assignments for property enablement variables
for (final SimulationProperty simProp : program.getSimulationProperties()) {
if (simProp.getEnablementVariableId() != null) {
nodeBuilder.addEquation(new Equation(new IdExpr(simProp.getEnablementVariableId()), new BoolExpr(disabledProperties.contains(simProp) ? false : true)));
}
}
// Build the lustre program for the frame
programBuilder.addNode(nodeBuilder.build());
final Program constrainedLustreProgram = programBuilder.build();
// Prepare to execute JKind
final KindApi api = PreferencesUtil.getKindApi();
// Enable IVC Reduction capability if using JKind
if (api instanceof JKindApi) {
final JKindApi jkindApi = (JKindApi) api;
jkindApi.setIvcReduction();
}
// Execute JKind
final JKindResult result = new JKindResult("Simulation");
// Lucas: This seems to be needed. If we do not add properties to the result explicitly,
// it looks like the result will grab the main property name with the main node prepended.
// This is causing an error when retrieving the property result in the
// if/then/else block structure below.
constrainedLustreProgram.getMainNode().properties.forEach(p -> result.addProperty(p));
System.out.println(constrainedLustreProgram.toString());
try {
final IProgressMonitor currentMonitor = new NullProgressMonitor();
api.execute(constrainedLustreProgram, result, currentMonitor);
// Create a model state from the results.
String simulationGuaranteeId = CreateSimulationGuarantee.SIMULATION_GUARANTEE_ID;
final PropertyResult propertyResult = result.getPropertyResult(simulationGuaranteeId);
final Property property = propertyResult.getProperty();
if (property == null) {
throw new AGREESimulatorException("Unexpected case. Unable to read property results", constrainedLustreProgram);
} else if (property instanceof InvalidProperty) {
final InvalidProperty invalidProperty = (InvalidProperty) property;
final Counterexample counterexample = invalidProperty.getCounterexample();
if (counterexample.getLength() != 1) {
throw new AGREESimulatorException("Unexpected case. Counterexample has " + counterexample.getLength() + " steps", constrainedLustreProgram);
}
SimulationState newState = SimulationState.WAITING_FOR_COMMANDS;
// and a counterexample will not have been generated. This should only occur when a disabled property, lemma, top-level guarantee, or a non-top-level assumption is false.
for (final SimulationProperty simulationProp : program.getSimulationProperties()) {
if (!disabledProperties.contains(simulationProp)) {
for (final String propLustreId : simulationProp.getLustreIds()) {
final Signal<BooleanValue> signal = counterexample.getBooleanSignal(propLustreId);
if (signal == null) {
throw new AGREESimulatorException("Unable to get signal for guarantee property: " + propLustreId, constrainedLustreProgram);
} else {
if (!signal.getValue(0).value) {
newState = SimulationState.WARNING_PROPERTY_NOT_SATISFIED;
break;
}
}
}
}
}
return new SimulationFrameResults(constrainedLustreProgram, counterexample, disabledProperties, newState);
} else if (property instanceof UnknownProperty) {
return new SimulationFrameResults(constrainedLustreProgram, assertions, disabledProperties, SimulationState.HALTED_UNABLE_TO_SATISFY_CONSTRAINTS);
} else if (property instanceof ValidProperty) {
return new SimulationFrameResults(constrainedLustreProgram, assertions, disabledProperties, ((ValidProperty) property).getIvc(), SimulationState.HALTED_UNABLE_TO_SATISFY_CONSTRAINTS);
} else {
throw new AGREESimulatorException("Unhandled case. Property is of type: " + property.getClass(), constrainedLustreProgram);
}
} catch (JKindException ex) {
if (ex.getCause() instanceof InterruptedException) {
throw (InterruptedException) ex.getCause();
}
throw new AGREESimulatorException(constrainedLustreProgram, ex, result.getText());
}
}
Aggregations