Skip to content

Commit

Permalink
Merge pull request #263 from ponder-lab/31-create-evaluation-plug-in2
Browse files Browse the repository at this point in the history
Create evaluation plug-in
  • Loading branch information
khatchad authored Oct 6, 2023
2 parents cad93d4 + 2282cc4 commit 15fc4a6
Show file tree
Hide file tree
Showing 24 changed files with 702 additions and 166 deletions.
8 changes: 7 additions & 1 deletion edu.cuny.hunter.hybridize.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Require-Bundle: org.eclipse.ltk.core.refactoring;bundle-version="3.12.200",
org.python.pydev.ast;bundle-version="[9.3.2,10.0.0)",
com.python.pydev.refactoring;bundle-version="[9.3.2,10.0.0)",
com.python.pydev.analysis;bundle-version="[9.3.2,10.0.0)",
org.eclipse.core.resources;bundle-version="3.18.0"
org.eclipse.core.resources;bundle-version="3.18.0",
org.eclipse.core.runtime;bundle-version="3.26.0"
Import-Package: com.ibm.wala.cast.ipa.callgraph,
com.ibm.wala.cast.ir.ssa,
com.ibm.wala.cast.loader,
Expand All @@ -39,10 +40,15 @@ Import-Package: com.ibm.wala.cast.ipa.callgraph,
com.ibm.wala.types,
com.ibm.wala.util,
com.ibm.wala.util.collections,
edu.cuny.hunter.hybridize.core.utils,
org.eclipse.core.commands,
org.eclipse.core.runtime;version="3.7.0",
org.eclipse.jface.text,
org.python.pydev.ast.refactoring,
org.python.pydev.core,
org.python.pydev.navigator,
org.python.pydev.navigator.elements,
org.python.pydev.outline,
org.python.pydev.parser.jython.ast,
org.python.pydev.shared_core.model
Export-Package: edu.cuny.hunter.hybridize.core.analysis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.eclipse.core.runtime.Platform.getLog;

import java.io.File;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -36,6 +37,7 @@
import com.ibm.wala.util.collections.Pair;

import edu.cuny.citytech.refactoring.common.core.RefactorableProgramEntity;
import edu.cuny.hunter.hybridize.core.utils.RefactoringAvailabilityTester;

/**
* A representation of a Python function.
Expand Down Expand Up @@ -286,22 +288,19 @@ public boolean getReduceRetracingParamExists() {
/**
* TODO: Populate.
*/
@SuppressWarnings("unused")
private Set<Transformation> transformationSet;
private Set<Transformation> transformations = new HashSet<>();

// private InstanceKey instanceKey;

/**
* TODO: Populate.
*/
@SuppressWarnings("unused")
private PreconditionSuccess passingPrecondition;

/**
* The refactoring that this {@link Function} qualifies for. There should be only one as the refactorings are mutually exclusive. TODO:
* Populate.
*/
@SuppressWarnings("unused")
private Refactoring refactoring;

private RefactoringStatus status = new RefactoringStatus();
Expand Down Expand Up @@ -695,4 +694,20 @@ public RefactoringStatus getStatus() {
public IProject getProject() {
return this.getFunctionDefinition().getProject();
}

public boolean isHybridizationAvailable() {
return RefactoringAvailabilityTester.isHybridizationAvailable(this.getFunctionDefinition().getFunctionDef());
}

public Set<Transformation> getTransformations() {
return transformations;
}

public PreconditionSuccess getPassingPrecondition() {
return passingPrecondition;
}

public Refactoring getRefactoring() {
return refactoring;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ public Set<Function> getFunctions() {
return this.functions;
}

public Set<Function> getOptimizableFunctions() {
return this.getFunctions().parallelStream().filter(f -> !f.getStatus().hasError()).collect(Collectors.toSet());
}

@Override
public String getIdentifier() {
return HybridizeFunctionRefactoringDescriptor.REFACTORING_ID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,161 @@
package edu.cuny.hunter.hybridize.core.utils;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.core.runtime.Platform.getLog;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
import org.python.pydev.ast.refactoring.TooManyMatchesException;
import org.python.pydev.core.IPythonNature;
import org.python.pydev.navigator.PythonModelProvider;
import org.python.pydev.navigator.elements.PythonFile;
import org.python.pydev.navigator.elements.PythonNode;
import org.python.pydev.navigator.elements.PythonSourceFolder;
import org.python.pydev.outline.ParsedItem;
import org.python.pydev.parser.jython.SimpleNode;
import org.python.pydev.parser.jython.ast.FunctionDef;
import org.python.pydev.parser.visitors.scope.ASTEntryWithChildren;
import org.python.pydev.plugin.nature.PythonNature;

import edu.cuny.hunter.hybridize.core.analysis.FunctionDefinition;
import edu.cuny.hunter.hybridize.core.analysis.FunctionExtractor;
import edu.cuny.hunter.hybridize.core.refactorings.HybridizeFunctionRefactoringProcessor;

public class Util {

private static final ILog LOG = getLog(Util.class);

private static final PythonModelProvider provider = new PythonModelProvider();

public static Refactoring createRefactoring(Set<FunctionDefinition> functionDefinitions) throws TooManyMatchesException {
HybridizeFunctionRefactoringProcessor processor = new HybridizeFunctionRefactoringProcessor(functionDefinitions);
return new ProcessorBasedRefactoring(processor);
return new ProcessorBasedRefactoring(new HybridizeFunctionRefactoringProcessor(functionDefinitions));
}

public static HybridizeFunctionRefactoringProcessor createHybridizeFunctionRefactoring(IProject[] projects)
throws ExecutionException, CoreException, IOException {
Set<FunctionDefinition> functionDefinitions = getFunctionDefinitions(Arrays.asList(projects));
return new HybridizeFunctionRefactoringProcessor(functionDefinitions);
}

public static Set<FunctionDefinition> getFunctionDefinitions(Iterable<?> iterable)
throws ExecutionException, CoreException, IOException {
Set<FunctionDefinition> ret = new HashSet<>();

for (Object obj : iterable) {
Set<PythonNode> nodeSet = getPythonNodes(obj);

for (PythonNode node : nodeSet)
ret.addAll(process(node));
}

return ret;
}

public static Set<PythonNode> getPythonNodes(Object obj) {
Set<PythonNode> ret = new HashSet<>();

if (obj instanceof PythonNode) {
PythonNode pythonNode = (PythonNode) obj;
ret.add(pythonNode);
} else {
Object[] children = provider.getChildren(obj);
for (Object child : children)
ret.addAll(getPythonNodes(child));
}
return ret;
}

public static Set<FunctionDefinition> process(PythonNode pythonNode) throws ExecutionException, CoreException, IOException {
Set<FunctionDefinition> ret = new HashSet<>();

String moduleName = getModuleName(pythonNode);
File file = getFile(pythonNode);
IDocument document = getDocument(pythonNode);
IPythonNature nature = Util.getPythonNature(pythonNode);

ParsedItem entry = pythonNode.entry;
ASTEntryWithChildren ast = entry.getAstThis();
SimpleNode simpleNode = ast.node;

// extract function definitions.
FunctionExtractor functionExtractor = new FunctionExtractor();
try {
simpleNode.accept(functionExtractor);
} catch (Exception e) {
LOG.error("Failed to start refactoring.", e);
throw new ExecutionException("Failed to start refactoring.", e);
}

Collection<FunctionDef> definitions = functionExtractor.getDefinitions();

for (FunctionDef def : definitions) {
FunctionDefinition function = new FunctionDefinition(def, moduleName, file, document, nature);
ret.add(function);
}

return ret;
}

private static IFile getActualFile(PythonNode pythonNode) {
PythonFile pythonFile = pythonNode.getPythonFile();
return pythonFile.getActualObject();
}

static IDocument getDocument(PythonNode pythonNode) throws CoreException, IOException {
IFile file = getActualFile(pythonNode);

try (InputStream contentStream = file.getContents()) {
byte[] bytes = contentStream.readAllBytes();
String content = new String(bytes, UTF_8);
return new Document(content);
}
}

static File getFile(PythonNode pythonNode) {
IFile file = getActualFile(pythonNode);
URI uri = file.getRawLocationURI();
return new File(uri);
}

static String getFileName(PythonNode pythonNode) {
IFile file = getActualFile(pythonNode);
return file.getName();
}

static String getModuleName(PythonNode pythonNode) {
String fileName = getFileName(pythonNode);
int separatorPos = fileName.indexOf('.');
return fileName.substring(0, separatorPos);
}

static IPythonNature getPythonNature(PythonNode pythonNode) {
IProject project = getProject(pythonNode);
PythonNature pythonNature = PythonNature.getPythonNature(project);
return pythonNature;
}

private static IProject getProject(PythonNode pythonNode) {
PythonFile pythonFile = pythonNode.getPythonFile();
PythonSourceFolder sourceFolder = pythonFile.getSourceFolder();
IResource resource = sourceFolder.getActualObject();
IProject project = resource.getProject();
return project;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class EclipsePythonProjectTensorAnalysisEngine extends PythonTensorAnalys

public EclipsePythonProjectTensorAnalysisEngine(IProject project) {
this.project = project;
IPath projectPath = project.getFullPath();
IPath projectPath = getPath(project);
Module dirModule = new EclipseSourceDirectoryTreeModule(projectPath, null, ".py");
LOG.info("Creating engine from: " + dirModule);

Expand All @@ -62,6 +62,15 @@ public EclipsePythonProjectTensorAnalysisEngine(IProject project) {
}
}

private static IPath getPath(IProject project) {
IPath path = project.getFullPath();

if (!path.toFile().exists())
path = project.getLocation();

return path;
}

public IProject getProject() {
return project;
}
Expand Down
7 changes: 7 additions & 0 deletions edu.cuny.hunter.hybridize.eval/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
34 changes: 34 additions & 0 deletions edu.cuny.hunter.hybridize.eval/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>edu.cuny.hunter.hybridize.eval</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
20 changes: 20 additions & 0 deletions edu.cuny.hunter.hybridize.eval/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: edu.cuny.hunter.hybridize.eval;singleton:=true
Bundle-Vendor: %Bundle-Vendor
Bundle-Version: 1.0.0.qualifier
Import-Package: com.google.common.collect,
org.python.pydev.parser.jython.ast,
org.python.pydev.plugin.nature
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime;bundle-version="3.26.0",
org.eclipse.core.resources;bundle-version="3.18.0",
org.python.pydev;bundle-version="[9.3.2,10.0.0)",
edu.cuny.citytech.refactoring.common.eval;bundle-version="3.3.0",
edu.cuny.citytech.refactoring.common.core;bundle-version="3.3.0",
edu.cuny.hunter.hybridize.core;bundle-version="1.0.0",
org.eclipse.ltk.core.refactoring
Automatic-Module-Name: edu.cuny.hunter.hybridize.eval
Bundle-Activator: edu.cuny.hunter.hybridize.eval.ui.plugins.EvaluateHybridizeFunctionRefactoringPlugin
Bundle-RequiredExecutionEnvironment: JavaSE-11
7 changes: 7 additions & 0 deletions edu.cuny.hunter.hybridize.eval/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source.. = src/
output.. = target/classes/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/,\
bundle.properties
2 changes: 2 additions & 0 deletions edu.cuny.hunter.hybridize.eval/bundle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Bundle-Vendor = City University of New York (CUNY) Hunter College
Bundle-Name = Eclipse Hybridize Functions Refactoring Evaluation Plug-in
Binary file added edu.cuny.hunter.hybridize.eval/icons/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 15fc4a6

Please sign in to comment.