Skip to content

Commit

Permalink
Merge pull request #3 from RuleML/2.0-SNAPSHOT
Browse files Browse the repository at this point in the history
2.0 supports FOL
  • Loading branch information
edmonl committed Mar 12, 2015
2 parents d95e384 + dafe1c8 commit 0b890f2
Show file tree
Hide file tree
Showing 17 changed files with 498 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.ruleml.translation.ruleml2tptp</groupId>
<groupId>org.ruleml.translation</groupId>
<artifactId>ruleml2tptp</artifactId>
<version>1.1</version>
<packaging>jar</packaging>
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/org/ruleml/translation/ruleml2tptp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ public class Main {
@Option(name="-o",aliases={"-output"},metaVar="<path>",usage="use given output path (the standard output by default)")
private File output;

@Option(name="-f",aliases={"-transformer-factory"},metaVar="<class>",usage="use given factory class")
private String transFactoryClass;

@Option(name="-w",aliases={"-overwrite"},usage="overwrite the output file")
private boolean overwrite;

@Option(name="-r",aliases={"-recursive"},hidden=true,usage="traverse the directory tree for input files")
private boolean recursive;

@Argument
private String input;

Expand All @@ -75,12 +69,8 @@ public static void main(String args[]) {
System.exit(EC_GENERAL);
}
try {
if (appMain.transFactoryClass == null) {
appMain.transFactory = (SAXTransformerFactory) (SAXTransformerFactory.newInstance());
} else {
appMain.transFactory = (SAXTransformerFactory) (SAXTransformerFactory.newInstance(
appMain.transFactoryClass, Main.class.getClassLoader()));
}
appMain.transFactory =
(SAXTransformerFactory) (SAXTransformerFactory.newInstance());
} catch (TransformerFactoryConfigurationError err) {
throw new IllegalStateException(err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class Translator {

private static final String XSLT_NORMALIZER_RES_PATH = "/xslt/101_nafneghornlogeq_normalizer.xslt";
private static final String XSLT_NORMALIZER_RES_PATH = "/xslt/101_naffologeq_normalizer.xslt";
private static final String XSLT_TRANSLATOR_RES_PATH = "/xslt/ruleml2tptp.xslt";
private static final String NEWLINE = String.format("%n");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
r:Entails/*[namespace-uri(.)='http://ruleml.org/spec'
and position()=last()]"
mode="phase-1">
<!-- <xsl:comment>last</xsl:comment>-->
<!--<xsl:comment>last</xsl:comment>-->
<xsl:choose>
<xsl:when test="local-name()='if' or local-name()='then'">
<xsl:call-template name="copy-1"/>
Expand Down Expand Up @@ -343,7 +343,7 @@


<!-- Note: Some of these templates may be combined. -->
<!-- Builds canonically-ordered content of Assert or Retract. -->
<!-- Builds canonically-ordered content of Retract. -->
<xsl:template match="r:Retract|r:Assert" mode="phase-2">
<xsl:copy>
<xsl:apply-templates select="@*"/>
Expand Down Expand Up @@ -634,14 +634,24 @@
<!-- For Implies -->
<!-- Makes @material explicit. -->
<!-- Makes @direction explicit. -->
<xsl:template match="r:Implies[not(@material) or not(@direction)]" mode="phase-3">
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template
match="r:Implies[not(@material) or not(@direction) or not(@mapMaterial) or not(@mapDirection)]"
mode="phase-3">
<xsl:copy>
<xsl:if test="not(@material)">
<xsl:attribute name="material">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@direction)">
<xsl:attribute name="direction">bidirectional</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>
Expand All @@ -664,7 +674,129 @@
<!-- For Retract -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Retract[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:template
match="r:Retract[not(@material) or not(@direction) or not(@mapMaterial) or not(@mapDirection)]"
mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Query -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Query[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Equivalent -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Equivalent[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for And -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:And[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Or -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Or[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Naf -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Naf[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Neg -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Neg[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Forall -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Forall[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
</xsl:if>
<xsl:if test="not(@mapDirection)">
<xsl:attribute name="mapDirection">bidirectional</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node() | @*" mode="phase-3"/>
</xsl:copy>
</xsl:template>

<!-- for Exists -->
<!-- Makes @mapMaterial explicit. -->
<!-- Makes @mapDirection explicit. -->
<xsl:template match="r:Exists[not(@mapMaterial) or not(@mapDirection)]" mode="phase-3">
<xsl:copy>
<xsl:if test="not(@mapMaterial)">
<xsl:attribute name="mapMaterial">yes</xsl:attribute>
Expand Down
113 changes: 101 additions & 12 deletions src/main/resources/xslt/ruleml2tptp.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:r="http://ruleml.org/spec">
<!-- The input of this translator must be a normalized RuleML instance. -->

<!-- line break -->
<xsl:param name="nl" select="'&#xA;'" as="xs:string" required="no"/>

Expand Down Expand Up @@ -197,6 +199,24 @@

</xsl:template>

<xsl:template match="r:Neg">
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>

<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:if>
<xsl:text>~ </xsl:text>

<xsl:apply-templates select="r:strong">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>

</xsl:template>

<xsl:template match="r:Implies">
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>
Expand All @@ -208,18 +228,70 @@
</xsl:if>
<xsl:text>( </xsl:text>

<xsl:apply-templates select="r:if">
<xsl:choose>
<xsl:when test="(r:if | r:then)[1] = r:if">
<xsl:apply-templates select="r:if">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>

<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
<xsl:with-param name="retreat" select="1"/>
</xsl:call-template>
<xsl:text>=&gt; </xsl:text>

<xsl:apply-templates select="r:then">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="r:then">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>

<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
<xsl:with-param name="retreat" select="1"/>
</xsl:call-template>
<xsl:text>&lt;= </xsl:text>

<xsl:apply-templates select="r:if">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>

<xsl:text> )</xsl:text>

</xsl:template>

<xsl:template match="r:Equivalent">
<xsl:param name="depth" required="yes" as="xs:integer" tunnel="yes"/>
<xsl:param name="line-breaking" required="yes" as="xs:boolean" tunnel="yes"/>

<xsl:if test="$line-breaking">
<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
</xsl:call-template>
</xsl:if>
<xsl:text>( </xsl:text>

<xsl:apply-templates select="r:torso[1]">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>

<xsl:call-template name="break-line">
<xsl:with-param name="depth" select="$depth"/>
<xsl:with-param name="retreat" select="1"/>
<xsl:with-param name="retreat" select="2"/>
</xsl:call-template>
<xsl:text>=> </xsl:text>
<xsl:text>&lt;=&gt; </xsl:text>

<xsl:apply-templates select="r:then">
<xsl:apply-templates select="r:torso[last()]">
<xsl:with-param name="depth" select="$depth + 1" tunnel="yes"/>
<xsl:with-param name="line-breaking" select="false()" tunnel="yes"/>
</xsl:apply-templates>
Expand Down Expand Up @@ -289,7 +361,7 @@
</xsl:choose>
</xsl:template>

<xsl:template match="r:Atom | r:Expr">
<xsl:template match="r:Atom | r:Expr"> <!-- Expr belongs to FOL (rather than Hornlog+). -->
<xsl:apply-templates select="r:op"/>
<!-- sorted args -->
<xsl:for-each select="r:arg">
Expand All @@ -315,16 +387,17 @@
</xsl:template>

<!-- constants and functors in the TPTP language start with a lowercase letter or is single-quoted -->
<xsl:template match="r:Rel | r:Ind">
<xsl:variable name="normalized-text" select="normalize-space(text())" as="xs:string"/>
<xsl:template match="r:Rel | r:Ind | r:Fun"> <!-- Fun belongs to FOL (rather than Hornlog+). -->
<xsl:choose>
<xsl:when test="matches($normalized-text, '^[a-z][a-z0-9_]*$', 'i')">
<xsl:value-of select="concat(lower-case(substring($normalized-text, 1, 1)), substring($normalized-text, 2))"/>
<xsl:when test="string(@iri)">
<xsl:call-template name="normalize-text">
<xsl:with-param name="text" select="@iri"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- escape \ and ' then single quote -->
<xsl:value-of select='concat("&apos;",
replace(replace(text(), "\\", "\\\\"), "&apos;", "\\&apos;"), "&apos;")'/>
<xsl:call-template name="normalize-text">
<xsl:with-param name="text" select="string(text())"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Expand All @@ -339,4 +412,20 @@
</xsl:for-each>
</xsl:template>

<xsl:template name="normalize-text">
<xsl:param name="text" required="yes" as="xs:string"/>

<xsl:variable name="normalized-text" select="normalize-space($text)" as="xs:string"/>
<xsl:choose>
<xsl:when test="matches($normalized-text, '^[a-z][a-z0-9_]*$', 'i')">
<xsl:value-of select="concat(lower-case(substring($normalized-text, 1, 1)), substring($normalized-text, 2))"/>
</xsl:when>
<xsl:otherwise>
<!-- escape \ and ' then single quote -->
<xsl:value-of select='concat("&apos;",
replace(replace($text, "\\", "\\\\"), "&apos;", "\\&apos;"), "&apos;")'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void setUpClass() {
public static Object[] baseNames() {
return new Object[] {
"Atom", "Implies", "Forall", "Exists", "Equal", "And", "Or", "Expr",
"RelIndVar", "comments"
"RelFunIndVar", "comments", "Equivalent", "Neg", "iri"
};
}

Expand Down
Loading

0 comments on commit 0b890f2

Please sign in to comment.