Added RascalManager and made changes so all metrics can be trasient.

git-svn-id: https://svn.cs.york.ac.uk/svn/ossmeter/Software/source@734 1b171192-1bc4-4300-84b6-1b6fde2e8ce3

a.shahi@cwi.nl a.shahi@cwi.nl 2013-11-13

removed metric-providers/org.ossmeter.metricprovider.rascal/schema
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history/model
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history/model/rascalhistory.ecore
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history/model/rascalhistory.emf
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/trans
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/trans/model
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/trans/model/RascalMetric.java
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/trans/model/rascalmetric.ecore
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/trans/model/rascalmetric.emf
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalHistoricMetricProvider.java
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalMetricProviderManager.java
removed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalTransientMetricProvider.java
changed metric-providers/org.ossmeter.metricprovider.rascal/META-INF/MANIFEST.MF
changed metric-providers/org.ossmeter.metricprovider.rascal/modules/LOC.rsc
changed metric-providers/org.ossmeter.metricprovider.rascal/modules/Manager.rsc
changed metric-providers/org.ossmeter.metricprovider.rascal/modules/NOM.rsc
changed metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalMetrics.java
changed metric-providers/org.ossmeter.metricprovider.rascal/plugin.xml
copied metric-providers/org.ossmeter.metricprovider.rascal/schema/org.ossmeter.metricprovider.rascal.exsd -> metric-providers/org.ossmeter.metricprovider.rascal/modules/WMC.rsc
copied metric-providers/org.ossmeter.metricprovider.rascal/schema/org.ossmeter.metricprovider.rascal.metric.exsd -> metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalManager.java
copied metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history/model/HistoricRascalMetrics.java -> metric-providers/org.ossmeter.metricprovider.rascal/modules/NOA.rsc
metric-providers/org.ossmeter.metricprovider.rascal/META-INF/MANIFEST.MF Diff Switch to side-by-side view
Loading...
metric-providers/org.ossmeter.metricprovider.rascal/modules/LOC.rsc Diff Switch to side-by-side view
Loading...
metric-providers/org.ossmeter.metricprovider.rascal/modules/Manager.rsc Diff Switch to side-by-side view
Loading...
metric-providers/org.ossmeter.metricprovider.rascal/modules/NOM.rsc Diff Switch to side-by-side view
Loading...
metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalMetrics.java Diff Switch to side-by-side view
Loading...
metric-providers/org.ossmeter.metricprovider.rascal/plugin.xml Diff Switch to side-by-side view
Loading...
metric-providers/org.ossmeter.metricprovider.rascal/schema/org.ossmeter.metricprovider.rascal.exsd to metric-providers/org.ossmeter.metricprovider.rascal/modules/WMC.rsc
--- a/metric-providers/org.ossmeter.metricprovider.rascal/schema/org.ossmeter.metricprovider.rascal.exsd
+++ b/metric-providers/org.ossmeter.metricprovider.rascal/modules/WMC.rsc
@@ -1,106 +1,59 @@
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- Schema file written by PDE -->
-<schema targetNamespace="org.ossmeter.metricprovider.rascal" xmlns="http://www.w3.org/2001/XMLSchema">
-<annotation>
-      <appinfo>
-         <meta.schema plugin="org.ossmeter.metricprovider.rascal" id="org.ossmeter.metricprovider.rascal" name="org.ossmeter.metricprovider.rascal"/>
-      </appinfo>
-      <documentation>
-         [Enter description of this extension point.]
-      </documentation>
-   </annotation>
+module WMC
 
-   <element name="extension">
-      <annotation>
-         <appinfo>
-            <meta.element />
-         </appinfo>
-      </annotation>
-      <complexType>
-         <sequence>
-            <element ref="rascalMetricProvider" minOccurs="1" maxOccurs="unbounded"/>
-         </sequence>
-         <attribute name="point" type="string" use="required">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-            </annotation>
-         </attribute>
-         <attribute name="id" type="string">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-            </annotation>
-         </attribute>
-         <attribute name="name" type="string">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-               <appinfo>
-                  <meta.attribute translatable="true"/>
-               </appinfo>
-            </annotation>
-         </attribute>
-      </complexType>
-   </element>
+import lang::java::m3::Core;
+import lang::java::m3::AST;
+import IO;
+import Node;
+import List;
+import String;
+import Map;
+import Manager;
 
-   <element name="rascalMetricProvider">
-      <complexType>
-         <attribute name="rascalUri" type="string" use="required">
-            <annotation>
-               <documentation>
-                  The Rascal function. Format is &quot;rascal://ModuleName/functionName&quot;.
-               </documentation>
-            </annotation>
-         </attribute>
-         <attribute name="isHistoric" type="boolean" use="required">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-            </annotation>
-         </attribute>
-      </complexType>
-   </element>
+map[str class, num wmcCount] getWMC(M3 fileM3) {
+  return (replaceAll(replaceFirst(cl.path, "/", ""), "/", ".") : sum([getCC(m, fileM3.ast) | m <- fileM3.model@containment[cl], isMethod(m)]) | <cl,_> <- fileM3.model@declarations, isClass(cl));
+}
 
-   <annotation>
-      <appinfo>
-         <meta.section type="since"/>
-      </appinfo>
-      <documentation>
-         [Enter the first release in which this extension point appears.]
-      </documentation>
-   </annotation>
+map[str class, num wmcCount] getWMC(unknownFileType(int lines)) = ("": -1);
+map[str class, int cc] getCC(unknownFileType(int lines)) = ("" : -1);
 
-   <annotation>
-      <appinfo>
-         <meta.section type="examples"/>
-      </appinfo>
-      <documentation>
-         [Enter extension point usage example here.]
-      </documentation>
-   </annotation>
+map[str class, int cc] getCC(M3 fileM3) {
+  return (replaceAll(replaceFirst(m.path, "/", ""), "/", ".") : getCC(m, fileM3.ast) | <cl,_> <- fileM3.model@declarations, isClass(cl), m <- fileM3.model@containment[cl], isMethod(m));
+}
 
-   <annotation>
-      <appinfo>
-         <meta.section type="apiinfo"/>
-      </appinfo>
-      <documentation>
-         [Enter API information here.]
-      </documentation>
-   </annotation>
+Declaration getMethodAST(loc methodLoc, Declaration fileAST) {
+  visit(fileAST) {
+    case Declaration d: {
+      if ("decl" in getAnnotations(d) && d@decl == methodLoc) {
+        return d;
+      }
+    }
+  }
+  throw "ast not found for method: <methodLoc>";
+}
 
-   <annotation>
-      <appinfo>
-         <meta.section type="implementation"/>
-      </appinfo>
-      <documentation>
-         [Enter information about supplied implementation of this extension point.]
-      </documentation>
-   </annotation>
+int getCC(loc m, Declaration ast) {
+  int count = 1;
+  Declaration methodAST = getMethodAST(m, ast);
+  
+  visit(methodAST) {
+    case Statement s: {
+      count += getPaths(s);
+    }
+  }
+  return count;
+}
 
+int getPaths(\foreach(Declaration parameter, Expression collection, Statement body)) = 1;
+int getPaths(\for(list[Expression] initializers, Expression condition, list[Expression] updaters, Statement body))  = 1 + getPaths(condition);
+int getPaths(\for(list[Expression] initializers, list[Expression] updaters, Statement body)) = 1;
+int getPaths(\if(Expression condition, Statement thenBranch, Statement elseBranch)) = 1 + getPaths(condition);
+int getPaths(\if(Expression condition, Statement thenBranch)) = 1 + getPaths(condition);
+int getPaths(\case(Expression expression)) = 1 + getPaths(expression);
+int getPaths(\defaultCase()) = 1;
+int getPaths(\while(Expression condition, Statement body)) = 1 + getPaths(condition);
+int getPaths(\do(Statement body, Expression condition)) = 1 + getPaths(condition);
+default int getPaths(Statement s) = 0;
 
-</schema>
+int getPaths(\infix(Expression lhs, "||", Expression rhs, list[Expression] extendedOperands)) = 1 + size(extendedOperands);
+int getPaths(\infix(Expression lhs, "&&", Expression rhs, list[Expression] extendedOperands)) = 1 + size(extendedOperands);
+default int getPaths(Expression e) = 0;
metric-providers/org.ossmeter.metricprovider.rascal/schema/org.ossmeter.metricprovider.rascal.metric.exsd to metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalManager.java
--- a/metric-providers/org.ossmeter.metricprovider.rascal/schema/org.ossmeter.metricprovider.rascal.metric.exsd
+++ b/metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/RascalManager.java
@@ -1,102 +1,116 @@
-<?xml version='1.0' encoding='UTF-8'?>
-<!-- Schema file written by PDE -->
-<schema targetNamespace="org.ossmeter.metricprovider.rascal" xmlns="http://www.w3.org/2001/XMLSchema">
-<annotation>
-      <appinfo>
-         <meta.schema plugin="org.ossmeter.metricprovider.rascal" id="org.ossmeter.metricprovider.rascal.metric" name="metric"/>
-      </appinfo>
-      <documentation>
-         [Enter description of this extension point.]
-      </documentation>
-   </annotation>
+package org.ossmeter.metricprovider.rascal;
 
-   <element name="extension">
-      <annotation>
-         <appinfo>
-            <meta.element />
-         </appinfo>
-      </annotation>
-      <complexType>
-         <sequence>
-            <element ref="metric" minOccurs="1" maxOccurs="unbounded"/>
-         </sequence>
-         <attribute name="point" type="string" use="required">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-            </annotation>
-         </attribute>
-         <attribute name="id" type="string">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-            </annotation>
-         </attribute>
-         <attribute name="name" type="string">
-            <annotation>
-               <documentation>
-                  
-               </documentation>
-               <appinfo>
-                  <meta.attribute translatable="true"/>
-               </appinfo>
-            </annotation>
-         </attribute>
-      </complexType>
-   </element>
+import java.io.PrintWriter;
+import java.util.HashMap;
 
-   <element name="metric">
-      <complexType>
-         <attribute name="provider" type="string" use="required">
-            <annotation>
-               <documentation>
-                  A
-               </documentation>
-               <appinfo>
-                  <meta.attribute kind="java" basedOn="org.ossmeter.metricprovider.rascal.RascalMetrics:"/>
-               </appinfo>
-            </annotation>
-         </attribute>
-      </complexType>
-   </element>
+import org.eclipse.imp.pdb.facts.IBool;
+import org.eclipse.imp.pdb.facts.ISourceLocation;
+import org.eclipse.imp.pdb.facts.IValue;
+import org.eclipse.imp.pdb.facts.IValueFactory;
+import org.rascalmpl.interpreter.Evaluator;
+import org.rascalmpl.interpreter.NullRascalMonitor;
+import org.rascalmpl.interpreter.env.GlobalEnvironment;
+import org.rascalmpl.interpreter.env.ModuleEnvironment;
+import org.rascalmpl.interpreter.load.StandardLibraryContributor;
+import org.rascalmpl.interpreter.load.URIContributor;
+import org.rascalmpl.uri.ClassResourceInput;
+import org.rascalmpl.uri.IURIInputStreamResolver;
+import org.rascalmpl.uri.URIUtil;
+import org.rascalmpl.values.ValueFactoryFactory;
 
-   <annotation>
-      <appinfo>
-         <meta.section type="since"/>
-      </appinfo>
-      <documentation>
-         [Enter the first release in which this extension point appears.]
-      </documentation>
-   </annotation>
+public class RascalManager {
+	private static RascalManager _instance;
+	
+	private final static GlobalEnvironment heap = new GlobalEnvironment();
+	private final static ModuleEnvironment root = new ModuleEnvironment("******metrics******", heap);
+	private final static IValueFactory VF = ValueFactoryFactory.getValueFactory();
+//	private final static OSSMeterURIResolver ossmStore = new OSSMeterURIResolver();
+	private final static Evaluator eval = initEvaluator();
+	
+	private static HashMap<String, ProjectRascalManager> managedProjects = new HashMap<>();
+	
+	private final String module = "Manager";
+	
+	public class ProjectRascalManager {
+		public HashMap<String, HashMap<String, IValue>> fileModelsPerCommit = new HashMap<>();
+		
+		public void checkOutRevision(String revision, String repositoryURL, String localPath) {
+			if (!fileModelsPerCommit.containsKey(revision)) {
+				eval.call(new NullRascalMonitor(), module, "checkOutRepository", VF.string(repositoryURL), VF.integer(revision), VF.sourceLocation(localPath));
+				fileModelsPerCommit.put(revision, new HashMap<String, IValue>());
+			}
+		}
+		
+		public ISourceLocation makeLocation(String fileURL) {
+			return VF.sourceLocation(fileURL);
+		}
+		
+		public IValue getModel(String revision, String fileURL, String localURL) {
+			HashMap<String, IValue> fileModels;
+			IValue result;
+			if (fileModelsPerCommit.containsKey(revision)) {
+				fileModels = fileModelsPerCommit.get(revision);
+				if (fileModels.containsKey(fileURL)) {
+					return fileModels.get(fileURL);
+				}
+			} else {
+				fileModels = new HashMap<>();
+			}
+			
+			result = eval.call(new NullRascalMonitor(), module, "createFileM3", VF.sourceLocation(localURL));
+			fileModels.put(fileURL, result);
+			fileModelsPerCommit.put(revision, fileModels);
+			return result;
+		}
+		
+		public boolean isValidModel(IValue fileM3) {
+			return ((IBool)eval.call(new NullRascalMonitor(), module, "isValid", fileM3)).getValue();
+		}
+		
+		public void clearManagerDataForProject() {
+			fileModelsPerCommit.clear();
+		}
 
-   <annotation>
-      <appinfo>
-         <meta.section type="examples"/>
-      </appinfo>
-      <documentation>
-         [Enter extension point usage example here.]
-      </documentation>
-   </annotation>
+		public IValue callRascal(String module, String function, IValue... parameters) {
+			return eval.call(new NullRascalMonitor(), module, function, parameters);
+		}
+	}
+	
+	private static Evaluator initEvaluator() {
+		Evaluator eval = new Evaluator(VF, new PrintWriter(System.err), new PrintWriter(System.out), root, heap);
+		IURIInputStreamResolver metrics = new ClassResourceInput(eval.getResolverRegistry(), "metrics", RascalManager.class, "");
+		eval.getResolverRegistry().registerInput(metrics);
+		URIContributor moduleContributor = new URIContributor(URIUtil.rootScheme("metrics"));
+		eval.addRascalSearchPathContributor(moduleContributor);
+		eval.addRascalSearchPathContributor(StandardLibraryContributor.getInstance());
+		eval.addRascalSearchPath(URIUtil.assumeCorrect("file:///Users/shahi/Documents/CWI/OSSMeter/Software/OSSMETER/source/metric-providers/org.ossmeter.metricprovider.rascal/modules"));
+		return eval;
+	}
+	
+	private RascalManager() {
+		eval.doImport(new NullRascalMonitor(), module);
+	}
 
-   <annotation>
-      <appinfo>
-         <meta.section type="apiinfo"/>
-      </appinfo>
-      <documentation>
-         [Enter API information here.]
-      </documentation>
-   </annotation>
-
-   <annotation>
-      <appinfo>
-         <meta.section type="implementation"/>
-      </appinfo>
-      <documentation>
-         [Enter information about supplied implementation of this extension point.]
-      </documentation>
-   </annotation>
-
-
-</schema>
+	public static ProjectRascalManager getInstance(String project) {
+		if (_instance == null)
+			_instance = new RascalManager();
+		if (!managedProjects.containsKey(project)) {
+			managedProjects.put(project, _instance.new ProjectRascalManager());
+		}
+		return managedProjects.get(project);
+	}
+	
+	public static void clearManagerData() {
+		managedProjects.clear();
+	}
+	
+	public static void importModule(String module) {
+		eval.doImport(new NullRascalMonitor(), module);
+	}
+	
+	public static void clearProjectData() {
+		for (ProjectRascalManager m : managedProjects.values()) {
+			m.clearManagerDataForProject();
+		}
+	}
+}
metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history/model/HistoricRascalMetrics.java to metric-providers/org.ossmeter.metricprovider.rascal/modules/NOA.rsc
--- a/metric-providers/org.ossmeter.metricprovider.rascal/src/org/ossmeter/metricprovider/rascal/history/model/HistoricRascalMetrics.java
+++ b/metric-providers/org.ossmeter.metricprovider.rascal/modules/NOA.rsc
@@ -1,28 +1,17 @@
-package org.ossmeter.metricprovider.rascal.history.model;
+module NOA
 
-import com.mongodb.*;
-import java.util.*;
-import com.googlecode.pongo.runtime.*;
+import lang::java::m3::Core;
+import List;
+import String;
+import Map;
+import Manager;
 
+int numberOfAttributes(loc cl, M3 model) = size([ m | m <- model@containment[cl], isField(m)]);
 
-public class HistoricRascalMetrics extends Pongo {
-	
-//	protected List<org.ossmeter.metricprovider.rascal.trans.model.Measurement> measurements = null;
-//	
-//	
-//	public HistoricRascalMetrics() { 
-//		super();
-//		dbObject.put("measurements", new BasicDBList());
-//	}
-//	
-//	
-//	
-//	public List<org.ossmeter.metricprovider.rascal.trans.model.Measurement> getMeasurements() {
-//		if (measurements == null) {
-//			measurements = new PongoList<org.ossmeter.metricprovider.rascal.trans.model.Measurement>(this, "measurements", true);
-//		}
-//		return measurements;
-//	}
-	
-	
+map[str class, int nom] getNOA(M3 fileM3) {
+  return (replaceAll(replaceFirst(cl.path, "/", ""), "/", "."):numberOfAttributes(cl, fileM3.model) | <cl,_> <- fileM3.model@containment, isClass(cl));
+}
+
+map[str class, int nom] getNOA(unknownFileType(int lines)) {
+  return ("": -1);
 }