Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Shapes : support multiple namespaces and ontology imports
Browse files Browse the repository at this point in the history
  • Loading branch information
sotty committed Aug 25, 2012
1 parent 2349bbb commit db673c2
Show file tree
Hide file tree
Showing 50 changed files with 1,592 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<groupId>org.drools</groupId>
<artifactId>drools-chance-core</artifactId>
<version>${drools.version}</version>
<scope>test</scope>
</dependency>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.clarkparsia.empire.config.ConfigKeys;
import com.clarkparsia.empire.sesametwo.OpenRdfEmpireModule;
import com.clarkparsia.empire.sesametwo.RepositoryDataSourceFactory;
import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
import com.sun.xml.bind.v2.runtime.NameList;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
Expand All @@ -15,6 +17,7 @@
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.w3._2002._07.owl.ThingImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -30,6 +33,7 @@
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -591,7 +595,7 @@ public void testXMLNamespaces() {
for ( int j = 0; j < nodeList.getLength(); j++ ) {
Node n = nodeList.item( j );
if ( n.getNodeName().equals( "xmlns" ) ) {
assertEquals( "http://owl.drools.org/conyard#", n.getNodeValue() );
assertEquals( "http://owl.drools.org/conyard", n.getNodeValue() );
} else if ( n.getNodeName().equals( "xmlns:xml" ) ) {
assertEquals( "http://www.w3.org/XML/1998/namespace", n.getNodeValue() );
} else if ( n.getNodeName().equals( "xmlns:xsi" ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
<generateTraitDRL>true</generateTraitDRL>
<generateDefaultImplClasses>true</generateDefaultImplClasses>
<preserveInheritanceInImpl>false</preserveInheritanceInImpl>
<buildSpecXSDs>true</buildSpecXSDs>
<buildSpecXSDs>false</buildSpecXSDs>
<useExistingImplementations>true</useExistingImplementations>
<generateIndividuals>true</generateIndividuals>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.drools.trnaslators;
package org.drools.translators;

import org.w3._2002._07.owl.Thing;
import org.semanticweb.ontologies._2012._1.rule_merged.*;
import org.semanticweb.ontologies._2012._1.rules.*;
import org.semanticweb.ontologies._2012._1.rule_example.*;
import org.drools.lang.api.*;
import org.drools.lang.descr.*;
import org.drools.semantics.UIdAble;
Expand Down Expand Up @@ -42,7 +45,7 @@ dialect "mvel"
when
$r : Rule( $uid : dyEntryId, $lhs : hasPremiseFormula )
not MetaRule ( this == $r ) //MVEL bug with instanceof... TODO
$f : Pattern( $pid : dyEntryId, this == $lhs, $fct : get("hasFunctorType") )
$f : Pattern( $pid : dyEntryId, this == $lhs, $fct : get( "hasFunctorType" ), $fct != null && $fct.size() > 0 )
$t : Thing( $type : this.semanticTypeName ) from $fct.get(0)
$x : RuleDescr( name == $uid )
then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.drools.lang.descr.PackageDescr;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Test;
import org.semanticweb.ontologies._2012._1.rule_example.Pattern1Type;
import org.semanticweb.ontologies._2012._1.rule_example.Pattern1TypeImpl;
import org.semanticweb.ontologies._2012._1.rule_merged.*;

import java.util.Collection;
Expand All @@ -24,8 +26,6 @@ public void testIndividuals() {
Pattern1Type p1 = new Pattern1TypeImpl();
Object x = p1.getHasFunctorType();
Object y = ((Pattern1TypeImpl)p1).getHasFunctorTypeInferred();



Collection c = IndividualFactory.getIndividuals();
assertEquals( 21, c.size() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public String getUsage() {
@Override
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException {
for (ClassOutline co : outline.getClasses() ) {

CPluginCustomization c = co.target.getCustomizations().find( uri, "default" );
if ( co.implClass.getConstructor( new JType[0]) == null ) {
createDefaultConstructor( co.implClass );
if ( c != null ) {
if ( co.implClass.getConstructor( new JType[0]) == null ) {
createDefaultConstructor( co.implClass );
}
c.markAsAcknowledged();
}
c.markAsAcknowledged();
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.drools.semantics.builder.model.compilers.ModelCompiler;
import org.drools.semantics.builder.model.compilers.ModelCompilerFactory;
import org.drools.semantics.builder.model.compilers.XSDModelCompiler;
import org.w3._2002._07.owl.Thing;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -44,6 +45,9 @@
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
* Goal which creates various possible fact model representations from an ontology
Expand Down Expand Up @@ -88,6 +92,19 @@ public void setOntology(String ontology) {
this.ontology = ontology;
}

/**
* @parameter
*/
private List<String> ontologyImports;

public List<String> getOntologyImports() {
return ontologyImports;
}

public void setOntologyImports(List<String> ontologyImports) {
this.ontologyImports = ontologyImports;
}


/**
* @parameter default-value="model"
Expand Down Expand Up @@ -263,7 +280,18 @@ public void execute() throws MojoExecutionException {
}

DLFactory factory = DLFactoryBuilder.newDLFactoryInstance();
Resource res = ResourceFactory.newFileResource( ontology );
if ( ontologyImports == null ) {
ontologyImports = Collections.emptyList();
}

int n = 1 + ontologyImports.size();
Resource[] res = new Resource[ n ];
int j = 0;
for ( String imp : ontologyImports ) {
res[j++] = ResourceFactory.newFileResource( imp );
}
res[j] = ResourceFactory.newFileResource( ontology );

factory.setInferenceStrategy( isDelegateInference() ? DLFactory.INFERENCE_STRATEGY.EXTERNAL : DLFactory.INFERENCE_STRATEGY.INTERNAL );
OntoModel results = factory.buildModel( getModelName(), res );

Expand Down Expand Up @@ -316,13 +344,12 @@ public void execute() throws MojoExecutionException {

((XSDModelCompiler) compiler).setTransientPropertiesEnabled( false );
((XSDModelCompiler) compiler).setUseImplementation( false );
((XSDModelCompiler) compiler).setSchemaMode( "_$spec" );
xsdModel = (SemanticXSDModel) compiler.compile( results );

try {
FileOutputStream fos = new FileOutputStream( target + metainf + slash + getModelName() +"_$spec.xsd" );
File fos = new File( target + metainf + slash + getModelName() +"_$spec.xsd" );
xsdModel.stream( fos );
fos.flush();
fos.close();
} catch (Exception e) {
throw new MojoExecutionException( e.getMessage() );
}
Expand All @@ -331,27 +358,25 @@ public void execute() throws MojoExecutionException {

((XSDModelCompiler) compiler).setTransientPropertiesEnabled( false );
((XSDModelCompiler) compiler).setUseImplementation( true );
((XSDModelCompiler) compiler).setSchemaMode( "_$impl" );
xsdModel = (SemanticXSDModel) compiler.compile( results );

try {
FileOutputStream fos = new FileOutputStream( target + metainf + slash + getModelName() +"_$impl.xsd" );
File fos = new File( target + metainf + slash + getModelName() +"_$impl.xsd" );
xsdModel.stream( fos );
fos.flush();
fos.close();
} catch (Exception e) {
throw new MojoExecutionException( e.getMessage() );
}


((XSDModelCompiler) compiler).setTransientPropertiesEnabled( true );
((XSDModelCompiler) compiler).setUseImplementation( false );
((XSDModelCompiler) compiler).setSchemaMode( "_$full" );
xsdModel = (SemanticXSDModel) compiler.compile( results );

try {
FileOutputStream fos = new FileOutputStream( target + metainf + slash + getModelName() +"_$full.xsd" );
File fos = new File( target + metainf + slash + getModelName() +"_$full.xsd" );
xsdModel.stream( fos );
fos.flush();
fos.close();
} catch (Exception e) {
throw new MojoExecutionException( e.getMessage() );
}
Expand Down Expand Up @@ -425,55 +450,21 @@ public void execute() throws MojoExecutionException {


((XSDModelCompiler) compiler).setUseImplementation( false );
((XSDModelCompiler) compiler).setSchemaMode( "" );
xsdModel = (SemanticXSDModel) compiler.compile( results );


try {
FileOutputStream fos = new FileOutputStream( target + metainf + slash + getModelName() +".xsd" );
File fos = new File( target + metainf + slash + getModelName() +".xsd" );
xsdModel.stream( fos );
fos.flush();
fos.close();
} catch (Exception e) {
throw new MojoExecutionException( e.getMessage() );
}


try {
FileOutputStream fos = new FileOutputStream( target + metainf + slash + "bindings.xjb" );

ByteArrayOutputStream baos = new ByteArrayOutputStream();
xsdModel.streamBindings( baos );

DocumentBuilderFactory doxFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = doxFactory.newDocumentBuilder();
InputSource is = new InputSource( new StringReader( new String( baos.toByteArray() ) ) );
Document dox = builder.parse( is );
dox.normalize();

XPathFactory xpathFactory = XPathFactory.newInstance();
XPathExpression xpathExp = xpathFactory.newXPath().compile(
"//text()[normalize-space(.) = '']");
NodeList emptyTextNodes = (NodeList)
xpathExp.evaluate(dox, XPathConstants.NODESET);

// Remove each empty text node from document.
for (int i = 0; i < emptyTextNodes.getLength(); i++) {
Node emptyTextNode = emptyTextNodes.item(i);
emptyTextNode.getParentNode().removeChild(emptyTextNode);
}



TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute( "indent-number", new Integer(2) );
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
DOMSource source = new DOMSource( dox );
StreamResult result = new StreamResult( new OutputStreamWriter( fos ) );
transformer.transform( source, result );

fos.flush();
fos.close();
File fos = new File( target + metainf + slash + "bindings.xjb" );
xsdModel.streamBindings( fos );
} catch (Exception e) {
throw new MojoExecutionException( e.getMessage() );
}
Expand All @@ -489,6 +480,25 @@ public void execute() throws MojoExecutionException {
}


// namespace fix. For some reason, hj needs the (local) owl package to be assigned to the default namespace
try {
String classPathTemp = target + "xjc" + slash + Thing.class.getPackage().getName().replace(".", slash);
File f2 = new File( classPathTemp );
if ( ! f2.exists() ) {
f2.mkdirs();
}

FileOutputStream fos2 = new FileOutputStream( classPathTemp + slash + "package-info.java" );
xsdModel.streamNamespaceFix( fos2 );

fos2.flush();
fos2.close();
} catch (Exception e) {

}




}

Expand All @@ -505,7 +515,7 @@ public void execute() throws MojoExecutionException {
xsdModel = (SemanticXSDModel) compiler.compile( results );

try {
String classPath = target + "xjc" + slash + xsdModel.getPackage().replace(".", slash);
String classPath = target + "xjc" + slash + xsdModel.getDefaultPackage().replace(".", slash);
File f = new File( classPath );
if ( ! f.exists() ) {
f.mkdirs();
Expand All @@ -515,14 +525,14 @@ public void execute() throws MojoExecutionException {
xsdModel.streamIndividualFactory( fos );
fos.flush();
fos.close();


} catch (Exception e) {
throw new MojoExecutionException( e.getMessage() );
}

}



// private boolean generateTraitDRL = true;


Expand Down
2 changes: 1 addition & 1 deletion drools-shapes/drools-shapes-reasoner-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public enum SupportedReasoners { HERMIT /*, PELLET */ }

public OntoModel buildModel( String name, Resource res, StatefulKnowledgeSession kSession );

public OntoModel buildModel( String name, Resource[] res, StatefulKnowledgeSession kSession );

public OntoModel buildModel( String name, Resource res );

public OntoModel buildModel( String name, Resource[] res );


}
Loading

0 comments on commit db673c2

Please sign in to comment.