Permalink
Browse files

Runtime predicate

- ast-variable-samebinding
- ast-variable-declaration
- ast-invocation-declaration
  • Loading branch information...
1 parent 75e1d9c commit b019b4affa027c8278412b2daa21c5a5385b4985 @siltvani siltvani committed Feb 22, 2013
@@ -322,15 +322,6 @@
`((runtime/assignment-relaxmatch-variable-declaration ~var-match ~var-left ~var-right)))))
(defn
- cf-negated
- "Returns a function that will generate constraining conditions for the given property value of a code snippet:
- For ASTNode instances: (fails (all (node-conditions) (child-conditions)))."
- [snippet-ast]
- (fn [snippet]
- (let [conditions-of-ast (ast-conditions snippet snippet-ast)]
- `((el/fails (cl/all ~@conditions-of-ast))))))
-
-(defn
ast-conditions
"Returns a list of logic conditions that will retrieve matches for the given snippet-ast in snippet."
[snippet ast]
@@ -349,6 +340,15 @@
(fn [nilval] (swap! query concat (conditions nilval))))
@query))
+(defn
+ cf-negated
+ "Returns a function that will generate constraining conditions for the given property value of a code snippet:
+ For ASTNode instances: (fails (all (node-conditions) (child-conditions)))."
+ [snippet-ast]
+ (fn [snippet]
+ (let [conditions-of-ast (ast-conditions snippet snippet-ast)]
+ `((el/fails (cl/all ~@conditions-of-ast))))))
+
(defn
make-constraining-function
[type]
@@ -295,6 +295,17 @@
[snippet node]
(introduce-logic-variables-with-condition snippet node '?dummy '()))
+(defn
+ introduce-list-of-logic-variables
+ [snippet nodes uservars]
+ (if (empty? nodes)
+ snippet
+ (let [new-snippet (introduce-logic-variable snippet (first nodes) (first uservars))]
+ (introduce-list-of-logic-variables
+ new-snippet
+ (rest nodes)
+ (rest uservars)))))
+
(defn
negated-node
"Match all kind of node, except the given node."
@@ -6,8 +6,10 @@
(:require [damp.ekeko [logic :as el]]
[damp.ekeko.jdt
[basic :as basic]
- [reification :as reification]]))
-
+ [reification :as reification]
+ [soot :as soot]])
+ (:import
+ [org.eclipse.jdt.core.dom SimpleName VariableDeclarationFragment]))
(defn
type-relaxmatch-subtype
@@ -47,5 +49,66 @@
(reification/ast-invocation-binding ?k-inv ?inv ?b)
(reification/ast-declares-binding ?k-dec ?dec ?b)))
+(defn
+ ast-fieldaccess-samebinding
+ "Relation between ASTNode var1 and var2 with the same resolveBinding."
+ [?var1 ?var2]
+ (cl/fresh [?k-var ?b]
+ (reification/ast-fieldaccess-binding ?k-var ?var1 ?b)
+ (reification/ast-fieldaccess-binding ?k-var ?var2 ?b)))
+
+(defn
+ ast-fieldaccess-declaration
+ "Relation between ASTNode fieldaccess with it's declaration."
+ [?var ?dec]
+ (cl/fresh [?k-var ?k-dec ?b]
+ (reification/ast-fieldaccess-binding ?k-var ?var ?b)
+ (reification/ast-declares-binding ?k-dec ?dec ?b)))
+(defn
+ ast-variable-binding
+ "Relation between an variable (SimpleName) instance ?ast,
+ the keyword ?key representing its kind,
+ and the IBinding ?binding for its type."
+ [?key ?ast ?binding]
+ (cl/all
+ (reification/ast :SimpleName ?ast)
+ (el/equals ?binding (.resolveBinding ^SimpleName ?ast))
+ (cl/!= nil ?binding)
+ (reification/ast ?key ?ast)))
+
+(defn
+ ast-variable-declaration-binding
+ "Relation between an variable declaration fragment instance ?ast,
+ the keyword ?key representing its kind,
+ and the IBinding ?binding for its type."
+ [?key ?ast ?binding]
+ (cl/all
+ (reification/ast :VariableDeclarationFragment ?ast)
+ (el/equals ?binding (.resolveBinding ^VariableDeclarationFragment ?ast))
+ (cl/!= nil ?binding)
+ (reification/ast ?key ?ast)))
+
+(defn
+ ast-variable-samebinding
+ "Relation between ASTNode var1 and var2 with the same resolveBinding."
+ [?var1 ?var2]
+ (cl/fresh [?k-var ?b]
+ (ast-variable-binding ?k-var ?var1 ?b)
+ (ast-variable-binding ?k-var ?var2 ?b)))
+(defn
+ ast-variable-declaration
+ "Relation between ASTNode variable with it's declaration."
+ [?var ?dec]
+ (cl/fresh [?k-var ?k-dec ?b]
+ (ast-variable-binding ?k-var ?var ?b)
+ (ast-variable-declaration-binding ?k-dec ?dec ?b)))
+
+(defn
+ ast-may-alias
+ "Relation between ASTNode ast1 may alias ast2."
+ [?ast1 ?ast2]
+ (cl/fresh [?model]
+ (soot/ast-references-soot-model-may-alias ?ast1 ?ast2 ?model)))
+
@@ -0,0 +1,187 @@
+(ns
+ ^{:doc "Test suite for snippet operators."
+ :author "Siltvani, Coen De Roover"}
+ test.damp.ekeko.snippets.runtime
+ (:refer-clojure :exclude [== type declare])
+ (:use [clojure.core.logic :exclude [is]] :reload)
+ (:require [damp.ekeko.snippets
+ [querying :as querying]
+ [representation :as representation]
+ [operators :as operators]
+ [matching :as matching]
+ [parsing :as parsing]
+ [util :as util]])
+ (:require [test.damp [ekeko :as test]])
+ (:require [damp.ekeko.jdt
+ [astnode :as astnode]
+ [reification :as reification]
+ ])
+ (:require [damp.ekeko])
+ (:require [damp.ekeko
+ [logic :as el]
+ [snippets :as snippets]])
+ (:use clojure.test))
+
+(defn method-with-name
+ [name]
+ (first (first
+ (damp.ekeko/ekeko [?m]
+ (fresh [?n ?id]
+ (reification/ast :MethodDeclaration ?m)
+ (reification/has :name ?m ?n)
+ (reification/has :identifier ?n ?id)
+ (reification/value-raw ?id name))))))
+
+(defn class-with-name
+ [name]
+ (first (first
+ (damp.ekeko/ekeko [?m]
+ (fresh [?n ?id]
+ (reification/ast :TypeDeclaration ?m)
+ (reification/has :name ?m ?n)
+ (reification/has :identifier ?n ?id)
+ (reification/value-raw ?id name))))))
+
+;; Predicate: ast-variable-samebinding
+;; ------------------------------------------
+
+(deftest
+ ^{:doc "Introduce logic var for two variable and add logic condition
+ (ast-variable-samebinding ?var1 ?var2)
+
+ Method:
+ public int methodD() {
+ int foo = 0; --> foo = ?var1
+ return foo; --> foo = ?var2
+ }
+ "}
+ predicate-ast-variable-samebinding
+ (let [node (method-with-name "methodD")
+ node-var1 (.getName (first (.fragments (first (.statements (.getBody node))))))
+ node-var2 (.getExpression (fnext (.statements (.getBody node))))
+ var1 '?var1 ;-> should do this, otherwise will become test.damp.ekeko.snippets.runtime/?var1
+ var2 '?var2
+ snippet (representation/jdt-node-as-snippet node)
+ generalized-snippet-with-lvar
+ (operators/introduce-list-of-logic-variables
+ snippet
+ (list (.getName node) node-var1 node-var2)
+ (list '?m var1 var2))
+ generalized-snippet
+ (operators/add-logic-conditions
+ generalized-snippet-with-lvar
+ `((damp.ekeko.snippets.runtime/ast-variable-samebinding ~var1 ~var2)))]
+ (test/tuples-correspond
+ (snippets/query-by-snippet generalized-snippet)
+ "#{(\"public int methodD(){\\n int foo=0;\\n return foo;\\n}\\n\" \"foo\" \"foo\" \"methodD\")
+ (\"public int methodE(){\\n int foo=0;\\n return foo;\\n}\\n\" \"foo\" \"foo\" \"methodE\")
+ (\"public int methodF(){\\n int foo2=0;\\n return foo2;\\n}\\n\" \"foo2\" \"foo2\" \"methodF\")}")))
+
+
+;; Predicate: ast-variable-declaration
+;; ------------------------------------------
+
+(deftest
+ ^{:doc "Introduce logic var for two variable and add logic condition
+ (ast-variable-declaration ?var1 ?var2)
+
+ Method:
+ public int methodD() {
+ int foo = 0; --> foo = 0 --> ?var1
+ return foo; --> foo ?var2
+ }
+ "}
+ predicate-ast-variable-declaration
+ (let [node (method-with-name "methodD")
+ node-var1 (first (.fragments (first (.statements (.getBody node)))))
+ node-var2 (.getExpression (fnext (.statements (.getBody node))))
+ var1 '?var1 ;-> should do this, otherwise will become test.damp.ekeko.snippets.runtime/?var1
+ var2 '?var2
+ snippet (representation/jdt-node-as-snippet node)
+ generalized-snippet-with-lvar
+ (operators/introduce-list-of-logic-variables
+ snippet
+ (list (.getName node) node-var1 node-var2)
+ (list '?m var1 var2))
+ generalized-snippet
+ (operators/add-logic-conditions
+ generalized-snippet-with-lvar
+ `((damp.ekeko.snippets.runtime/ast-variable-declaration ~var2 ~var1)))]
+ (test/tuples-correspond
+ (snippets/query-by-snippet generalized-snippet)
+ "#{(\"public int methodF(){\\n int foo2=0;\\n return foo2;\\n}\\n\" \"foo2\" \"foo2=0\" \"methodF\")
+ (\"public int methodD(){\\n int foo=0;\\n return foo;\\n}\\n\" \"foo\" \"foo=0\" \"methodD\")
+ (\"public int methodE(){\\n int foo=0;\\n return foo;\\n}\\n\" \"foo\" \"foo=0\" \"methodE\")}")))
+
+
+;; Predicate: ast-invocation-declaration
+;; ------------------------------------------
+
+(deftest
+ ^{:doc "Introduce logic var for two variable and add logic condition
+ (ast-invocation-declaration ?var1 ?var2)
+
+ Method:
+ public void myMethod() {
+ this.methodA();
+ this.methodB(); --> invocation ?var1
+ this.methodC();
+ }
+
+ declaration of methodB --> ?var2
+ Still need to check!! the result should be not empty"}
+ predicate-ast-invocation-declaration
+ (let [;;snippet1
+ node1 (method-with-name "myMethod")
+ node-var1 (.getExpression (fnext (.statements (.getBody node1))))
+ var1 '?var1 ;-> should do this, otherwise will become test.damp.ekeko.snippets.runtime/?var1
+ snippet1 (representation/jdt-node-as-snippet node1)
+ generalized-snippet1
+ (operators/introduce-list-of-logic-variables
+ snippet1
+ (list (.getName node1) node-var1)
+ (list '?m var1))
+ ;;snippet2
+ node2 (method-with-name "methodB")
+ var2 '?var2
+ snippet2 (representation/jdt-node-as-snippet node2)
+ generalized-snippet2
+ (operators/introduce-logic-variable-of-node-exact
+ snippet2
+ (.getName node2)
+ var2)
+ ;;add snippet1 and snippet2 to group
+ group (representation/make-snippetgroup)
+ added-group1 (operators/add-snippet group generalized-snippet1)
+ added-group2 (operators/add-snippet added-group1 generalized-snippet2)
+ generalized-snippet
+ (operators/add-logic-conditions-to-snippetgroup
+ added-group2
+ `((damp.ekeko.snippets.runtime/ast-invocation-declaration ~var1 ~var2)))]
+ (test/tuples-correspond
+ (snippets/query-by-snippetgroup generalized-snippet)
+ "#{}")))
+
+;; Test suite
+;; ----------
+(deftest
+ test-suite
+
+ (test/against-project-named "TestCase-Snippets-BasicMatching" false predicate-ast-variable-samebinding)
+ (test/against-project-named "TestCase-Snippets-BasicMatching" false predicate-ast-variable-declaration)
+ (test/against-project-named "TestCase-Snippets-BasicMatching" false predicate-ast-invocation-declaration)
+
+)
+
+(defn
+ test-ns-hook
+ []
+ (test/with-ekeko-disabled test-suite))
+
+
+(comment
+ ;;Example repl session
+ (run-tests)
+ )
+
+
@@ -10,14 +10,8 @@
<arguments>
</arguments>
</buildCommand>
- <buildCommand>
- <name>damp.ekeko.plugin.ekekoBuilder</name>
- <arguments>
- </arguments>
- </buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
- <nature>damp.ekeko.plugin.ekekoNature</nature>
</natures>
</projectDescription>
@@ -91,6 +91,18 @@ public int methodE() {
return foo;
}
+ int foob;
+
+ public int methodF() {
+ int foo2 = 0;
+ return foo2;
+ }
+
+ public int methodG() {
+ int foo2 = 0;
+ return foob;
+ }
+
public static void runTest() {
BasicMatching test = new BasicMatching();
test.methodA();

0 comments on commit b019b4a

Please sign in to comment.