KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > builtins > IsDType


1 /******************************************************************
2  * File: IsDType.java
3  * Created by: Dave Reynolds
4  * Created on: 24-Aug-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: IsDType.java,v 1.7 2005/02/21 12:17:25 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.builtins;
11
12 import com.hp.hpl.jena.reasoner.rulesys.*;
13 import com.hp.hpl.jena.vocabulary.RDFS;
14 import com.hp.hpl.jena.datatypes.RDFDatatype;
15 import com.hp.hpl.jena.datatypes.TypeMapper;
16 import com.hp.hpl.jena.graph.*;
17 import com.hp.hpl.jena.graph.impl.LiteralLabel;
18
19 /**
20  * Tests whether the first argument is an instance of the datatype defined
21  * by the resource in the second argument.
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:17:25 $
25  */

26 public class IsDType extends BaseBuiltin {
27
28     /**
29      * Return a name for this builtin, normally this will be the name of the
30      * functor that will be used to invoke it.
31      */

32     public String JavaDoc getName() {
33         return "isDType";
34     }
35     
36     /**
37      * Return the expected number of arguments for this functor or 0 if the number is flexible.
38      */

39     public int getArgLength() {
40         return 2;
41     }
42
43     /**
44      * This method is invoked when the builtin is called in a rule body.
45      * @param args the array of argument values for the builtin, this is an array
46      * of Nodes, some of which may be Node_RuleVariables.
47      * @param length the length of the argument list, may be less than the length of the args array
48      * for some rule engines
49      * @param context an execution context giving access to other relevant data
50      * @return return true if the buildin predicate is deemed to have succeeded in
51      * the current environment
52      */

53     public boolean bodyCall(Node[] args, int length, RuleContext context) {
54         checkArgs(length, context);
55         Node val = getArg(0, args, context);
56         Node dt = getArg(1, args, context);
57         return isTypeOK(val, dt);
58     }
59     
60     /**
61      * Check if a literal value node is a legal value for the given datatype.
62      * @param val the literal value node
63      * @param dt the Node designating a datatype URI
64      */

65     public static boolean isTypeOK(Node val, Node dt) {
66         if (!dt.isURI()) return false;
67         if (val.isBlank()) return true;
68         if (val.isLiteral()) {
69             LiteralLabel ll = val.getLiteral();
70             if (ll.getDatatype() != null && (! ll.isWellFormed())) return false;
71             if (dt.equals(RDFS.Nodes.Literal)) {
72                 return true;
73             } else {
74                 RDFDatatype dtype = TypeMapper.getInstance().getSafeTypeByName(dt.getURI());
75                 return dtype.isValidLiteral(val.getLiteral());
76             }
77         }
78         return false;
79     }
80     
81 }
82
83
84 /*
85     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
86     All rights reserved.
87
88     Redistribution and use in source and binary forms, with or without
89     modification, are permitted provided that the following conditions
90     are met:
91
92     1. Redistributions of source code must retain the above copyright
93        notice, this list of conditions and the following disclaimer.
94
95     2. Redistributions in binary form must reproduce the above copyright
96        notice, this list of conditions and the following disclaimer in the
97        documentation and/or other materials provided with the distribution.
98
99     3. The name of the author may not be used to endorse or promote products
100        derived from this software without specific prior written permission.
101
102     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
103     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
104     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
105     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
106     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
107     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
108     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
109     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
110     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
111     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112 */
Popular Tags