getMethodASTEclipse() returns undeclared variable - abstract-syntax-tree

I am completely new to Rascal and am trying to create ASTs from an eclipse project using the following code
Module FirstTryAST
import lang::java::m3::Core;
import lang::java::jdt::m3::Core;
import Set;
import Relation;
import IO;
import List;
import Map;
import Exception;
import ParseTree;
void test(){
M3 myModel = createM3FromEclipseProject(|project://MyProject/|);
println("Loaded M3 model, looping through methods..");
for(m <- methods(model)) mAST = getMethodASTEclipse(m);
println("Success");
}
When the program (run in eclipse Rascal Console) tries to execute
getMethodASTEclipse(m);
it returns:
|plugin://rascal_eclipse/src/org/rascalmpl/eclipse/library/lang/java/jdt/m3/Core.rsc|(1238,18,<40,12>,<40,30>):
Undeclared variable: getModelContaining
How do I get around this / what did I do wrong ?
I am using Eclipse Mars on Ubuntu 15.04 (Cinnamon).
If you need more information, please let me know.
Update:
When I use getMethodASTEclipse(m, model = myModel) it works fine.
Although this workaround works for me, I still would like to know the answer on why I get this undeclared variable message.

This has been translated into an issue on github. This was a bit of an unused function, and I think we will delete it.
To get the AST use either lang::java::jdt::m3::AST::createAstsFromEclipseProject or lang::java::jdt::m3::AST::createAstsFromEclipseFile. With rascal pattern matching you can find the methods inside.

Related

How to avoid RuntimeError while call __dict__ on module?

it is appearing in some big modules like matplotlib. For example expression :
import importlib
obj = importlib.import_module('matplotlib')
obj_entries = obj.__dict__
Between runs len of obj_entries can vary. From 108 to 157 (expected) entries. Especially pyplot can be ignored like some another submodules.
it can work stable during manual debug mode with len computing statement after dict extraction. But in auto it dont work well.
such error occures:
RuntimeError: dictionary changed size during iteration
python-BaseException
using clear python 3.10 on windows. Version swap change nothing at all
during some attempts some interesting features was found.
use of repr is helpfull before dict initiation.
But if module transported between classes like variable more likely lazy-import happening? For now there is evidence that not all names showing when command line interpriter doing opposite - returning what expected. So this junk of code help bypass this bechavior...
Note: using pkgutil.iter_modules(some_path) to observe modules im internal for pkgutil ModuleInfo form.
import pkgutil, importlib
module_info : pkgutil.ModuleInfo
name = module_info.name
founder = module_info.module_finder
spec = founder.find_spec(name)
module_obj = importlib.util.module_from_spec(spec)
loader = module_obj.__loader__
loader.exec_module(module_obj)
still unfamilliar with interior of import mechanics so it will be helpfull to recive some links to more detail explanation (spot on)

Importing packages in Python3

How do I import packages in Python3?
-Package1
|
--InnerPackage1
||
| ---InnerInnerPackage1
| ---InnerInnerPackage2
| ---InnerInnerPackage3
--InnerPackage2
||---InnerInnerPackage4
||---InnerInnerPackage5
||---InnerInnerPackage6
||---InnerInnerPackage7
--InnerPackage3
||---InnerInnerPackage8
||---InnerInnerPackage9
|||
|| ----InnerInnerInnerPackage10
|| ----InnerInnerInnerPackage11
||---InnerInnerPackage12
|||----InnerInnerInnerPackage12
|||----InnerInnerInnerPackage13
|||----InnerInnerInnerPackage14
||---InnerInnerPackge13
Now, let my directory structure be similar to what I've mentioned above, how do I import a package, when my current directory is suppose InnerInnerInnerPackage12, and I have to import InnerInnerPackage6.
Though, when I import, while traversing above using this syntax,
from InnerPackage2 import InnerInnerPackage6
Else, one can also imply importing from the root package
Package1.InnerPackage2.InnerInnerPackage6
Another logic says,
import sys.path
sys.path.insert(0, '%package_directory%\\Package1\\InnerPackage2\\InnerInnerPackage6')
That works in cmd.
Still what would be the correct way to import a module.
It gives an error on running the code in CMD, but when I run the code in JetBrains PyCharm, it works.
Can someone help me out with this problem? Would be grateful! :)
Maybe this is because what we export from python is modules not packages.
I think this issue arises due to that.
Also, to run the same in cmd, one can follow this syntax:
from InnerPackage2.InnerInnerPackage6 import InnerInnerPackage6
This might be the reason, the difficulty arises when we run the same program in both JetBrains PyCharm and on cmd.

PowerMockito error: Cannot access Stubber/IOngoingStubbing

The IDE compiler shows these errors when using PowerMockito.
I tried both variants for specifying return values:
Variant 1
doReturn(jsonString).when(MyStaticClass.myStaticMethod());
Variant 2
when(MyStaticClass.myStaticMethod()).thenReturn(jsonString);
Why do the compiler display these errors?
I used
import static org.powermock.api.mockito.PowerMockito.when;
but you should use:
import static org.mockito.Mockito.when;

ipython notebook: custom cells and execute hook

I would like to override what happens when run is pressed for certain cells in ipython notebook.
For example, I would like to be able to write an SQL query directly in a cell and define a function that processes it.
It seems it should be possible to do this as with ipython-notebook extensions. Does anyone know of a similar extension? An easy way to do this directly from ipython?
Ideally this would involve defining a custom cell type, but I would be happy to use special tags to separate the usual python code from, say, a custom SQL query cell.
I've once had the similar desire and I ended up with the following solution:
from sqlalchemy import create_engine
import pandas as pd
from IPython.core.magic import register_cell_magic
from IPython import get_ipython
con = create_engine(DB_URL)
#register_cell_magic
def sql(line, cell):
cell = cell.format(**globals())
if line.strip() != '-':
res = pd.read_sql(cell, con)
if line.strip() != '': get_ipython().user_ns[line.strip()] = res
return res
else:
con.execute(cell)
del sql
You can now write in other cells:
%sql outputvar
select * from whatever where ...
For example:

how to run and get document stats from boilerpipe article extractor?

There's something I'm not quite understanding about the use of boilerpipe's ArticleExtractor class. Albeit, I am also very new to java, so perhaps my basic knowledge of this enviornemnt is at fault.
anyhow, I'm trying to use boilerpipe to extract the main article from some raw html source I have collected. The html source text is stored in a java.lang.String variable (let's call it htmlstr) variable that has the raw HTML contents of a webpage.
I know how to run boilerpipe to print the extracted text to the output window as follows:
java.lang.String htmlstr = "<!DOCTYPE.... ****html source**** ... </html>";
java.lang.String article = ArticleExtractor.INSTANCE.getText(htmlstr);
System.out.println(article);
However, I'm not sure how to run BP by first instantiating an instance of the ArticleExtractor class, then calling it with the 'TextDocument' input datatype. The TextDocument datatype is itself somehow constructed from BP's 'TextBlock' datatype, and perhaps I am not doing this correctly...
What is the proper way to construct a TextDocument type variable from my htmlstr string variable?
So my problem is then in using the processing method of BP's Article Extractor class aside from calling the ArticleExtractor getText method as per the example above. In other words, I'm not sure how to use the
ArticleExtractor.process(TextDocument doc);
method.
It is my understanding that one is required to run this ArticleExtractor process method to then be able to use the same "TextDocument doc" variable for getting document stats, using BP's
TextDocumentStatistics(TextDocument doc, boolean contentOnly)
method? I would like to use the stats to determine how good the filtering was estimated to be.
Any code examples someone could help me out with?
Code written in Jython (Conversion to java should be easy)
1) How to get TextDocument from a HTML String:
import org.xml.sax.InputSource as InputSource
import de.l3s.boilerpipe.sax.HTMLDocument as HTMLDocument
import de.l3s.boilerpipe.document.TextDocument as TextDocument
import de.l3s.boilerpipe.sax.BoilerpipeSAXInput as BoilerpipeSAXInput
import de.l3s.boilerpipe.extractors.ArticleExtractor as ArticleExtractor
import de.l3s.boilerpipe.estimators.SimpleEstimator as SimpleEstimator
import de.l3s.boilerpipe.document.TextDocumentStatistics as TextDocumentStatistics
import de.l3s.boilerpipe.document.TextBlock as TextBlock
htmlDoc = HTMLDocument(rawHtmlString)
inputSource = htmlDoc.toInputSource()
boilerpipeSaxInput = BoilerpipeSAXInput(inputSource)
textDocument = boilerpipeSaxInput.getTextDocument()
2) How to process TextDocument using Article Extractor (continued from above)
content = ArticleExtractor.INSTANCE.getText(textDocument)
3) How to get TextDocumentStatistics (continued from above)
content_list = [] #replace python 'List' Object with ArrayList in java
content_list.append(TextBlock(content)) #replace with arrayList.add(TextBlock(content))
content_td = TextDocument(content_list)
content_stats = TextDocumentStatistics(content_td, True)#True for article content statistics only
Note: The java docs accompanied with the boilerpipe 1.2.jar library should be somewhat useful for future reference

Resources