⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oaamapper.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                return new IclInt(((Long)value).longValue());
            else if(value instanceof Character)
                return new IclStr(value.toString());
            else if(value instanceof Byte)
                return new IclInt(((Byte)value).intValue());
            else if(value instanceof Short)
                return new IclInt(((Short)value).intValue());
            else if(value instanceof Boolean)
                return new IclStr(value.toString());
            else if(value.getClass().isArray())
            {
                int length = Array.getLength(value);
                IclList iclList = new IclList();
                for(int i = 0; i < length; i++) iclList.add(javaToIcl(Array.get(value, i)));
                return iclList;
            }
            else if(value instanceof Collection)
            {
                IclList iclList = new IclList();
                for(Iterator iterator = ((Collection)value).iterator(); iterator.hasNext();)
                    iclList.add(javaToIcl(iterator.next()));
                return iclList;
            }
            // handle bean:
            else
            {
                ClassMapping classMapping = findClassMapping(value.getClass());
                if(classMapping == null) throw new MappingException("No mapping found for " + value);

                // defer to custom mapper:
                if(classMapping.getMapper() != null) return classMapping.getMapper().javaToIcl(value, this);

                // copy out property values:
                Map propertyValues = new HashMap();
                for(int i = 0; i < classMapping.getProperties().length; i++)
                {
                    PropertyMapping propertyMapping = classMapping.getProperties()[i];
                    Object propertyValue = propertyMapping.getProperty().getReadMethod().invoke(value, null);
                    if(propertyValue != null)
                        propertyValues.put(propertyMapping.getProperty().getName(), propertyValue);
                }

                // walk through the template ICL and replace IclVars with properties:
                IclTerm iclTemplate = IclUtils.fromString(classMapping.getIcl().toString());
                PropertySetter propertySetter = new PropertySetter(propertyValues);
                propertySetter.traverse(iclTemplate);
                return iclTemplate;
            }
        }
        catch(MappingException me)
        {
            throw me;
        }
        catch(Exception e)
        {
            throw new MappingException("Error translating object " + value + ": " + e, e);
        }
    }

    /**
     * Maps ICL to a Java object of the specified type as indicated by the mapping file.
     *
     * @param type
     * @param icl
     * @return
     * @throws MappingException
     */
    public Object iclToJava(Class type, IclTerm icl)
            throws MappingException
    {
        try
        {
            // map lists to arrays and collections:
            if((type.isArray() && icl.isList()))
            {
                Class componentType = type.getComponentType();
                Object value = Array.newInstance(type.getComponentType(), icl.getNumChildren());
                for(int i = 0; i < icl.getNumChildren(); i++)
                    Array.set(value, i, iclToJava(componentType, icl.getTerm(i)));
                return value;
            }
            // handle Strings:
            else if(String.class.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return value;
            }
            // handle Integers:
            else if(Integer.class.isAssignableFrom(type) || Integer.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Integer(value);
            }
            // handle Floats:
            else if(Float.class.isAssignableFrom(type) || Float.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Float(value);
            }
            // handle Longs:
            else if(Long.class.isAssignableFrom(type) || Long.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Long(value);
            }
            // handle Doubles:
            else if(Double.class.isAssignableFrom(type) || Double.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Double(value);
            }

            // handle Characters:
            else if(Character.class.isAssignableFrom(type) || Character.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null || value.length() != 1)
                    throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Character(value.charAt(0));
            }
            // handle Bytes:
            else if(Byte.class.isAssignableFrom(type) || Byte.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Byte(value);
            }
            // handle Shorts:
            else if(Double.class.isAssignableFrom(type) || Double.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Short(value);
            }
            // handle Booleans:
            else if(Boolean.class.isAssignableFrom(type) || Boolean.TYPE.isAssignableFrom(type))
            {
                String value = getValue(icl);
                if(value == null) throw new MappingException("Expecting " + type + ", found: " + icl);
                return new Boolean(value);
            }
            // handle beans:
            else
            {
                ClassMapping classMapping = findClassMapping(type);
                if(classMapping == null) throw new MappingException("No mapping for " + type + " for icl " + icl);

                // defer to custom mapping:
                if(classMapping.getMapper() != null) return classMapping.getMapper().iclToJava(type, icl, this);

                // pull the property values out:
                VarMappingFinder varMappingFinder = new VarMappingFinder(classMapping);
                varMappingFinder.traverse(classMapping.getIcl(), icl);
                if(varMappingFinder.isMatchFailed())
                    throw new MappingException("Mapping failed: " + varMappingFinder.getReasonFailed());
                Map varMapping = varMappingFinder.getVarMapping();

                // assign the properties:
                Object value = classMapping.getMappingClass().newInstance();
                for(int i = 0; i < classMapping.getProperties().length; i++)
                {
                    PropertyMapping propertyMapping = classMapping.getProperties()[i];
                    IclTerm iclValue = (IclTerm)varMapping.get(propertyMapping.getProperty().getName());
                    if(iclValue != null)
                    {
                        Object propertyValue = iclToJava(propertyMapping.getType(), iclValue);

                        // check if we need to map an array to a Collection:
                        Class propertyType = propertyMapping.getProperty().getPropertyType();
                        if(Collection.class.isAssignableFrom(propertyType) && propertyValue.getClass().isArray())
                        {
                            Collection collection = null;
                            if(propertyType.equals(List.class))
                                collection = new LinkedList();
                            else if(propertyType.equals(Set.class))
                                collection = new HashSet();
                            else
                                collection = (Collection)propertyType.newInstance();
                            int length = Array.getLength(propertyValue);
                            for(int j = 0; j < length; j++) collection.add(Array.get(propertyValue, j));
                            propertyMapping.getProperty().getWriteMethod().invoke(value, new Object[]{collection});
                        }
                        else
                            propertyMapping.getProperty().getWriteMethod().invoke(value, new Object[]{propertyValue});
                    }
                }

                return value;
            }
        }
        catch(MappingException me)
        {
            throw me;
        }
        catch(Exception e)
        {
            throw new MappingException("Error translating icl " + icl + " to " + type + ": " + e, e);
        }
    }

    public IclMapping getIclMapping()
    {
        return iclMapping;
    }

    public List getSolvableObjects()
    {
        return solvableObjects;
    }

    ////////////////////////////////////////////////////////////////////
    /**
     * This is called once by the OAAMapperAgent only.     *
     */
    void setLibOaa(LibOaa libOaa)
    {
        this.libOaa = libOaa;
    }
    
    ////////////////////////////////////////////////////////////////////

    private Object callFunction(IclTerm goal, FunctionBinding functionBinding)
            throws MappingException
    {
        try
        {
            // find the object upon which to place this call:
            FunctionMapping functionMapping = functionBinding.getFunctionMapping();
            Object solvableObject = null;
            for(Iterator iterator = solvableObjects.iterator(); iterator.hasNext() && solvableObject == null;)
            {
                Object o = iterator.next();
                if(functionMapping.getFunctionClass().isAssignableFrom(o.getClass())) solvableObject = o;
            }
            if(solvableObject == null)
                throw new MappingException("No object found that satisfies the mapping: " + goal);

            // get parameters:
            Map iclParameters = functionBinding.getVarMapping();
            Object[] functionParameters = new Object[functionMapping.getParameters().length];
            for(int i = 0; i < functionParameters.length; i++)
            {
                ParameterMapping parameterMapping = functionMapping.getParameters()[i];
                IclTerm iclParameter = (IclTerm)iclParameters.get(parameterMapping.getName());
                if(iclParameter == null) throw new MappingException("No function found for icl: " + goal);

                // check if we defer to custom mapping:
                if(parameterMapping.getMapper() != null)
                    functionParameters[i] =
                            parameterMapping.getMapper().iclToJava(parameterMapping.getType(), iclParameter, this);
                else
                    functionParameters[i] = iclToJava(parameterMapping.getType(), iclParameter);
            }
            validateFunctionParameters(functionParameters, functionMapping.getFunctionSignature().getParameterTypes());

            // call function:
            return functionMapping.getFunctionSignature().invoke(solvableObject, functionParameters);
        }
        catch(MappingException me)
        {
            throw me;
        }
        catch(Exception e)
        {
            throw new MappingException("Error calling function " +
                    functionBinding.getFunctionMapping().getFunctionClass().getName() + "." + functionBinding.getFunctionMapping()
                    .getFunctionSignature().getName() + ": " + e, e);
        }
    }

    private void validateFunctionParameters(Object[] functionParameters, Class[] parameterTypes)
            throws MappingException, IllegalAccessException, InstantiationException
    {
        if(functionParameters.length != parameterTypes.length)
            throw new MappingException("Expected " + parameterTypes.length + " parameters for function, only found " +
                    functionParameters.length);
        for(int i = 0; i < functionParameters.length; i++)
        {
            Object functionParameter = functionParameters[i];
            Class parameterType = parameterTypes[i];
            // check if we need to map an array to a Collection:
            if(Collection.class.isAssignableFrom(parameterType) && functionParameter.getClass().isArray())
            {
                Collection collection = null;
                if(parameterType.equals(List.class))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -