Generate code from Metamodel via XTend - code-generation

I have an Ecore model in an existing EMF project and want to print the name of all containing classes into a text file via XTend. How do you achieve this? The XTend examples don't show how to use a model and get Information out of it.

If you only need the EClasses of your Meta-Model then you can get them from your Model Package:
YourEMFModelPackage.eINSTANCE.getEClassifiers() which returns a EList<EClassifier>. Since an EClass is an EClassifier you get all your EClass implementations org.eclipse.emf.ecore.impl.EClassImpl.
For type-safety concerns you probably check if this List only contains EClasses, since all your EDataTypes are also EClassifier.
So this should to the Trick:
EcoreUtil.getObjectsByType(YourEMFModelPackage.eINSTANCE.getEClassifiers(), EcorePackage.eINSTANCE.getEClass())
or:
List<EClass> allEClasses = YourEMFModelPackage.eINSTANCE.getEClassifiers().stream().filter(p -> EClass.class.isInstance(p)).map(m -> EClass.class.cast(m)).collect(Collectors.toList());
Update:
If you don't have your Model-Code generated you're still able to do this, you only need to load your Ecore into a Resource:
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore",
new EcoreResourceFactoryImpl());
Resource resource = resourceSet.getResource(
URI.createFileURI(
"../path/to/your/Ecore.ecore"),
true);
EPackage model = (EPackage) resource.getContents().get(0);
If you have the EPackage then you get your EClass like mentioned above

Related

Extracting data from owl file using java,gwt,eclipse

I have to display content from the owl file namely the class names.. onto my browser, I am using GWT,eclipse to do so, could some one tell me the following :-
1)how do I integrate the owl file with the eclipse project?
2)How do I run queries from my java project to extract class names from the owl file?
3)Where can I get the protege api to nclude into my project?!
You could just store your .owl file anywhere inside your project or on any other location on your harddrive. You just provide a path to it, when you load/store it (see code below).
Take a look at the OWLAPI, it allows you to load an existing ontology and retrieve all classes from it. Your code could look like this:
private static void loadAndPrintEntities() {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
IRI documentIRI = IRI.create("file:///C:/folder/", "your_rontology.owl");
try {
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(documentIRI);
//Prints all axioms, not just classes
ontology.axioms().forEach(a -> System.out.println(a));
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
}
}
Rather than trying to integrate the Protegé API into your project, I suggest you write a plugin for Protegé. There are some great examples that should get you started. Import this project into Eclipse, modify the content, build your plugin and drop it into Protegé. That's it, you're ready to go!

Eclipse ElementTreeSelectionDialog set initial selection from existing path in package form

I need to represent the path of compilation unit in package style, i.e. com.bla.testapp.Main. How do I set the initial selection for ElementTreeSelectionDialog?
I have an Object of a source folder, which i can use to get project where to look:
proj = packageFragmentRoot.getResource().getProject();
Then I tried to apply findMember method to it:
IResource initElement = proj.findMember(text.getText());
// getText returns "com.bla.testapp.Main"
Unfortunately it returns null. How do I correctly implement that?
This works like a charm although:
IPackageFragmentRoot initElement = packageFragmentRoot;
But I'd like to add more convenience to my wizard.

Code First TVF in 6.1.0-alpha1-30113

EF People,
My understanding is that the newly made public APIs for metadata will allow us to add enough metadata in to the model so that TVF can be called and be composable.
If anyone can point me in the right direction I would greatly appreciate it. Without Composable TVF I have to jump through some major work a rounds.
From looking at the unit test it looks like something a long this line of thought:
var functionImport = EdmFunction.Create()
"Foo", "Bar", DataSpace.CSpace,
new EdmFunctionPayload
{
IsComposable = true,
IsFunctionImport = true,
ReturnParameters = new[]
{
FunctionParameter.Create("functionname", EdmType.GetBuiltInType()
EdmConstants.ReturnType,
TypeUsage.Create(collectionTypeMock.Object),
ParameterMode.ReturnValue),
}
});
...
entityContainer.AddFunctionImport(functionImport);
Thanks,
Brian F
Yes, it is now possible in EF6.1. I actually created a custom model convention which allows using store functions in CodeFirst using the newly opened mapping API. The convention is available on NuGet http://www.nuget.org/packages/EntityFramework.CodeFirstStoreFunctions. Here is the link to the blogpost containing all the details: http://blog.3d-logic.com/2014/04/09/support-for-store-functions-tvfs-and-stored-procs-in-entity-framework-6-1/. The project is open source and you can get sources here: https://codefirstfunctions.codeplex.com/

eclipse jdt automatic method stub generation

i am creating java source files using eclipse JDT & AST. There are cases that generated source files are implementing or extending something.
is it possible to add method stubs automatically before actually creating them? like invoking this "Add unimplemented methods" quick fix via JDT.
i know i can add them myself via those API's, but i want to tweak.
i found solution after a couple of hours; code is roughly like this. there are also many good code manipulation classes in this package "org.eclipse.jdt.internal.corext.codemanipulation.*"
ICompilationUnit createCompilationUnit = getItSomeHow();
RefactoringASTParser parser1 = new RefactoringASTParser(AST.JLS3);
CompilationUnit unit = parser1.parse(createCompilationUnit, true);
AbstractTypeDeclaration declaration = (AbstractTypeDeclaration) ASTNodes.getParent(
NodeFinder.perform(unit, createCompilationUnit.getTypes()[0].getNameRange()),
AbstractTypeDeclaration.class);
ITypeBinding binding = declaration.resolveBinding();
IMethodBinding[] overridableMethods = StubUtility2.getOverridableMethods(unit.getAST(), binding, false);
AddUnimplementedMethodsOperation op = new AddUnimplementedMethodsOperation(unit, binding,
null/* overridableMethods */, -1, true, true, true);

Regarding Eclispe EMF Command Frame WorK

Can any one tell me how to use AddCommand rather than `SetCommand" to do the following.
I have a class like this:
class Profile {
List achievements;
List grades;
List extracurrics;
}
Now, suppose I need to add a grade object to this Profile object,how can I achieve this by using AddCommand only
SetCommand is basically used to set values in EMF model, and AddCommand is used to modify collection values inside EMF model, so in general it should not be a problem to use AddCommand.
You can create new AddCommand using static creation function in AddCommand:
AddCommand.create(EditingDomain domain, EObject owner, EStructuralFeature feature, java.lang.Object value)
Explanation of given values:
domain: the editing domain your model lives in
owner: element you are doing the modifications to
feature: feature in model, that should be given to you by the EPackage of your model.
So this case is the Grades list feature
value: the new object you add to the list
There are many different create helpers in add command, so if you need to define index to list, it is also doable.
I don't have EMF running here, so I cannot provide any direct sources, but let me know if that didn't do the trick.
It should look something like this:
Profile p = ...;
Grade g = ...;
Command add = AddCommand.create(domain,p, YourProfilePackage.Literals.PROFILE__GRADES, Collections.singleton(g));
where YourProfilePackage should be in the code generated automatically from your EMF model.