Search in sources :

Example 26 with Variable

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

the class GribParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    //Set MIME type as grib2
    metadata.set(Metadata.CONTENT_TYPE, GRIB_MIME_TYPE);
    TikaInputStream tis = TikaInputStream.get(stream, new TemporaryResources());
    File gribFile = tis.getFile();
    try {
        NetcdfFile ncFile = NetcdfDataset.openFile(gribFile.getAbsolutePath(), null);
        // 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.startElement("ul");
        xhtml.characters("dimensions:");
        xhtml.newline();
        for (Dimension dim : ncFile.getDimensions()) {
            xhtml.element("li", dim.getFullName() + "=" + String.valueOf(dim.getLength()) + ";");
            xhtml.newline();
        }
        xhtml.startElement("ul");
        xhtml.characters("variables:");
        xhtml.newline();
        for (Variable var : ncFile.getVariables()) {
            xhtml.element("p", String.valueOf(var.getDataType()) + var.getNameAndDimensions() + ";");
            for (Attribute element : var.getAttributes()) {
                xhtml.element("li", " :" + element + ";");
                xhtml.newline();
            }
        }
        xhtml.endElement("ul");
        xhtml.endElement("ul");
        xhtml.endDocument();
    } catch (IOException e) {
        throw new TikaException("NetCDF parse error", e);
    }
}
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) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile) Property(org.apache.tika.metadata.Property)

Example 27 with Variable

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

the class Test method test_11.

static void test_11() {
    System.out.print("test 11...");
    Term tuple = new Compound("t", new Term[] { new Atom("a"), new Atom("b"), new Atom("c"), new Atom("d"), new Atom("e") });
    try {
        Variable X = new Variable("X");
        Query q11 = new Query("tuple", new Term[] { X });
        Term result = q11.oneSolution().get("X");
        if (result == null || !result.equals(tuple)) {
            System.out.println("failed:");
            System.out.println("\tresult: " + result);
            System.out.println("\ttuple:  " + tuple);
        // System.exit(1);
        }
        if (result.arg(1) == null || !result.arg(1).equals(new Atom("a"))) {
            System.out.println("failed:");
            System.out.println("\tresult.arg(1): " + result.arg(1));
        // System.exit(1);
        }
        if (result.arg(2) == null || !result.arg(2).equals(new Atom("b"))) {
            System.out.println("failed:");
            System.out.println("\tresult.arg(2): " + result.arg(2));
        // System.exit(1);
        }
        if (result.arg(5) == null || !result.arg(5).equals(new Atom("e"))) {
            System.out.println("failed:");
            System.out.println("\tresult.arg(5): " + result.arg(5));
        // System.exit(1);
        }
    // arg0(6) throws an exception, as I'd expect it to...
    // if ( ((Compound)result).arg( 7 ) != null ){
    // System.out.println( "failed:" );
    // System.out.println( "\t((Compound)result).arg( 7 ): " + ((Compound)result).arg( 7 ) );
    // System.out.println( "\tshould be null" );
    // System.exit( 1 );
    // }
    } catch (PrologException e) {
        System.out.println("failed");
        e.printStackTrace();
    // System.exit(1);
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) PrologException(org.jpl7.PrologException) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Atom(org.jpl7.Atom)

Example 28 with Variable

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

the class Test method test_7.

static void test_7() {
    System.out.print("test 7...");
    String t7 = "r(f(X,X), Y)";
    Variable vX = new Variable("X");
    Variable vY = new Variable("Y");
    Query q7 = new Query("r", new Term[] { new Compound("f", new Term[] { vX, vX }), vY });
    Map<String, Term>[] solutions = q7.allSolutions();
    if (solutions.length != 2) {
        System.out.println(t7 + " failed:");
        System.out.println("\tExpected: 2 solutions");
        System.out.println("\tGot:      " + solutions.length);
    // System.exit(1);
    }
    Term X = solutions[0].get("X");
    Term Y = solutions[0].get("Y");
    if (X != Y) {
        System.out.println(t7 + " failed:");
        System.out.println(Util.toString(solutions[0]));
        System.out.println("\tThe variables to which X and Y are bound in the first solution should be identical.");
    // System.exit(1);
    }
    X = solutions[1].get("X");
    Y = solutions[1].get("Y");
    if (X == Y) {
        System.out.println(t7 + " failed:");
        System.out.println(Util.toString(solutions[1]));
        System.out.println("\tThe variables to which X and Y are bound in the second solution should be distinct.");
    // System.exit(1);
    }
    if (X.equals(Y)) {
        System.out.println(t7 + " failed:");
        System.out.println(Util.toString(solutions[1]));
        System.out.println("\tThe variables to which X and Y are bound in the second solution should not be \"equal\".");
    // System.exit(1);
    }
    System.out.println("passed");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query) Compound(org.jpl7.Compound) Term(org.jpl7.Term) Map(java.util.Map)

Example 29 with Variable

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

the class Time method test_3.

static void test_3() {
    Variable Y = new Variable("Y");
    Query query = new Query("noop_nobind", new Term[] { tree, Y });
    System.out.print("noop_nobind");
    for (int i = 0; i < num_trials; ++i) {
        timer.start();
        query.hasSolution();
        timer.stop();
        data[3][i] = timer.getElapsedTimeInMillis();
        System.out.print(".");
    }
    System.out.println("done");
}
Also used : Variable(org.jpl7.Variable) Query(org.jpl7.Query)

Example 30 with Variable

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

the class Term method getTerm.

/**
 * create and return a org.jpl7.Term representation of the given Prolog term
 *
 * @param vars_to_Vars
 *            A Map from Prolog variables to org.jpl7.Variable instances
 * @param term
 *            The Prolog term (in a term_t holder) to convert
 * @return The converted Term subclass instance.
 */
protected static Term getTerm(Map<term_t, Variable> vars_to_Vars, term_t term) {
    StringHolder hString;
    IntHolder hInt;
    Int64Holder hInt64;
    ObjectHolder hObject;
    switch(Prolog.term_type(term)) {
        case // 1
        Prolog.VARIABLE:
            for (Iterator<term_t> i = vars_to_Vars.keySet().iterator(); i.hasNext(); ) {
                // a previously seen Prolog
                term_t varX = (term_t) i.next();
                // variable
                if (Prolog.compare(varX, term) == 0) {
                    // return the
                    return (Term) vars_to_Vars.get(varX);
                // associated JPL
                // Variable
                }
            }
            // otherwise, the Prolog variable in term has not been seen before
            // allocate a new (sequentially
            Variable Var = new Variable();
            // named) Variable to represent it
            // this should become redundant...
            Var.term_ = term;
            // use Hashtable(var,null), but only
            vars_to_Vars.put(term, Var);
            // need set(var)
            return Var;
        case // 2
        Prolog.ATOM:
            hString = new StringHolder();
            // ignore return val; assume
            Prolog.get_atom_chars(term, hString);
            // success...
            return new Atom(hString.value, "text");
        case // 5
        Prolog.STRING:
            hString = new StringHolder();
            // ignore return val; assume
            Prolog.get_string_chars(term, hString);
            // success...
            return new Atom(hString.value, "string");
        case // 3
        Prolog.INTEGER:
            hInt64 = new Int64Holder();
            if (Prolog.get_integer(term, hInt64)) {
                // Java long...
                return new org.jpl7.Integer(hInt64.value);
            } else {
                hString = new StringHolder();
                if (Prolog.get_integer_big(term, hString)) {
                    // System.out.println("bigint = " + hString.value);
                    return new org.jpl7.Integer(new java.math.BigInteger(hString.value));
                } else {
                    // arbitrary
                    return new org.jpl7.Integer(-3);
                }
            }
        case // 4
        Prolog.FLOAT:
            DoubleHolder hFloatValue = new DoubleHolder();
            // assume it succeeds...
            Prolog.get_float(term, hFloatValue);
            return new org.jpl7.Float(hFloatValue.value);
        // 6
        case Prolog.COMPOUND:
        case // 9
        Prolog.LIST_PAIR:
            hString = new StringHolder();
            hInt = new IntHolder();
            // assume it succeeds
            Prolog.get_name_arity(term, hString, hInt);
            Term[] args = new Term[hInt.value];
            // term_t term1 = Prolog.new_term_refs(hArity.value);
            for (int i = 1; i <= hInt.value; i++) {
                term_t termi = Prolog.new_term_ref();
                Prolog.get_arg(i, term, termi);
                args[i - 1] = Term.getTerm(vars_to_Vars, termi);
            }
            return new Compound(hString.value, args);
        case // 7
        Prolog.LIST_NIL:
            return JPL.LIST_NIL;
        case // 8
        Prolog.BLOB:
            hObject = new ObjectHolder();
            if (Prolog.get_jref_object(term, hObject)) {
                if (hObject.value == null) {
                    return JPL.JNULL;
                } else {
                    return new JRef(hObject.value);
                }
            } else {
                throw new JPLException("unsupported blob type passed from Prolog");
            }
        default:
            // should never happen
            throw new JPLException("unknown term type=" + Prolog.term_type(term));
    }
}
Also used : DoubleHolder(org.jpl7.fli.DoubleHolder) Int64Holder(org.jpl7.fli.Int64Holder) BigInteger(java.math.BigInteger) ObjectHolder(org.jpl7.fli.ObjectHolder) StringHolder(org.jpl7.fli.StringHolder) org.jpl7.fli.term_t(org.jpl7.fli.term_t) IntHolder(org.jpl7.fli.IntHolder) BigInteger(java.math.BigInteger)

Aggregations

Query (org.jpl7.Query)25 Variable (org.jpl7.Variable)24 Term (org.jpl7.Term)23 Variable (ucar.nc2.Variable)23 IOException (java.io.IOException)20 Compound (org.jpl7.Compound)17 Atom (org.jpl7.Atom)12 ArrayDouble (ucar.ma2.ArrayDouble)11 Map (java.util.Map)9 Attribute (ucar.nc2.Attribute)6 Integer (org.jpl7.Integer)4 org.jpl7.fli.term_t (org.jpl7.fli.term_t)4 BigInteger (java.math.BigInteger)3 Dimension (ucar.nc2.Dimension)3 Group (ucar.nc2.Group)3 DataPoint (net.sf.mzmine.datamodel.DataPoint)2 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