Search in sources :

Example 1 with Variable

use of org.jpl7.Variable in project tika by apache.

the class NetCDFParser method parse.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.tika.parser.Parser#parse(java.io.InputStream,
     * org.xml.sax.ContentHandler, org.apache.tika.metadata.Metadata,
     * org.apache.tika.parser.ParseContext)
     */
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    TemporaryResources tmp = TikaInputStream.isTikaInputStream(stream) ? null : new TemporaryResources();
    TikaInputStream tis = TikaInputStream.get(stream, tmp);
    NetcdfFile ncFile = null;
    try {
        ncFile = NetcdfFile.open(tis.getFile().getAbsolutePath());
        metadata.set("File-Type-Description", ncFile.getFileTypeDescription());
        // first parse out the set of global attributes
        for (Attribute attr : ncFile.getGlobalAttributes()) {
            Property property = resolveMetadataKey(attr.getFullName());
            if (attr.getDataType().isString()) {
                metadata.add(property, attr.getStringValue());
            } else if (attr.getDataType().isNumeric()) {
                int value = attr.getNumericValue().intValue();
                metadata.add(property, String.valueOf(value));
            }
        }
        XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
        xhtml.startDocument();
        xhtml.newline();
        xhtml.element("h1", "dimensions");
        xhtml.startElement("ul");
        xhtml.newline();
        for (Dimension dim : ncFile.getDimensions()) {
            xhtml.element("li", dim.getFullName() + " = " + dim.getLength());
        }
        xhtml.endElement("ul");
        xhtml.element("h1", "variables");
        xhtml.startElement("ul");
        xhtml.newline();
        for (Variable var : ncFile.getVariables()) {
            xhtml.startElement("li");
            xhtml.characters(var.getDataType() + " " + var.getNameAndDimensions());
            xhtml.newline();
            List<Attribute> attributes = var.getAttributes();
            if (!attributes.isEmpty()) {
                xhtml.startElement("ul");
                for (Attribute element : attributes) {
                    xhtml.element("li", element.toString());
                }
                xhtml.endElement("ul");
            }
            xhtml.endElement("li");
        }
        xhtml.endElement("ul");
        xhtml.endDocument();
    } catch (IOException e) {
        throw new TikaException("NetCDF parse error", e);
    } finally {
        if (ncFile != null) {
            ncFile.close();
        }
        if (tmp != null) {
            tmp.dispose();
        }
    }
}
Also used : NetcdfFile(ucar.nc2.NetcdfFile) Variable(ucar.nc2.Variable) TikaException(org.apache.tika.exception.TikaException) Attribute(ucar.nc2.Attribute) TemporaryResources(org.apache.tika.io.TemporaryResources) TikaInputStream(org.apache.tika.io.TikaInputStream) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) Property(org.apache.tika.metadata.Property)

Example 2 with Variable

use of org.jpl7.Variable in project packages-jpl by SWI-Prolog.

the class FamilyMT method run.

public void run() {
    Map<String, Term> solution;
    // Variable X = new Variable("X");
    // 
    Query q2 = new Query("child_of(joe,ralf)");
    System.err.println("child_of(joe,ralf) is " + (q2.hasSolution() ? "provable" : "not provable"));
    new Query("sleep(?)", new Term[] { new org.jpl7.Integer(delay) }).hasSolution();
    // 
    Query q3 = new Query("descendent_of(steve,ralf)");
    System.err.println("descendent_of(steve,ralf) is " + (q3.hasSolution() ? "provable" : "not provable"));
    delay();
    // 
    Query q4 = new Query("descendent_of(X, ralf)");
    solution = q4.oneSolution();
    System.err.println("first solution of descendent_of(X, ralf)");
    System.err.println("X = " + solution.get("X"));
    delay();
    // 
    Map<String, Term>[] solutions = q4.allSolutions();
    System.err.println("all solutions of descendent_of(X, ralf)");
    for (int i = 0; i < solutions.length; i++) {
        System.err.println("X = " + solutions[i].get("X"));
    }
    delay();
    // 
    System.err.println("each solution of descendent_of(X, ralf)");
    while (q4.hasMoreSolutions()) {
        solution = q4.nextSolution();
        System.err.println("X = " + solution.get("X"));
    }
    delay();
    // 
    Query q5 = new Query("descendent_of(X, Y)");
    System.err.println(id + ": each solution of descendent_of(X, Y)");
    while (q5.hasMoreSolutions()) {
        solution = q5.nextSolution();
        System.err.println(id + ": X = " + solution.get("X") + ", Y = " + solution.get("Y"));
        delay();
    }
}
Also used : Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 3 with Variable

use of org.jpl7.Variable in project packages-jpl by SWI-Prolog.

the class Test method test_6.

static void test_6() {
    System.out.print("test 6...");
    Variable X = new Variable("X");
    Query q6 = new Query("p", new Term[] { X, X });
    Term[] x_target = new Term[] { a };
    Map<String, Term>[] solutions = q6.allSolutions();
    if (solutions.length != 1) {
        System.out.println("p(X, X) failed:");
        System.out.println("\tExpected: 1 solution");
        System.out.println("\tGot:      " + solutions.length);
    // System.exit(1);
    }
    for (int i = 0; i < solutions.length; ++i) {
        Object x_binding = solutions[i].get("X");
        if (!x_binding.equals(x_target[i])) {
            System.out.println("p(X, X) failed:");
            System.out.println("\tExpected: " + x_target[i]);
            System.out.println("\tGot:      " + x_binding);
        // System.exit(1);
        }
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 4 with Variable

use of org.jpl7.Variable in project packages-jpl by SWI-Prolog.

the class Test method test_5.

static void test_5() {
    System.out.print("test 5...");
    Variable X = new Variable("X");
    Variable Y = new Variable("Y");
    Query q5 = new Query("p", new Term[] { X, Y });
    Term[] x_target = new Term[] { a, a };
    Term[] y_target = new Term[] { a, b };
    Map<String, Term>[] solutions = q5.allSolutions();
    if (solutions.length != 2) {
        System.out.println("p(X, Y) failed:");
        System.out.println("\tExpected: 2 solutions");
        System.out.println("\tGot:      " + solutions.length);
    // System.exit(1);
    }
    for (int i = 0; i < solutions.length; ++i) {
        Term x_binding = solutions[i].get("X");
        if (!x_binding.equals(x_target[i])) {
            System.out.println("p(X, Y) failed:");
            System.out.println("\tExpected: " + x_target[i]);
            System.out.println("\tGot:      " + x_binding);
        // System.exit(1);
        }
        Term y_binding = solutions[i].get("Y");
        if (!y_binding.equals(y_target[i])) {
            System.out.println("p( X, Y ) failed:");
            System.out.println("\tExpected: " + y_target[i]);
            System.out.println("\tGot:      " + y_binding);
        // System.exit(1);
        }
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Example 5 with Variable

use of org.jpl7.Variable in project packages-jpl by SWI-Prolog.

the class Test method test_4.

static void test_4() {
    System.out.print("test 4...");
    Variable X = new Variable("X");
    Query q4 = new Query("p", new Term[] { X });
    Term[] target = new Term[] { a, f_a, pair_a_b, new Variable("_") };
    Map<String, Term>[] solutions = q4.allSolutions();
    if (solutions.length != 4) {
        System.out.println("p(X) failed:");
        System.out.println("\tExpected: 4 solutions");
        System.out.println("\tGot:      " + solutions.length);
    // System.exit(1);
    }
    for (int i = 0; i < solutions.length - 1; ++i) {
        Term binding = (Term) solutions[i].get("X");
        if (!binding.equals(target[i])) {
            System.out.println("p(X) failed");
            System.out.println("\tExpected: " + target[i]);
            System.out.println("\tGot:      " + binding);
        // System.exit(1);
        }
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Term(org.jpl7.Term) Map(java.util.Map)

Aggregations

Query (org.jpl7.Query)25 Variable (org.jpl7.Variable)24 Term (org.jpl7.Term)23 Variable (ucar.nc2.Variable)21 IOException (java.io.IOException)19 Compound (org.jpl7.Compound)17 Atom (org.jpl7.Atom)12 ArrayDouble (ucar.ma2.ArrayDouble)10 Map (java.util.Map)9 Integer (org.jpl7.Integer)4 org.jpl7.fli.term_t (org.jpl7.fli.term_t)4 Attribute (ucar.nc2.Attribute)4 BigInteger (java.math.BigInteger)3 Group (ucar.nc2.Group)3 TikaException (org.apache.tika.exception.TikaException)2 TemporaryResources (org.apache.tika.io.TemporaryResources)2 TikaInputStream (org.apache.tika.io.TikaInputStream)2 Property (org.apache.tika.metadata.Property)2 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)2 Dimension (ucar.nc2.Dimension)2