Permalink
Browse files

[Shapes] - Improve compatibility with traits ans simple relational da…

…tabases
  • Loading branch information...
1 parent ac53de0 commit 3ae12f0061413a16a9091c4c33f8414b3172562d @sotty sotty committed Dec 31, 2013
Showing with 855 additions and 43 deletions.
  1. +1 −0 drools-shapes/drools-shapes-generator-plugin/src/main/java/org/drools/shapes/MetadataPlugin.java
  2. +16 −2 drools-shapes/drools-shapes-generator-plugin/src/main/java/org/drools/shapes/OntoModelCompiler.java
  3. +36 −5 drools-shapes/drools-shapes-generator-plugin/src/main/java/org/drools/shapes/ShapeCaster.java
  4. +2 −0 ...ls-shapes/drools-shapes-generator/src/main/java/org/drools/semantics/builder/model/OntoModel.java
  5. +22 −1 ...es/drools-shapes-generator/src/main/java/org/drools/semantics/builder/model/PropertyRelation.java
  6. +26 −4 ...tor/src/main/java/org/drools/semantics/builder/model/compilers/JarInterfaceModelCompilerImpl.java
  7. +4 −0 ...or/src/main/java/org/drools/semantics/builder/model/compilers/JavaInterfaceModelCompilerImpl.java
  8. +19 −2 ...ator/src/main/java/org/drools/semantics/builder/model/compilers/SemanticXSDModelCompilerImpl.java
  9. +10 −1 ...es-generator/src/main/java/org/drools/semantics/builder/model/compilers/XSDModelCompilerImpl.java
  10. +126 −0 ...-generator/src/main/java/org/drools/semantics/builder/model/hierarchy/DatabaseModelProcessor.java
  11. +12 −2 ...apes-generator/src/main/java/org/drools/semantics/builder/model/hierarchy/NullModelProcessor.java
  12. +1 −4 ...or/src/main/java/org/drools/semantics/builder/model/inference/AbstractModelInferenceStrategy.java
  13. +18 −3 ...nerator/src/main/java/org/drools/semantics/builder/model/inference/DelegateInferenceStrategy.java
  14. +6 −2 ...s-generator/src/main/resources/org/drools/semantics/builder/model/compilers/bindings.xjb.template
  15. +8 −2 ...es/drools-shapes-generator/src/main/resources/org/drools/semantics/templates/model/drl/trait.drlt
  16. +12 −1 ...es-generator/src/main/resources/org/drools/semantics/templates/model/java/TraitInterface.template
  17. +4 −0 ...shapes-generator/src/main/resources/org/drools/semantics/templates/model/java/genericAdd.template
  18. +5 −0 ...-shapes-generator/src/main/resources/org/drools/semantics/templates/model/java/metaDescr.template
  19. +8 −2 ...nerator/src/main/resources/org/drools/semantics/templates/model/java/restrictedAddRemove.template
  20. +1 −1 drools-shapes/drools-shapes-generator/src/test/java/org/drools/semantics/lang/dl/DL_4_AreaTest.java
  21. +1 −1 drools-shapes/drools-shapes-generator/src/test/java/org/drools/semantics/lang/dl/DL_7_RuleTest.java
  22. +17 −3 drools-shapes/drools-shapes-generator/src/test/java/org/drools/semantics/lang/dl/DL_8_ModelTest.java
  23. +359 −0 drools-shapes/drools-shapes-generator/src/test/resources/ontologies/dbModel.owl
  24. +3 −1 drools-shapes/drools-shapes-utilities/src/main/java/org.drools.semantics/Thing.java
  25. +137 −5 drools-shapes/drools-shapes-utilities/src/main/java/org/w3/_2002/_07/owl/ThingImpl.java
  26. +1 −1 drools-shapes/drools-shapes-xsd-processor/src/main/resources/org/drools/shapes/xsd/drl/xsd2owl.drl
@@ -69,6 +69,7 @@ public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) thro
HashMap<String, Object> map = new HashMap<String, Object>();
map.put( "klassName", co.target.shortName );
map.put( "typeName", keyed.getAttribute( "name" ) );
+ map.put( "package", keyed.getAttribute( "package" ) );
map.put( "supertypeName", keyed.getAttribute( "parent" ) );
map.put( "propertyNames", propNames );
map.put( "typeNames", typeNames );
@@ -47,6 +47,7 @@
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
@@ -269,7 +270,7 @@ public boolean existsResult() {
streamJavaInterfaces( false );
- streamXSDsWithBindings( true );
+ streamXSDsWithBindings( true, null );
mojo( options, variant );
List<Diagnostic<? extends JavaFileObject>> diagnostics = doCompile();
@@ -352,8 +353,11 @@ public boolean streamJavaInterfaces( boolean includeJar ) {
-
public boolean streamXSDsWithBindings( boolean includePersistenceConfiguration ) {
+ return streamXSDsWithBindings( includePersistenceConfiguration, null );
+ }
+
+ public boolean streamXSDsWithBindings( boolean includePersistenceConfiguration, String persistenceTemplatePath ) {
SemanticXSDModelCompiler xcompiler = (SemanticXSDModelCompiler) ModelCompilerFactory.newModelCompiler( ModelFactory.CompileTarget.XSDX );
SemanticXSDModel xmlModel = (SemanticXSDModel) xcompiler.compile( model );
@@ -366,6 +370,16 @@ public boolean streamXSDsWithBindings( boolean includePersistenceConfiguration )
success = xmlModel.streamBindings( getMetaInfDir() );
if ( includePersistenceConfiguration ) {
+ if ( persistenceTemplatePath != null ) {
+ File persistenceTemplate = new File( persistenceTemplatePath );
+ if ( persistenceTemplate.exists() ) {
+ InputStream is = new FileInputStream( persistenceTemplate );
+ byte[] data = new byte[ is.available() ];
+ is.read( data );
+ xmlModel.setPersistenceXml( new String( data ) );
+ is.close();
+ }
+ }
success = success && streamPersistenceConfigs( xcompiler, xmlModel );
}
@@ -33,8 +33,11 @@
import org.w3._2002._07.owl.Thing;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
@@ -83,6 +86,19 @@ public void setOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
}
+ /**
+ * @parameter
+ */
+ private String persistenceTemplate;
+
+ public String getPersistenceTemplate() {
+ return persistenceTemplate;
+ }
+
+ public void setPersistenceTemplate(String persistenceTemplate) {
+ this.persistenceTemplate = persistenceTemplate;
+ }
+
/**
* @parameter
@@ -295,7 +311,7 @@ public void execute() throws MojoExecutionException {
if ( isGenerateDefaultImplClasses() ) {
- compiler.streamXSDsWithBindings( true );
+ compiler.streamXSDsWithBindings( true, persistenceTemplate );
}
if ( isGenerateIndividuals() ) {
@@ -367,9 +383,20 @@ public void execute() throws MojoExecutionException {
private OntoModel processOntology( OntoModel.Mode mode ) throws MojoExecutionException {
+ InputStream ontologyStream = null;
File ontoFile = new File( ontology );
- if ( ! ontoFile.exists() ) {
- throw new MojoExecutionException( " Ontology file not found : " + ontology );
+ if ( ontoFile.exists() ) {
+ try {
+ ontologyStream = new FileInputStream( new File( ontology ) );
+ } catch ( FileNotFoundException e ) {
+ e.printStackTrace();
+ }
+ } else {
+ try {
+ ontologyStream = ResourceFactory.newClassPathResource( ontology ).getInputStream();
+ } catch ( IOException e ) {
+ //
+ }
}
DLFactory factory = DLFactoryBuilder.newDLFactoryInstance();
@@ -381,9 +408,13 @@ private OntoModel processOntology( OntoModel.Mode mode ) throws MojoExecutionExc
Resource[] res = new Resource[ n ];
int j = 0;
for ( String imp : ontologyImports ) {
- res[j++] = ResourceFactory.newFileResource( imp );
+ if ( new File( imp ).exists() ) {
+ res[j++] = ResourceFactory.newFileResource( imp );
+ } else {
+ res[j++] = ResourceFactory.newClassPathResource( imp );
+ }
}
- res[j] = ResourceFactory.newFileResource( ontology );
+ res[j] = ResourceFactory.newInputStreamResource( ontologyStream );
return factory.buildModel( getModelName(),
res,
@@ -16,6 +16,7 @@
package org.drools.semantics.builder.model;
+import org.drools.semantics.builder.model.hierarchy.DatabaseModelProcessor;
import org.drools.semantics.builder.model.hierarchy.FlatModelProcessor;
import org.drools.semantics.builder.model.hierarchy.HierarchicalModelProcessor;
import org.drools.semantics.builder.model.hierarchy.ModelHierarchyProcessor;
@@ -37,6 +38,7 @@
FLAT( new FlatModelProcessor() ),
VARIANT( new VariantModelProcessor() ),
OPTIMIZED( new OptimizedModelProcessor() ),
+ DATABASE( new DatabaseModelProcessor() ),
NONE( new NullModelProcessor() );
private ModelHierarchyProcessor processor;
@@ -16,6 +16,7 @@
package org.drools.semantics.builder.model;
+import com.hp.hpl.jena.sparql.function.library.min;
import org.drools.definition.type.Position;
import org.drools.semantics.utils.NameUtils;
@@ -24,7 +25,7 @@
import java.util.List;
import java.util.Set;
-public class PropertyRelation extends Relation {
+public class PropertyRelation extends Relation implements Cloneable {
@Position(3)
protected String name;
@@ -140,6 +141,26 @@ public String getIri() {
}
+ public PropertyRelation clone() {
+ PropertyRelation rel = new PropertyRelation( this.subject, this.property, this.object, this.name );
+ rel.minCard = this.minCard;
+ rel.maxCard = this.maxCard;
+ rel.target = this.target;
+ rel.domain = this.domain;
+ rel.addableTarget = this.addableTarget;
+
+ rel.restricted = this.restricted;
+ rel.inherited = this.inherited;
+ rel.simple = this.simple;
+ rel.inverse = this.inverse;
+
+ rel.baseProperty = this.baseProperty;
+ rel.restrictedProperties = new ArrayList<PropertyRelation>( this.restrictedProperties );
+ rel.chains = new HashSet<List<PropertyRelation>>( this.chains );
+
+ return rel;
+ }
+
public PropertyRelation getBaseProperty() {
if ( baseProperty == this ) {
return this;
@@ -21,14 +21,19 @@
import com.clarkparsia.empire.annotation.Namespaces;
import com.clarkparsia.empire.annotation.RdfProperty;
import com.clarkparsia.empire.annotation.RdfsClass;
+import org.drools.definition.type.PropertyReactive;
import org.drools.factmodel.BuildUtils;
+import org.drools.factmodel.traits.Trait;
import org.drools.semantics.utils.NameUtils;
import org.drools.semantics.builder.model.*;
import org.drools.semantics.utils.NamespaceUtils;
import org.mvel2.asm.*;
+import thewebsemantic.Namespace;
+import thewebsemantic.RdfType;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
+import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlID;
@@ -114,7 +119,20 @@ public void compile( Concept con, Object context, Map<String, Object> params ) {
}
av0.visitEnd();
}
-
+ {
+ av0 = cw.visitAnnotation( Type.getDescriptor( Namespace.class ), true);
+ av0.visit( "value", params.get( "namespace" ) );
+ }
+ {
+ av0 = cw.visitAnnotation( Type.getDescriptor( RdfType.class ), true);
+ av0.visit( "value", "" + params.get( "name" ) + params.get( "namespace" ) );
+ }
+ {
+ av0 = cw.visitAnnotation( Type.getDescriptor( Trait.class ), true);
+ }
+ {
+ av0 = cw.visitAnnotation( Type.getDescriptor( PropertyReactive.class ), true);
+ }
for ( String propKey : props.keySet() ) {
PropertyRelation rel = props.get( propKey );
@@ -150,14 +168,18 @@ public void compile( Concept con, Object context, Map<String, Object> params ) {
av0 = mv.visitAnnotation( Type.getDescriptor( RdfProperty.class ), true );
av0.visit( "value", "tns:" + propName );
av0.visitEnd();
- if ( rel.isSimple() ) {
+ if ( rel.getTarget().isPrimitive() ) {
av0 = mv.visitAnnotation( Type.getDescriptor( Basic.class ), true );
av0.visitEnd();
} else {
- av0 = mv.visitAnnotation( Type.getDescriptor( OneToMany.class ), true );
+ if ( rel.getMaxCard() == null || rel.getMaxCard() > 1 ) {
+ av0 = mv.visitAnnotation( Type.getDescriptor( OneToMany.class ), true );
+ } else {
+ av0 = mv.visitAnnotation( Type.getDescriptor( ManyToOne.class ), true );
+ }
{
AnnotationVisitor av1 = av0.visitArray( "cascade" );
- av1.visitEnum( null, Type.getDescriptor( CascadeType.class ), "PERSIST" );
+ av1.visitEnum( null, Type.getDescriptor( CascadeType.class ), "ALL" );
av1.visitEnd();
}
av0.visitEnd();
@@ -47,6 +47,10 @@ public void compile( Concept con, Object context, Map<String, Object> params ) {
return;
}
+ if ( ( model.getMode() == OntoModel.Mode.NONE || model.getMode() == OntoModel.Mode.DATABASE ) && con.isAbstrakt() ) {
+ return;
+ }
+
CompiledTemplate template = registry.getNamedTemplate( templateName );
String name = con.getFullyQualifiedName();
@@ -167,10 +167,13 @@ private Document createBindings( String ns, SemanticXSDModel sxsdModel ) {
String prefix = ((XSDModel) getModel()).mapNamespaceToPrefix( ns );
try {
String template = readFile( "bindings.xjb.template" );
+ Collection<Concept> cons = filterConceptsByNS( getModel().getConcepts(), ns );
+ cons = filterUnneedecConcepts( cons, model );
+
Map<String,Object> vars = new HashMap<String,Object>();
vars.put( "package", NameUtils.namespaceURIToPackage( ns ) );
vars.put( "namespace", ns );
- vars.put( "concepts", filterConceptsByNS( getModel().getConcepts(), ns ) );
+ vars.put( "concepts", cons );
vars.put( "flat", getModel().getMode() != OntoModel.Mode.HIERARCHY );
vars.put( "properties", propCache );
vars.put( "modelName", getModel().getName() );
@@ -192,6 +195,20 @@ private Document createBindings( String ns, SemanticXSDModel sxsdModel ) {
}
+ private Collection<Concept> filterUnneedecConcepts( Collection<Concept> cons, CompiledOntoModel model ) {
+ ArrayList<Concept> filtered = new ArrayList<Concept>( cons.size() );
+ for ( Concept c : cons ) {
+ if ( model.getMode() != OntoModel.Mode.FLAT && model.getMode() != OntoModel.Mode.NONE && model.getMode() != OntoModel.Mode.DATABASE ) {
+ filtered.add( c );
+ } else {
+ if ( ! c.isAbstrakt() ) {
+ filtered.add( c );
+ }
+ }
+ }
+ return filtered;
+ }
+
public void mergeNamespacedPackageInfo( SemanticXSDModel model ) {
for ( Namespace ns : model.getNamespaces() ) {
@@ -295,7 +312,7 @@ public void mergeIndex( File preexistingIndex, SemanticXSDModel model ) {
- private Object filterConceptsByNS( List<Concept> concepts, String ns ) {
+ private Collection<Concept> filterConceptsByNS( List<Concept> concepts, String ns ) {
List<Concept> filtered = new ArrayList<Concept>();
for ( Concept con : concepts ) {
if ( NamespaceUtils.compareNamespaces(con.getNamespace(), ns) ) {
@@ -102,7 +102,16 @@ public void compile( Concept con, Object context, Map<String, Object> params ) {
element,
el,
dependencies );
- getModel().addTrait( name, descr );
+
+ if ( model.getMode() != OntoModel.Mode.FLAT && model.getMode() != OntoModel.Mode.NONE && model.getMode() != OntoModel.Mode.DATABASE ) {
+ getModel().addTrait( name, descr );
+ } else {
+ if ( ! con.isAbstrakt() ) {
+ getModel().addTrait( name, descr );
+ }
+ }
+
+
Oops, something went wrong.

0 comments on commit 3ae12f0

Please sign in to comment.