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

📄 normmap.java

📁 normkit is a set of tools supporting ontology learning. from xml,xsd to rdfs.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    private void createNormMapElement() throws Exception {

        KAONUtils.addConceptInstanceToOIModel( m_oimodelCNOInstance,
                                               CNOVocabulary.URI_CNO_NORM_MAP,
                                               m_strURINormMap);

        KAONUtils.addPropertyValueToOIModel( m_oimodelCNOInstance,
                                             m_strURINormMap,
                                             CNOVocabulary.URI_CNO_RELATES_XS_DOCUMENT,
                                             m_strURIXSDocument);

        KAONUtils.addPropertyValueToOIModel( m_oimodelCNOInstance,
                                             m_strURINormMap,
                                             CNOVocabulary.URI_CNO_RELATES_ONTOLOGY,
                                             m_strURIOntology);
    }

    /**
     * Creates and adds new property node to the map.
     * @param newPropertyNode URI of the property node to be added
     */
    public void addPropertyNode( PropertyNode newPropertyNode ) throws Exception
    {
        m_mapPropertyNodes.put( newPropertyNode.getURI(), newPropertyNode );
    }

    /**
     * Creates and adds new node bridge to the map.
     * @param strURINew URI of the node bridge to be added
     */
    public void addNewNodeBridge( String strURINew ) throws Exception
    {

    	NodeBridge newNB = new NodeBridge( this, strURINew );

        // add the new NodeBridge to the pool
        m_mapNodeBridges.put(strURINew, newNB);
    }

    /**
     * Creates and adds new path bridge to the map.
     * @param strURINew URI of the path bridge to be added
     */
    public void addNewPathBridge( String strURINew ) throws Exception
    {
        // the new property bridge
     	PathBridge newPB = new PathBridge( this, strURINew );

        // add the new PropertyBridge to the pool
        m_mapPathBridges.put(strURINew, newPB);
    }

/*
----------------------------------- helper methods --------------------------------------------
*/

    /**
     * Checks whether the given RDFS path links to a literal concept.
     * @param strURIOPath URI of the path (property node) to be examined.
     * @return true if the path links to rdfs:literal
     */
    public boolean isLiteralOPath(String strURIOPath) {

            return (getPropertyNode(strURIOPath).getURIObject()).equals(CNOVocabulary.URI_RDFS_LITERAL);
    }

    /**
     * Creates a new unique URI for this NormMap.
     * @return URI of new NormMap entity (instance)
     */
    public String createNewURIForNormMapInstance() throws Exception
    {
        return m_oimodelCNOInstance.createNewURI();
    }


/*
----------------------------------- saving methods --------------------------------------------
*/

    /**
     * Saves this normalisation map to the associated physical location.
     */
    public void saveNormMap() throws Exception
    {
        TransformationUtil.saveOIModel( m_oimodelCNOInstance, m_strNormMapPhysicalURI );
    }

/*
----------------------------------- getters & setters --------------------------------------------
*/

    /**
     * Returns the normalisation map instance.
     */
    public OIModel getNormMapOIModel()
    {
        return m_oimodelNormMap;
    }

    /**
     * Returns the normalisation map instance.
     */
    public OIModel getNormMapInstance()
    {
        return m_oimodelCNOInstance;
    }

    /**
     * Returns all path bridges of this map.
     */
    public Map getPathBridges() {

        return new HashMap( this.m_mapPathBridges );
    }

    /**
     * Returns all node bridges of this map.
     */
    public Map getNodeBridges() {

        return new HashMap( this.m_mapNodeBridges );
    }

    /**
     * Returns a member Node Bridge instance for the given URI.
     * @param strURINodeBridge URI of the node bridge to be returned
     */
    public NodeBridge getNodeBridge(String strURINodeBridge) {

        Object obj = m_mapNodeBridges.get(strURINodeBridge);

        if( obj instanceof NodeBridge )
            return (NodeBridge) obj;
        else
            return null;
    }

    /**
     * Returns a member Path Bridge instance for the given URI.
     * @param strURIPathBridge URI of the path bridge to be returned
     */
    public PathBridge getPathBridge(String strURIPathBridge) {

        return (PathBridge) m_mapPathBridges.get(strURIPathBridge);
    }

    /**
     * Returns a member Property Node instance for the given URI.
     * @param strURIPropertyNode URI of the property node to be returned
     */
    public PropertyNode getPropertyNode(String strURIPropertyNode) {

        return (PropertyNode) m_mapPropertyNodes.get(strURIPropertyNode);
    }

    public String getRelatedOntologyURI () {

        return m_strURIOntology;

    }

    public NodeBridge getRootNodeBridge() throws NormalisationException {

        try {

            String rootNodeBridgeQuery = "[http://ectrl.itc.it/normkit/NormBridges#NodeBridge] AND NOT FROM (<http://ectrl.itc.it/normkit/NormBridges#inContextOf>)";

            Collection rootQueryResult = m_oimodelNormMap.executeQuery(rootNodeBridgeQuery);

            if (rootQueryResult.size() != 1)
                throw new NormalisationException("NormMap error: there must be exactly one root Node Bridge defined.");

            Iterator i = rootQueryResult.iterator();

            return getNodeBridge(((Instance) i.next()).getURI());

        } catch (Exception e) {
            e.printStackTrace();
            throw new NormalisationException("Exception getting root NodeBridge.", e);
        }

    }

    public void reparseToXMLSchemaDefinedOrder(XMLSchemaNode m_xmlSchema) throws NormalisationException {

        try {

            XSDNode[] topLevelElementDeclarations = m_xmlSchema.getElementSet();

            XSDNode rootElmDeclaration = null;

            NodeBridge rootNodeBridge = getRootNodeBridge();

            String currentXPath = rootNodeBridge.getRelatedXPathEntity();

            for (int i = 0; i < topLevelElementDeclarations.length; i++) {
                if (("/" + topLevelElementDeclarations[i].getName()).equals(currentXPath)) {
                    rootElmDeclaration = topLevelElementDeclarations[i];
                    break;
                }
            }

            traverseXSDNodeChildrenDeclarations((XSDElement)rootElmDeclaration, currentXPath, rootNodeBridge);

        } catch (Exception e) {
            e.printStackTrace();
            throw new NormalisationException("Exception reparsing NormMap.", e);
        }

    }


    private void traverseXSDNodeChildrenDeclarations(XSDElement currentElementDeclaration, String currentXPath, NodeBridge currentNodeBridge) throws Exception {

        XSDNode[] nestedElms;

        XSDNode m_elmType = currentElementDeclaration.getType();

        if (currentNodeBridge != null)
            currentNodeBridge.reparseHasBridgesToXMLSchemaDefinedOrder(currentElementDeclaration, currentXPath);

        // System.out.println("element: " + currentElementDeclaration.getName());

        if (m_elmType.getNodeType()==1) {   //complex type ... not sure whether this condition necessary
            nestedElms = ((XSDComplexType) m_elmType).getElementSet();

            if (nestedElms!=null)
                for (int i = 0; i < nestedElms.length; i++) {

                    XSDNode elmDeclaration = nestedElms[i];

                    String followXPath = currentXPath + "/" + elmDeclaration.getName();

                    NodeBridge followNodeBridge = getNodeBridgeByRelatedXPath(followXPath);

                    if (followNodeBridge != null)
                      if (elmDeclaration instanceof XSDElement)
                        traverseXSDNodeChildrenDeclarations((XSDElement)elmDeclaration, followXPath, followNodeBridge);
                }
        }

    }

    public NodeBridge getNodeBridgeByRelatedXPath(String relatedXPath) throws NormalisationException { // maybe this should return set of NodeBridges instead ... (not in this version of map)

        try {

            String mapQuery = "[http://ectrl.itc.it/normkit/NormBridges#NodeBridge] AND SOME(<http://ectrl.itc.it/normkit/NormBridges#relatesXPathNode>='" + relatedXPath + "')";

            Collection mapQueryResult = m_oimodelNormMap.executeQuery(mapQuery);

            Iterator k = mapQueryResult.iterator();

            for(; k.hasNext(); ) {
                Instance nodeBridge = (Instance) k.next();

                NodeBridge currentNB = getNodeBridge(nodeBridge.getURI());

                return currentNB;
            }

            return null;

        } catch (Exception e) {
            e.printStackTrace();
            throw new NormalisationException("Exception reparsing NormMap.", e);
        }

    }

    public Document compileToXSLSheet() throws NormalisationException {

        try {

            TransformerFactory tFactory = TransformerFactory.newInstance();

            //Instantiate a DocumentBuilderFactory.
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();

            // And setNamespaceAware, which is required when parsing xsl files
            dFactory.setNamespaceAware(true);

            //Use the DocumentBuilderFactory to create a DocumentBuilder.
            DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

            //Use the DocumentBuilder to create new XSL stylesheet.
            Document xslDoc = dBuilder.newDocument();

           // m_xslRoot = createAndAppendXSLElement(xslDoc, xslNS_URI, "xsl:transform", "version", "1.0");
            Element xslRoot = TransformationUtil.createAndAppendXSLElement(xslDoc, xslDoc, "transform", "version", "1.0");
            xslRoot.setAttribute("xmlns:" + CNOVocabulary.NS_PREFIX_RDF, CNOVocabulary.URI_RDF);
            xslRoot.setAttribute("xmlns:" + CNOVocabulary.NS_PREFIX_DNORM, m_strURIOntology + "#");

            Element currentNode = TransformationUtil.createAndAppendXSLElement(xslDoc, xslRoot, "param", "name", "baseURIparam");
            currentNode = TransformationUtil.createAndAppendXSLElement(xslDoc, xslRoot, "template", "match", "/");
            currentNode = TransformationUtil.createAndAppendXSLElement(xslDoc, currentNode, "element", "name", "rdf:RDF");

            Element anode = TransformationUtil.createAndAppendXSLElement(xslDoc, currentNode, "attribute", "name", "xml:base");
            TransformationUtil.createAndAppendXSLElement(xslDoc, anode, "value-of", "select", "$baseURIparam");
            currentNode = TransformationUtil.createAndAppendXSLElement(xslDoc, currentNode, "for-each", "select", "/|//*");
            currentNode = TransformationUtil.createAndAppendXSLElement(xslDoc, currentNode, "apply-templates", null, null);

            Iterator i = m_mapNodeBridges.keySet().iterator();
            for (;i.hasNext();) {

                NodeBridge currentNB = getNodeBridge((String)i.next());

                Element nbTemplate = currentNB.getXSLTemplate(xslDoc);

                if (nbTemplate != null)
                    xslRoot.appendChild(nbTemplate);
            }

            TransformationUtil.createAndAppendXSLElement(xslDoc, xslRoot, "template", "match", "@* | node()");

            return xslDoc;

        } catch (Exception e) {

           e.printStackTrace();
            throw new NormalisationException("Exception compiling NormMap to XSLT.", e);
        }

    }

}

⌨️ 快捷键说明

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