Skip to content

Commit

Permalink
Clear CacheResourceSet and use timestamp to check new generated content
Browse files Browse the repository at this point in the history
- Clear *CacheResourceSet* so every change to a model file can start with a new resource.
Otherwise, every new EMF model created always uses the *DefaultXMIResource* which holds the previous state when the EMF model has not been disposedclosed.
  • Loading branch information
Alfa Yohannis committed Nov 16, 2022
1 parent 211af50 commit 8c688e3
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 468 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,19 @@ public void setPictoJsonController(PictoJsonController pictoJsonController) {
}

public static void startWatching() {
if (FILE_WATCHER == null || !FILE_WATCHER.isAlive())
if (FILE_WATCHER == null)
FILE_WATCHER = new FileWatcher();
FILE_WATCHER.start();
}

public static void setResponseController(PictoJsonController pictoJsonController) {
if (FILE_WATCHER == null || !FILE_WATCHER.isAlive())
FILE_WATCHER = new FileWatcher();
if (FILE_WATCHER == null)
FILE_WATCHER = new FileWatcher();
FILE_WATCHER.setPictoJsonController(pictoJsonController);
}

public static void startWatching(PictoJsonController pictoJsonController) {
if (FILE_WATCHER == null || !FILE_WATCHER.isAlive())
if (FILE_WATCHER == null)
FILE_WATCHER = new FileWatcher(pictoJsonController);
FILE_WATCHER.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.epsilon.picto.dom.PictoFactory;
import org.eclipse.epsilon.picto.dom.PictoPackage;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -26,7 +29,6 @@
*
*/
@SpringBootApplication
@Component
public class PictoApplication implements ApplicationListener<ApplicationContextEvent> {

/***
Expand All @@ -41,7 +43,7 @@ public class PictoApplication implements ApplicationListener<ApplicationContextE

/***
* To keep the arguments accessible.
*/
*/
public static String[] args;

private static List<PictoProject> pictoProjects = new ArrayList<PictoProject>();
Expand All @@ -54,6 +56,9 @@ public void onLoaded() {
}
};

private static PictoFactory pictoFactory;
private static PictoPackage pictoPackage;

/***
* Initialise Picto Application
*
Expand All @@ -74,9 +79,12 @@ public PictoApplication() throws Exception {
*/
public static void main(String[] args) throws Exception {
PictoApplication.args = args;
context = SpringApplication.run(PictoApplication.class, args);
pictoFactory = PictoFactory.eINSTANCE;
pictoPackage = PictoPackage.eINSTANCE;
FileWatcher.scanPictoFiles();
FileWatcher.startWatching();
context = SpringApplication.run(PictoApplication.class, args);
pictoWebOnLoadedListener.onLoaded();
}

/**
Expand Down Expand Up @@ -105,7 +113,6 @@ public void onApplicationEvent(ApplicationContextEvent event) {
if (event instanceof ContextStartedEvent) {
System.out.println("PICTO: started - " + Timestamp.from(Instant.now()).toString());
} else if (event instanceof ContextRefreshedEvent) {
pictoWebOnLoadedListener.onLoaded();
System.out.println("PICTO: loaded - " + Timestamp.from(Instant.now()).toString());
} else if (event instanceof ContextStoppedEvent) {
System.out.println("PICTO: stopped - " + Timestamp.from(Instant.now()).toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String getPictoFiles(String information, Model model) throws IOException
}

@GetMapping(value = "/picto")
public String getPicto(String file, String path, String name, String hash, Model model) throws Exception {
public String getPicto(String file, String path, String name, String timestamp, Model model) throws Exception {
model.addAttribute("pictoName", file);

if (FileViewContentCache.getViewContentCache(file) == null) {
Expand All @@ -49,18 +49,18 @@ public String getPicto(String file, String path, String name, String hash, Model
if (path == null) {
PromiseView treePromiseView = FileViewContentCache.getViewContentCache(file)
.getPromiseView(FileViewContentCache.PICTO_TREE);
String treeResult = (hash == null) ? treePromiseView.getViewContent() : treePromiseView.getViewContent(hash);
String treeResult = (timestamp == null) ? treePromiseView.getViewContent() : treePromiseView.getViewContent(timestamp);
model.addAttribute("treeResponse", treeResult);
} else {
PromiseView treePromiseView = FileViewContentCache.getViewContentCache(file)
.getPromiseView(FileViewContentCache.PICTO_TREE);
String treeResult = (hash == null) ? treePromiseView.getViewContent() : treePromiseView.getViewContent(hash);
String treeResult = (timestamp == null) ? treePromiseView.getViewContent() : treePromiseView.getViewContent(timestamp);
model.addAttribute("treeResponse", treeResult);

String viewResult = null;
PromiseView promiseView = FileViewContentCache.getViewContentCache(file).getPromiseView(path);
if (promiseView != null) {
viewResult = promiseView.getViewContent(hash);
viewResult = promiseView.getViewContent(timestamp);
model.addAttribute("viewResponse", viewResult);
}
model.addAttribute("selectedUri", path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public PictoJsonController() {
}

@GetMapping(path = "/picto", produces = MediaType.APPLICATION_JSON_VALUE)
public String getPictoJson(String file, String path, String name, String hash, Model model) throws Exception {
public String getPictoJson(String file, String path, String name, String timestamp, Model model) throws Exception {

if (FileViewContentCache.getViewContentCache(file) == null) {
File modifiedFile = new File(new File(PictoApplication.WORKSPACE + file).getAbsolutePath());
Expand All @@ -63,7 +63,7 @@ public String getPictoJson(String file, String path, String name, String hash, M
if (promiseViewCache != null) {
PromiseView promiseView = promiseViewCache.getPromiseView(path);
if (promiseView != null)
result = promiseView.getViewContent(hash);
result = promiseView.getViewContent(timestamp);
}
return result;
}
Expand All @@ -84,8 +84,8 @@ public void sendChangesToBroker(File modifiedFile) throws Exception {
.replace(new File(PictoApplication.WORKSPACE).getAbsolutePath(), "").replace("\\", "/");

WebEglPictoSource source = new WebEglPictoSource();
// Map<String, String> modifiedObjects = source.transform(modifiedFilePath);
Map<String, String> modifiedObjects = source.generatePromises(modifiedFilePath, pictoProject);

Map<String, String> modifiedObjects = source.generatePromises(modifiedFilePath, pictoProject, true);
System.out.println("PICTO: number of modified objects = " + modifiedObjects.size());

String pictoFilePath = pictoProject.getPictoFile().getAbsolutePath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
Expand All @@ -15,16 +14,14 @@
import org.eclipse.epsilon.picto.dom.Model;
import org.eclipse.epsilon.picto.dom.Parameter;
import org.eclipse.epsilon.picto.dom.Picto;
import org.eclipse.epsilon.picto.dom.PictoPackage;

public class PictoProject {
private String name;
private File pictoFile;
private Set<File> files = new HashSet<File>();
private Set<File> modelFiles = new HashSet<File>();
private Set<File> metamodelFiles = new HashSet<File>();
private EClass eClass = PictoPackage.eINSTANCE.eClass();


public PictoProject(File pictoFile) {
this.setName(pictoFile.getName());
this.pictoFile = pictoFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.eclipse.epsilon.picto.web;

import java.sql.Timestamp;
import java.time.Instant;

public class PictoResponse {

private String hash;
private String timestamp = Timestamp.from(Instant.now()).toString();
private String timestamp;
private String filename;
private String path;
private String type;
Expand All @@ -15,6 +12,10 @@ public class PictoResponse {
public PictoResponse() {
}

public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}

public String getTimestamp() {
return timestamp;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,106 @@
package org.eclipse.epsilon.picto.web;

import org.eclipse.epsilon.picto.ContentPromise;
import java.sql.Timestamp;
import java.time.Instant;

import org.eclipse.epsilon.eol.models.IModel;
import org.eclipse.epsilon.picto.PictoView;
import org.eclipse.epsilon.picto.StaticContentPromise;
import org.eclipse.epsilon.picto.ViewContent;
import org.eclipse.epsilon.picto.ViewTree;
import org.eclipse.epsilon.picto.incrementality.IncrementalLazyEgxModule;
import org.eclipse.epsilon.picto.incrementality.IncrementalLazyEgxModule.IncrementalLazyGenerationRuleContentPromise;
import org.eclipse.epsilon.picto.incrementality.Util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class PromiseView {

private PictoView pictoView;
private String timestamp = Timestamp.from(Instant.now()).toString();
private boolean generate = true;
private String path;
private String filename;
private String viewHash = "";
private ContentPromise promise;
private ViewTree viewTree;
private String viewContent; // this is the serialised picto response
private String emptyViewContent;

public PromiseView(PictoResponse pictoResponse) throws JsonProcessingException {
this(pictoResponse.getPath(), pictoResponse);
}

public PromiseView(String path, PictoResponse pictoResponse) throws JsonProcessingException {
pictoResponse.setTimestamp(this.timestamp);
this.path = path;
this.viewHash = pictoResponse.getHash();
this.filename = pictoResponse.getFilename();
this.viewContent = (new ObjectMapper()).writerWithDefaultPrettyPrinter().writeValueAsString(pictoResponse);
this.viewHash = Util.getHash(pictoResponse.getContent());
}

public PromiseView(PictoView pictoView, ViewTree viewTree) {
this.pictoView = pictoView;
this.viewTree = viewTree;
this.path = viewTree.getPathString();
this.promise = viewTree.getPromise();
this.viewHash = Util.getHash(viewTree.getContent().getText());

}

public String getViewHash() {
return viewHash;
}

public String getViewContent() throws Exception {
return this.viewContent;
}

public String getViewContent(String requestedHash) throws Exception {
if (viewContent == null || !viewHash.equals(requestedHash)) {
public ViewTree getViewTree() {
return this.viewTree;
}

ViewContent vc = null;

if (path.equals(FileViewContentCache.PICTO_TREE)) {
return viewContent;
} //
else if (promise instanceof StaticContentPromise) {
public String getViewContent(String clientTimestamp) throws Exception {

if (path.equals(FileViewContentCache.PICTO_TREE)) {
return viewContent;
}

ViewContent vc = null;
if (generate) {

if (viewTree.getPromise() instanceof StaticContentPromise) {

pictoView.renderView(viewTree);
vc = this.viewTree.getContent().getFinal(pictoView);
} //
else if (promise instanceof IncrementalLazyGenerationRuleContentPromise) {
else if (viewTree.getPromise() instanceof IncrementalLazyGenerationRuleContentPromise) {

IncrementalLazyEgxModule module = (IncrementalLazyEgxModule) ((IncrementalLazyGenerationRuleContentPromise) promise)
IncrementalLazyEgxModule module = (IncrementalLazyEgxModule) ((IncrementalLazyGenerationRuleContentPromise) viewTree
.getPromise())
.getGenerationRule().getModule();

module.startRecording();
pictoView.renderView(viewTree);
vc = viewTree.getContent().getFinal(pictoView);
module.stopRecording();

// for (IModel model : module.getContext().getModelRepository().getModels()) {
// model.close();
// }
}

PictoResponse pictoResponse = new PictoResponse();
pictoResponse.setHash(this.viewHash);
pictoResponse.setFilename(filename);
pictoResponse.setPath(this.path);
pictoResponse.setTimestamp(this.timestamp);
emptyViewContent = new ObjectMapper().writerWithDefaultPrettyPrinter()
.writeValueAsString(pictoResponse);

pictoResponse = new PictoResponse();
pictoResponse.setFilename(filename);
pictoResponse.setPath(this.path);
pictoResponse.setTimestamp(this.timestamp);
pictoResponse.setType(vc.getFormat());
pictoResponse.setContent(vc.getText());
viewContent = new ObjectMapper().writerWithDefaultPrettyPrinter()
.writeValueAsString(pictoResponse);

this.generate = false;
}

if (clientTimestamp != null && timestamp.compareTo(clientTimestamp) <= 0) {
return emptyViewContent;
}

return viewContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.epsilon.picto.incrementality.IncrementalLazyEgxModule.IncrementalLazyGenerationRuleContentPromise;

public class PromiseViewCache {

// map 'path' --> 'view cache'
private final Map<String, PromiseView> promiseViewMap = new HashMap<String, PromiseView>();


public Map<String, PromiseView> getMap() {
return promiseViewMap;
}

public void clear() {
promiseViewMap.clear();
}

public void putPromiseView(PromiseView promiseView) {
promiseViewMap.put(promiseView.getPath(), promiseView);
}

public void putPromiseView(String path, PromiseView promiseView) {
promiseViewMap.put(path, promiseView);
}
Expand Down
Loading

0 comments on commit 8c688e3

Please sign in to comment.