diff --git a/example/CodeFuse/CommentCodePairsGo.gdl b/example/CodeFuse/CommentCodePairsGo.gdl new file mode 100644 index 00000000..f2295460 --- /dev/null +++ b/example/CodeFuse/CommentCodePairsGo.gdl @@ -0,0 +1,28 @@ +// script +use coref::go::* + +pub fn default_db() -> GoDB { + return GoDB::load("coref_go_src.db") +} + +pub fn output_(methodText: string, commentText: string, signature: string) -> bool { + let (db = default_db()) { + // Check if there exists a Function object 'func' + for (func in Function(db)) { + // Get the name of the function and assign it to the variable 'methodText' + if (methodText = func.getText()) { + // Get the associated comment string for the function and assign it to the variable 'commentText' + if (commentText = func.getAssociatedCommentString()) { + // Get the function type signature and assign it to the variable 'signature' + if (signature = func.getFunctionTypeSignature()) { + return true + } + } + } + } + } +} + +fn main() { + output(output_()) +} \ No newline at end of file diff --git a/example/CodeFuse/CommentCodePairsJava.gdl b/example/CodeFuse/CommentCodePairsJava.gdl new file mode 100644 index 00000000..24a1e87c --- /dev/null +++ b/example/CodeFuse/CommentCodePairsJava.gdl @@ -0,0 +1,303 @@ +// script +use coref::java::* + +pub fn default_db() -> JavaDB { + return JavaDB::load("coref_java_src.db") +} + +//过滤用关键字,可修改 +pub fn KEYWORDS(k: string) -> bool { + [{"PLEASE DO NOT MODIFY THIS FILE MANUALLY"}, + {"This method was generated by MyBatis Generator"}, + {"Generated by the protocol buffer compiler."}, + {"@mbg.generated"}, + {"Autogenerated by Thrift Compiler"}, + {"@author smartf-generate"}, + {"Ibatis implementation for"}, + {"This class was generated by MyBatis Generator"}, + {"This file was automatically generated"}, + {"Generated By:JavaCC: Do not edit this line."}, + {"This class was generated by Ali-Generator"}, + {"This field was generated by MyBatis Generator."} + ] +} + +//过滤用文件夹名,可修改 +pub fn FOLDERNAMES(n: string) -> bool { + [ + {"/daointerface/"}, + {"/dataobject/"}, + {"/ibatis/"}, + {"/mybatis/domain/"}, + {"/mybatis/mapper/"}, + {"/mybatis/model/"}, + {"/dal/"}, + {"/dao/"}, + {"/entity/"} + ] +} + +//用关键字进行过滤 +pub fn filterByKeyWords(f: string) -> bool { + let (db = default_db()) { + for (n in string::__undetermined_all__(), + k in string::__undetermined_all__()) { + for (i in ElementParent(db), + c in JavadocComment(db)) { + if (n = c.getText()) { + if (KEYWORDS(k)) { + if (n.contains(k)) { + if (i = c.getDocumentableElement()) { + if (f = c.getLocation().getFile().getRelativePath()) { + return true + } + } + } + } + } + } + } + } +} + +//用文件夹名进行过滤 +pub fn filterByFolderNames(d: File) -> bool { + for (m in string::__undetermined_all__(), + n in string::__undetermined_all__()) { + if (n = d.getRelativePath()) { + if (FOLDERNAMES(m)) { + if (n.contains(m)) { + return true + } + } + } + } +} + +//用注解 @Generated("org.mybatis.generator.api.MyBatisGenerator") 进行过滤 +pub fn filterByAnnotation(f: string) -> bool { + let (db = default_db()) { + for (a in Annotation(db)) { + if (f = a.getLocation().getFile().getRelativePath()) { + if (a.getName() = "javax.annotation.Generated") { + for (auto_tmp1 in a.getAnnotationArgument()) { + if (auto_tmp1.getAnnotationArgumentValue() = "\"org.mybatis.generator.api.MyBatisGenerator\"") { + return true + } + } + } + } + } + } +} + +//过滤得到的自动生成文件名 +pub fn getAutoGeneratedFiles(n: string) -> bool { + let (db = default_db()) { + for (f in File(db)) { + if (n = f.getRelativePath()) { + if (filterByFolderNames(f)) { + if (filterByAnnotation(n)) { + return true + } + if (filterByKeyWords(n)) { + return true + } + } + } + } + } +} + +schema PublicVisitedElement extends Callable { + +} + +//定义分母 +impl PublicVisitedElement { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicVisitedElement { + for (tmp in Callable(db)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + + //返回在上方的单行注释 + pub fn getPossibleAboveComment(self) -> Comment { + for (c in Comment(__all_data__)) { + if (self.key_eq(c.getParent())) { + for (auto_tmp1 in self.getModifier()) { + if (auto_tmp1.getLocation().getStartLineNumber() > c.getLocation().getStartLineNumber()) { + return c + } + } + } + } + } + + pub fn getDefinitionBody(self) -> string { + for (definitionBody in string::__undetermined_all__()) { + for (m in Method(__all_data__)) { + if (self.key_eq(m)) { + if (definitionBody = m.getDefinitionBody()) { + return definitionBody + } + } + } + for (c in Constructor(__all_data__)) { + if (self.key_eq(c)) { + if (definitionBody = c.getDefinitionBody()) { + return definitionBody + } + } + } + } + } +} + +//找到注释与JavaDoc注释连在一起的情况,用于排除 +pub fn commentConnectDocComment(c: Comment) -> bool { + let (db = default_db()) { + for (i in int::__undetermined_all__(), + m in int::__undetermined_all__(), + l in int::__undetermined_all__()) { + for (j in JavadocComment(db)) { + if (getJavadocCommentLocInfo(j, i, m)) { + if (l = m - 1) { + if (getCommentLocInfo(c, i, l)) { + return true + } + } + } + } + } + } +} + +//找到注释所在文件和行号信息 +pub fn getCommentLocInfo(e: Comment, i: int, j: int) -> bool { + let (db = default_db()) { + if (i = e.getLocation().getFile().element_hash_id) { + if (j = e.getLocation().getStartLineNumber()) { + return true + } + } + } +} + +//找到公共访问元素的所属文件和行号信息 +pub fn getPublicElementLocInfo(e: PublicVisitedElement, i: int, j: int) -> bool { + let (db = default_db()) { + if (i = e.getLocation().getFile().element_hash_id) { + if (j = e.getLocation().getStartLineNumber()) { + return true + } + } + } +} + +//找到Javadoc注释所在文件和行号信息 +pub fn getJavadocCommentLocInfo(e: JavadocComment, i: int, j: int) -> bool { + let (db = default_db()) { + for (n in int::__undetermined_all__()) { + if (i = e.getLocation().getFile().element_hash_id) { + if (n = e.getLocation().getStartLineNumber()) { + if (j = n - 1) { + return true + } + } + if (n = e.getLocation().getEndLineNumber()) { + if (j = n + 1) { + return true + } + } + } + } + } +} + +//找到JavaDoc注释的owner为空的情况 +pub fn connectDoc(j: JavadocComment, e: PublicVisitedElement) -> bool { + let (db = default_db()) { + for (i in int::__undetermined_all__(), + m in int::__undetermined_all__()) { + if (getJavadocCommentLocInfo(j, i, m)) { + if (getPublicElementLocInfo(e, i, m)) { + return true + } + } + } + } +} + +pub fn hasComment(e: PublicVisitedElement, tmp_j: int) -> bool { + let (db = default_db()) { + for (j in JavadocComment(db)) { + if (j.element_hash_id = tmp_j) { + if (e.key_eq(j.getDocumentableElement())) { + return true + } + if (connectDoc(j, e)) { + return true + } + } + } + for (j in Comment(db)) { + if (j = e.getPossibleAboveComment() && + j.element_hash_id = tmp_j) { + return true + } + } + } +} + +pub fn getCommentInfo(j: int, cContext: string) -> bool { + for (d in JavadocComment(__all_data__)) { + if (d.element_hash_id = j) { + if (cContext = d.getText()) { + return true + } + } + } + for (d in Comment(__all_data__)) { + if (d.element_hash_id = j) { + if (cContext = d.getText()) { + return true + } + } + } +} + +pub fn output_(signature: string, methodText: string, commentText: string) -> bool { + let (db = default_db()) { + for (j in int::__undetermined_all__()) { + for (e in PublicVisitedElement(db), + i in Identifier(db), + filePath in string::__undetermined_all__()) { + if (signature = e.getSignature()) { + if (filePath = e.getLocation().getFile().getRelativePath()) { + if (!getAutoGeneratedFiles(filePath)) { + if (e.key_eq(i.getParent())) { + if (hasComment(e, j)) { + if (getCommentInfo(j, commentText)) { + if (methodText = e.getDefinitionBody()) { + return true + } + } + } + } + } + } + } + } + } + } +} + +fn main() { + output(output_()) +} \ No newline at end of file diff --git a/example/CodeFuse/CommentCodePairsJavaScript.gdl b/example/CodeFuse/CommentCodePairsJavaScript.gdl new file mode 100644 index 00000000..186271ec --- /dev/null +++ b/example/CodeFuse/CommentCodePairsJavaScript.gdl @@ -0,0 +1,151 @@ +// script +use coref::javascript::* + +pub fn default_db() -> JavascriptDB { + return JavascriptDB::load("coref_javascript_src.db") +} + +/** + * 获取函数和对应的注释 + * + * 其中:FunctionDeclaration, MethodDeclaration, Constructor, GetAccessor, SetAccessor, + * 直接查询节点前是否有关联的注释 + * 而FunctionExpression 和 ArrowFunction 除了前面关联的注释,还需要统计包含在 VariableStatement 或 PropertyDeclaration 中, + * 而且 VariableStatement 或 PropertyDeclaration 节点前有关联的注释 + * + * VariableStatement 中包含函数的结构为: + * VariableStatement + * └-- VariableDeclarationList + * └-- VariableDeclaration + * └-- FunctionExpression / ArrowFunction + * + * PropertyDeclaration 中包含函数的结构为: + * PropertyDeclaration + * └-- FunctionExpression / ArrowFunction + */ +pub fn getFunctionCommentPair(function: FunctionLikeDeclaration, comment: Comment) -> bool { + let (db = default_db(), node = Node(db).find(function)) { + if (isFunctionDeclaration(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isMethodDeclaration(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isConstructor(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isGetAccessor(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isSetAccessor(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isFunctionExpression(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isArrowFunction(node)) { + for (auto_tmp1 in function.getALeadingComment()) { + if (auto_tmp1 = comment) { + return true + } + } + } + + if (isFunctionExpression(node)) { + for (variableStatement in VariableStatement(db)) { + if (variableStatement.key_eq(function.getParent().getParent().getParent())) { + for (auto_tmp2 in variableStatement.getALeadingComment()) { + if (auto_tmp2 = comment) { + return true + } + } + } + } + + for (propertyDeclaration in PropertyDeclaration(db)) { + if (propertyDeclaration.key_eq(function.getParent())) { + for (auto_tmp3 in propertyDeclaration.getALeadingComment()) { + if (auto_tmp3 = comment) { + return true + } + } + } + } + } + + if (isArrowFunction(node)) { + for (variableStatement in VariableStatement(db)) { + if (variableStatement.key_eq(function.getParent().getParent().getParent())) { + for (auto_tmp2 in variableStatement.getALeadingComment()) { + if (auto_tmp2 = comment) { + return true + } + } + } + } + + for (propertyDeclaration in PropertyDeclaration(db)) { + if (propertyDeclaration.key_eq(function.getParent())) { + for (auto_tmp3 in propertyDeclaration.getALeadingComment()) { + if (auto_tmp3 = comment) { + return true + } + } + } + } + } + } +} + +pub fn output_(methodText: string, commentText: string) -> bool { + let (db = default_db()) { + for (function in FunctionLikeDeclaration(db), + comment in Comment(db)) { + if (getFunctionCommentPair(function, comment)) { + if (methodText = function.getText()) { + for (location in Location(db)) { + if (location = comment.getLocation()) { + if (commentText = location.getText()) { + return true + } + } + } + } + } + } + } +} + +fn main() { + output(output_()) +} \ No newline at end of file diff --git a/example/CodeFuse/CommentCodePairsPython.gdl b/example/CodeFuse/CommentCodePairsPython.gdl new file mode 100644 index 00000000..df9d999d --- /dev/null +++ b/example/CodeFuse/CommentCodePairsPython.gdl @@ -0,0 +1,99 @@ +// script +use coref::python::* + +pub fn default_db() -> PythonDB { + return PythonDB::load("coref_python_src.db") +} + +schema PublicVisitedElement extends CombineElement { + +} + +//定义分母,只有Function元素会被统计 +impl PublicVisitedElement { + + @data_constraint + @inline + pub fn __all__(db: PythonDB) -> *PublicVisitedElement { + for (tmp in CombineElement(db)) { + for (c in Function(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + } + } + + pub fn getName(self) -> string { + for (name in string::__undetermined_all__()) { + for (c in Class(__all_data__)) { + if (self.key_eq(c)) { + if (name = c.getQualifiedName()) { + return name + } + } + } + for (c in Function(__all_data__)) { + if (self.key_eq(c)) { + if (name = c.getQualifiedName()) { + return name + } + } + } + } + } + + pub fn getCommentContent(self) -> string { + for (text in string::__undetermined_all__()) { + for (j in DocstringComment(__all_data__)) { + if (self.key_eq(j.getDocumentableElement())) { + if (text = j.getText()) { + return text + } + } + } + for (j in Comment(__all_data__)) { + if (self.key_eq(j.getDocumentedClassOrFunctionElement())) { + if (text = j.getText()) { + return text + } + } + } + } + } +} + +pub fn hasComment(e: PublicVisitedElement) -> bool { + let (db = default_db()) { + for (j in DocstringComment(db)) { + if (e.key_eq(j.getDocumentableElement())) { + return true + } + } + for (j in Comment(db)) { + if (e.key_eq(j.getDocumentedClassOrFunctionElement())) { + return true + } + } + } +} + +pub fn output_(methodText: string, commentText: string, signature: string) -> bool { + let (db = default_db()) { + for (e in PublicVisitedElement(db)) { + if (signature = e.getName()) { + if (methodText = e.print()) { + if (commentText = e.getCommentContent()) { + return true + } + } + } + } + } +} + +fn main() { + output(output_()) +} \ No newline at end of file diff --git a/example/CodeFuse/CommentRatioJava.gdl b/example/CodeFuse/CommentRatioJava.gdl new file mode 100644 index 00000000..6b9b5797 --- /dev/null +++ b/example/CodeFuse/CommentRatioJava.gdl @@ -0,0 +1,444 @@ +// script +use coref::java::* + +pub fn default_db() -> JavaDB { + return JavaDB::load("coref_java_src.db") +} + +schema PublicClass extends Class { + +} + +//具有public修饰符的类 +impl PublicClass { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicClass { + for (tmp in Class(db)) { + for (m in Modifier(db)) { + if (m.getName() = "public") { + if (tmp.key_eq(m.getModifierList().getParent())) { + yield PublicClass { + element_hash_id: tmp.element_hash_id, + qualified_name: tmp.qualified_name, + identifier_hash_id: tmp.identifier_hash_id, + location_hash_id: tmp.location_hash_id, + parent_hash_id: tmp.parent_hash_id + } + } + } + } + } + } +} + +//具有public修饰符的接口 +schema PublicInterface extends Interface { + +} + +impl PublicInterface { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicInterface { + for (tmp in Interface(db)) { + for (m in Modifier(db)) { + if (m.getName() = "public") { + if (tmp.key_eq(m.getModifierList().getParent())) { + yield PublicInterface { + element_hash_id: tmp.element_hash_id, + qualified_name: tmp.qualified_name, + identifier_hash_id: tmp.identifier_hash_id, + location_hash_id: tmp.location_hash_id, + parent_hash_id: tmp.parent_hash_id + } + } + } + } + } + } +} + +//具有public修饰符的函数,并且不属于匿名函数 +schema PublicMethod extends Method { + +} + +impl PublicMethod { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicMethod { + for (tmp in Method(db)) { + for (m in Modifier(db)) { + for (auto_tmp1 in tmp.getModifier()) { + if (m = auto_tmp1) { + if (m.getName() = "public") { + if (!_isAnonymousMethod(tmp) && !isOverrideMethod(tmp)) { + yield PublicMethod { + element_hash_id: tmp.element_hash_id, + name: tmp.name, + signature: tmp.signature, + type_hash_id: tmp.type_hash_id, + parent_hash_id: tmp.parent_hash_id, + location_hash_id: tmp.location_hash_id, + definition_body: tmp.definition_body + } + } + } + } + } + } + } + } +} + +pub fn _isAnonymousMethod(m: Method) -> bool { + let (db = default_db()) { + for (a in AnonymousClass(db)) { + if (a.key_eq(m.getParent())) { + return true + } + } + } +} + +pub fn isOverrideMethod(m: Method) -> bool { + let (db = default_db()) { + for (a in Annotation(db)) { + for (auto_tmp1 in m.getAnnotation()) { + if (a = auto_tmp1) { + if (a.getName() = "java.lang.Override") { + return true + } + } + } + } + } +} + +schema PublicField extends Field { + +} + +//具有public修饰符的字段 +impl PublicField { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicField { + for (tmp in Field(db)) { + for (m in Modifier(db)) { + for (auto_tmp1 in tmp.getModifier()) { + if (m = auto_tmp1) { + if (m.getName() = "public") { + yield PublicField { + element_hash_id: tmp.element_hash_id, + name: tmp.name, + parent_hash_id: tmp.parent_hash_id, + printable_text: tmp.printable_text, + location_hash_id: tmp.location_hash_id + } + } + } + } + } + } + } +} + +schema PublicEnum extends EnumConstant { + +} + +//具有public修饰符的枚举常量 +impl PublicEnum { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicEnum { + for (tmp in EnumConstant(db)) { + for (m in Modifier(db)) { + if (m.getName() = "public") { + if (tmp.key_eq(m.getModifierList().getParent())) { + yield PublicEnum { + element_hash_id: tmp.element_hash_id, + name: tmp.name, + parent_hash_id: tmp.parent_hash_id, + location_hash_id: tmp.location_hash_id, + printable_text: tmp.printable_text + } + } + } + } + } + } +} + +schema PublicConstructor extends Constructor { + +} + +//具有public修饰符的构造函数 +impl PublicConstructor { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicConstructor { + for (tmp in Constructor(db)) { + for (m in Modifier(db)) { + if (m = tmp.getModifier()) { + if (m.getName() = "public") { + yield PublicConstructor { + element_hash_id: tmp.element_hash_id, + name: tmp.name, + signature: tmp.signature, + parent_hash_id: tmp.parent_hash_id, + location_hash_id: tmp.location_hash_id, + definition_body: tmp.definition_body + } + } + } + } + } + } +} + +schema ConstructorWithParameter extends PublicConstructor { + +} + +//具有public修饰符的带参构造函数 +impl ConstructorWithParameter { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *ConstructorWithParameter { + for (tmp in PublicConstructor(db)) { + for (p in Parameter(db)) { + if (tmp.key_eq(p.getParent())) { + yield ConstructorWithParameter { + element_hash_id: tmp.element_hash_id, + name: tmp.name, + signature: tmp.signature, + parent_hash_id: tmp.parent_hash_id, + location_hash_id: tmp.location_hash_id, + definition_body: tmp.definition_body + } + } + } + } + } +} + +schema ConstructorWithoutParameter extends PublicConstructor { + +} + +fn getParameter(tmp: PublicConstructor, db: JavaDB) -> *Parameter { + for (p in Parameter(db)) { + if (p.getParent().key_eq(tmp)) { + yield p + } + } +} + +//具有public修饰符的不带参构造函数 +impl ConstructorWithoutParameter { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *ConstructorWithoutParameter { + for (tmp in PublicConstructor(db)) { + for (p in Parameter(db)) { + if (getParameter(tmp, db).len() = 0) { + yield ConstructorWithoutParameter { + element_hash_id: tmp.element_hash_id, + name: tmp.name, + signature: tmp.signature, + parent_hash_id: tmp.parent_hash_id, + location_hash_id: tmp.location_hash_id, + definition_body: tmp.definition_body + } + } + } + } + } +} + +schema PublicVisitedElement extends ElementParent { + +} + +impl PublicVisitedElement { + + @data_constraint + @inline + pub fn __all__(db: JavaDB) -> *PublicVisitedElement { + for (tmp in ElementParent(db)) { + for (c in PublicClass(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + for (c in PublicInterface(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + for (c in PublicField(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + for (c in PublicEnum(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + for (c in ConstructorWithoutParameter(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + for (c in ConstructorWithParameter(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + for (c in PublicMethod(db)) { + if (tmp.key_eq(c)) { + yield PublicVisitedElement { + id: tmp.id + } + } + } + } + } + + pub fn getPossibleAboveComment(self) -> Comment { + for (c in Comment(__all_data__)) { + if (self.key_eq(c.getParent())) { + if (self.getLocation().getStartLineNumber() = c.getLocation().getStartLineNumber()) { + if (!commentConnectDocComment(c)) { + return c + } + } + } + } + } +} + +//找到注释与JavaDoc注释连在一起的情况,用于排除 +pub fn commentConnectDocComment(c: Comment) -> bool { + let (db = default_db()) { + for (i in int::__undetermined_all__(), j in JavadocComment(__all_data__)) { + if (c.getLocation().getFile() = j.getLocation().getFile()) { + if (i = j.getLocation().getStartLineNumber()) { + if (i - 1 = c.getLocation().getEndLineNumber()) { + return true + } + } + } + } + } +} + +//找到JavaDoc注释的owner为空的情况 +pub fn connectDoc(j: JavadocComment, e: PublicVisitedElement) -> bool { + let (db = default_db()) { + for (i in int::__undetermined_all__()) { + if (j.getLocation().getFile() = e.getLocation().getFile()) { + if (i = j.getLocation().getEndLineNumber()) { + if (i + 1 = e.getLocation().getStartLineNumber()) { + return true + } + } + } + } + } +} + +pub fn hasComment(e: PublicVisitedElement) -> bool { + let (db = default_db()) { + for (j in JavadocComment(__all_data__)) { + if (e.key_eq(j.getDocumentableElement())) { + return true + } + if (connectDoc(j, e)) { + return true + } + } + for (j in Comment(__all_data__)) { + if (j = e.getPossibleAboveComment()) { + return true + } + } + } +} + +//分母统计,公开访问元素一共有多少个 +pub fn countTotalPublicElement() -> int { + let (db = default_db()) { + return PublicVisitedElement(db).len() + } +} + +pub fn totalPublicDocumentedElement() -> *PublicVisitedElement { + let (db = default_db()) { + for (e in PublicVisitedElement(db), + j in JavadocComment(db)) { + if (e.key_eq(j.getDocumentableElement())) { + yield e + } + if (connectDoc(j, e)) { + yield e + } + } + for (e in PublicVisitedElement(db), + j in Comment(db)) { + if (j.key_eq(e.getPossibleAboveComment())) { + yield e + } + } + } +} + +//分子统计,带有注释的公开访问元素有多少个 +pub fn countTotalPublicDocumentedElement() -> int { + return totalPublicDocumentedElement().len() +} + +//注释率计算,如果分母为0,则输出为-1,否则,分子*100000/分母 +pub fn commentRatio(documentedPercentage: int) -> bool { + let (i = countTotalPublicElement(), j = countTotalPublicDocumentedElement()) { + if (i = 0) { + if (documentedPercentage = -1) { + return true + } + } + if (i > 0) { + if (documentedPercentage = j * 1000 / i) { + return true + } + } + } +} + +fn main() { + output(commentRatio()) +} diff --git a/example/CodeFuse/CyclomaticComplexityJava.gdl b/example/CodeFuse/CyclomaticComplexityJava.gdl new file mode 100644 index 00000000..b8f1df66 --- /dev/null +++ b/example/CodeFuse/CyclomaticComplexityJava.gdl @@ -0,0 +1,8 @@ +// script +use coref::java::{MetricCallable, JavaDB} + +query CyclComplexity from + m in MetricCallable(JavaDB::load("coref_java_src.db")) +select + m.getCyclomaticComplexity() as i, // 圈复杂度 + m.getSignature() as n \ No newline at end of file diff --git a/example/CodeFuse/Fan-InJava.gdl b/example/CodeFuse/Fan-InJava.gdl new file mode 100644 index 00000000..8c937fd8 --- /dev/null +++ b/example/CodeFuse/Fan-InJava.gdl @@ -0,0 +1,6 @@ +// script +use coref::java::{MetricCallable, JavaDB} + +query fan_in from + m in MetricCallable(JavaDB::load("coref_java_src.db")) +select m.getAfferentCoupling() as i, m.getSignature() as n diff --git a/example/CodeFuse/Fan-OutJava.gdl b/example/CodeFuse/Fan-OutJava.gdl new file mode 100644 index 00000000..39b30d8e --- /dev/null +++ b/example/CodeFuse/Fan-OutJava.gdl @@ -0,0 +1,6 @@ +// script +use coref::java::{MetricCallable, JavaDB} + +query fan_out from + m in MetricCallable(JavaDB::load("coref_java_src.db")) +select m.getEfferentCoupling() as i, m.getSignature() as n diff --git a/example/CodeFuse/GetImportSpringFrameworkFileJava.gdl b/example/CodeFuse/GetImportSpringFrameworkFileJava.gdl new file mode 100644 index 00000000..fb0b4874 --- /dev/null +++ b/example/CodeFuse/GetImportSpringFrameworkFileJava.gdl @@ -0,0 +1,14 @@ +// script +use coref::java::{Import, JavaDB} + +fn db() -> JavaDB { + return JavaDB::load("coref_java_src.db") +} + +query importStatement from + i in Import(db()) +where + i.getName().contains("springframework") +select + i.getName() as name, + i.getLocation().getFile().getRelativePath() as filepath \ No newline at end of file diff --git a/example/CodeFuse/GetTotalNumberOfLinesOfCodeJava.gdl b/example/CodeFuse/GetTotalNumberOfLinesOfCodeJava.gdl new file mode 100644 index 00000000..dcf92d09 --- /dev/null +++ b/example/CodeFuse/GetTotalNumberOfLinesOfCodeJava.gdl @@ -0,0 +1,10 @@ +// script + +// 计算代码有效行数,以便过滤无效的代码文件 +use coref::java::{Method, JavaDB} + +query total_number_of_lines_of_code from + c in Method(JavaDB::load("coref_java_src.db")) +select + c.getSignature() as name, + c.getSize().getNumberOfLinesOfCode() as linesOfCode \ No newline at end of file diff --git a/example/query/hello.gdl b/example/query/hello.gdl deleted file mode 100644 index c4189b71..00000000 --- a/example/query/hello.gdl +++ /dev/null @@ -1,8 +0,0 @@ -// script -fn hello(greeting: string) -> bool { - return greeting = "hello world!" -} - -fn main() { - output(hello()) -} \ No newline at end of file