Search in sources :

Example 11 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class ScriptsInterpreterVisitor method visit.

@Override
public void visit(Script entity) {
    Writer outputWriter = op().getWriter();
    IBindingManager bm = getBindings();
    BindingsAdapter bindingsAdapter = new BindingsAdapter(bm, true);
    LanguageName languageName = entity.getLanguageName();
    /*
		 * TODO
		 * if contains an upper level fragment fail
		 */
    Source source = entity.getSource();
    IEntity sourceRoot = EntityUtils.isFragment(source) ? source.wGetRoot() : source;
    IEntity result0 = bm.getResult();
    IVisitor ov = op().setVisitor(sourceRoot, 0, op().getVisitor(sourceRoot, 1));
    op().stagedVisit(source);
    IEntity sourceModel = bm.getResult();
    // workaround restore previous value (also for self and queryIterator?)
    bm.setResult(result0);
    op().setVisitor(sourceRoot, 0, ov);
    String sourceString = toPrettyPrintString(sourceModel);
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(languageName.getValue());
    if (scriptEngine == null)
        throw new VisitException("Cannot find an interpreter for the language: " + languageName);
    scriptEngine.setBindings(bindingsAdapter, ScriptContext.ENGINE_SCOPE);
    PrintWriter printWriter = new PrintWriter(outputWriter);
    scriptEngine.getContext().setWriter(printWriter);
    scriptEngine.getContext().setErrorWriter(printWriter);
    Object result = null;
    bm.wEnterScope();
    do {
        try {
            result = scriptEngine.eval(sourceString);
            break;
        } catch (ScriptException e) {
            bm.wExitScope();
            if (!bindingsAdapter.isAutoboxing())
                throw new VisitException("Script Interpreter failure.", e);
            bindingsAdapter.setAutoboxing(false);
            bm.wEnterScope();
        }
    } while (true);
    bm.wExitScope(true);
    if (result instanceof IEntity)
        bm.setResult((IEntity) result);
    else if (result != null)
        bm.setResult(BindingManagerFactory.instance.createSpecificValue(result));
}
Also used : IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) VisitException(org.whole.lang.visitors.VisitException) ScriptEngineManager(javax.script.ScriptEngineManager) BindingsAdapter(org.whole.lang.scripts.bindings.BindingsAdapter) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) Source(org.whole.lang.scripts.model.Source) ScriptEngine(javax.script.ScriptEngine) ScriptException(javax.script.ScriptException) IBindingManager(org.whole.lang.bindings.IBindingManager) LanguageName(org.whole.lang.scripts.model.LanguageName) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 12 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class ModelMergeViewer method refresh.

protected void refresh(Object input) {
    IMergeViewerContentProvider contentProvider = getContentProvider();
    if (contentProvider != null && input != null) {
        readSideModel(MergeSide.ANCESTOR, contentProvider.getAncestorContent(input), contentProvider.getAncestorLabel(input));
        readSideModel(MergeSide.LEFT, contentProvider.getLeftContent(input), contentProvider.getLeftLabel(input));
        readSideModel(MergeSide.RIGHT, contentProvider.getRightContent(input), contentProvider.getRightLabel(input));
        setDirty(false);
        IBindingManager bm = BindingManagerFactory.instance.createArguments();
        BehaviorUtils.apply("whole:org.whole.lang.changes:RevisionsLibrarySemantics#compare", viewer.getEntityContents(), bm);
    // viewer.rebuildNotation();
    }
}
Also used : IBindingManager(org.whole.lang.bindings.IBindingManager) IMergeViewerContentProvider(org.eclipse.compare.contentmergeviewer.IMergeViewerContentProvider)

Example 13 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class BytecodeGeneratorTest method testASM.

@Test
public void testASM() throws ClassNotFoundException {
    IEntity program = new ExampleTest().create();
    IBindingManager args = BindingManagerFactory.instance.createArguments();
    final byte[] code = BytecodeGeneratorOperation.compile(program, args);
    ReflectionFactory.setUserClassLoader(new ClassLoader() {

        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            if ("Example".equals(name))
                return defineClass("Example", code, 0, code.length);
            throw new ClassNotFoundException("Unable to find class: " + name);
        }
    });
    Class<?> exampleClass = ReflectionFactory.getPlatformClassLoader().loadClass("Example");
    try {
        Method main = exampleClass.getMethod("test", new Class[0]);
        main.invoke(exampleClass.newInstance(), new Object[0]);
    } catch (Exception e) {
        fail();
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) BeforeClass(org.junit.BeforeClass) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 14 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class ArtifactsWorkspaceUtils method getPersistenceKits.

public static IEntity getPersistenceKits(FileArtifact fileArtifact) {
    IEntity persistenceKitIds = BindingManagerFactory.instance.createTuple();
    IBindingManager bindings = BindingManagerFactory.instance.createBindingManager();
    bindPath(fileArtifact, bindings, false);
    if (bindings.wIsSet("persistenceKitId")) {
        persistenceKitIds.wAdd(BindingManagerFactory.instance.createSpecificValue(ReflectionFactory.getPersistenceKit(bindings.wStringValue("persistenceKitId"))));
        return persistenceKitIds;
    } else
        return getPersistenceKits();
}
Also used : IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager)

Example 15 with IBindingManager

use of org.whole.lang.bindings.IBindingManager in project whole by wholeplatform.

the class ArtifactsWorkspaceUtils method getArtifacts.

public static IEntity getArtifacts(IEntity artifact) {
    ArtifactsEntityFactory aef = ArtifactsEntityFactory.instance(RegistryConfigurations.RESOLVER);
    IEntity children = BindingManagerFactory.instance.createTuple();
    if (Matcher.match(ArtifactsEntityDescriptorEnum.Workspace, artifact)) {
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        for (IProject project : workspaceRoot.getProjects()) {
            Project projectArtifact = aef.createProject();
            projectArtifact.setName(aef.createName(project.getName()));
            projectArtifact.setArtifacts(aef.createArtifacts(0));
            children.wAdd(projectArtifact);
        }
    } else {
        try {
            IBindingManager bindings = BindingManagerFactory.instance.createBindingManager();
            bindPath(artifact, bindings, false);
            IContainer container = (IContainer) (bindings.wIsSet("folder") ? bindings.wGetValue("folder") : bindings.wGetValue("project"));
            IJavaElement javaContainer = JavaCore.create(container);
            if (javaContainer == null || javaContainer.getElementType() == IJavaElement.JAVA_PROJECT) {
                for (IResource resource : container.members()) {
                    if (resource.getType() == IResource.FILE) {
                        appendFileArtifact(((IFile) resource).getName(), children);
                    } else {
                        FolderArtifact folderArtifact = aef.createFolderArtifact();
                        Metadata metadata = aef.createMetadata();
                        metadata.setAttributes(aef.createAttributes(aef.createAttribute(AttributeEnum.source)));
                        folderArtifact.setMetadata(metadata);
                        folderArtifact.setName(aef.createName(StringUtils.getFileName(resource.getName())));
                        folderArtifact.setArtifacts(aef.createArtifacts(0));
                        children.wAdd(folderArtifact);
                    }
                }
            } else {
                if (javaContainer.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
                    IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) javaContainer;
                    if (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        for (IJavaElement element : fragmentRoot.getChildren()) {
                            if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
                                if (element.getElementName() == IPackageFragment.DEFAULT_PACKAGE_NAME) {
                                    appendPackageFragmentChildren((IPackageFragment) element, children);
                                } else {
                                    PackageArtifact packageArtifact = aef.createPackageArtifact();
                                    packageArtifact.setName(aef.createName(StringUtils.getFileName(element.getElementName())));
                                    packageArtifact.setArtifacts(aef.createArtifacts(0));
                                    children.wAdd(packageArtifact);
                                }
                            }
                        }
                    }
                } else
                    // IJavaElement.PACKAGE_FRAGMENT
                    appendPackageFragmentChildren((IPackageFragment) javaContainer, children);
            }
        } catch (Exception e) {
        }
    }
    return children;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IEntity(org.whole.lang.model.IEntity) Metadata(org.whole.lang.artifacts.model.Metadata) FolderArtifact(org.whole.lang.artifacts.model.FolderArtifact) IProject(org.eclipse.core.resources.IProject) PackageArtifact(org.whole.lang.artifacts.model.PackageArtifact) CoreException(org.eclipse.core.runtime.CoreException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ArtifactsEntityFactory(org.whole.lang.artifacts.factories.ArtifactsEntityFactory) Project(org.whole.lang.artifacts.model.Project) IProject(org.eclipse.core.resources.IProject) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IBindingManager(org.whole.lang.bindings.IBindingManager) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Aggregations

IBindingManager (org.whole.lang.bindings.IBindingManager)223 IEntity (org.whole.lang.model.IEntity)141 Test (org.junit.Test)91 PathExpression (org.whole.lang.queries.model.PathExpression)49 ITemplateManager (org.whole.lang.templates.ITemplateManager)40 Grammar (org.whole.lang.grammars.model.Grammar)28 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)27 ESelectionService (org.eclipse.e4.ui.workbench.modeling.ESelectionService)17 Model (org.whole.lang.models.model.Model)15 Production (org.whole.lang.grammars.model.Production)14 IEntityPartViewer (org.whole.lang.ui.viewers.IEntityPartViewer)14 File (java.io.File)12 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)12 XmlModel (org.whole.lang.models.codebase.XmlModel)11 SimpleEntity (org.whole.lang.models.model.SimpleEntity)10 VisitException (org.whole.lang.visitors.VisitException)9 IBindingScope (org.whole.lang.bindings.IBindingScope)6 ModelBuilderOperation (org.whole.lang.builders.ModelBuilderOperation)6 HashMap (java.util.HashMap)5 IFile (org.eclipse.core.resources.IFile)5