Skip to content

RuleManager.matchAs does not return matched rules. #398

Closed
@georgeajit

Description

@georgeajit

While trying to verify the RuleManager.matchAs method, against the database, the returned RuleDefinitionList object is empty.

Here are the supporting files, methods and test method used.
constraint1.xml

<root>
  <title>Vannevar Bush</title>
  <popularity>5</popularity>
  <id>0011</id>
  <date xmlns="http://purl.org/dc/elements/1.1/">2005-01-01</date>
  <price xmlns="http://cloudbank.com" amt="0.1"/>
  <p>Vannevar Bush wrote an article for The Atlantic Monthly</p>
</root>

constraint2.xml

<root>
  <title>The Bush article</title>
  <popularity>4</popularity>
  <id>0012</id>
  <date xmlns="http://purl.org/dc/elements/1.1/">2006-02-02</date>
  <price xmlns="http://cloudbank.com" amt="0.12"/>
  <p>The Bush article described a device called a Memex.</p>
</root>

constraint3.xml

<root>
  <title>For 1945</title>
  <popularity>3</popularity>
  <id>0113</id>
  <date xmlns="http://purl.org/dc/elements/1.1/">2007-03-03</date>
  <price xmlns="http://cloudbank.com" amt="1.23"/>
  <p>For 1945, the thoughts expressed in The Atlantic Monthly were groundbreaking.</p>
</root>

constraint4.xml

<root>
  <title>Vannevar served</title>
  <popularity>5</popularity>
  <id>0024</id>
  <date xmlns="http://purl.org/dc/elements/1.1/">2008-04-04</date>
  <price xmlns="http://cloudbank.com" amt="12.34"/>
  <p>Vannevar served as a prominent policymaker and public intellectual.</p>
</root>

constraint5.xml

<root>
  <title>The memex</title>
  <popularity>5</popularity>
  <id>0026</id>
  <date xmlns="http://purl.org/dc/elements/1.1/">2009-05-05</date>
  <price xmlns="http://cloudbank.com" amt="123.45"/>
  <p>The Memex, unfortunately, had no automated search feature.</p>
</root>

Rule Name 1 - RULE-TEST-1

<rapi:rule xmlns:rapi="http://marklogic.com/rest-api">
  <rapi:name>RULE-TEST-1</rapi:name>
  <rapi:description>rule for test1</rapi:description>
  <search:search xmlns:search="http://marklogic.com/appservices/search">
    <search:query>
      <search:word-constraint-query>
        <search:constraint-name>intitle</search:constraint-name>
        <search:text>Vannevar</search:text>
      </search:word-constraint-query>
    </search:query>
    <search:options>
      <search:constraint name="intitle">
        <search:word>
          <search:element name="title" ns=""/>
        </search:word>
      </search:constraint>
    </search:options>
  </search:search>
  <rapi:rule-metadata>
    <rule-number>one</rule-number>
  </rapi:rule-metadata>
</rapi:rule>

Rule Name 2 - RULE-TEST-2

<rapi:rule xmlns:rapi="http://marklogic.com/rest-api">
  <rapi:name>RULE-TEST-2</rapi:name>
  <rapi:description>rule for test2</rapi:description>
  <search:search xmlns:search="http://marklogic.com/appservices/search">
    <search:query>
      <search:range-constraint-query>
        <search:constraint-name>date</search:constraint-name>
        <search:value>2005-01-01</search:value>
      </search:range-constraint-query>
    </search:query>
    <search:options>
      <search:constraint name="date">
        <search:range type="xs:date" facet="false">
          <search:element ns="http://purl.org/dc/elements/1.1/" name="date"/>
        </search:range>
      </search:constraint>
    </search:options>
  </search:search>
  <rapi:rule-metadata>
    <rule-number>two</rule-number>
  </rapi:rule-metadata>
</rapi:rule>

Test Method

public void testRMMatchAsWithCandidates() throws IOException, ParserConfigurationException, SAXException, XpathException, TransformerException
    {
        System.out.println("Running testRMMatchAsWithCandidates");      
        String[] filenames = {"constraint1.xml", "constraint2.xml", "constraint3.xml", "constraint4.xml", "constraint5.xml"};
        String ruleName1 = "RULE-TEST-1";
        String ruleName2 = "RULE-TEST-2";

        //DatabaseClient client = DatabaseClientFactory.newClient("localhost", Uberport, UberdbName, "eval-user", "x", Authentication.DIGEST);
        DatabaseClient client = DatabaseClientFactory.newClient("localhost", 8011, "rest-admin", "x", Authentication.DIGEST);

        // write docs
        for(String filename : filenames) {
            writeDocumentUsingInputStreamHandle(client, filename, "/raw-alert/", "XML");
        }

        // create a manager for configuring rules
        RuleManager ruleMgr = client.newRuleManager();

        // create handle
        InputStreamHandle ruleHandle1 = new InputStreamHandle();
        InputStreamHandle ruleHandle2 = new InputStreamHandle();

        // get the rule file
        InputStream inputStream1 = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/rules/alertRule1.xml");
        InputStream inputStream2 = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/rules/alertRule2.xml");

        ruleHandle1.set(inputStream1);
        ruleHandle2.set(inputStream2);

        // write the rule to the database
        ruleMgr.writeRule(ruleName1, ruleHandle1);
        ruleMgr.writeRule(ruleName2, ruleHandle2);

        // create a manager for document search criteria
        QueryManager queryMgr = client.newQueryManager();   

        // create a manager for matching rules
        RuleManager ruleMatchMgr = client.newRuleManager();

        // match the rules against the documents qualified by the criteria
        RuleDefinitionList matchedRulesDefList = new RuleDefinitionList();

        //InputStream inputStreamMatch = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/rules/alertRule2.xml");
        File file = new File("src/test/java/com/marklogic/client/functionaltest/rules/alertRule2.xml");
        // create a handle on the content
        FileHandle handle = new FileHandle(file);
        handle.set(file);
        RuleDefinitionList matchedRules = ruleMatchMgr.matchAs(handle, new String[] {"RULE-TEST-1","RULE-TEST-2"}, matchedRulesDefList);        

        System.out.println(matchedRules.size());

        String expected = "";

        // iterate over the matched rules
        Iterator<RuleDefinition> ruleItr = matchedRules.iterator();
        while (ruleItr.hasNext()) 
        {
            RuleDefinition rule = ruleItr.next();
            System.out.println(
                    "document criteria matched rule "+
                            rule.getName()+" with metadata "+rule.getMetadata()
                    );
            expected = expected + rule.getName() + " - " + rule.getMetadata() + " | ";
        }

        System.out.println(expected);

        assertTrue("incorrect rules", expected.contains("RULE-TEST-1 - {rule-number=one}")&& expected.contains("RULE-TEST-2 - {rule-number=two}"));

        // release client
        client.release();   
    }

Supporting Methods

/**
     * Write document using InputStreamHandle
     * @param client
     * @param filename
     * @param uri
     * @param type
     * @throws FileNotFoundException 
     */
    public void writeDocumentUsingInputStreamHandle(DatabaseClient client, String filename, String uri, String type) throws FileNotFoundException
    {
        // create doc manager
        DocumentManager docMgr = null;
        docMgr = documentManagerSelector(client, docMgr, type); 

        // create handle
        InputStreamHandle contentHandle = new InputStreamHandle();

        // get the file
        InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + filename);

        // set uri
        String docId = uri + filename;

        contentHandle.set(inputStream);

        // write doc
        docMgr.write(docId, contentHandle);

        System.out.println("Write " + docId + " to database");
    }

And

/**
     * Function to select and create document manager based on the type
     * @param client
     * @param docMgr
     * @param type
     * @return
     */
    public DocumentManager documentManagerSelector(DatabaseClient client, DocumentManager docMgr, String type)
    {
        // create doc manager
        if(type == "XML")
        {
            docMgr = client.newXMLDocumentManager();
        }
        else if(type == "Text")
        {   
            docMgr = client.newTextDocumentManager();
        }
        else if(type == "JSON")
        {   
            docMgr = client.newJSONDocumentManager();
        }
        else if(type == "Binary")
        {   
            docMgr = client.newBinaryDocumentManager();
        }
        else if (type == "JAXB") {
            docMgr = client.newXMLDocumentManager();
        }
        else 
        { 

            System.out.println("Invalid type");
        }

        return docMgr;
    }

Issue Seen:
At this matchAs method call

RuleDefinitionList matchedRules = ruleMatchMgr.matchAs(handle, new String[] {"RULE-TEST-1","RULE-TEST-2"}, matchedRulesDefList);

matchedRules is empty. Not sure if usage of "matchAs() usage is incorrect. Need some usage documentation also on this topic. Refer to #385 for a similar request on RuleManager class.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions