Matlab 2012a, static method invocation, not recognizing class outside current folder - oop

I have a class Ellipse (handle, inherits from other class), that has one static method called createFromGaussian. It is located in a remote folder, that I add to Matlab path.
The thing is that, if I try to invoke the static function BEFORE creating any Ellipse object, it fails:
>> Ellipse.createFromGaussian(arg1,arg2)
Undefined variable "Ellipse" or class "Ellipse.createFromGaussian".
It works if I try any of the following things:
I change current directory to that in which Ellipse.m file is located
Working from a remote directory, I create an Ellipse object beforehand:
>> Ellipse()
[C=, axis=[0.0,0.0], angle=0.0]
>> Ellipse.createFromGaussian([],2)
Is this supposed to be this way? The error message sounds weird to me: of course it cannot find variable "Ellipse" or class "Ellipse.createFromGaussian"! It should find "Ellipse" class

So the Ellipse classdef file, and the function file, are in the same folder called #Ellipse, and the PARENT of the #Ellipse folder is on the path? That is what Matlab requires for it's system to work.
From the the ML help : "You must use an #-folder if you want to use more than one file for your class definition. Methods defined in separate files match the file name to the function name and must be declared in the classdef file."

Related

VB.NET Automatically include DLLs in specific folder

I have a project where I have a directory called plugins/ which will contain multiple DLLs.
As an example, I will have two (2) DLLs in my plugins/ directory: Visual.dll and Momen.dll
Each of those will be a Class, Visual.dll will have the Class name Visual and Momen.dll will have the Class name Momen.
I need to make a loop that will loop through all DLL files in my plugins/ directory; once in that loop, it will obviously know the file is Visual.dll or Momen.dll. First, it will need to include the DLLs (just as if I added them as References to my Project) so that I can use them. If the loop is at Visual.dll, I will then need it to create an instance of the Visual Class, and then execute a method called Run()
I'm a PHP developer, and relatively new to VB.NET, in PHP I'd be able to do something like, which would create a new instance of my Visual Class (while passing a few arguments), then execute the Run() method:
$className = "Visual";
$instance = new $className("arg1", "arg2");
$instance->Run();
I appreciate any help.
Thank you
If the DLLs are .NET assemblies, then you can load them with Assembly.LoadFile. That will give you a reference to an Assembly object.
You can use the assembly's GetExportedTypes method to view public types in the assembly, or the GetType overload that takes a string parameter to get a type from the assembly based on its name.
From there, you should be able to use an appropriate overload of Activator.CreateInstance to create an instance of the class. From there, you should also be able to find and run its Run method through reflection.
Edit This is untested, but something like this might work (or be close to working):
Dim plugin As Assembly = Assembly.LoadFile("test.dll")
Dim pluginType As Type = plugin.GetType("Test")
Dim instance As Object = Activator.CreateInstance(pluginType)
Dim runMethod As MethodInfo = pluginType.GetMethod("Run")
Dim param1 As String = "Hello"
Dim param2 As String = "World"
runMethod.Invoke(instance, New Object() {param1, param2})
If you're going to do much with plugins other than just load and run a method, then it might be worthwhile to look for a plugin framework to use.

Test Class, File Name

I wrote my code already and wrote a test class in Dr. Java. But how do I save this to have it compile and run. I keep getting " class TestGradeDistribution is public, should be declared in a file named TestGradeDistribution.java" but I have the file name as GradeDistribution already. Help!
In Java, the class name and the name of the .java file have to match exactly.
If you've copied GradeDistribution and made it into a class called TestGradeDistribution, you have to save it as TestGradeDistribution.java. Otherwise, the Java compiler won't be able to recognize it.
Best of luck.

Rails 3 - Need shared model methods for polymorphic models

Currently I have repeated code in multiple modules something like this:
def do_something_polymorphic
self.something_polymorphic_able.where(.....).each do |thing|
...
end
end
In the spirit of DRY, I tried moving do_something_polymorphic() into a module at /lib/shared_methods.rb. When I added include SharedMethods in my models I got an error:
uninitialized constant Chapter::SharedMethods (NameError)
Which I could not figure out how to get around.
Then I tried loading the module file into the models (load 'shared_methods.rb'). It loaded OK, but the Module construct threw off the namespace and do_something_polymorphic() was undefined for the model in which it was loaded/included. So "Module" appears not to be an option if you are trying to self-reference an object.
Lastly, I removed the module construct from the shared_methods.rb file and just left the do_something_polymorphic() method alone in the file. It loaded OK, but when I ran the method I got the error:
NoMethodError: private method `do_something_polymorphic' called for #<Polymorphic_Object:0x007fc27e5b8338>
Not sure where to go from here. I could go back to setting this up as a module and try to pass "self" in as an object parameter to the method, but I would like to preserve the ability to cleanly call the method against its object: current_object.do_something_polymorphic
Short of going the inheritance route (which I really want to avoid,) is there a way to share, across multiple models, a method that uses the self keyword?
You can include lib/ modules automatically by modifying the application configuration.
#config/application.rb
config.autoload_paths += %W(#{config.root}/lib)

SystemC constructor, class

I am new in systemc. There is one confusion that I am having.
I am creating a sc_module(hello_world). The sc_ctor(hello_world) has nothing between the curly braces and I just have a simple void say_hello() function inside the module which prints "hello world."
In the sc_main, I did this:
hello_world hello;
hello.say_hello();
However, I am getting an error that error C2228: left of '.say_hello' must have class/struct/union.
I tried this and it worked:
in sc_main, I did this:
hello_world hello("hi ");
hello.say_hello();
Why it is showing error in the first place? I didn't use one argument constructor.
So, instead of hello_world hello("hi ") shouldn't it be hello_world hello ? I was just trying to compare with C++ class.
Every SystemC module, whether defined with macro SC_MODULE or inherits sc_module, needs to have a module name. Constructors of SystemC modules must have one parameter of class sc_module_name.
In SystemC standard (IEEE Std 1666-2011)
Every class derived (directly or indirectly) from class sc_module shall have at least one constructor. Every such constructor shall have one and only one parameter of class sc_module_name but may have further parameters of classes other than sc_module_name. That parameter is not required to be the first parameter of the constructor.
If you are using macro SC_CTOR, it is actually a constructor with one sc_module_name parameter!
in sc_module.h:
#define SC_CTOR(user_module_name) \
typedef user_module_name SC_CURRENT_USER_MODULE; \
user_module_name( ::sc_core::sc_module_name )
I canĀ“t see nothing wrong.
In fact, it seems to me, that you have the same code like this example -> http://www.asic-world.com/systemc/first1.html
I hope you could check your with this one.
The macro SC_CTOR has created a hello(const sc_module_name name&) constructor for you. Therefor the compiler will not generate a default constructor for you to call and the object hello cannot be created.
Inbuilt constructor after macro expansion must have an argument.
It is possible that you defined your constructor as private. As a result compiler cannot name it from main.cpp.

Java: Why method type in .class file contains return type, not only signature?

There is a "NameAndType" structure in the constants pool in .class file.
It is used for dynamic binding.
All methods that class can "export" described as "signature + return type".
Like
"getVector()Ljava/util/Vector;"
That breakes my code when return type of the method in some .jar is changed, even if new type is narrower.
i.e:
I have the following code:
List l = some.getList();
External .jar contains:
public List getList()
Than external jar changes method signature to
public ArrayList getList().
And my code dies in run-time with NoSuchMethodException, because it can't find
getList()Ljava/util/List;
So, I have to recompile my code.
I do not have to change it. Just recompile absolutely the same code!
That also gives ability to have two methods with one signature, but different return types! Compiler would not accept it, but it is possible to do it via direct opcoding.
My questions is why?
Why they did it?
I have only one idea: to prevent sophisticated type checking in the runtime.
You need to look up to the hierarchy and check if there is a parent with List interface.
It takes time, and only compiler has it. JVM does not.
Am I right?
thanks.
One reason may be because method overloading (as opposed to overriding) is determined at compile time. Consider the following methods:
public void doSomething(List util) {}
public void doSomething(ArrayList util) {}
And consider code:
doSomething(getList());
If Java allowed the return type to change and did not throw an exception, the method called would still be doSomething(List) until you recompiled - then it would be doSomething(ArrayList). Which would mean that working code would change behavior just for having recompiled it.