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

📄 unparser.java

📁 Jena推理机
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            if (l.getLanguage().equals("")) {
                // j.cook.up bug fix
                if (prettyWriter.isDefaultNamespace(getNameSpace(p)))
                    return false;

                String str = l.getString();
                if (str.length() < 40) {
                    char buf[] = str.toCharArray();
                    for (int i = 0; i < buf.length; i++) {
                        // See http://www.w3.org/TR/REC-xml#AVNormalize
                        if (buf[i] <= ' ' 
                        	|| buf[i] == 0xFFFF
                        	|| buf[i] == 0xFFFE)
                            return false;
                    }
                    return !wantReification(s);
                }
            }
        }
        return false;
    }

    private boolean allPropsAreAttr(Resource r) {
        ClosableIterator ss = listProperties(r);
        Set seen = new HashSet();
        try {
            while (ss.hasNext()) {
                Statement s = (Statement) ss.next();
                if (!canBeAttribute(s, seen))
                    return false;
            }
        } finally {
            ss.close();
        }
        return true;
    }

    private void done(Statement s) {
        doneSet.add(s);
        // return false;
    }

    /**
     * If r represent a daml:collection return a 2D array of its statements. For
     * each member there are three statements the first gives the DAML.first
     * statement, the second the DAML.rest statement and the third the RDF.type
     * statement.
     * 
     * @return null on failure or the elements of the collection.
     * 
     */
    private Statement[][] getDamlList(RDFNode r) {
        return prettyWriter.sDamlCollection ? null : getList(r, DAML_OIL.List,
                DAML_OIL.first, DAML_OIL.rest, DAML_OIL.nil);
    }

    private Statement[][] getRDFList(RDFNode r) {
        return prettyWriter.sParseTypeCollectionPropertyElt ? null : getList(r,
                null, RDF.first, RDF.rest, RDF.nil);
    }

    private Statement[][] getList(RDFNode r, Resource list, Property first,
            Property rest, Resource nil) {
       
        Vector rslt = new Vector();
        Set seen = new HashSet();
        RDFNode next = r;
        // We walk down the list and check each member.
        try {

            while (!next.equals(nil)) {
                Statement elt[] = new Statement[list == null ? 2 : 3];
                if (next instanceof Literal)
                    return null;
                Resource res = (Resource) next;
                // We cannot label the nodes in the daml:collection
                // construction.
                if (!isGenuineAnon(res))
                    return null;
                // The occurs check - cyclic loop rather than a list.
                if (seen.contains(next))
                    return null;
                seen.add(next);

                // We must have exactly three properties.
                StmtIterator ss = res.listProperties();
                try {
                    while (ss.hasNext()) {
                        Statement s = ss.nextStatement();
                        Property p = s.getPredicate();
                        int ix;
                        RDFNode obj = s.getObject();
                        if (doneSet.contains(s))
                            return null;
                        if (!(obj instanceof Resource)) {
                            return null;
                        }
                        if (p.equals(RDF.type)) {
                            ix = 2;
                            if (!obj.equals(list))
                                return null;
                        } else if (p.equals(first)) {
                            ix = 0;
                        } else if (p.equals(rest)) {
                            ix = 1;
                            next = obj;
                        } else {
                            return null;
                        }
                        if (elt[ix] != null)
                            return null;
                        elt[ix] = s;
                    }
                } finally {
                    ss.close();
                }
                for (int i = 0; i < elt.length; i++)
                    if (elt[i] == null)
                        // didn't have the three required elements.
                        return null;
                rslt.add(elt);
            }
            if (rslt.size() == 0)
                return null;
        } finally {
            
        }
        Statement array[][] = new Statement[rslt.size()][];
        rslt.copyInto(array);
        return array;

    }

    /**
     * @return A statement that is suitable for a typed node construction or
     *         null.
     */
    private Statement getType(Resource r) {
        Statement rslt;
        try {
            if (r instanceof Statement) {
                rslt = ((Statement) r).getStatementProperty(RDF.type);
                if (rslt == null || (!rslt.getObject().equals(RDF.Statement)))
                    error("Statement type problem");
            } else {
                rslt = r.getRequiredProperty(RDF.type);
            }
        } catch (PropertyNotFoundException rdfe) {
            if (r instanceof Statement)
                error("Statement type problem");
            rslt = null;
        }
        if (rslt == null || isOKType(rslt.getObject()) == -1)
            return null;

        return rslt;
    }

    /**
     * @param n
     *            The value of some rdf:type (precondition).
     * @return The split point or -1.
     */

    private int isOKType(RDFNode n) {

        if (!(n instanceof Resource))
            return -1;
        if (((Resource) n).isAnon())
            return -1;
        // Only allow resources with namespace and fragment ID
        String uri = ((Resource) n).getURI();

        int split = Util.splitNamespace(uri);
        if (split == 0 || split == uri.length())
            return -1;

        return split;
    }

    /**
     * The order of outputting the resources. This all supports wObjStar.
     */
    private Set infinite;

    private void findInfiniteCycles() {
        // find all statements that haven't been done.
        StmtIterator ss = model.listStatements();
        Relation relation = new Relation();
        try {
            while (ss.hasNext()) {
                Statement s = ss.nextStatement();
                if (!doneSet.contains(s)) {
                    RDFNode rn = s.getObject();
                    if (rn instanceof Resource) {
                        relation.set(s.getSubject(), rn);
                    }
                }
            }
        } finally {
            ss.close();
        }
        relation.transitiveClosure();
        infinite = relation.getDiagonal();
    }

    /**
     * This class is an iterator over the set infinite, but we wait until it is
     * used before instantiating the underlying iterator.
     */
    private Iterator allInfiniteLeft() {
        return new LateBindingIterator() {
            public Iterator create() {
                return infinite.iterator();
            }
        };
    }

    private Iterator pleasingTypeIterator() {
        if (pleasingTypes == null)
            return new NullIterator();
        Map buckets = new HashMap();
        Set bucketArray[] = new Set[pleasingTypes.length];
        // Set up buckets and bucketArray. Each is a collection
        // of the same buckets, one ordered, the other hashed.
        for (int i = 0; i < pleasingTypes.length; i++) {
            bucketArray[i] = new HashSet();
            buckets.put(pleasingTypes[i], bucketArray[i]);
        }

        ResIterator rs = model.listSubjects();
        try {
            while (rs.hasNext()) {
                Resource r = rs.nextResource();
                Statement s = getType(r);
                if (s != null) {
                    Set bucket = (Set) buckets.get(s.getObject());
                    if (bucket != null) {
                        if (isGenuineAnon(r)) {
                            Integer v = (Integer) objectTable.get(r);
                            if (v != null && v.intValue() == 1)
                                continue;
                        }
                        bucket.add(r);
                    }
                }
            }
        } finally {
            rs.close();
        }

        // Now all the pleasing resources are in the buckets.
        // Add all their iterators togethor:

        return new IteratorIterator(new Map1Iterator(new Map1() {
            public Object map1(Object bkt) {
                return ((Set) bkt).iterator();
            }
        }, new ArrayIterator(bucketArray)));

    }

    /**
     * listSubjects - generates a list of subjects for the wObjStar rule. We
     * wish to order these elegantly. The current implementation goes for:
     * <ul>
     * <li> The current file - mainly intended for good DAML.
     * <li> Subjects that are not objects of anything, excluding reifications
     * <li> At these stage we evaluate a dependency graph of the remaining
     * resources.
     * <li>non-anonymous resources that are the object of more than one rule
     * that are in infinite cycles.
     * <li> any non genuinely anonymous resources that are in infinite cycles
     * <li>any other resource in an infinite cyle
     * <li>any other resource.
     * <li>reifications
     * </ul>
     * 
     * 
     * At the end, we need to close any underlying ResIterators from the model,
     * however to avoid complications in much of this code we use general
     * java.util.Iterator-s. We hence use a wrapper around a ResIterator to
     * allow us to manage the closing issue.
     */
    private Iterator listSubjects() {
        // The current file - mainly intended for good DAML.
        Iterator currentFile =
        // new ArrayIterator(
        // new Resource[] { model.createResource(this.localName)});
        new SingletonIterator(model.createResource(this.localName));
        // The pleasing types
        Iterator pleasing = pleasingTypeIterator();

        Iterator fakeStopPleasing = new NullIterator() {
            public boolean hasNext() {
                pleasingTypeSet = new HashSet();
                return false;
            }
        };

        // Subjects that are not objects of anything.
        Iterator nonObjects = new FilterIterator(new Filter() {
            public boolean accept(Object o) {
                return (!objectTable.containsKey(o))
                        && (!wantReification((Resource) o));
            }
        }, modelListSubjects());
        // At these stage we evaluate a dependency graph of the remaining
        // resources.
        // This is stuck in the master iterator so that it's hasNext is called
        // at an appropriate time (after the earlier stages, before the later
        // stages).
        // We use this to trigger the dependency graph evalaution.
        Iterator fakeLazyEvaluator = new NullIterator() {
            public boolean hasNext() {
                // Evalaute dependency graph.
                findInfiniteCycles();
                return false;
            }
        };
        // non-anonymous resources that are the object of more than one
        // triple that are in infinite cycles.
        Iterator firstChoiceCyclic = new FilterIterator(new Filter() {
            public boolean accept(Object o) {
                Resource r = (Resource) o;
                codeCoverage[4]++;
                if (r.isAnon())
                    return false;
                Integer cnt = (Integer) objectTable.get(r);
                if (cnt == null || cnt.intValue() <= 1)
                    return false;
                return true;
            }
        }, this.allInfiniteLeft());
        // any non genuinely anonymous resources that are in infinite cycles
        Iterator nonAnonInfinite = new FilterIterator(new Filter() {
            public boolean accept(Object o) {
                codeCoverage[5]++;
                Resource r = (Resource) o;
                return !isGenuineAnon(r);
            }
        }, allInfiniteLeft());
        // any other resource in an infinite cyle
        Iterator inf = allInfiniteLeft();
        Iterator anotherFake = new NullIterator() {
            public boolean hasNext() {
                avoidExplicitReification = false;
                return false;
            }
        };
        Iterator reifications = new FilterIterator(new Filter() {
            public boolean accept(Object o) {
                codeCoverage[6]++;
                return res2statement.containsKey(o);
            }
        }, allInfiniteLeft());
        // any other resource.
        Iterator backStop = modelListSubjects();

        Iterator all[] = new Iterator[] { currentFile, pleasing,
                fakeStopPleasing, nonObjects, fakeLazyEvaluator,
                firstChoiceCyclic, nonAnonInfinite, inf, anotherFake,
                reifications, new NullIterator() {
                    public boolean hasNext() {
                        if (modelListSubjects().hasNext())
                            codeCoverage[7]++;
                        return false;
                    }
                }, backStop };
        Iterator allAsOne = new IteratorIterator(new ArrayIterator(all));

        // Filter for those that still have something to list.
        return new FilterIterator(new Filter() {
            public boolean accept(Object o) {
                return hasProperties((Resource) o);
            }
        }, allAsOne);
    }

    private Set openResIterators = new HashSet();

   

    private synchronized void closeAllResIterators() {
        Iterator members = openResIterators.iterator();
        while (members.hasNext()) {
            ((ResIterator) members.next()).close();
        }
        openResIterators = new HashSet();
    }

    private Iterator modelListSubjects() {
        ResIterator resIt = model.listSubjects();
        openResIterators.add(resIt);
        return resIt;

    }

}

/*
 * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard
 * Development Company, LP All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * 
 * 3. The name of the author may not be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

⌨️ 快捷键说明

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