Skip to content
This repository was archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
Implemented ChangeProcessor methods, so the files and the changes cou…
Browse files Browse the repository at this point in the history
…ld be processed one-by-one.
  • Loading branch information
steindani committed Oct 3, 2014
1 parent 0376c1f commit b811376
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 12 deletions.
24 changes: 18 additions & 6 deletions src/main/java/hu/bme/mit/codemodel/jamoppdiscoverer/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package hu.bme.mit.codemodel.jamoppdiscoverer;

import hu.bme.mit.codemodel.jamoppdiscoverer.utils.DependencyCollector;
import hu.bme.mit.codemodel.jamoppdiscoverer.utils.FileDiscoverer;
import hu.bme.mit.codemodel.jamoppdiscoverer.utils.FileIterator;
import hu.bme.mit.codemodel.jamoppdiscoverer.utils.*;

import java.io.File;

Expand All @@ -11,10 +9,24 @@ public class Main {
public static void main(String[] args) {

File f = new File("toprocess/");
FileIterator.iterate(f, new InitializationIterator());
// File dm = new File("toprocess/src/view/graphic/Main.java");
File dm = new File("toprocess/");
// File dm = new File("toprocess/");
//
// FileIterator.iterate(f, new DependencyCollector());
// FileIterator.iterate(dm, new FileDiscoverer());

// ChangeProcessor.addedNewFile(new File("toprocess/src/view/graphic/Main.java").getAbsolutePath());
// ChangeProcessor.modifiedFile(new File("toprocess/src/view/graphic/Main.java").getAbsolutePath());
// ChangeProcessor.deletedFile(new File("toprocess/src/view/graphic/Main.java").getAbsolutePath());

System.out.println("\n\n");

FileDiscoverer fileDiscoverer = new FileDiscoverer();

for (String file : ChangeProcessor.getFilesToProcess()) {
fileDiscoverer.execute(new File(file));
}

FileIterator.iterate(f, new DependencyCollector());
FileIterator.iterate(dm, new FileDiscoverer());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package hu.bme.mit.codemodel.jamoppdiscoverer.utils;

import hu.bme.mit.codemodel.jamoppdiscoverer.whitepages.pojo.Dependency;
import hu.bme.mit.codemodel.jamoppdiscoverer.whitepages.utils.DependencyManager;

import java.io.File;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ChangeProcessor {

/**
* The set of files to be processed in the second iteration. Beside every modification, the relevant files also need
* to be reprocessed. This includes every file, that depends on the modified ones and every one in the same package.
*/
protected static Set<String> filesToProcess = new HashSet<>();

protected static DependencyCollector dependencyCollector = new DependencyCollector();

// -----------------------------------------------------------------------------------------------------------------

/**
* Process a newly added file.
*
* When a new file is added, the dependency information should be saved in the database. The dependencies already in
* the system had been processed processed, and the new ones will be processed in this iteration.
*
* The only file to be processed is the one added.
*
* @param filePath
* The path of the newly added file.
*/
public static void addedNewFile(String filePath) {
dependencyCollector.execute(new File(filePath));
filesToProcess.add(filePath);

System.out.println("Added a new file: " + RelativePath.of(filePath));
}

/**
* Process a deleted file.
*
* When a file is deleted, its dependency information should be deleted, and every file, that depends on it should
* be reprocessed.
*
* @param filePath
* The path of the deleted file.
*/
public static void deletedFile(String filePath) {
DependencyManager dm = null;
try {
dm = DependencyManager.getInstance();
Dependency dependency = dm.find(RelativePath.of(filePath));

if (dependency == null) {
System.out.println("File is not in the database.\t" + RelativePath.of(filePath));
return;
}

Set<String> usedBy = dependency.getUsedBy();
filesToProcess.addAll(usedBy);
dm.removeDescriptor(dependency);

// process the files in the same package
for (Dependency dInPackage : dm.findAll(PackageName.of(dependency.getFQN()))) {
filesToProcess.add(dInPackage.getRelativeLocation());
}

System.out.println("Deleted the file: " + RelativePath.of(filePath));
System.out.println("\tNeeds to be reprocessed:\t" + Arrays.toString(usedBy.toArray()));

} catch (UnknownHostException e) {
e.printStackTrace();
}
}

/**
* Process a modified file.
*
* When a file is modified, all of its dependencies should be deleted and reprocessed. Every file, that depends on
* it should be also reprocessed.
*
* @param filePath
* The path of the modified file.
*/
public static void modifiedFile(String filePath) {
deletedFile(filePath); // marks the files depending on the file to be reprocessed
addedNewFile(filePath); // updates the dependencies of the file
}

// -----------------------------------------------------------------------------------------------------------------

public static Set<String> getFilesToProcess() {
return filesToProcess;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DependencyCollector implements FileIterator.Function {
public DependencyCollector() {
PACKAGE_PATTERN = Pattern.compile("^package (.*?);");
IMPORT_PATTERN = Pattern.compile("^\\s?(static)?\\s?import\\s(.*?);");
CLASSIFIER_PATTERN = Pattern.compile("^(.*?)(interface|class)\\s(.*?)\\s(.*)$");
CLASSIFIER_PATTERN = Pattern.compile("^(.*?)(interface|class|enum)\\s(.*?)\\s(.*)$");

try {
dependencyManager = DependencyManager.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void execute(File file) {

resolveDependencies(resourceSet, cp, relativeFilePath);

resolveTargetReferences(resourceSet, target);
resolveTargetReferences(resourceSet, target);

// ------------------------------------------------------------------------------------------------------ Export
Expand Down Expand Up @@ -113,6 +114,14 @@ protected void resolveDependencies(ResourceSet resourceSet, JavaClasspath cp, St
dependencies = new HashSet<>();
}

for (String dependency : dependencies) {
if (dependency.contains("*")) {
for(Dependency dInPackage : dependencyManager.findAll(PackageName.of(dependency))) {
dependencies.add(dInPackage.getFQN());
}
}
}

System.out.println("\t\t[INFO]\tDependencies: " + Arrays.toString(dependencies.toArray()));

for (String dependencyFQN : dependencies) {
Expand Down Expand Up @@ -167,15 +176,15 @@ protected void resolveTargetReferences(ResourceSet resourceSet, Resource target)

protected String exportTTL(Resource target) {
String outputPath = target.getURI().toString().replace("toprocess", "export").replace("file:", "");

try {
File outputFile = new File(outputPath + ".nt");
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}

try (PrintWriter w = new PrintWriter(outputPath + ".nt", "UTF-8")) {

w.println("@prefix java: <https://www.java.com/> .\n" +
Expand Down Expand Up @@ -292,7 +301,7 @@ protected void exportXMIResource(ResourceSet resourceSet, Resource target) {
} catch (IOException e1) {
e1.printStackTrace();
}

Resource xmi = resourceSet.createResource(URI.createFileURI(outputPath + ".xmi"));
for (EObject o : target.getContents()) {
xmi.getContents().add(EcoreUtil.copy(o));
Expand All @@ -306,15 +315,15 @@ protected void exportXMIResource(ResourceSet resourceSet, Resource target) {

protected void convertTTL(Resource target) {
String outputPath = target.getURI().toString().replace("toprocess", "export").replace("file:", "");

try {
File outputFile = new File(outputPath + ".nt");
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}

try {
File input = new File(outputPath + ".nt");
File output = new File(outputPath + ".ttl");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package hu.bme.mit.codemodel.jamoppdiscoverer.utils;

import java.io.File;

public class InitializationIterator implements FileIterator.Function {
@Override
public void execute(File file) {
ChangeProcessor.addedNewFile(file.getAbsolutePath());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package hu.bme.mit.codemodel.jamoppdiscoverer.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PackageName {

protected static final Pattern PACKAGENAME_PATTERN = Pattern.compile("^(.*)\\..*?$");

public static String of(String FQN) {
Matcher matcher = PACKAGENAME_PATTERN.matcher(FQN);
if (matcher.matches()) {
return matcher.group(1);
}

return FQN;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package hu.bme.mit.codemodel.jamoppdiscoverer.utils;

import java.io.File;

public class RelativePath {

protected static String root = new File("").getAbsolutePath() + "/";

public static String of(String absolutePath) {
return absolutePath.replace(root, "");
}

public static String rev(String relativePath) {return root + relativePath; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ public void removeDependency(Dependency d, String doesNotDependOnFQN) {
dependencies.save(d);
}

public void removeDescriptor(Dependency d) {
for (String depOn : new HashSet<>(d.getDependsOn())) {
removeDependency(d, depOn);
}
dependencies.remove("{ \"FQN\": # }", d.getFQN());
}

// -----------------------------------------------------------------------------------------------------------------

protected void addUsage(String FQN, String usedByFQN) {
Expand Down

0 comments on commit b811376

Please sign in to comment.