Is it possible in a Telosys template to call a function created specifically? - code-generation

I use Telosys (https://www.telosys.org) to generate Python source code and it works fine. But I have a specific need that could be solved by calling a specific conversion function.
Is it possible to create a specific function and to call it inside a Telosys template?
For example: myFunction(“abc”) or $something.myFunction(“abc”) or anything else
If necessary it's possible for me to create this function in different languages ​​like Java, Python or JavaScript.

Telosys is designed to be extensible, so yes you can create your own functions and call them in your templates.
As Telosys is written in Java you will have to create these functions in Java, then use the "loader" object in the ".vm" file to load your class and call the methods defined in this class.
Here's how to do that step by step:
Use your preferred IDE to create a Java class defining your specific method(s). This class can be in any package (including the "default / unnamed package"), the method(s) can be "static" if you don't need an instance of the class.
Compile this class (the goal is to produce a simple ".class" file or a ".jar" file if you prefer)
Put the class (or the jar) in the templates bundle folder :
if you have a ".class" file put it in "classes" folder
if you have a ".jar" file put it in the "lib" folder
Examples :
TelosysTools/templates/my-bundle/classes/MyClass.class
TelosysTools/templates/my-bundle/lib/my-lib.jar
In the template file (".vm") use the "$loader" object to load your Java class and call any of its methods
See "$loader" reference here : http://www.telosys.org/templates-doc/objects/loader.html
If all your methods are “static” you don’t need an instance so just use “$loader.loadClass()”. Example :
## load the class and keep it in a new “$Math” object (no instance created)
#set( $Math = $loader.loadClass("java.lang.Math")
## use the static methods of this class
$Math.random()
If your methods are not “static” so you need an instance, then use “$loader.newInstance()”. Examples :
## create an instance of StringBuilder and put it in the context with #set
#set( $strBuilder = $loader.newInstance('java.lang.StringBuilder') )
## use the instance to call a method
$strBuilder.append('aa')
## create new instance of a specific class : MyTool.class
#set( $tool = $loader.newInstance('MyTool') )
## use the instance to call a method
$tool.myFunction()
So to sum up, you can use any class provided by Java-JRE (eg "Math", “StringBuilder”), you can reuse existing libraries by adding a “.jar” file (don't forget to add dependencies required if the jar file is not stand-alone) or just add a single “.class” file.

Related

Proper way to customize Spark ML estimator (e.g. GaussianMixture) by modified its private method?

My code use apache.ml.clustering.GaussianMixture, but its init method private def initRandom(...) does not work well, so I want to customize a new init method.
At first I want to "extends" class GuassianMixture, but initRandom is a private method.
Then I tried another way, it is to set initial GMM, but sadly source code says that TODO: SPARK-15785 Support users supplied initial GMM.
I've also tried to copy the code of class GuassianMixture for my custom class, but there are too many things attached to it. GaussianMixture.scala comes with sort of classes and traits, some of which are only accessible within ML packages.
I solved it by myself. Here is my solution.
I created class CustomGaussianMixture which extends GaussianMixture from official package org.apache.spark.ml.clustering.
And within my project, I created a new package, also named as org.apache.spark.ml.clustering(to prevent deal with scope of sort of complexity classes/traits/objects in org.apache.spark.ml.clustering). And place my custom class in it.
The next thing is to override the method(fit) call initRandom, a non-private method, so I can override it. Specifically, Just write my new init method in class CustomGaussianMixture, and copy method fit from official source code in GaussianMixture.scala to class CustomGaussianMixture, remember to modify code in CustomGaussianMixture.fit() to call my custom init method.
At last, just use CustomGaussianMixture instead of GaussianMixture when needed.

Swift 2 internal vs private

I'm confused about the internal and private access modifier.
The docs says:
“Internal access enables entities to be used within any source file
from their defining module, but not in any source file outside of that
module. You typically use internal access when defining an app’s or a
framework’s internal structure.”
How I thought it was, was that with internal you can access everything if you are in your own app. But this is not true, because when I have a viewcontroller what is default internal and I'm having a internal function on that viewcontroller I can't access this from another file in another group (You create these in xCode).
What I tried was having a ViewController that has a method foo in group A then in group B I created a ViewController like this:
let vc: FakeViewController = FakeViewController()
vc.foo()
So is internal restricted to the same group? Or I'm I interpreting it wrong?
Is it useful that in a viewcontroller you create private methods and vars/lets?
#user1007522 Could you post the entire source code for FakeViewController? You should have access to foo() from your vc variable. If you do not, I suspect something else is in play here.
I found the following definitions much easier to understand (copied from UseYourLoaf - Swift 4 Access Levels)
The Five Access Levels of Swift 3/4
Swift 3 has five access levels that control from which source file or module you can access something. In order from most open to most restricted:
open you can access open classes and class members from any source file in the defining module or any module that imports that module. You can subclass an open class or override an open class member both within their defining module and any module that imports that module.
public allows the same access as open - any source file in any module - but has more restrictive subclassing and overriding. You can only subclass a public class within the same module. A public class member can only be overriden by subclasses in the same module. This is important if you are writing a framework. If you want a user of that framework to be able to subclass a class or override a method you must make it open.
internal allows use from any source file in the defining module but not from outside that module. This is generally the default access level.
fileprivate allows use only within the defining source file.
private Swift 4: allows use only from the enclosing declaration and new in Swift 4, to any extensions of that declaration in the same source file Swift 3: allows use only from the enclosing declaration.
Suppose you have 3 different view controller source files A, B, C
then
In Private:- If Intancses in A are Private than only A's Methods can use them
In Internal :- IF A is as Internal than B and C can easily use them.
Here is an example:
Thanks
Internal access restricts access to the files within a singular application or framework.
Private restricts access to the individual source file that your object is created in.
See this link for a more in-depth explanation.
Overall, if your "Group A" and "Group B" are in the same application or framework, you should be able to access the methods from each, assuming the viewController allows internal access.
My understanding is that private won't allow the variable from being accessed from outside that class. However, there are times, like with gesture recognizers, you can't make them private because they are needed behind the scenes. Marking them as "internal" lets them be accessed from within other functions, but not called directly.
Mostly I use internal to keep my code organized, so I know that's not a public facing function but it can still be used.

How to define own functions in zend framework

Actually I wanted to know that is there any way to define and call a function in Zend framework which does not need to put with $this like pr() function in cake php. I want to define a such a function which can be called directly in all controllers and views in zend framework. Can it be done by putting all functions in helper and use them without writing $this in front of them.
For example I wanted to make a function arrprint() to print the array in helper and use it globally with only by writing simply arrprint();
Please help me
You could just create a file anywhere and put the function in there. The just include it like you would any other files.
Or create a library called Utils and put your custom functions there as static and call them using:
Utils::myFunction();
Contrary to cake zf does not enforce much anything. You can leverage the framework to do whatever you want.
BTW $this is just a pointer to the current class and when you use $this->myFunction(); it's because that function is a member of the current class (or its parent).

CodeDom - Linking multiple classes within a single Assembly

I have a C# application that I am trying to re-create through the use of CodeDom. This application has four classes inside of it. If I were to go into this applications directory, I would find the project file (App.csproj), and if I were to start this project file, all four classes would load together. Furthermore, if I were to build this application, all four classes would build together.
My Question: How on earth can I create this functionality through the use of CodeDom?
I have sucessfully created one of the four classes using CodeDom, but how can I go about creating the next three classes (and linking them) to the first class that I already created?
I know this may sound confusing but I will explain more if necessary.
If the classes are in the same namespace you can add them all to one CodeNamespace object and generate the code from that.
If there in different namespaces you can add the namespace of the other Classes to your first class by adding the namespaces reference of the other class's to the namespace object you are working in:-
// Add the Namespace of the other class to the current namespace onject
defaultNameSpace.Imports.Add(new CodeNamespaceImport("Project.Namespace.Namespace"));
Where defaultNameSpace is a type of CodeNamespace. The first Class you have built is added to this CodeNamespace object as below and then the code is generated from that :-
defaultNameSpace.Types.Add(mainClass);
mainClass being a type of CodeTypeDeclaration.
Hope this helps.

Where do I place base action class in Symfony plugin?

I'm creating a plugin for my symfony project, which includes a base action class, i.e.:
<?php
// myActions.class.php
class myActions extends sfActions {
/* ... */
}
Where in my plugin folder (e.g.: plugins/sfMyPlugin/???) should I place this file?
The goal is to have actions that are NOT a part of this plugin extend this class, hopefully having the class be autoloaded (similar to if it were placed under apps/my_app/lib). If it can't be autoloaded, how do I get symfony to include my php file?
You typically put it in your plugin's lib directory. The general conventions is also to to name with Base in the name so given your example that would be BasemyActions. Then you would also make an empty concrete class called myActions and you would extend that within your plugin thus allowing other user to complety replace myActions with their own implementation (so long as it extends the base class) or to simply extend myActions.
you can place it in the lib directory of your plugin. This is what the generate:plugin-module task of the sfTaskExtraPlugin does.