jjzjj

java - 无法使用 Saxon 处理器应用区间算法

coder 2024-07-04 原文

我正在使用 Saxon 处理器 来执行验证。
包含所有函数定义的文件如下:
default-definition.txt:

declare variable $a external; 
declare variable $b external; 
declare variable $c external; 

declare function iaf:sum(
$params as item()*
) as item()+ {

  let $facts := if (empty($params)) then (0) else one-or-more($params)
  let $values := for $i in $facts return (iaf:splitValueThreshold($i)[1])
  let $thresholds := for $i in $facts return (iaf:splitValueThreshold($i)[2])
  let $sumValues := sum($values)
  let $sumThresholds := sum($thresholds)
  let $output := iaf:joinValueThreshold($sumValues, $sumThresholds) 
       return ($output)

};

declare function iaf:numeric-equal(
$paramA as item(), $paramB as item()
) as xs:boolean {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := abs($itemA[1] - $itemB[1]) le ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-less-than(
$paramA as item(), $paramB as item()
) as xs:boolean {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[1] - $itemB[1]) lt ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-less-equal-than(
$paramA as item(), $paramB as item()
) as xs:boolean {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[1] - $itemB[1]) le ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-greater-than(
$paramA as item(), $paramB as item()
) as xs:boolean {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := $itemA[1] gt ($itemB[1] - ($itemA[2] + $itemB[2])) 
       return ($output)

};

declare function iaf:numeric-greater-equal-than(
$paramA as item(), $paramB as item()
) as xs:boolean {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := $itemA[1] ge ($itemB[1] - ($itemA[2] + $itemB[2])) 
       return ($output)

};

declare function iaf:numeric-add(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $value := $itemA[1]+$itemB[1]
  let $threshold := $itemA[2]+$itemB[2]
  let $output := iaf:joinValueThreshold($value,$threshold) 
       return ($output)

};

declare function iaf:numeric-subtract(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $value := $itemA[1]-$itemB[1]
  let $threshold := $itemA[2]+$itemB[2]
  let $output := iaf:joinValueThreshold($value,$threshold) 
       return ($output)

};

declare function iaf:numeric-divide(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $A := $itemA[1]
  let $B := $itemB[1]
  let $deltaA := $itemA[2]
  let $deltaB := $itemB[2]
  let $AdivB := $A div $B
  let $J0 := ($A + $deltaA) div ($B + $deltaB)
  let $J1 := ($A + $deltaA) div ($B - $deltaB)
  let $J2 := ($A - $deltaA) div ($B + $deltaB)
  let $J3 := ($A - $deltaA) div ($B - $deltaB)
  let $threshold := max((abs($AdivB - $J0), abs($AdivB - $J1), abs($AdivB - $J2), abs($AdivB - $J3)))
  let $output := iaf:joinValueThreshold($AdivB, $threshold) 
       return ($output)

};

declare function iaf:numeric-multiply(
$paramA as item(), $paramB as item()
) as item() {
  iaf:multiply-two-elements($paramA, $paramB)
};

declare function iaf:numeric-multiply(
$params as item()+
) as item() {

  let $output := iaf:multiply-recursive($params, 1, 0) 
       return ($output)

};

declare function iaf:abs(
$input as item()
) as item() {

  let $item := if (empty($input)) then 0 else $input
  let $output := if ($item instance of element() and empty($item[2])) then
    iaf:joinValueThreshold(abs($item), iaf-int:fact-threshold($item)) else (if (not($item instance of
    element()) and empty($item[2]) and not(contains(string($item), ";"))) then
    iaf:joinValueThreshold(abs($item), 0) else
    iaf:joinValueThreshold(abs(xs:decimal(substring-before($item, ";"))),
    xs:decimal(substring-after($item, ";"))))  
       return ($output)

};

declare function iaf:numeric-unary-minus(
$item as item()
) as item() {

  let $output := if ($item instance of element() and empty($item[2])) then
    iaf:joinValueThreshold(-($item), iaf-int:fact-threshold($item)) else (if (not($item instance of
    element()) and empty($item[2]) and not(contains(string($item), ";"))) then
    iaf:joinValueThreshold(-($item), 0) else
    iaf:joinValueThreshold(-(xs:decimal(substring-before($item, ";"))),
    xs:decimal(substring-after($item, ";"))))  
       return ($output)

};

declare function iaf:min(
$params as item()*
) as item() {

  let $facts := if (empty($params)) then (0) else one-or-more($params)
  let $values := for $i in $facts return (iaf:splitValueThreshold($i)[1])
  let $thresholds := for $i in $facts return (iaf:splitValueThreshold($i)[2])
  let $minValue := min($values)
  let $indexMin := index-of($values, $minValue)[1]
  let $minThreshold := $thresholds[$indexMin]
  let $output := iaf:joinValueThreshold($minValue,$minThreshold) 
       return ($output)

};

declare function iaf:max(
$params as item()*
) as item() {

  let $facts := if (empty($params)) then (0) else one-or-more($params)
  let $values := for $i in $facts return (iaf:splitValueThreshold($i)[1])
  let $thresholds := for $i in $facts return (iaf:splitValueThreshold($i)[2])
  let $maxValue := max($values)
  let $indexMax := index-of($values, $maxValue)[1]
  let $maxThreshold := $thresholds[$indexMax]
  let $output := iaf:joinValueThreshold($maxValue,$maxThreshold) 
       return ($output)

};

declare function iaf:splitValueThreshold(
$item as item()
) as item()+ {

  let $valorUmbral := if ($item instance of element() and empty($item[2])) then ($item,
    iaf-int:fact-threshold($item)) else (if (not($item instance of element()) and empty($item[2])
    and not(contains(string($item), ";"))) then ($item, 0) else
    (xs:decimal(substring-before($item, ";")), xs:decimal(substring-after($item, ";"))))  
       return ($valorUmbral)

};

declare function iaf:joinValueThreshold(
$value as item(), $threshold as item()
) as xs:string {

  let $output := concat(string($value),";",string($threshold)) 
       return ($output)

};

declare function iaf:precision(
$item as item()+
) as xs:decimal {

  let $ouput := xfi:decimals($item) 
       return ($ouput)

};

declare function iaf:multiply-recursive(
$sequence as item()+, $count as item(), $subtotalParam as item()
) as item() {

  let $facts := if (empty($sequence)) then (0) else
    one-or-more($sequence)
  let $numberOfParams := count($facts)
  let $subtotal := if ($count eq 1) then $facts[1] else $subtotalParam
  let $multiply := if($count lt $numberOfParams) then iaf:multiply-two-elements($subtotal,
    $facts[$count + 1]) else $subtotal
  let $output := if($count lt $numberOfParams) then iaf:multiply-recursive($sequence,
    ($count +1), $multiply) else $multiply 
       return ($output)

};

declare function iaf:multiply-two-elements(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $A := $itemA[1]
  let $B := $itemB[1]
  let $deltaA := $itemA[2]
  let $deltaB := $itemB[2]
  let $AxB := $A * $B
  let $threshold := sum((abs($A * $deltaB), abs($B * $deltaA), $deltaA * $deltaB)) 
       return (iaf:joinValueThreshold($AxB, $threshold))

};

declare function iaf:numeric-equal-threshold(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-less-than-threshold(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-less-equal-than-threshold(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-greater-than-threshold(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:numeric-greater-equal-than-threshold(
$paramA as item(), $paramB as item()
) as item() {

  let $itemA := iaf:splitValueThreshold($paramA)
  let $itemB := iaf:splitValueThreshold($paramB)
  let $output := ($itemA[2] + $itemB[2]) 
       return ($output)

};

declare function iaf:abs-sequence(
$params as item()*
) as item()+ {

  let $facts := if (empty($params)) then (0) else one-or-more($params)
  let $values := for $i in $facts return (iaf:abs($i)) 
       return (($values))

};

declare function iaf:numeric-equal-test(
$paramA as item(), $paramB as item()
) as item()+ {

  let $valueA := iaf:splitValueThreshold($paramA)[1]
  let $valueB := iaf:splitValueThreshold($paramB)[1]
  let $thA := iaf:splitValueThreshold($paramA)[2]
  let $thB := iaf:splitValueThreshold($paramB)[2]
  let $absol := abs($valueA - $valueB)
  let $sumTh := $thA + $thB 
       return (($valueA, $valueB, $thA, $thB, $absol, $sumTh))

};

declare function iaf-int:exp10(
$power as xs:integer
) as xs:decimal {
  if ($power eq 0) then 1 else if ($power gt 0) then 10 * iaf-int:exp10($power - 1) else 1 div iaf-int:exp10(-$power)
};

declare function iaf-int:fact-threshold(
$fact as item()
) as xs:decimal {

  let $decimals := xfi:decimals($fact) 
       return (if (string($decimals) = 'INF') then 0 else iaf-int:exp10(-xs:integer($decimals)) div 2)

};
declare function xff:has-fallback-value(
$fact as xs:QName
) as xs:boolean{
            let $result := if (string($a) = '') then fn:true() else fn:false()
            return ($result)
};
declare function xfi:decimals($fact as item() ) as item()+ { let $deci := $fact/@decimals  return ($deci)};
declare function xfi:fact-typed-dimension-value($fact as item() , $typedDim as xs:QName?) as xs:string {let $dimName := substring-before(substring-after($fact,concat("s2c_dim:",$typedDim,"__TDVALUE__")),";") return ($dimName)};
$a = +$b + $c

Junit 测试用例:

XdmValue xqueryResult = null;
XQueryCompiler xqueryCompiler = new Processor(false).newXQueryCompiler();
declareNamespace(xqueryCompiler);
XQueryExecutable queryExecutable = xqueryCompiler.compile(new File("default-definition.txt")); //I18NOK:LSM
XQueryEvaluator xqueryEvaluator = queryExecutable.load();
double aa=331640738.91;
double bb=393432239.2;
double cc=-61791500.29;

//previous strategy, which is now throwing an exception
//  XPTY0004: Cannot compare xs:string to xs:double
xqueryEvaluator.setExternalVariable(new QName("a"), new XdmAtomicValue(String.format("%s;%s", aa,FactValue.getPrecision("-3"))));
xqueryEvaluator.setExternalVariable(new QName("b"), new XdmAtomicValue(bb));
xqueryEvaluator.setExternalVariable(new QName("c"), new XdmAtomicValue(cc));
xqueryEvaluator.toString();
xqueryResult = xqueryEvaluator.evaluate();
System.out.println(Boolean.valueOf(xqueryResult.itemAt(0).getStringValue()));

//exact match --Result: FAIL
xqueryEvaluator.setExternalVariable(new QName("a"), new XdmAtomicValue(aa));
xqueryEvaluator.setExternalVariable(new QName("b"), new XdmAtomicValue(bb));
xqueryEvaluator.setExternalVariable(new QName("c"), new XdmAtomicValue(cc));
xqueryEvaluator.toString();
xqueryResult = xqueryEvaluator.evaluate();
System.out.println(Boolean.valueOf(xqueryResult.itemAt(0).getStringValue()));

//matching after rounding off till the range of 1000 --Result: FAIL
double a = Math.round(Double.parseDouble("331640738.91") * Math.pow(10,-3)) / Math.pow(10,-3);
double b = Math.round(Double.parseDouble("393432239.2") * Math.pow(10,-3)) / Math.pow(10,-3);
double c = Math.round(Double.parseDouble("-61791500.29") * Math.pow(10,-3)) / Math.pow(10,-3);

xqueryEvaluator.setExternalVariable(new QName("a"), new XdmAtomicValue(a));
xqueryEvaluator.setExternalVariable(new QName("b"), new XdmAtomicValue(b));
xqueryEvaluator.setExternalVariable(new QName("c"), new XdmAtomicValue(c));
xqueryEvaluator.toString();
xqueryResult = xqueryEvaluator.evaluate();
System.out.println(Boolean.valueOf(xqueryResult.itemAt(0).getStringValue()));

理想情况下,“a”和“b+c”的值之间的差异是小数点,它应该通过。如果有其他方法可以使用 Saxon 执行区间运算,请告诉我。
如果需要更多输入,请告诉我。
谢谢!

最佳答案

如果你添加断言

assertEquals(aa, bb+cc);

在声明这些变量后立即查看您的 Java 代码,您将看到它失败了:

Expected (aa) :3.3164073891E8
Actual(bb+cc) :3.3164073890999997E8 

失败是因为舍入错误将您写入的值(例如 331640738.91)转换为 double 值空间内可表达的最接近的值。

Java 使用与 XQuery 使用的 double 浮点算法完全相同的规则,所以我不明白为什么您期望它在 Java 中失败时在 XQuery 中成功。

如果您想使用精确的十进制算法,那么您需要使用 xs:decimal 数据类型而不是 xs:double

关于java - 无法使用 Saxon 处理器应用区间算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51927447/

有关java - 无法使用 Saxon 处理器应用区间算法的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  5. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  6. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  7. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  8. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  9. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  10. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

随机推荐