Skip to content

Commit

Permalink
generate prolog files from xmirs
Browse files Browse the repository at this point in the history
  • Loading branch information
OlesiaSub committed Dec 23, 2022
0 parents commit a98b54f
Show file tree
Hide file tree
Showing 18 changed files with 1,025 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*aoi_l
.idea
proloog.pl
75 changes: 75 additions & 0 deletions aoi-l.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="5" platform="JVM 1.8" allPlatforms="JVM [1.8]" useProjectSettings="false">
<compilerSettings />
<compilerArguments>
<stringArguments>
<stringArg name="jvmTarget" arg="1.8" />
<stringArg name="apiVersion" arg="1.7" />
<stringArg name="languageVersion" arg="1.7" />
</stringArguments>
<arrayArguments>
<arrayArg name="pluginClasspaths" />
<arrayArg name="pluginOptions" />
</arrayArguments>
</compilerArguments>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.eolang:eo-parser:0.28.10" level="project" />
<orderEntry type="library" name="Maven: org.cactoos:cactoos:0.54.0" level="project" />
<orderEntry type="library" name="Maven: xml-apis:xml-apis:1.4.01" level="project" />
<orderEntry type="library" name="Maven: com.yegor256:xsline:0.13.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: net.sf.saxon:Saxon-HE:11.4" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.xmlresolver:xmlresolver:4.4.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.apache.httpcomponents.client5:httpclient5:5.1.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.apache.httpcomponents.core5:httpcore5-h2:5.1.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: commons-codec:commons-codec:1.15" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.apache.httpcomponents.core5:httpcore5:5.1.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.xmlresolver:xmlresolver:data:4.4.3" level="project" />
<orderEntry type="library" name="Maven: org.antlr:antlr4-runtime:4.11.1" level="project" />
<orderEntry type="library" name="Maven: com.jcabi:jcabi-manifests:1.2.1" level="project" />
<orderEntry type="library" name="Maven: com.jcabi:jcabi-log:0.22.0" level="project" />
<orderEntry type="library" name="Maven: org.mockito:mockito-core:4.8.0" level="project" />
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy:1.12.14" level="project" />
<orderEntry type="library" name="Maven: net.bytebuddy:byte-buddy-agent:1.12.14" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.objenesis:objenesis:3.2" level="project" />
<orderEntry type="library" name="Maven: com.jcabi.incubator:xembly:0.28.1" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.33" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-text:1.10.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.12.0" level="project" />
<orderEntry type="library" name="Maven: org.eolang:deog:0.0.4" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib:1.7.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-common:1.7.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-test:1.7.21" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains:annotations:23.0.0" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:2.0.4" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.4.5" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.4.5" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.11.0" level="project" />
<orderEntry type="library" name="Maven: com.jcabi:jcabi-xml:0.25.5" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.jetbrains.kotlin:kotlin-test-junit5:1.7.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.9.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.9.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.8.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.9.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.2" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.10" level="project" />
<orderEntry type="library" name="Maven: org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.10" level="project" />
</component>
</module>
125 changes: 125 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<parent>
<groupId>com.jcabi</groupId>
<artifactId>parent</artifactId>
<version>0.63.2</version>
</parent>

<artifactId>aoi-l</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>consoleApp</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
</properties>

<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>1.7.10</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>MainKt</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.eolang</groupId>
<artifactId>eo-parser</artifactId>
<version>0.28.10</version>
</dependency>
<dependency>
<groupId>org.eolang</groupId>
<artifactId>deog</artifactId>
<version>0.0.4</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-xml</artifactId>
<version>0.25.5</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit5</artifactId>
<version>1.7.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.7.10</version>
</dependency>
</dependencies>

</project>
110 changes: 110 additions & 0 deletions src/main/kotlin/org/objectionary/aoi/generate/PlGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.objectionary.aoi.generate

import org.objectionary.aoi.generate.util.*
import org.objectionary.aoi.sources.SourcesExtractor
import org.objectionary.deog.abstract
import org.objectionary.deog.base
import org.objectionary.deog.name
import org.objectionary.deog.packageName
import org.w3c.dom.Document
import org.w3c.dom.Node
import java.io.File
import java.io.Serializable

typealias GraphAbstracts = MutableMap<String, MutableSet<Node>>

class PlGenerator {
fun generatePrologScripts(path: String) {
File("proloog.pl").createNewFile()
val sourcesExtractor = SourcesExtractor()
val documents = sourcesExtractor.collectDocuments(path)
documents.forEach {
generatePrologScript(it.key)
}
rules()
}

private fun generatePrologScript(document: Document) {
val objects: MutableList<Node> = mutableListOf()
val docObjects = document.getElementsByTagName("o")
val packageName = packageName(docObjects.item(0))
for (i in 0 until docObjects.length) {
objects.add(docObjects.item(i))
}
val obj = document.getElementsByTagName("objects").item(0)
val children = obj.childNodes ?: return
for (i in 0 until children.length) {
val node = children.item(i)
abstract(node)?.let {
collectNodeInfo(node)
}
}
}

private fun collectNodeInfo(node: Node) {
val children = node.childNodes ?: return
var offset = 0
for (i in 0 until children.length) {
if (i + offset >= children.length) break
val child = children.item(i + offset)
abstract(child)?.let {
containsAttrFact(name(node)!!, name(child)!!)
}
base(child)?.let {
if (!it.startsWith(".")) {
val (txt, j, name) = walkDotChain(child)
name?.let {n ->
if (n == "@") {
parentFact(txt, getFqn(name(node)!!, node.parentNode))
} else {
isInstanceFact(n, getFqn(name(node)!!, node.parentNode))
}
}
offset += j
}
}
collectNodeInfo(child)
}
}

/**
* @return concatenated test of bases, nu,ber of passed children, true if name == "@" else false
*/
private fun walkDotChain(
node: Node
): Triple<String, Int, String?> {
var txt = base(node)
var i = 0
var sibling = node.nextSibling?.nextSibling
while (base(sibling)?.startsWith(".") == true) {
txt += base(sibling)
i++
var tmp = sibling?.nextSibling
tmp?.attributes ?: run { tmp = tmp?.nextSibling }
if (base(tmp)?.startsWith('.') == true) sibling = tmp
else break
}
return Triple(txt!!, i, name(sibling))
}

private fun getFqn(name: String, par: Node): String {
var fqn = name
var parent = par
while (name(parent) != null) {
fqn = "${name(parent)}.$fqn"
parent = parent.parentNode
}
return fqn
}

@Suppress("PARAMETER_NAME_IN_OUTER_LAMBDA")
private fun abstracts(objects: MutableList<Node>) {
val abstracts: GraphAbstracts = mutableMapOf()
objects.forEach {
val name = name(it)
if (abstract(it) != null && name != null) {
abstracts.getOrPut(name) { mutableSetOf() }.add(it)
}
}
}
}
28 changes: 28 additions & 0 deletions src/main/kotlin/org/objectionary/aoi/generate/util/FileWriter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.objectionary.aoi.generate.util

import java.io.File

val plFile = File("proloog.pl").outputStream()

fun containsAttrFact(nodeName: String, childName: String) {
plFile.write("contains_attr($nodeName, $childName, fact).\n".toByteArray())
}

fun parentFact(nodeName: String, childName: String) {
plFile.write("parent($nodeName, $childName, fact).\n".toByteArray())
}

fun isInstanceFact(nodeName: String, childName: String) {
plFile.write("is_instance($nodeName, $childName, fact).\n".toByteArray())
}

fun rules() {
val rules = "\n% rules\n" +
"contains_attr(X, Y, rule) :- parent(Z, X, _),\n" +
" contains_attr(Z, Y, _).\n" +
"contains_attr(X, Y, rule) :- is_instance(X, Z, fact),\n" +
" contains_attr(Z, Y, _).\n" +
"parent(X, Y, rule) :- parent(X, Z, fact), parent(Z, Y, fact)."

plFile.write(rules.toByteArray())
}
7 changes: 7 additions & 0 deletions src/main/kotlin/org/objectionary/aoi/launch/AoiLauncher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.objectionary.aoi.launch

import org.objectionary.aoi.generate.PlGenerator

fun main(args: Array<String>) {
PlGenerator().generatePrologScripts("C:\\Users\\lesya\\aoi-l\\src\\test\\resources\\unit\\in\\inner_usages\\basic")
}
Loading

0 comments on commit a98b54f

Please sign in to comment.