Search in sources :

Example 16 with EntityDescriptor

use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.

the class QueriesDynamicCompilerVisitor method visit.

@Override
public void visit(Choose entity) {
    int size = entity.wSize();
    if (size == 1)
        entity.get(0).accept(this);
    else {
        boolean canOptimize = true;
        ILanguageKit languageKit = null;
        Map<EntityDescriptor<?>, Expression> typeMap = new HashMap<EntityDescriptor<?>, Expression>();
        If ifWithTemplate = new IfWithTemplate().create();
        If ifWithTypeTest = new IfWithTypeTest().create();
        ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
        getBindings().wEnterScope(ts);
        for (int i = 0; i < size; i++) {
            Expression child = entity.get(i);
            try {
                if (!Matcher.match(ifWithTemplate, child, getBindings()) && !Matcher.match(ifWithTypeTest, child, getBindings())) {
                    canOptimize = false;
                    break;
                }
                EntityDescriptor<?> ed = getBindings().wIsSet("typeTest") && getBindings().wGet("typeTest").wGetEntityDescriptor().getDataKind().isString() ? CommonsDataTypePersistenceParser.getEntityDescriptor(getBindings().wStringValue("typeTest")) : getBindings().wIsSet("pattern") ? getBindings().wGet("pattern").wGetEntityDescriptor() : null;
                if (ed == null) {
                    canOptimize = false;
                    break;
                }
                if (typeMap.containsKey(ed)) {
                    Expression behavior = typeMap.get(ed);
                    boolean isPattern = behavior.wGetParent() == entity;
                    if (isPattern) {
                        canOptimize = false;
                        break;
                    }
                } else {
                    if (languageKit == null)
                        languageKit = ed.getLanguageKit();
                    else if (!languageKit.equals(ed.getLanguageKit())) {
                        // ||
                        // !languageKit.getURI().equals("whole:org.whole.lang.javascript:JavaScript"))
                        // {
                        canOptimize = false;
                        break;
                    }
                    typeMap.put(ed, getBindings().wIsSet("pattern") ? child : getBindings().wGet("expression").wGetAdapter(QueriesEntityDescriptorEnum.Expression));
                }
            } finally {
                ts.rollback();
            }
        }
        getBindings().wExitScope();
        if (canOptimize && languageKit != null) {
            ChooseByTypeIterator<IEntity> chooseIterator = IteratorFactory.chooseIterator(languageKit);
            for (Entry<EntityDescriptor<?>, Expression> entry : typeMap.entrySet()) {
                Set<String> oldDeclaredNames = declaredNames;
                entry.getValue().accept(this);
                chooseIterator.setCase(entry.getKey(), getResultIterator());
                declaredNames = oldDeclaredNames;
            }
            setResultIterator(chooseIterator.withSourceEntity(entity));
        } else {
            IEntityIterator<? extends IEntity>[] iteratorChain = new IEntityIterator<?>[size];
            for (int i = 0; i < size; i++) {
                entity.get(i).accept(this);
                iteratorChain[i] = getResultIterator();
            }
            setResultIterator(IteratorFactory.chooseIterator(iteratorChain).withSourceEntity(entity));
        }
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) HashMap(java.util.HashMap) IEntity(org.whole.lang.model.IEntity) ILanguageKit(org.whole.lang.reflect.ILanguageKit) EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) IfWithTypeTest(org.whole.lang.queries.codebase.IfWithTypeTest) IfWithTemplate(org.whole.lang.queries.codebase.IfWithTemplate) IEntityIterator(org.whole.lang.iterators.IEntityIterator)

Example 17 with EntityDescriptor

use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.

the class DBSchemaTemplateFactoryTest method testRDBSchemaTemplateFactory.

@Test
public void testRDBSchemaTemplateFactory() throws Exception {
    IBindingManager bm = BindingManagerFactory.instance.createArguments();
    bm.wDefValue("connection", connection);
    bm.wDefValue("generateDropStatements", false);
    Database template = new CycleModel().create();
    Database database = EntityUtils.clone(template);
    Matcher.removeVars(database, true);
    InterpreterOperation.interpret(database, bm);
    DBSchemaTemplateFactory dbSchemaTemplateFactory = new DBSchemaTemplateFactory(connection, null, "PUBLIC");
    Database generatedDatabase = dbSchemaTemplateFactory.create();
    HashMap<EntityDescriptor<?>, Comparator<IEntity>> comparatorsMap = new HashMap<EntityDescriptor<?>, Comparator<IEntity>>();
    comparatorsMap.put(RDBEntityDescriptorEnum.Tables, new OrderedMatcher.SimpleFeatureComparator(RDBFeatureDescriptorEnum.name));
    Assert.assertTrue(OrderedMatcher.match(template, generatedDatabase, comparatorsMap));
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) HashMap(java.util.HashMap) IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) Database(org.whole.lang.rdb.model.Database) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 18 with EntityDescriptor

use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.

the class CompiledMappingStrategy method isMixedType.

public boolean isMixedType(EntityDescriptor<?> ed) {
    if (mixedTypes == null) {
        mixedTypes = new HashSet<EntityDescriptor<?>>();
        Types uris = strategy.getMixedTypes();
        for (int i = 0, size = uris.wSize(); i < size; i++) mixedTypes.add(parseEntityDescriptor(uris.wGet(i).wStringValue()));
    }
    return mixedTypes.contains(ed);
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) Types(org.whole.lang.xsd.mapping.model.Types) SchemaDataTypes(org.whole.lang.xsd.parsers.SchemaDataTypes)

Example 19 with EntityDescriptor

use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.

the class CommonsContentAssistVisitor method visit.

public void visit(VarType entity) {
    try {
        IEntity variableEntity = entity.wGetParent();
        IEntity variableEntityParent = variableEntity.wGetParent();
        Set<EntityDescriptor<?>> assignableSet = getAllAssignableTo(variableEntityParent.wGetEntityDescriptor(variableEntity));
        assignableSet.remove(entity.getValue());
        List<VarType> assignableEntities = new ArrayList<VarType>(assignableSet.size());
        for (EntityDescriptor<?> ed : assignableSet) assignableEntities.add(CommonsEntityFactory.instance.createVarType(ed));
        Collections.sort(assignableEntities, new Comparator<VarType>() {

            public int compare(VarType e1, VarType e2) {
                return CommonsDataTypePresentationParser.unparseEntityDescriptor(e1.getValue()).compareToIgnoreCase(CommonsDataTypePresentationParser.unparseEntityDescriptor(e2.getValue()));
            }
        });
        getOperation().setResult(assignableEntities.toArray(new IEntity[0]));
    } catch (Exception e) {
    // do nothing on error
    }
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList) VarType(org.whole.lang.commons.model.VarType)

Example 20 with EntityDescriptor

use of org.whole.lang.reflect.EntityDescriptor in project whole by wholeplatform.

the class DefaultValueFactory method setUniqueDefault.

public static void setUniqueDefault(IEntity entity) {
    final String defaultPrefix = entity.wStringValue();
    entity.wAddRequestEventHandler(new IdentityRequestEventHandler() {

        int max = 0;

        void updateMax(int value) {
            if (value > max)
                max = value;
        }

        public String notifyRequested(final IEntity source, FeatureDescriptor feature, String value) {
            if (value.equals(defaultPrefix)) {
                final EntityDescriptor<?> sourceDescriptor = source.wGetEntityDescriptor();
                final EntityDescriptor<?> parentDescriptor = source.wGetParent().wGetEntityDescriptor();
                GenericTraversalFactory.instance.topDown(new AbstractVisitor() {

                    public void visit(IEntity entity) {
                        if (entity == source)
                            return;
                        if (sourceDescriptor.equals(entity.wGetEntityDescriptor()) && parentDescriptor.equals(entity.wGetParent().wGetEntityDescriptor())) {
                            String strValue = entity.wStringValue();
                            if (strValue.startsWith(defaultPrefix)) {
                                try {
                                    int value = Integer.parseInt(strValue.substring(defaultPrefix.length()));
                                    updateMax(value);
                                } catch (NumberFormatException e) {
                                } catch (IndexOutOfBoundsException e) {
                                }
                            }
                        }
                    }
                }, false).visit(EntityUtils.getCompoundRoot(source));
                source.wSetValue(value = defaultPrefix + ++max);
                source.wRemoveRequestEventHandler(this);
            }
            return value;
        }
    });
}
Also used : EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) AbstractVisitor(org.whole.lang.visitors.AbstractVisitor) IEntity(org.whole.lang.model.IEntity) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor)

Aggregations

EntityDescriptor (org.whole.lang.reflect.EntityDescriptor)29 IEntity (org.whole.lang.model.IEntity)13 ArrayList (java.util.ArrayList)9 ILanguageKit (org.whole.lang.reflect.ILanguageKit)7 HashMap (java.util.HashMap)5 EntityDescriptorEnum (org.whole.lang.reflect.EntityDescriptorEnum)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)4 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)4 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)4 Actions (org.whole.lang.actions.model.Actions)3 GroupAction (org.whole.lang.actions.model.GroupAction)3 ActionsUIEntityFactory (org.whole.lang.actions.ui.factories.ActionsUIEntityFactory)3 IBindingManager (org.whole.lang.bindings.IBindingManager)3 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)3 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 List (java.util.List)2 IAction (org.eclipse.jface.action.IAction)2 Test (org.junit.Test)2 Database (org.whole.lang.rdb.model.Database)2