Compare (query directive)
Contents |
Description
Compares the values of two roles in the input column.
The directive constructor is given a comparison operator as a string. It can be one of "==", "!=", "<", ">", "<=", ">=" or various aliases of these commonly used in programming languages. It can also be one of two topic map related operators "t=" or "t!=". These compare the equality or inequality, respectively, of topics instead of numeric or string representations the values.
The operands can be either role identifiers in the input column or literal values. Literals must be inside double quotation marks, role values are not. null is also a valid operand which is treated as a literal. You will of course have to escape the quotation marks since the operands are normal Java strings. For example "#DEFAULT" refers to the value of column with the default role and "\"#DEFAULT\"" is a string literal.
Constructor
Compare(String operand1,String operator,String operand2)
Compare(String operand1,String operator,String operand2,boolean numeric) - If numeric is true, converts the values to numbers before comparing
Notes
The where method present in every directive can be given three strings corresponding to the first constructor. A new Compare directive will be implicitly created. A.where("r1","==","r2") will resolve to new From(new Compare("r1","==","r2"),A).
Examples
importPackage(org.wandora.query2); importPackage(org.wandora.topicmap); new Join( new Instances().as("#instance"), new Players( XTMPSI.SUPERCLASS_SUBCLASS, XTMPSI.SUBCLASS).whereInputIs(XTMPSI.SUPERCLASS) ).where("#instance","t=",XTMPSI.SUBCLASS)
Above example joins instances and subclasses of input and only includes rows where the topics with roles "#instance" and XTMPSI.SUBCLASS are the same. Effectively this query finds out if any of the instances of the input topic are also subclasses of it. Compare directive is implicitly created by passing the where method string parameters corresponding to the Compare constructor.
importPackage(org.wandora.query2); new If( new BaseName().where("#DEFAULT","!=",null), new BaseName(), new First(new SubjectIdentifiers()) )
Above example gets the base name of input or the first subject identifier if base name is not defined. The BaseName directive returns null when base name is not defined. The return value is compared to null to find out if it is defined.
importPackage(org.wandora.query2); importPackage(org.wandora.topicmap); new Occurrence("http://www.wandora.org/occurrence",XTMPSI.getLang("en")) .as("#occ") .from(new Instances()) .where("#occ","=","\"Test\"")
Above example gets all instances of the input topic which have an occurrence with the value of a string literal "Test".
Following example finds all topics in the topic map that have more than 5 instances. Note the use of true as fourth parameter to where to enable numeric comparison instead of textual. Also, the literal 5 must also be surrounded by quotes even though it is treated as a number.
importPackage(org.wandora.query2); new Count(new Instances()) .as("#count") .from(new AllTopics().as("#topic")) .where("#count",">","\"5\"",true)