continue to the example of generating a field such as:
def handle (EClass c) {
val attr = EcoreFactory::eINSTANCE.createEAttribute
attr.name = "test"
attr.EType = EcorePackage::eINSTANCE.EString
c.EStructuralFeatures += attr
}
can I generate it as a static field? how to do so?
thanks in advance,
Ecore differs from Java's object model, and unfortunately, EAttributes can't be static like fields in Java can.
Related
I'm trying to use Drools with Scala and i would like to know if it is possible to call a chain of events and create var/val when the function has a return.
Here is what i'm trying but i'm stuck:
import com.models.*
import com.service.*
rule "First Fule"
when
person:Person(name == 'aa')
then
//Here should return a string
//and i should set this string
//something like:
//var x = new Person(ServiceLongDong.sayHello(), person.age, person.name)
//or var y = ServiceLongDong.sayHello();
ServiceLongDong.sayHello();
ServiceLongDong.finish(x);
end
Is possible to create varl/vals and pass it to another function?
Thank in advance.
Rules aren't functions (or methods) and don't "return" values or objects. The right hand side is just Java code. You can call static methods, but stick to correct Java syntax:
Person p = new Person(ServiceLongDong.sayHello(),
person.age, person.name);
ServiceLongDong.sayHello();
ServiceLongDong.finish(x);
This can't be correct Java if ServiceLongDong is a class:
... = ServiceLongDong().sayHello();
Seeing a record already has a public Getter/(Setter) for its fields, is it possible to specify that a record satisfies a matching interface without re-implementing it?
For example:
type IText =
abstract text : string with get,set
type TextRec =
{
mutable text : string
}
Now seeing the Record already implements this interface implicitly, I'd like to put an "inherit IText" or "interface IText" (with no body) on the record, but it appears I can't do that. As it is, I believe I have to re-implement the interface by adding this to the record:
interface IText with
member this.text
with get() = this.text
and set(v) = this.text <- v
thanks
F# currently does not support implicit interface implementations (not even for classes), but it is one of the frequently requested features, so it might happen in the future. I certainly see why this would be useful.
I don't think there is any good workaround for this - the best option is probably to write the additional piece of code needed to implement the interface.
If you wanted to be adventurous, you could try writing a "wrapping" function that creates an interface implementation from a value that provides the required members. Using static member constraints, you can require the members to be there (without actually implementing the interface):
type IText =
abstract Text : string with get, set
let inline wrap (a:^T) =
{ new IText with
member x.Text
with get() = (^T : (member Text : string) (a))
and set(v) = (^T : (member set_Text : string -> unit) (a, v)) }
Static member constraints (used in the implementation of wrap) are mainly useful for generic numerical computations, so this is a bit of a stretch (and certainly an advanced F# feature), but it does the trick:
type Rect = { mutable Text : string }
let i = wrap { Text = "Hi" }
i.Text <- i.Text + " there!"
I am trying to achieve attribute introduction like here but my attributes have property arguments like: [Foo(Bar = "Baz")]
How do I correctly pass the arguments? I'm not copying the attributes from something else, so I don't think I can use CustomAttributeData?
You can set properties of your custom attributes by using ObjectConstruction.NamedArguments dictionary.
For example:
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
Type targetType = (Type) targetElement;
var objectConstruction =
new ObjectConstruction(typeof (MyCustomAttribute).GetConstructor(Type.EmptyTypes));
objectConstruction.NamedArguments["Bar"] = "Baz";
var introduceAttributeAspect = new CustomAttributeIntroductionAspect(objectConstruction);
yield return new AspectInstance(targetType, introduceAttributeAspect);
}
I come from a Java background and as expected, am having problem understanding some patterns used in Scala (see below). Every time I feel that I have a good understanding of Scala patterns or programming methodology, something pops up that is beyond my programming understanding and puts me back in learning mode. I guess that's a beauty of scala that always inspires me to keep learning :)
Anyway I trying to do some sample programming in scala swing.............
val frame = new MainFrame {
title = "Electronic Classroom"
contents = new BorderPanel {
layout += new GridPanel(1, 2) {
contents += new ScrollPane(semesterList)
contents += new ScrollPane(courseList)
} -> West
}
menuBar = new MenuBar {
contents += new Menu("File") {
contents += new MenuItem("Login") {
action = Action("Login"){
login
user match{
case Some(inst:Instructor) => instructorMenu.enabled = true
enabled = false
case Some(_) => instructorMenu.enabled = false
enabled = false
case _ =>
}
}
}
contents += new Separator
contents += new MenuItem(Action("Exit")(sys.exit(0)))
}
}
contents += instructorMenu
}
size = new Dimension(1000, 600)
centerOnScreen
}
Here we are setting values to def and val without using def or val keyword while defining them (like title, size, contents etc) and it's now looking more like a body script which is different that the way we do in java where all the assignments etc takes place in a method body.. I guess I am missing a big design pattern here
Can someone help, abd explain to me the Scala design pattern??
This is actually not very different from Java—instead of creating an instance and then customising it, you are creating anonymous sub classes. E.g.
val frame = new MainFrame {
title = "Electronic Classroom"
}
instead of
val frame = new MainFrame
frame.title = "Electronic Classroom"
The difference to Java is that since Scala doesn't have dedicated constructor methods but treats all expressions within the body of a class part of the constructor, your anonymous sub class kind of "overrides" the constructor.
To compare directly with Java, lets say it wasn't anonymous:
class MyMainFrame extends MainFrame {
title = "Electronic Classroom"
}
In Java, this would be roughly equivalent to:
public class MyMainFrame extends JFrame {
public MyMainFrame() {
super();
setTitle("Electronic Classroom");
}
}
(I hope this is valid Java syntax, I'm a bit rusty)
This is the same case for MenuBar, Menu, MenuItem. Only Action { ... } is not subclassing but calling method apply on the Action companion object, making the syntax a bit more succinct (this way you won't have "constructor" statements, e.g. you couldn't write accelerator = None and so forth).
I subscribe to 0__'s explanation, just wanting to add that if I recall correctly you can do the same in java with
JFrame frame = new JFrame {{
title = "Electronic Classroom";
}};
This should create an anonymous subclass with the additional code appended to the constructor
We are using JDT for generating java source code. We are stuck in generating a class where the class itself is annotated as belows:
#SomeAnnotation({Class1.class, Class2.class})
Please let me know how this can be achieved. I am using NormalAnnotation class for this but could not set the expression accordingly. Though String literals can be set but Class cannot be.
I was able to do this using NormalAnnotation class, here is the code:
// values contains the class names.
NormalAnnotation normalAnnotation = ast.newNormalAnnotation();
Name name = ast.newName(annotationName);
normalAnnotation.setTypeName(name);
ArrayInitializer arrayInit = ast.newArrayInitializer();
for(String value : values){
TypeLiteral tL = ast.newTypeLiteral();
tL.setType(ast.newSimpleType(ast.newName(value)));
arrayInit.expressions().add(tL);
}
MemberValuePair memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
memberValuePair.setValue(arrayInit);
normalAnnotation.values().add(memberValuePair);