/** * Computes and results the line count for a file revision using its storage. Uses a line number * reader to compute the line count. * * @param fr file revision * @param monitor progress monitor for storage access * @return line count or zero, exceptions caught and reported to log */ protected int getLineCount(IFileRevision fr, IProgressMonitor monitor) { int lineCount = 0; try { IStorage storage = fr.getStorage(monitor); InputStream is = storage.getContents(); LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(is)); while (lineNumberReader.ready()) { lineNumberReader.readLine(); lineCount++; } lineNumberReader.close(); is.close(); } catch (Exception e) { CertWareLog.logError( String.format("%s %s", "Gathering line count for revision", fr.getName()), e); } return lineCount; }
/** * Computes and results the byte count for a file revision using its storage. Uses an input stream * reader to count bytes read. * * @param fr file revision * @param monitor progress monitor for storage access * @return line count or zero, exceptions caught and reported to log */ protected int getByteCount(IFileRevision fr, IProgressMonitor monitor) { int byteCount = 0; try { IStorage storage = fr.getStorage(monitor); InputStream is = storage.getContents(); InputStreamReader isr = new InputStreamReader(is); while (isr.read() != -1) { byteCount++; } isr.close(); is.close(); } catch (Exception e) { CertWareLog.logError( String.format("%s %s", "Gathering byte count for revision", fr.getName()), e); } return byteCount; }
/** * Process the EGit history associated with a given project. * * @param selectedProject selected project, presumably an object contribution selection * @throws CoreException * @throws IOException */ public void processHistory(IProject selectedProject, IProgressMonitor monitor) throws CoreException, IOException { // find the repository mapping for the project // if none found, return RepositoryMapping repositoryMapping = RepositoryMapping.getMapping((IResource) selectedProject); if (repositoryMapping == null) { CertWareLog.logWarning( String.format("%s %s", "Missing repository for project", selectedProject.getName())); return; } // build the commit history model, load it from the tree walk final CommitHistory commitHistory = ScoFactory.eINSTANCE.createCommitHistory(); Repository repo = repositoryMapping.getRepository(); RevWalk revWalk = new RevWalk(repo); ObjectId headObject = repo.resolve("HEAD"); revWalk.markStart(revWalk.parseCommit(headObject)); final Set<String> repositoryPaths = Collections.singleton(repositoryMapping.getRepoRelativePath(selectedProject)); revWalk.setTreeFilter(PathFilterGroup.createFromStrings(repositoryPaths)); for (RevCommit commit : revWalk) { String commitName = commit.getName(); ArtifactCommit artifactCommit = ScoFactory.eINSTANCE.createArtifactCommit(); artifactCommit.setCommitIdentifier(commitName); commitHistory.getCommitRecord().add(artifactCommit); } revWalk.dispose(); // use the Git provider to find the file history, then converge into the model GitProvider provider = (GitProvider) RepositoryProvider.getProvider(selectedProject); IFileHistoryProvider fileHistoryProvider = provider.getFileHistoryProvider(); IResource[] projectMembers = selectedProject.members(); monitor.beginTask("Processing project resources", projectMembers.length); for (IResource resource : projectMembers) { processResource(resource, fileHistoryProvider, commitHistory, monitor); monitor.worked(1); if (monitor.isCanceled()) { return; } } // model complete with commit history and associated file sizes // write the resulting model to an SCO file // expecting preference to have no extension, so add it if necessary IPreferenceStore store = Activator.getDefault().getPreferenceStore(); String fileName = store.getString(PreferenceConstants.P_FILENAME_SCO); if (fileName.endsWith(ICertWareConstants.SCO_EXTENSION) == false) { fileName = fileName + '.' + ICertWareConstants.SCO_EXTENSION; } // fully specify the path to the new file given the container project final String modelFile = selectedProject.getFullPath().toPortableString() + IPath.SEPARATOR + fileName; // create the resource in a workspace modify operation WorkspaceModifyOperation operation = new WorkspaceModifyOperation() { @Override protected void execute(IProgressMonitor progressMonitor) { try { // create a resource set and resource for a new file ResourceSet resourceSet = new ResourceSetImpl(); URI fileURI = URI.createPlatformResourceURI(modelFile, true); Resource resource = resourceSet.createResource(fileURI); resource.getContents().add(commitHistory); // save the contents of the resource to the file system Map<Object, Object> options = new HashMap<Object, Object>(); options.put(XMLResource.OPTION_ENCODING, FILE_ENCODING); resource.save(options); } catch (Exception e) { CertWareLog.logError(String.format("%s %s", "Saving SCO file", modelFile), e); } } }; // modify the workspace try { operation.run(monitor); } catch (Exception e) { CertWareLog.logError( String.format("%s %s", "Modifying workspace for", selectedProject.getName()), e); } monitor.done(); }
/** * Production method for computations. * * @param monitor progress monitor * @return CANCEL or OK */ @SuppressWarnings({"unchecked", "rawtypes"}) public IStatus produce(IProgressMonitor monitor) { /** network name for messages */ String networkName = "undefined"; // $NON-NLS-1$ try { if (initialNetwork == null) { cancel(); CertWareLog.logWarning("Network not defined for calculation"); view.setWarningMessage(CANCEL_MSG); return Status.CANCEL_STATUS; } if (nodes.isEmpty()) { cancel(); CertWareLog.logWarning("No variable selections available for Pr(e) calculation"); view.setWarningMessage(CANCEL_MSG); return Status.CANCEL_STATUS; } // network name from loaded file networkName = view.getSelectedFile().getName(); // initialize variables from selections Set setPreVariables = new HashSet(); for (VariableNode v : nodes) { if (v.isSelected()) { setPreVariables.add(v.getNode()); } } // initialize evidence from selections HashMap evidence = new HashMap(); for (VariableNode vn : nodes) { for (VariableNodeState vns : vn.states) { if (vns.isSelected()) { FiniteVariable fv = vn.getNode(); evidence.put(fv, fv.instance(vns.getStateName())); } } } // instantiation formatter VariableImpl.setStringifier(AbstractStringifier.VARIABLE_ID); monitor.worked(1); if (monitor.isCanceled()) { CertWareLog.logWarning(CANCEL_MSG); view.setWarningMessage(CANCEL_MSG); return Status.CANCEL_STATUS; } /* // create a new set of variables and remove those whose evidence have set keys Set allVarsMinusEvidence = new HashSet( initialNetwork ); allVarsMinusEvidence.removeAll( evidence.keySet() ); // initialize a MapEngine and perform the MPE computation MapEngine mpe = new MapEngine( initialNetwork, allVarsMinusEvidence, evidence ); VariableImpl.setStringifier( AbstractStringifier.VARIABLE_ID ); */ // create the dynamator JEngineGenerator dynamator = new JEngineGenerator(); // edit settings JoinTreeSettings settings = dynamator.getSettings((PropertySuperintendent) initialNetwork, true); // define the elimination order heuristic used to create the join tree settings.setEliminationHeuristic(EliminationHeuristic.MIN_FILL); // create the inference engine InferenceEngine engine = dynamator.manufactureInferenceEngine(initialNetwork); // set the observations try { initialNetwork.getEvidenceController().setObservations(evidence); } catch (StateNotFoundException e) { CertWareLog.logError("Performing Pr(e) query", e); view.setErrorMessage(e.getMessage()); return null; } ; // results double score = engine.probability(); // Map instantiation = engine.getInstance(); monitor.worked(2); if (monitor.isCanceled()) { CertWareLog.logWarning(CANCEL_MSG); return Status.CANCEL_STATUS; } // reset marginals // clearMarginals(); setMarginals(engine); // update results in view LinkedHashMap<String, String> rows = new LinkedHashMap<String, String>(); rows.put("Pr(e)", getProbability(score)); view.addResult(rows); monitor.worked(1); } catch (Exception e) { CertWareLog.logError(String.format("%s %s", "Pr(e) query for", networkName), e); return Status.CANCEL_STATUS; } String doneMessage = String.format("%s %s %s", "Pr(e) query for", networkName, "complete."); CertWareLog.logInfo(doneMessage); return Status.OK_STATUS; }